Skip to content

Better representation for MySQL list of variable assignments #1697

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

Closed
mvzink opened this issue Jan 31, 2025 · 1 comment
Closed

Better representation for MySQL list of variable assignments #1697

mvzink opened this issue Jan 31, 2025 · 1 comment

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

readysetbot pushed a commit to readysettech/readyset that referenced this issue Feb 26, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further requires [sqlparser#1697] getting fixed.

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Feb 26, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Feb 26, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Feb 28, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Feb 28, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Mar 5, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Mar 5, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Mar 5, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Mar 5, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
readysetbot pushed a commit to readysettech/readyset that referenced this issue Mar 5, 2025
This was caught by logictests and some framework tests. Several
framework tests hit one of these conditions, but I don't believe we'll
inspect such statements anyway. If we do, we'll improve this conversion
at a later date.

Likely, improving this further will require fixing [sqlparser#1697].

[sqlparser#1697]: apache/datafusion-sqlparser-rs#1697

Change-Id: I2ddfc73a8b6d86d4aec8b4d4fefab86511a01b48
Reviewed-on: https://gerrit.readyset.name/c/readyset/+/8910
Tested-by: Buildkite CI
Reviewed-by: Marcelo Altmann <[email protected]>
@mvzink
Copy link
Contributor Author

mvzink commented Mar 12, 2025

Closed by #1757

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