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

Replace type_id() by trait method to allow wrapping dialects #1065

Merged
merged 2 commits into from
Dec 19, 2023
Merged

Replace type_id() by trait method to allow wrapping dialects #1065

merged 2 commits into from
Dec 19, 2023

Conversation

jjbayer
Copy link
Contributor

@jjbayer jjbayer commented Dec 14, 2023

Part of the dialect-specific behavior of the parser is defined in the methods of the Dialect trait, but other parts of the behavior depend on the dialect_of! macro, which in turn checks the Any::type_id of the current dialect.

Because if this, it is currently not possible to define variation of a dialect that is treated as the base dialect (e.g. MySQL) by the parser.

Ideally, all dialect-specific behavior would be defined in the implementation of the Dialect trait. As a workaround, this PR solves the problem by introducing an overridable trait method that returns the TypeId.

let res1 = Parser::parse_sql(&MySqlDialect {}, statement);
let res2 = Parser::parse_sql(&WrappedDialect(MySqlDialect {}), statement);
assert!(res1.is_ok());
assert_eq!(res1, res2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without overriding dialect(), this assertion fails with

Err(
    TokenizerError(
        "Unterminated string literal at Line: 1, Column 23",
    ),
)

@jjbayer jjbayer marked this pull request as ready for review December 14, 2023 15:04
jjbayer added a commit to getsentry/relay that referenced this pull request Dec 19, 2023
Some of the dialect-specific parsing logic did not apply because we use
a wrapped dialect `DialectWithParameters`, which has the wrong
`type_id()`.

Until sqlparser-rs/sqlparser-rs#1065 gets
merged, use a fork of `sqlparser` to make `DialectWithParameters` behave
like the underlying dialect.
Copy link
Collaborator

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me -- thank you @jjbayer

Ideally it would be nicer to have trait functions like supports_create_view() that control the parsers behavior rather than checking for specific dialects, as you mention, but that is a much larger change


#[test]
fn parse_with_wrapped_dialect() {
/// Wrapper for a dialect. In a real-world example, this wrapper
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👨‍🍳 👌 -- very nice example of creating a test illustrating the use case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@coveralls
Copy link

Pull Request Test Coverage Report for Build 7210682589

Warning: This coverage report may be inaccurate.

We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
To ensure accuracy in future PRs, please see these guidelines.
A quick fix for this PR: rebase it; your next report should be accurate.

  • 25 of 39 (64.1%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.05%) to 87.661%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/dialect/mod.rs 25 39 64.1%
Totals Coverage Status
Change from base Build 7009122777: -0.05%
Covered Lines: 18016
Relevant Lines: 20552

💛 - Coveralls

@alamb alamb merged commit 29b4ce8 into sqlparser-rs:main Dec 19, 2023
10 checks passed
@jjbayer
Copy link
Contributor Author

jjbayer commented Dec 21, 2023

@alamb thank you for accepting this PR, much appreciated!

jjbayer added a commit to getsentry/relay that referenced this pull request Feb 6, 2024
The `sqlparser` lib has been on a fork since
#2846. We can now go back to the
official version because
sqlparser-rs/sqlparser-rs#1065 has been merged &
released.
jjbayer added a commit to getsentry/relay that referenced this pull request Feb 6, 2024
The `sqlparser` lib has been on a fork since
#2846. We can now go back to the
official version because
sqlparser-rs/sqlparser-rs#1065 has been merged &
released.
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

Successfully merging this pull request may close these issues.

None yet

3 participants