Skip to content

can not parser union select clause in insert query #1491

Closed

Description

@young0098

I mock a simple insert query like this
insert into table1 (tf1,tf2,tf2)
((select sf1,sf2,sf3 from s1) union (select rf1,rf2,rf2 from r1))
and get follow paseexception:
net.sf.jsqlparser.JSQLParserException: Encountered unexpected token: "union" "UNION"
at line 2, column 31.
Was expecting one of:

"&"
")"
","
"::"
"<<"
">>"
"COLLATE"
"["
"^"
"|"

net.sf.jsqlparser.parser.CCJSqlParserUtil.parseStatement(CCJSqlParserUtil.java:190)
net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:63)
net.sf.jsqlparser.parser.CCJSqlParserUtil.parse(CCJSqlParserUtil.java:38)

System

  • Postgresql
  • Java 1.8
  • JSqlParser 4.2

Activity

  1. manticore-projects commented on Mar 20, 2022

    @manticore-projects
    Contributor

    Greetings.

    Confirmed with JSQLParser 4.4 Snapshot.
    The brackets in combination with the INSERT are the problem, without the brackets it works:

    INSERT INTO table1 (
        tf1
        , tf2
        , tf2 )
    SELECT  sf1
            , sf2
            , sf3
    FROM s1
    UNION
    SELECT  rf1
            , rf2
            , rf2
    FROM r1
    ;

    Without the INSERT it also works:

    --insert into table1
    ( (
            SELECT  sf1
                    , sf2
                    , sf3
            FROM s1 )
        UNION (
            SELECT  rf1
                    , rf2
                    , rf2
            FROM r1 ) )
    ;

    I will need to look into the INSERT production.

  2. manticore-projects commented on Mar 22, 2022

    @manticore-projects
    Contributor

    In my opinion, we need to refactor the whole INSERT production to make this work.
    Currently, it supports ( SELECT ) (with maximum one embracing set of bracket). But in fact, this should be an EXPRESSION with possible many embracing brackets (e. g. INSERT INTO ... ( ( ( SELECT ... FROM ... ) ) ))

    This leads to the general question, how we want to treat redundant brackets:
    a) we strip them and suppress them (which is better for performance at the cost the alter the original statement)
    b) OR we keep them (which would be bad for performance, as we have to treat everything as a Complex Expression)
    c) OR we try to count them and change the Boolean hasBrackets into an Integer numberOfBrackets (most complicated though)

    @wumpz: Your opinion please? I tend to strip it.

  3. added a commit that references this issue on May 8, 2022
    267b22a
  4. added a commit that references this issue on May 15, 2022
    b5672c5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions