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

Incorrect regex for matching the String.Single token #652

Open
john-bodley opened this issue Jan 11, 2022 · 1 comment
Open

Incorrect regex for matching the String.Single token #652

john-bodley opened this issue Jan 11, 2022 · 1 comment

Comments

@john-bodley
Copy link
Contributor

john-bodley commented Jan 11, 2022

The regex for determining a tokens.String.Single seems incorrect, i.e., it is too greedy, when the SQL contains an escape character:

>>> from sqlparse import parse

>>> stmt = parse(r"foo = '\' AND bar = 'baz'")[0]
>>> stmt._pprint_tree()
|- 0 Identifier 'foo = ...'
|  |- 0 Comparison 'foo = ...'
|  |  |- 0 Identifier 'foo'
|  |  |  `- 0 Name 'foo'
|  |  |- 1 Whitespace ' '
|  |  |- 2 Comparison '='
|  |  |- 3 Whitespace ' '
|  |  `- 4 Single ''\' AN...'
|  `- 1 Identifier 'baz'
|     `- 0 Name 'baz'
`- 1 Error "'"

I'm not sure why the ' is present in ...|\\'|... and was wondering whether this should be removed.

@villebro
Copy link

villebro commented Jan 12, 2022

To my knowledge, MySQL and BigQuery are one of the few databases that support escaping single quotes using a backslash: https://dev.mysql.com/doc/refman/8.0/en/string-literals.html#character-escape-sequences and https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#literals. But all other databases I can think of will retain the backslash when when placed before the single quote:

Postgres, sqlite, Oracle, MSSQL, Presto, Athena etc

SELECT '\'' as test

ERROR

SELECT '\''' as test

\'

MySQL, BigQuery:

SELECT '\'' as test

'

SELECT '\''' as test

ERROR

@andialbrecht andialbrecht modified the milestone: 0.4.3 Sep 6, 2022
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

No branches or pull requests

3 participants