Skip to content

Add support of table function WITH ORDINALITY modifier for Postgre Parser #1337

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

Merged
merged 2 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,10 @@ pub enum TableFactor {
/// Optional version qualifier to facilitate table time-travel, as
/// supported by BigQuery and MSSQL.
version: Option<TableVersion>,
// Optional table function modifier to generate the ordinality for column.
/// For example, `SELECT * FROM generate_series(1, 10) WITH ORDINALITY AS t(a, b);`
/// [WITH ORDINALITY](https://www.postgresql.org/docs/current/functions-srf.html), supported by Postgres.
with_ordinality: bool,
/// [Partition selection](https://dev.mysql.com/doc/refman/8.0/en/partitioning-selection.html), supported by MySQL.
partitions: Vec<Ident>,
},
Expand Down Expand Up @@ -1285,6 +1289,7 @@ impl fmt::Display for TableFactor {
with_hints,
version,
partitions,
with_ordinality,
} => {
write!(f, "{name}")?;
if !partitions.is_empty() {
Expand All @@ -1293,6 +1298,9 @@ impl fmt::Display for TableFactor {
if let Some(args) = args {
write!(f, "({})", display_comma_separated(args))?;
}
if *with_ordinality {
write!(f, " WITH ORDINALITY")?;
}
if let Some(alias) = alias {
write!(f, " AS {alias}")?;
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ define_keywords!(
OR,
ORC,
ORDER,
ORDINALITY,
OUT,
OUTER,
OUTPUTFORMAT,
Expand Down
13 changes: 13 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9216,6 +9216,18 @@ impl<'a> Parser<'a> {
None
};

let mut with_ordinality = false;
if dialect_of!(self is PostgreSqlDialect|GenericDialect)
&& self.parse_keyword(Keyword::WITH)
{
if self.parse_keyword(Keyword::ORDINALITY) {
with_ordinality = true;
} else {
// rewind, as WITH may belong to the next statement's CTE or hints
self.prev_token();
}
}

let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;

// MSSQL-specific table hints:
Expand All @@ -9237,6 +9249,7 @@ impl<'a> Parser<'a> {
with_hints,
version,
partitions,
with_ordinality,
};

while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT]) {
Expand Down
2 changes: 2 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ pub fn table(name: impl Into<String>) -> TableFactor {
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
}
}

Expand All @@ -323,6 +324,7 @@ pub fn table_with_alias(name: impl Into<String>, alias: impl Into<String>) -> Ta
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ fn parse_delete_statement() {
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
},
from[0].relation
);
Expand Down Expand Up @@ -1353,6 +1354,7 @@ fn parse_table_identifiers() {
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
},
joins: vec![]
},]
Expand Down Expand Up @@ -1525,6 +1527,7 @@ fn parse_table_time_travel() {
Value::SingleQuotedString(version)
))),
partitions: vec![],
with_ordinality: false,
},
joins: vec![]
},]
Expand Down Expand Up @@ -1620,6 +1623,7 @@ fn parse_merge() {
with_hints: Default::default(),
version: Default::default(),
partitions: Default::default(),
with_ordinality: false,
},
table
);
Expand All @@ -1634,6 +1638,7 @@ fn parse_merge() {
with_hints: Default::default(),
version: Default::default(),
partitions: Default::default(),
with_ordinality: false,
},
source
);
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ fn parse_map_access_expr() {
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
},
joins: vec![],
}],
Expand Down Expand Up @@ -162,6 +163,7 @@ fn parse_delimited_identifiers() {
args,
with_hints,
version,
with_ordinality: _,
partitions: _,
} => {
assert_eq!(vec![Ident::with_quote('"', "a table")], name.0);
Expand Down
Loading
Loading