Skip to content

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

Description

@hijackworld

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

Activity

  1. hijackworld commented on Feb 11, 2025

    @hijackworld
    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 >>.

  2. manticore-projects commented on Feb 11, 2025

    @manticore-projects
    Contributor

    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'
    
  3. hijackworld commented on Feb 11, 2025

    @hijackworld
    Author

    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 .

  4. manticore-projects commented on Feb 11, 2025

    @manticore-projects
    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'
    
    
  5. fudianchn commented on Aug 22, 2026

    @fudianchn
    Contributor

    Verified on current master (a5765f3): select * from t where js #>> '{a,b,1}' <> 'bar' now parses with the expected precedence: the WHERE clause is NotEqualsTo(JsonExpression(js #>> '{a,b,1}'), 'bar'). The deparser output also keeps the spaces: SELECT * FROM t WHERE js #>> '{a,b,1}' <> 'bar'. Both reported errors are resolved. Can this be closed?

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