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

Parse negative numbers for Postgres Dialect #549

Open
kwannoel opened this issue Aug 1, 2022 · 1 comment
Open

Parse negative numbers for Postgres Dialect #549

kwannoel opened this issue Aug 1, 2022 · 1 comment

Comments

@kwannoel
Copy link

kwannoel commented Aug 1, 2022

Parser should parse negative numbers as is, rather than into a unary_op neg, wrapping an inner value, to be consistent with Postgres.

Postgres behaviour using (pglast):

[nix-shell:~/projects/risingwave-nix]$ ./query_parser.py "select -(-(-(9223372036854775808)))"
{'@': 'SelectStmt',
 'all': False,
 'limitOption': {'#': 'LimitOption',
                 'name': 'LIMIT_OPTION_DEFAULT',
                 'value': 0},
 'op': {'#': 'SetOperation', 'name': 'SETOP_NONE', 'value': 0},
 'targetList': ({'@': 'ResTarget',
                 'location': 7,
                 'val': {'@': 'A_Const',
                         'location': 7,
                         'val': {'@': 'Float',
                                 'val': Decimal('-9223372036854775808')}}},)}
@kwannoel
Copy link
Author

kwannoel commented Aug 1, 2022

To reproduce:

use sqlparser::dialect::PostgreSqlDialect;
use sqlparser::parser::Parser;

fn main() {
    let sql = "SELECT -9223372036854775808;";
    let dialect = PostgreSqlDialect {};

    let ast = Parser::parse_sql(&dialect, sql).unwrap();

    println!("AST: {:#?}", ast);
}

Output:

AST: [
    Query(
        Query {
            with: None,
            body: Select(
                Select {
                    distinct: false,
                    top: None,
                    projection: [
                        UnnamedExpr(
                            UnaryOp {
                                op: Minus,
                                expr: Value(
                                    Number(
                                        "9223372036854775808",
                                        false,
                                    ),
                                ),
                            },
                        ),
                    ],
                    into: None,
                    from: [],
                    lateral_views: [],
                    selection: None,
                    group_by: [],
                    cluster_by: [],
                    distribute_by: [],
                    sort_by: [],
                    having: None,
                    qualify: None,
                },
            ),
            order_by: [],
            limit: None,
            offset: None,
            fetch: None,
            lock: None,
        },
    ),
]

@kwannoel kwannoel changed the title Parse negative numbers Parse negative numbers for Postgres Dialect Aug 1, 2022
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

1 participant