Skip to content

Commit 1e3bea0

Browse files
committed
better variant docs, rename Alias -> As
1 parent 496131c commit 1e3bea0

2 files changed

Lines changed: 68 additions & 41 deletions

File tree

src/ast/query.rs

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,35 +2413,76 @@ impl fmt::Display for OffsetRows {
24132413
}
24142414
}
24152415

2416+
/// Pipe syntax, first introduced in Google BigQuery.
2417+
/// Example:
2418+
///
2419+
/// ```sql
2420+
/// FROM Produce
2421+
/// |> WHERE sales > 0
2422+
/// |> AGGREGATE SUM(sales) AS total_sales, COUNT(*) AS num_sales
2423+
/// GROUP BY item;
2424+
/// ```
2425+
///
2426+
/// See https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#pipe_syntax
24162427
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
24172428
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
24182429
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
24192430
pub enum PipeOperator {
2420-
Limit {
2421-
expr: Expr,
2422-
offset: Option<Expr>,
2423-
},
2424-
Where {
2425-
expr: Expr,
2426-
},
2427-
OrderBy {
2428-
exprs: Vec<OrderByExpr>,
2429-
},
2430-
Select {
2431-
exprs: Vec<SelectItem>,
2432-
},
2433-
Extend {
2434-
exprs: Vec<SelectItem>,
2435-
},
2436-
Set {
2437-
assignments: Vec<Assignment>,
2438-
},
2439-
Drop {
2440-
columns: Vec<Ident>,
2441-
},
2442-
Alias {
2443-
alias: Ident,
2444-
},
2431+
/// Limits the number of rows to return in a query, with an optional OFFSET clause to skip over rows.
2432+
///
2433+
/// Syntax: `|> LIMIT <n> [OFFSET <m>]`
2434+
///
2435+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#limit_pipe_operator
2436+
Limit { expr: Expr, offset: Option<Expr> },
2437+
/// Filters the results of the input table.
2438+
///
2439+
/// Syntax: `|> WHERE <condition>`
2440+
///
2441+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#where_pipe_operator
2442+
Where { expr: Expr },
2443+
/// ORDER BY <expr> [ASC|DESC], ...
2444+
OrderBy { exprs: Vec<OrderByExpr> },
2445+
/// Produces a new table with the listed columns, similar to the outermost SELECT clause in a table subquery in standard syntax.
2446+
///
2447+
/// Syntax `|> SELECT <expr> [[AS] alias], ...`
2448+
///
2449+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#select_pipe_operator
2450+
Select { exprs: Vec<SelectItem> },
2451+
/// Propagates the existing table and adds computed columns, similar to SELECT *, new_column in standard syntax.
2452+
///
2453+
/// Syntax: `|> EXTEND <expr> [[AS] alias], ...`
2454+
///
2455+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#extend_pipe_operator
2456+
Extend { exprs: Vec<SelectItem> },
2457+
/// Replaces the value of a column in the current table, similar to SELECT * REPLACE (expression AS column) in standard syntax.
2458+
///
2459+
/// Syntax: `|> SET <column> = <expression>, ...`
2460+
///
2461+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#set_pipe_operator
2462+
Set { assignments: Vec<Assignment> },
2463+
/// Removes listed columns from the current table, similar to SELECT * EXCEPT (column) in standard syntax.
2464+
///
2465+
/// Syntax: `|> DROP <column>, ...`
2466+
///
2467+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#drop_pipe_operator
2468+
Drop { columns: Vec<Ident> },
2469+
/// Introduces a table alias for the input table, similar to applying the AS alias clause on a table subquery in standard syntax.
2470+
///
2471+
/// Syntax: `|> AS <alias>`
2472+
///
2473+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#as_pipe_operator
2474+
As { alias: Ident },
2475+
/// Performs aggregation on data across grouped rows or an entire table.
2476+
///
2477+
/// Syntax: `|> AGGREGATE <agg_expr> [[AS] alias], ...`
2478+
///
2479+
/// Syntax:
2480+
/// ```
2481+
/// |> AGGREGATE [<agg_expr> [[AS] alias], ...]
2482+
/// GROUP BY <grouping_expr> [AS alias], ...
2483+
/// ```
2484+
///
2485+
/// See more at https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#aggregate_pipe_operator
24452486
Aggregate {
24462487
full_table_exprs: Vec<ExprWithAlias>,
24472488
group_by_exprs: Vec<ExprWithAlias>,
@@ -2463,7 +2504,7 @@ impl fmt::Display for PipeOperator {
24632504
PipeOperator::Drop { columns } => {
24642505
write!(f, "DROP {}", display_comma_separated(columns.as_slice()))
24652506
}
2466-
PipeOperator::Alias { alias } => {
2507+
PipeOperator::As { alias } => {
24672508
write!(f, "AS {}", alias)
24682509
}
24692510
PipeOperator::Limit { expr, offset } => {

src/parser/mod.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10336,37 +10336,30 @@ impl<'a> Parser<'a> {
1033610336
Keyword::ORDER,
1033710337
])?;
1033810338
match kw {
10339-
// SELECT <expr> [[AS] alias], ...
1034010339
Keyword::SELECT => {
1034110340
let exprs = self.parse_comma_separated(Parser::parse_select_item)?;
1034210341
pipe_operators.push(PipeOperator::Select { exprs })
1034310342
}
10344-
// EXTEND <expr> [[AS] alias], ...
1034510343
Keyword::EXTEND => {
1034610344
let exprs = self.parse_comma_separated(Parser::parse_select_item)?;
1034710345
pipe_operators.push(PipeOperator::Extend { exprs })
1034810346
}
10349-
// SET <column> = <expression>, ...
1035010347
Keyword::SET => {
1035110348
let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
1035210349
pipe_operators.push(PipeOperator::Set { assignments })
1035310350
}
10354-
// DROP <column>, ...
1035510351
Keyword::DROP => {
1035610352
let columns = self.parse_identifiers()?;
1035710353
pipe_operators.push(PipeOperator::Drop { columns })
1035810354
}
10359-
// AS <alias>
1036010355
Keyword::AS => {
1036110356
let alias = self.parse_identifier()?;
10362-
pipe_operators.push(PipeOperator::Alias { alias })
10357+
pipe_operators.push(PipeOperator::As { alias })
1036310358
}
10364-
// WHERE <condition>
1036510359
Keyword::WHERE => {
1036610360
let expr = self.parse_expr()?;
1036710361
pipe_operators.push(PipeOperator::Where { expr })
1036810362
}
10369-
// LIMIT <n> [OFFSET <m>]
1037010363
Keyword::LIMIT => {
1037110364
let expr = self.parse_expr()?;
1037210365
let offset = if self.parse_keyword(Keyword::OFFSET) {
@@ -10376,12 +10369,6 @@ impl<'a> Parser<'a> {
1037610369
};
1037710370
pipe_operators.push(PipeOperator::Limit { expr, offset })
1037810371
}
10379-
// AGGREGATE <agg_expr> [[AS] alias], ...
10380-
//
10381-
// and
10382-
//
10383-
// AGGREGATE [<agg_expr> [[AS] alias], ...]
10384-
// GROUP BY <grouping_expr> [AS alias], ...
1038510372
Keyword::AGGREGATE => {
1038610373
let full_table_exprs = self.parse_comma_separated0(
1038710374
|parser| {
@@ -10407,7 +10394,6 @@ impl<'a> Parser<'a> {
1040710394
group_by_exprs,
1040810395
})
1040910396
}
10410-
// ORDER BY <expr> [ASC|DESC], ...
1041110397
Keyword::ORDER => {
1041210398
self.expect_one_of_keywords(&[Keyword::BY])?;
1041310399
let exprs = self.parse_comma_separated(Parser::parse_order_by_expr)?;

0 commit comments

Comments
 (0)