sqlfmt icon indicating copy to clipboard operation
sqlfmt copied to clipboard

Parsing error on valid SQL using string literals with single quotes

Open albertsgrc opened this issue 1 year ago • 2 comments

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'

albertsgrc avatar Jan 23 '24 06:01 albertsgrc

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 avatar Jan 23 '24 15:01 tconbeer