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

Expand handling of LIMIT 1, 2 handling to include sqlite #1447

Merged
merged 3 commits into from
Sep 30, 2024

Conversation

joshuawarner32
Copy link
Contributor

@joshuawarner32 joshuawarner32 commented Sep 28, 2024

Sqlite itself parses this syntax, so this parser should parse it when operating in sqlite mode.

Tangential fixups:

  • I also noticed that ClickhouseDialect was missing from all_dialects(), so I rectified that. This required excluding ClickhouseDialect from one existing test, but it works everywhere else.
  • Added TestedDialects::new(...) function to more cleanly be able to specify exactly the dialects this LIMIT comma syntax works for, in tests
  • Added a test for LIMIT 1, 2
  • (very minor) fix capitalization of is in doc comment for dialect_of! macro

@coveralls
Copy link

coveralls commented Sep 29, 2024

Pull Request Test Coverage Report for Build 11095970376

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 30 of 36 (83.33%) changed or added relevant lines in 8 files are covered.
  • 1065 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.009%) to 89.303%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/dialect/clickhouse.rs 1 2 50.0%
src/dialect/generic.rs 1 2 50.0%
src/dialect/mod.rs 1 2 50.0%
src/dialect/mysql.rs 1 2 50.0%
src/dialect/sqlite.rs 1 2 50.0%
tests/sqlparser_common.rs 10 11 90.91%
Files with Coverage Reduction New Missed Lines %
src/parser/alter.rs 28 76.07%
src/ast/ddl.rs 66 86.36%
src/parser/mod.rs 272 93.35%
tests/sqlparser_common.rs 300 89.57%
src/ast/mod.rs 399 82.4%
Totals Coverage Status
Change from base Build 11038149323: -0.009%
Covered Lines: 29937
Relevant Lines: 33523

💛 - Coveralls

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 @joshuawarner32 -- this looks like a very nice improvement. I left some comments but I don't think any is needed to merge this PR

FYI @iffyio

@@ -8715,7 +8715,7 @@ impl<'a> Parser<'a> {
offset = Some(self.parse_offset()?)
}

if dialect_of!(self is GenericDialect | MySqlDialect | ClickHouseDialect)
if dialect_of!(self is GenericDialect | MySqlDialect | ClickHouseDialect | SQLiteDialect)
Copy link
Contributor

Choose a reason for hiding this comment

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

Something else we have been discussing is moving from this dialect_of macro to a method on the Dialect trait instead (the current code uses both patterns)

So it would look something like

if self.dialect.supports_limit_comma() ...

The benefits are that user defined Dialects can control the behavior and that there is a better place for documenting the behavior

Maybe something to think about for a future PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense to me! Super easy, so will include this in the next commit to this PR.

Box::new(DuckDbDialect {}) as Box<dyn Dialect>,
Box::new(DatabricksDialect {}) as Box<dyn Dialect>,
];
specific_dialects(vec![
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

}

/// Returns a specific set of dialects.
pub fn specific_dialects(dialects: Vec<Box<dyn Dialect>>) -> TestedDialects {
Copy link
Contributor

Choose a reason for hiding this comment

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

This could also potentially be a constructor on TestedDialects

So like

impl TestedDialects {
  pub fn new(dialects: Vec<Box<dyn Dialect>>) -> Self {
    Self { 
     dialects, 
     options: None,
    }
  }
}

Perhaps

@@ -6795,7 +6795,8 @@ fn parse_create_view_with_options() {
#[test]
fn parse_create_view_with_columns() {
let sql = "CREATE VIEW v (has, cols) AS SELECT 1, 2";
match verified_stmt(sql) {
// TODO: why does this fail for ClickHouseDialect?
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you perhaps file a ticket to track this isse?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do!

@@ -8587,17 +8588,26 @@ fn verified_expr(query: &str) -> Expr {

#[test]
fn parse_offset_and_limit() {
let sql = "SELECT foo FROM bar LIMIT 2 OFFSET 2";
let sql = "SELECT foo FROM bar LIMIT 1 OFFSET 2";
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 that is a good change

@@ -60,7 +60,7 @@ use alloc::boxed::Box;

/// Convenience check if a [`Parser`] uses a certain dialect.
///
/// `dialect_of!(parser Is SQLiteDialect | GenericDialect)` evaluates
/// `dialect_of!(parser is SQLiteDialect | GenericDialect)` evaluates
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

…ts_limit_comma, add reference to newly discovered ClickHouseDialect issue
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM!

@alamb alamb merged commit 1e0460a into apache:main Sep 30, 2024
10 checks passed
@alamb
Copy link
Contributor

alamb commented Sep 30, 2024

Thanks again @joshuawarner32 and @iffyio

@joshuawarner32 joshuawarner32 deleted the expand-limit-comma-syntax-to-sqlite branch September 30, 2024 19:57
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.

4 participants