Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/bigquery_3042_pipe_named_window.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM t |> SELECT k, SUM(v) OVER w AS s WINDOW w AS (PARTITION BY k) |> EXTEND RANK() OVER w AS r WINDOW w AS (ORDER BY s)
35 changes: 29 additions & 6 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3317,21 +3317,25 @@ pub enum PipeOperator {
},
/// Produces a new table with the listed columns, similar to the outermost SELECT clause in a table subquery in standard syntax.
///
/// Syntax `|> SELECT <expr> [[AS] alias], ...`
/// Syntax `|> SELECT <expr> [[AS] alias], ... [WINDOW <name> AS <window_spec>, ...]`
///
/// See more at <https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#select_pipe_operator>
Select {
/// The select items to produce.
exprs: Vec<SelectItem>,
/// Named windows from the `WINDOW` clause, visible only to this operator.
named_window: Vec<NamedWindowDefinition>,
},
/// Propagates the existing table and adds computed columns, similar to SELECT *, new_column in standard syntax.
///
/// Syntax: `|> EXTEND <expr> [[AS] alias], ...`
/// Syntax: `|> EXTEND <expr> [[AS] alias], ... [WINDOW <name> AS <window_spec>, ...]`
///
/// See more at <https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#extend_pipe_operator>
Extend {
/// Expressions defining added columns.
exprs: Vec<SelectItem>,
/// Named windows from the `WINDOW` clause, visible only to this operator.
named_window: Vec<NamedWindowDefinition>,
},
/// Replaces the value of a column in the current table, similar to SELECT * REPLACE (expression AS column) in standard syntax.
///
Expand Down Expand Up @@ -3481,11 +3485,19 @@ pub enum PipeOperator {
impl fmt::Display for PipeOperator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PipeOperator::Select { exprs } => {
write!(f, "SELECT {}", display_comma_separated(exprs.as_slice()))
PipeOperator::Select {
exprs,
named_window,
} => {
write!(f, "SELECT {}", display_comma_separated(exprs.as_slice()))?;
Self::fmt_named_window(f, named_window)
}
PipeOperator::Extend { exprs } => {
write!(f, "EXTEND {}", display_comma_separated(exprs.as_slice()))
PipeOperator::Extend {
exprs,
named_window,
} => {
write!(f, "EXTEND {}", display_comma_separated(exprs.as_slice()))?;
Self::fmt_named_window(f, named_window)
}
PipeOperator::Set { assignments } => {
write!(f, "SET {}", display_comma_separated(assignments.as_slice()))
Expand Down Expand Up @@ -3594,6 +3606,17 @@ impl PipeOperator {
Ok(())
}

/// Helper function to format the optional `WINDOW` clause of pipe operators
fn fmt_named_window(
f: &mut fmt::Formatter<'_>,
named_window: &[NamedWindowDefinition],
) -> fmt::Result {
if !named_window.is_empty() {
write!(f, " WINDOW {}", display_comma_separated(named_window))?;
}
Ok(())
}

/// Helper function to format set operations (UNION, INTERSECT, EXCEPT) with queries
fn fmt_set_operation(
f: &mut fmt::Formatter<'_>,
Expand Down
20 changes: 18 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16194,11 +16194,27 @@ impl<'a> Parser<'a> {
match kw {
Keyword::SELECT => {
let exprs = self.parse_comma_separated(Parser::parse_select_item)?;
pipe_operators.push(PipeOperator::Select { exprs })
let named_window = if self.parse_keyword(Keyword::WINDOW) {
self.parse_comma_separated(Parser::parse_named_window)?
} else {
vec![]
};
pipe_operators.push(PipeOperator::Select {
exprs,
named_window,
})
}
Keyword::EXTEND => {
let exprs = self.parse_comma_separated(Parser::parse_select_item)?;
pipe_operators.push(PipeOperator::Extend { exprs })
let named_window = if self.parse_keyword(Keyword::WINDOW) {
self.parse_comma_separated(Parser::parse_named_window)?
} else {
vec![]
};
pipe_operators.push(PipeOperator::Extend {
exprs,
named_window,
})
}
Keyword::SET => {
let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3013,3 +3013,16 @@ fn test_byte_and_raw_string_quote_escaping() {
err
);
}

#[test]
fn parse_pipe_operator_named_window() {
bigquery().verified_stmt(
"FROM Produce |> SELECT item, sales, category, SUM(sales) OVER item_window AS category_total WINDOW item_window AS (PARTITION BY category)",
);
bigquery().verified_stmt(
"FROM Produce |> EXTEND SUM(sales) OVER item_window AS category_total WINDOW item_window AS (PARTITION BY category)",
);
bigquery().verified_stmt(
"FROM t |> EXTEND SUM(v) OVER w2 AS s WINDOW w1 AS (PARTITION BY k), w2 AS w1",
);
}
49 changes: 49 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23916,6 +23916,55 @@ fn parse_quantified_comparison_with_arbitrary_operator() {
}
}

#[test]
fn parse_pipe_operator_named_window() {
let dialects = all_dialects_where(|d| d.supports_pipe_operator());
let expected = vec![NamedWindowDefinition(
Ident::new("w"),
NamedWindowExpr::WindowSpec(WindowSpec {
window_name: None,
partition_by: vec![Expr::Identifier(Ident::new("k"))],
order_by: vec![],
window_frame: None,
}),
)];
for sql in [
"SELECT * FROM t |> SELECT k, SUM(v) OVER w AS s WINDOW w AS (PARTITION BY k)",
"SELECT * FROM t |> EXTEND SUM(v) OVER w AS s WINDOW w AS (PARTITION BY k)",
] {
match &dialects.verified_query(sql).pipe_operators[..] {
[PipeOperator::Select { named_window, .. }]
| [PipeOperator::Extend { named_window, .. }] => assert_eq!(named_window, &expected),
other => panic!("unexpected pipe operators: {other:?}"),
}
}
dialects.verified_stmt(
"SELECT * FROM t |> EXTEND SUM(v) OVER w1 AS s, RANK() OVER w2 AS r WINDOW w1 AS (PARTITION BY k), w2 AS (ORDER BY v DESC) |> WHERE r = 1",
);

assert_eq!(
dialects
.parse_sql_statements("SELECT * FROM t |> SELECT SUM(v) OVER w AS s WINDOW")
.unwrap_err(),
ParserError::ParserError("Expected: identifier, found: EOF".to_string())
);
assert_eq!(
dialects
.parse_sql_statements(
"SELECT * FROM t |> EXTEND SUM(v) OVER w AS s WINDOW w (PARTITION BY k)"
)
.unwrap_err(),
ParserError::ParserError("Expected: AS, found: (".to_string())
);
// WINDOW belongs to SELECT and EXTEND only
assert_eq!(
dialects
.parse_sql_statements("SELECT * FROM t |> WHERE a > 1 WINDOW w AS (PARTITION BY k)")
.unwrap_err(),
ParserError::ParserError("Expected: end of statement, found: WINDOW".to_string())
);
}

#[test]
#[cfg(feature = "recursive-protection")]
fn nested_derived_tables_do_not_overflow_the_stack() {
Expand Down