sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

FEATURE: Make Keyword an Identifier when it is used before "AS"

Open JCZuurmond opened this issue 5 years ago • 4 comments

In a SQL query we have a keyword as identifier for a CTE:

WITH data AS (
    SELECT * 
    FROM table  
    WHERE id < 100
)

SELECT * FROM data 

It would be nice to mark a keyword conditionally on the context. In this case do not mark it as a keyword if it is used before the AS token.

JCZuurmond avatar Aug 14 '20 14:08 JCZuurmond

Hey @JCZuurmond , were you able to find a solution/workaround to this? It seems that as of sqlparse 0.4.3, this remains unsolved.

mkg5476 avatar Nov 09 '22 16:11 mkg5476

Nope, I decided to not do SQL parsing

JCZuurmond avatar Nov 28 '22 10:11 JCZuurmond

DATA is recognized as a keyword since the beginning of sqlparse. But it seems to be a keyword only in certain dialects liek DB2 or Terradata. Version 0.5.0 will introduce some changes in the current behavior that allows us to remove DATA as a default keyword.

For my later self... This might be a good guide for deciding which keyword belongs to which dialect: https://en.wikipedia.org/wiki/List_of_SQL_reserved_words

andialbrecht avatar Dec 30 '22 11:12 andialbrecht

@andialbrecht Thanks very much for your response. Although you are correct about DATA appearing as a keyword in only a few dialects of SQL, the solution proposed by the questioner is different.

It may be better to parse the name occurring after WITH as an alias/identifier, despite it being a keyword in any dialect of SQL.

mkg5476 avatar Jan 02 '23 22:01 mkg5476