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

Parsing error on valid SQL using string literals with single quotes #553

Open
albertsgrc opened this issue Jan 23, 2024 · 1 comment
Open
Labels
bug Something isn't working

Comments

@albertsgrc
Copy link

albertsgrc commented Jan 23, 2024

Describe the bug
sqlfmt fails to parse valid SQL when using string literals that contain single quotes.

To Reproduce

select '''' || 'quoted_text' || ''''

Expected behavior
sqlfmt runs without errors.

Actual behavior

1 file had errors while formatting.
0 files left unchanged.
test.sql
    sqlfmt encountered an error: Could not parse SQL at position 35: '''

Additional context
Reproducible from https://sqlfmt.com/

These two are formatted without errors by sqlfmt, so it's only some specific combinations that break the parsing:

select '''quoted_text'''
select '''' || 'quoted_text'
@tconbeer
Copy link
Owner

tconbeer commented Jan 23, 2024

My guess is we're parsing this as a triple-quoted expression followed by a single quote. We get this right (postgres dollar sign quotes):

select $$'$$ || 'quoted_text' || $$'$$

I thought triple quotes for string literals were more common, but a quick search of pg, mysql, sql server, snowflake, and bq shows that only bigquery allows them. maybe we should pull those out into a bq-specific dialect.

@tconbeer tconbeer added the bug Something isn't working label Jan 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants