Skip to content

Accept CREATE SEQUENCE options in any order - #2414

Open
LucaCappelletti94 wants to merge 2 commits into
apache:mainfrom
LucaCappelletti94:pg-create-sequence-unordered-options
Open

LucaCappelletti94 wants to merge 2 commits into
apache:mainfrom
LucaCappelletti94:pg-create-sequence-unordered-options

Conversation

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

CREATE SEQUENCE options were parsed by a fixed chain of independent if blocks, one per option, so only a single order was accepted. CREATE SEQUENCE s INCREMENT BY 1 START WITH 1 parsed, but CREATE SEQUENCE s START WITH 1 INCREMENT BY 1 left the INCREMENT keyword unconsumed and failed with Expected: end of statement. PostgreSQL treats these as an unordered list.

Therefore, following the same pattern we used in other methods, parse_create_sequence_options now loops, dispatching on whichever option keyword comes next and breaking when none matches, and rejects repeated options. Options are recorded in the order written, so Display round-trips the input unchanged.

@LucaCappelletti94 LucaCappelletti94 changed the title Accept CREATE SEQUENCE options in any order Accept CREATE SEQUENCE options in any order Jul 30, 2026
@LucaCappelletti94
LucaCappelletti94 marked this pull request as ready for review July 30, 2026 16:29
@LucaCappelletti94
LucaCappelletti94 force-pushed the pg-create-sequence-unordered-options branch 2 times, most recently from f0ba878 to b5f4edb Compare August 24, 2026 11:45
@LucaCappelletti94
LucaCappelletti94 force-pushed the pg-create-sequence-unordered-options branch from b5f4edb to fb40c1a Compare September 3, 2026 17:05
@LucaCappelletti94
LucaCappelletti94 force-pushed the pg-create-sequence-unordered-options branch 4 times, most recently from 2f43c43 to 15c849c Compare September 22, 2026 21:20
@codecov-commenter

codecov-commenter commented Sep 23, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.33%. Comparing base (ada8314) to head (3c856d5).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/parser/mod.rs 89.79% 0 Missing and 5 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2414      +/-   ##
==========================================
+ Coverage   81.28%   81.33%   +0.05%     
==========================================
  Files          42       42              
  Lines       33920    33952      +32     
  Branches    33920    33952      +32     
==========================================
+ Hits        27571    27615      +44     
+ Misses       2785     2783       -2     
+ Partials     3564     3554      -10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94
LucaCappelletti94 force-pushed the pg-create-sequence-unordered-options branch from 4d8634f to b296c52 Compare October 2, 2026 16:15
@LucaCappelletti94
LucaCappelletti94 force-pushed the pg-create-sequence-unordered-options branch from b296c52 to ff3cc30 Compare October 17, 2026 01:00
@LucaCappelletti94 LucaCappelletti94 added enhancement New feature or request waiting on reviewer PR needs to be reviewed labels Oct 18, 2026
@LucaCappelletti94
LucaCappelletti94 force-pushed the pg-create-sequence-unordered-options branch from ff3cc30 to 1bde6d1 Compare October 18, 2026 14:00

@s5dsn-eqee s5dsn-eqee left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Great idea, but it needs some minor changes

Comment thread tests/sqlparser_postgres.rs Outdated
}
pg().verified_stmt(r#"SELECT a AS "x" FROM t AS "y""#);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"CREATE SEQUENCE and the identity paths are not dialect-gated, so per AGENTS.md pls add more tests to sqlparser_common.rs.

 #[test]
  fn parse_sequence_options_in_any_order() {
      let d = all_dialects();
      d.verified_stmt("CREATE SEQUENCE s START WITH 1 INCREMENT BY 1");
      d.verified_stmt("CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY ( START WITH 10 INCREMENT BY 1 CYCLE ))");
      d.verified_stmt("ALTER TABLE t ALTER COLUMN id ADD GENERATED AS IDENTITY ( CACHE 2 INCREMENT 1 )");
      for (sql, err) in [
          ("CREATE SEQUENCE s INCREMENT 1 INCREMENT BY 2", "INCREMENT specified more than once"),
          ("CREATE SEQUENCE s MAXVALUE 1 NO MAXVALUE", "MAXVALUE | NO MAXVALUE specified more than once"),
          ("CREATE SEQUENCE s CACHE 1 CACHE 2", "CACHE specified more than once"),
          ("CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY (START 1 START 2))", "START specified more than once"),
          ("CREATE SEQUENCE s AS", "Expected: a data type name, found: EOF"),
          ("CREATE SEQUENCE s OWNED BY", "Expected: identifier, found: EOF"),
      ] {
          assert_eq!(d.parse_sql_statements(sql).unwrap_err(), ParserError::ParserError(err.to_string()));
      }
      // Identity columns take the shared list without AS / OWNED BY.
      for sql in [
          "CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY (AS BIGINT))",
          "CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY (OWNED BY t.id))",
      ] {
          d.parse_sql_statements(sql).unwrap_err();
      }
  }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Moved

Comment thread src/parser/mod.rs
} else if allow_type_and_owner && self.parse_keyword(Keyword::AS) {
//[ AS data_type ]
(SequenceOptions::DataType(self.parse_data_type()?), "AS")
} else if allow_type_and_owner && self.parse_keywords(&[Keyword::OWNED, Keyword::BY]) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PostgreSQL's grammar takes any qualified name after OWNED BY and only the single bare word none means no owner.

https://www.postgresql.org/docs/current/sql-createsequence.html#SQL-CREATESEQUENCE-PARAMETERS

Pls fix and add test

pg().verified_stmt("CREATE SEQUENCE s OWNED BY none.c")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added as a red test and fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PostgreSQL waiting on reviewer PR needs to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants