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

[BUG] JSQLParser Version : 5.1 RDBMS : PostgreSQL 10 mix the JSON and relational operators, it outputs the wrong AST and sql. #2163

Open
hijackworld opened this issue Feb 11, 2025 · 4 comments

Comments

@hijackworld
Copy link

Description:

  • When using a mix of JSON and relational operators, there is a priority error in the parsing results.
  • The new sql lost some blank spaces.

SQL Example:

  • Original SQL:

    select  *  from  t  where  js  #>>  '{a,b,1}' <> 'bar'
  • Error 1:
    In the AST, the filter likes this: js #>> ('{a,b,1}' <> 'bar')
    but what we need is: (js #>> '{a,b,1}') <> 'bar'

  • Error 2:
    JSQLParser generates the new sql:

     SELECT * FROM t WHERE js#>>'{a,b,1}' <> 'bar'

    but what we need is: SELECT * FROM t WHERE js #>> '{a,b,1}' <> 'bar'
    JSQLParser lost the blank spaces.

Software Information:

  • JSqlParser version: 5.1
  • Database: PostgreSQL
@hijackworld
Copy link
Author

BTW
if we parse the new sql SELECT * FROM t WHERE js#>>'{a,b,1}' <> 'bar'
JSQLParser will recognize it as a wrong Bitwise operation in the AST:
the collumn name is js#, and the operator is >>.

@manticore-projects
Copy link
Contributor

manticore-projects commented Feb 11, 2025

the collumn name is js#, and the operator is >>

In this case you will need to quote the column name like "js#" since it interferes with the JSON operator #>>.

SQL Text
 └─Statements: statement.select.PlainSelect
    ├─selectItems: statement.select.SelectItem
    │  └─AllColumns: *
    ├─Table: t
    └─where: expression.operators.relational.NotEqualsTo
       ├─leftExpression: expression.operators.arithmetic.BitwiseRightShift
       │  ├─Column: "js#"
       │  └─StringValue: '{a,b,1}'
       └─StringValue: 'bar'

@hijackworld
Copy link
Author

hijackworld commented Feb 11, 2025

thanks.
what about the issue of the wrong priority?
js #>> ('{a,b,1}' <> 'bar')

in fact, what we need is: (js #>> '{a,b,1}') <> 'bar', just like PG does.

we parse the original sql by JSQLParser 4.9, and it works well.
it seems like a bug for version 5.1 .

@manticore-projects
Copy link
Contributor

what about the issue of the wrong priority?

Adding brackets for clarity may be your best instant solution.

SQL Text
 └─Statements: statement.select.PlainSelect
    ├─selectItems: statement.select.SelectItem
    │  └─AllColumns: *
    ├─Table: t
    └─where: expression.operators.relational.NotEqualsTo
       ├─ParenthesedExpressionList: (js#>>'{a,b,1}')
       └─StringValue: 'bar'

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

2 participants