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

Improve unparser MySQL compatibility #11589

Merged
merged 6 commits into from
Jul 23, 2024

Conversation

sgrebnov
Copy link
Member

@sgrebnov sgrebnov commented Jul 22, 2024

Please let me know if you prefer me to split this into separate PRs. Combined as the changes are related/follow the same logic and to avoid resolving conflicts after PRs are merged.

Which issue does this PR close?

PR addresses unparser issues producing invalid sql for MySQL and implements support for IntervalStyle::MySQL

1. Invalid CAST(col AS Timestamp) SQL for MySQL.

MySQL cast function does not support Timestamp for CAST and requires DATETIME

DATETIME[(M)]
Produces a DATETIME value. If the optional M value is given, it specifies the fractional seconds precision.

Timezone information is correctly handled by MySQL when casting with DATETIME.

2. Unsupported date_part function to extract Datetime subfield

MySQL does not support date_part function so PR introduces configurable option to specify Datetime subfield extraction style for unparsing. Different DBMSs follow different standards; popular ones are:

date_part('YEAR', date '2001-02-16')
EXTRACT(YEAR from date '2001-02-16')

Some DBMSs, like Postgres, support both, whereas others like MySQL, Amazon Athena require EXTRACT.

3. invalid CAST(col AS BigInt) SQL for MySQL.

MySQL cast function does not support BigInt and requires SIGNED for CAST

SIGNED [INTEGER]
Produces a signed BIGINT value.

Note: sqlparser crate does not have Signed type so we use ast::DataType::Custom as a workaround

Rationale for this change

PRs implements IntervalStyle::MySQL and adds support for alternate formats for Timestamp, Int64, Datetime part unparsing via Dialect customization.

What changes are included in this PR?

Please see above

Are these changes tested?

Manual testing + unit test for each modification.

Are there any user-facing changes?

CustomDialectBuilder now supports more options for Dialect customization (Timestamp, Int64 unparsing and Datetime part extraction).

@github-actions github-actions bot added the sql SQL Planner label Jul 22, 2024
Copy link
Contributor

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

Thank you @sgrebnov -- this makes sense to me

@@ -36,8 +42,8 @@ pub trait Dialect: Send + Sync {
true
}

// Does the dialect use TIMESTAMP to represent Date64 rather than DATETIME?
// E.g. Trino, Athena and Dremio does not have DATETIME data type
/// Does the dialect use TIMESTAMP to represent Date64 rather than DATETIME?
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

@alamb alamb merged commit 65dd364 into apache:main Jul 23, 2024
25 checks passed
@alamb
Copy link
Contributor

alamb commented Jul 23, 2024

Thanks again @sgrebnov

@sgrebnov
Copy link
Member Author

Thank you Andrew @alamb!

@phillipleblanc phillipleblanc deleted the sgrebnov/mysql-improvements branch July 23, 2024 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sql SQL Planner
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants