Skip to content

Commit ceb9496

Browse files
committed
support rename
1 parent 5327f0c commit ceb9496

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/ast/query.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,6 +2684,12 @@ pub enum PipeOperator {
26842684
/// Syntax: `|> TABLESAMPLE SYSTEM (10 PERCENT)
26852685
/// See more at <https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#tablesample_pipe_operator>
26862686
TableSample { sample: Box<TableSample> },
2687+
/// Renames columns in the input table.
2688+
///
2689+
/// Syntax: `|> RENAME old_name AS new_name, ...`
2690+
///
2691+
/// See more at <https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#rename_pipe_operator>
2692+
Rename { mappings: Vec<IdentWithAlias> },
26872693
}
26882694

26892695
impl fmt::Display for PipeOperator {
@@ -2739,6 +2745,9 @@ impl fmt::Display for PipeOperator {
27392745
PipeOperator::TableSample { sample } => {
27402746
write!(f, "{}", sample)
27412747
}
2748+
PipeOperator::Rename { mappings } => {
2749+
write!(f, "RENAME {}", display_comma_separated(mappings))
2750+
}
27422751
}
27432752
}
27442753
}

src/parser/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9947,6 +9947,13 @@ impl<'a> Parser<'a> {
99479947
Ok(IdentWithAlias { ident, alias })
99489948
}
99499949

9950+
pub fn parse_identifier_with_optional_alias(&mut self) -> Result<IdentWithAlias, ParserError> {
9951+
let ident = self.parse_identifier()?;
9952+
let _after_as = self.parse_keyword(Keyword::AS);
9953+
let alias = self.parse_identifier()?;
9954+
Ok(IdentWithAlias { ident, alias })
9955+
}
9956+
99509957
/// Optionally parses an alias for a select list item
99519958
fn maybe_parse_select_item_alias(&mut self) -> Result<Option<Ident>, ParserError> {
99529959
fn validator(explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
@@ -11076,6 +11083,7 @@ impl<'a> Parser<'a> {
1107611083
Keyword::AGGREGATE,
1107711084
Keyword::ORDER,
1107811085
Keyword::TABLESAMPLE,
11086+
Keyword::RENAME,
1107911087
])?;
1108011088
match kw {
1108111089
Keyword::SELECT => {
@@ -11142,6 +11150,10 @@ impl<'a> Parser<'a> {
1114211150
let sample = self.parse_table_sample(TableSampleModifier::TableSample)?;
1114311151
pipe_operators.push(PipeOperator::TableSample { sample });
1114411152
}
11153+
Keyword::RENAME => {
11154+
let mappings = self.parse_comma_separated(Parser::parse_identifier_with_optional_alias)?;
11155+
pipe_operators.push(PipeOperator::Rename { mappings });
11156+
}
1114511157
unhandled => {
1114611158
return Err(ParserError::ParserError(format!(
1114711159
"`expect_one_of_keywords` further up allowed unhandled keyword: {unhandled:?}"

tests/sqlparser_common.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15193,6 +15193,14 @@ fn parse_pipeline_operator() {
1519315193
dialects.verified_stmt("SELECT * FROM tbl |> TABLESAMPLE SYSTEM (50 PERCENT)");
1519415194
dialects.verified_stmt("SELECT * FROM tbl |> TABLESAMPLE SYSTEM (50) REPEATABLE (10)");
1519515195

15196+
// rename pipe operator
15197+
dialects.verified_stmt("SELECT * FROM users |> RENAME old_name AS new_name");
15198+
dialects.verified_stmt("SELECT * FROM users |> RENAME id AS user_id, name AS user_name");
15199+
dialects.verified_query_with_canonical(
15200+
"SELECT * FROM users |> RENAME id user_id",
15201+
"SELECT * FROM users |> RENAME id AS user_id",
15202+
);
15203+
1519615204
// many pipes
1519715205
dialects.verified_stmt(
1519815206
"SELECT * FROM CustomerOrders |> AGGREGATE SUM(cost) AS total_cost GROUP BY customer_id, state, item_type |> EXTEND COUNT(*) OVER (PARTITION BY customer_id) AS num_orders |> WHERE num_orders > 1 |> AGGREGATE AVG(total_cost) AS average GROUP BY state DESC, item_type ASC",

0 commit comments

Comments
 (0)