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

Better representation for MySQL list of variable assignments #1697

Open
mvzink opened this issue Jan 31, 2025 · 0 comments
Open

Better representation for MySQL list of variable assignments #1697

mvzink opened this issue Jan 31, 2025 · 0 comments

Comments

@mvzink
Copy link
Contributor

mvzink commented Jan 31, 2025

sqlparser currently accepts Postgres-style set variable = expr and Snowflake-style set (variable, other_variable) = (expr, expr) syntax. It also accepts MySQL-style set variable = expr, other_variable = expr syntax, but not in the way one might expect: other_variable = expr becomes another expression in the list of values.

cargo run --example cli - --mysql
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/examples/cli - --mysql`
Parsing from stdin using MySqlDialect
set @foo = 'bar', @baz = 'quux';
2025-01-31T18:01:10.029Z DEBUG [sqlparser::parser] Parsing sql 'set @foo = 'bar', @baz = 'quux';
'...
2025-01-31T18:01:10.033Z DEBUG [sqlparser::parser] parsing expr
2025-01-31T18:01:10.033Z DEBUG [sqlparser::parser] prefix: Value(SingleQuotedString("bar"))
2025-01-31T18:01:10.033Z DEBUG [sqlparser::dialect] get_next_precedence_full() TokenWithSpan { token: Comma, span: Span(Location(1,17)..Location(1,18)) }
2025-01-31T18:01:10.033Z DEBUG [sqlparser::parser] next precedence: 0
2025-01-31T18:01:10.033Z DEBUG [sqlparser::parser] parsing expr
2025-01-31T18:01:10.034Z DEBUG [sqlparser::parser] prefix: Identifier(Ident { value: "@baz", quote_style: None, span: Span(Location(1,19)..Location(1,23)) })
2025-01-31T18:01:10.034Z DEBUG [sqlparser::dialect] get_next_precedence_full() TokenWithSpan { token: Eq, span: Span(Location(1,24)..Location(1,25)) }
2025-01-31T18:01:10.034Z DEBUG [sqlparser::parser] next precedence: 20
2025-01-31T18:01:10.034Z DEBUG [sqlparser::parser] parsing expr
2025-01-31T18:01:10.034Z DEBUG [sqlparser::parser] prefix: Value(SingleQuotedString("quux"))
2025-01-31T18:01:10.034Z DEBUG [sqlparser::dialect] get_next_precedence_full() TokenWithSpan { token: SemiColon, span: Span(Location(1,32)..Location(1,33)) }
2025-01-31T18:01:10.034Z DEBUG [sqlparser::parser] next precedence: 0
2025-01-31T18:01:10.034Z DEBUG [sqlparser::dialect] get_next_precedence_full() TokenWithSpan { token: SemiColon, span: Span(Location(1,32)..Location(1,33)) }
2025-01-31T18:01:10.034Z DEBUG [sqlparser::parser] next precedence: 0
Round-trip:
'SET @foo = 'bar', @baz = 'quux''
Parse results:
[
    SetVariable {
        scope: None,
        hivevar: false,
        variables: One(
            ObjectName(
                [
                    Identifier(
                        Ident {
                            value: "@foo",
                            quote_style: None,
                            span: Span(Location(1,5)..Location(1,9)),
                        },
                    ),
                ],
            ),
        ),
        value: [
            Value(
                SingleQuotedString(
                    "bar",
                ),
            ),
            BinaryOp {
                left: Identifier(
                    Ident {
                        value: "@baz",
                        quote_style: None,
                        span: Span(Location(1,19)..Location(1,23)),
                    },
                ),
                op: Eq,
                right: Value(
                    SingleQuotedString(
                        "quux",
                    ),
                ),
            },
        ],
    },
]

This round-trips okay, and could be sufficient for many use cases if they are willing to decipher the binary operators in the expressions. However, it might be appropriate to represent this differently in the AST, along the lines of:

SetVariables {
    assignments: [
        (
            Variable {
                scope: User,
                name: Ident {
                    value: "foo",
                },
            },
            Expr {
                value: Value(
                    SingleQuotedString(
                        "bar",
                    ),
                ),
            },
        ),
        (
            Variable {
                scope: User,
                name: Ident {
                    value: "baz",
                },
            },
            Expr {
                value: Value(
                    SingleQuotedString(
                        "quux",
                    ),
                ),
            },
        ),
    ],
},

This could be a bit annoying to parse unless we simply switch on the dialect at the very start of parse_set (MySQL doesn't support either Postgres- or Snowflake-style assignments).

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