Skip to content

Better representation for MySQL list of variable assignments #1697

Closed
@mvzink

Description

@mvzink

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).

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