sqlparse icon indicating copy to clipboard operation
sqlparse copied to clipboard

INSERT INTO t1 (t0) VALUES ('2'); identifier t1 will be parsed as Function

Open Han-Gang opened this issue 6 years ago • 5 comments

|- 0 Newline ' ' |- 1 DML 'INSERT' |- 2 Whitespace ' ' |- 3 Keyword 'INTO' |- 4 Whitespace ' ' |- 5 Function 't1 (t0)' | |- 0 Identifier 't1' | | - 0 Name 't1' | |- 1 Whitespace ' ' | - 2 Parenthesis '(t0)' | |- 0 Punctuation '(' | |- 1 Identifier 't0' | | - 0 Name 't0' | - 2 Punctuation ')' |- 6 Newline ' ' |- 7 Values 'VALUES...' | |- 0 Keyword 'VALUES' | |- 1 Whitespace ' ' | - 2 Parenthesis '('2000...' | |- 0 Punctuation '(' | |- 1 Single ''2000-...' | - 2 Punctuation ')' `- 8 Punctuation ';' None t1 Tables:

INSERT INTO t1 (t0) VALUES ('2');

Han-Gang avatar Mar 22 '19 10:03 Han-Gang

I came across the same situation too, and I'm trying to extract table name based on whether t1 in the example is of type Identifier. Since this issue has been open for a few months. Would you kindly confirm is this a bug or not? or what we can do when parsing this kinds of insert statement. thx @andialbrecht

reata avatar Jul 21 '19 07:07 reata

This is a bug. The Parenthesis within an Insert shouldn't be treated as a Function, it's more like a function definition, as you are defining the column names that you will be inserting into and the order that they will be specified.

I don't think it's ever going to be valid to have a function call after INSERT and before a SELECT or VALUES. Checking Oracle and BigQuery SQL references this is true. The same would be true for the INSERT clause of MERGE.

EDIT: I forgot to suggest a fix.

I get Name Punctuation Function (with a schema.table (col1, col2, col3.....) then under Function get Name Whitespace Parenthesis.

What should be returned is probably Name Punctuation Name Whitespace Parenthesis all at the same level. So just don't add the Function while you are within an INSERT clause

dhanes16 avatar Mar 11 '20 23:03 dhanes16

I have the same issue, how to solve

JohnYan2017 avatar May 03 '20 04:05 JohnYan2017

Try: INSERT INTO "t1" (t0) VALUES ('2'); And you will get something like this: <Identifier '"t1"' at 0x7F6D9AD7CD50>

This solution is not very clean, but it may help

tofuurem avatar Sep 26 '21 15:09 tofuurem

Still an issue @andialbrecht :

INSERT INTO TAB1
 (SELECT   COL1
        , COL2
 FROM  TAB2
 WHERE COL1 = 185
)

TAB1 will be recognized as Function.

echoDaveD avatar Feb 14 '24 20:02 echoDaveD