sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

Single quoted literal with alias generates wrong tokens

Open dcaballero opened this issue 5 years ago • 3 comments

Using version 0.3.0

In a query such as this:

SELECT 'fixed_text' ALIAS1 FROM .......

The tokens generated are: Token (ttype=Token.Literal.String.Single) = 'fixed_text' Identifier = ALIAS1

The expected tokens are: 1 Identifier with tokens containing a Single and an Identifier

Expected tokens are generated correctly when the 'AS' keword is used:

SELECT 'fixed_text' AS ALIAS1 FROM .......

If there are many fields in the select list, the behavior remains, but Identifier or IdentifierList tokens are generated before and/or after the Single:

SELECT 'fixed_text' ALIAS1, field1, field2 FROM ....... Will generate 1 Single and 1 IdentifierList, the IdentifierList will contain 3 Identifiers (ALIAS1, field1 and field2)

SELECT field1, field2, 'fixed_text' ALIAS1 FROM ....... Will generate 1 IdentifierList (field1, field2, 1 Single), 1 Identifier (ALIAS1)

dcaballero avatar Oct 28 '19 21:10 dcaballero

same as

SELECT NULL ALIAS1, field1, field2 FROM .......

for oracle

yzjkzq avatar Jul 08 '20 08:07 yzjkzq

i made it by adding "T.String.Single" to "ttypes" of "grouping.py::group_identifier()", but no further test.

yzjkzq avatar Jul 09 '20 01:07 yzjkzq

i made it by adding "T.String.Single" to "ttypes" of "grouping.py::group_identifier()", but no further test.

i did this too, but it will take some Literal words in select (such as select "a" ...) into Identifier instead of Literal token, which seems reasonable.

BlackWatchuser avatar Jun 09 '21 09:06 BlackWatchuser