Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: handle empty string in connector comment padding #3437

Merged
merged 9 commits into from
May 9, 2024

Conversation

uncledata
Copy link
Contributor

Problem:
if comment is an empty string pad_comment fails on IndexError on comment[0]. Adding a simple check if comment is not empty

@tobymao
Copy link
Owner

tobymao commented May 9, 2024

please add a test case

Comment on lines 48 to 55
def test_pad_comment(self):
self.assertEqual(Generator().pad_comment(""), "")
self.assertEqual(Generator().pad_comment(" leading"), " leading ")
self.assertEqual(Generator().pad_comment("trailing "), " trailing ")
self.assertEqual(
Generator().pad_comment(" leading and trailing "), " leading and trailing "
)
self.assertEqual(Generator().pad_comment("no padding"), " no padding ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead test this end-to-end by simply adding an example that fails to roundtrip in pretty.sql?

tests/test_generator.py Outdated Show resolved Hide resolved
sqlglot/generator.py Outdated Show resolved Hide resolved
1. removing staticmethod
2. generator object only once
sqlglot/generator.py Outdated Show resolved Hide resolved
@georgesittas georgesittas changed the title handle empty string in pad_comment + staticmethod Fix: handle empty string in connector comment padding May 9, 2024
@uncledata
Copy link
Contributor Author

uncledata commented May 9, 2024

hm it seems that if moving the check if comment is empty str/None to the method will still cause some issues for some tests:

  1. returning an empty string will cause /**/ in situations like:
select x, --
from foo""",
  1. returning None will make the comment become
/*None*/

Maybe it's better to add a check to connector_sql and in maybe_comment? I see it's validated like this everywhere else (

if comment:

@uncledata
Copy link
Contributor Author

TL;DR:

Revert my changes in the first place and add if comment check in connector_sql:

                        if comment:
                            op += f" /*{self.pad_comment(comment)}*/"

@georgesittas
Copy link
Collaborator

Sure let's do that

Copy link
Collaborator

@georgesittas georgesittas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's add a test in test_transpile.py and I'll get this in

@georgesittas georgesittas merged commit a2a6eaa into tobymao:main May 9, 2024
5 checks passed
@uncledata uncledata deleted the uncledata/adjust_pad_comments branch May 9, 2024 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants