Skip to content

Commit 4739c45

Browse files
committed
Support TABLESAMPLE pipe operator
1 parent 3017265 commit 4739c45

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/ast/query.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,6 +2680,10 @@ pub enum PipeOperator {
26802680
full_table_exprs: Vec<ExprWithAliasAndOrderBy>,
26812681
group_by_expr: Vec<ExprWithAliasAndOrderBy>,
26822682
},
2683+
/// Selects a random sample of rows from the input table.
2684+
/// Syntax: `|> TABLESAMPLE <method> (<size> {ROWS | PERCENT})`
2685+
/// See more at <https://cloud.google.com/bigquery/docs/reference/standard-sql/pipe-syntax#tablesample_pipe_operator>
2686+
TableSample { sample: Box <TableSample> },
26832687
}
26842688

26852689
impl fmt::Display for PipeOperator {
@@ -2731,6 +2735,10 @@ impl fmt::Display for PipeOperator {
27312735
PipeOperator::OrderBy { exprs } => {
27322736
write!(f, "ORDER BY {}", display_comma_separated(exprs.as_slice()))
27332737
}
2738+
2739+
PipeOperator::TableSample { sample } => {
2740+
write!(f, " {}", sample)
2741+
}
27342742
}
27352743
}
27362744
}

src/parser/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11047,6 +11047,7 @@ impl<'a> Parser<'a> {
1104711047
Keyword::LIMIT,
1104811048
Keyword::AGGREGATE,
1104911049
Keyword::ORDER,
11050+
Keyword::TABLESAMPLE,
1105011051
])?;
1105111052
match kw {
1105211053
Keyword::SELECT => {
@@ -11109,6 +11110,12 @@ impl<'a> Parser<'a> {
1110911110
let exprs = self.parse_comma_separated(Parser::parse_order_by_expr)?;
1111011111
pipe_operators.push(PipeOperator::OrderBy { exprs })
1111111112
}
11113+
Keyword::TABLESAMPLE => {
11114+
if let Some(sample) = self.maybe_parse_table_sample()? {
11115+
pipe_operators.push(PipeOperator::TableSample { sample });
11116+
};
11117+
11118+
}
1111211119
unhandled => {
1111311120
return Err(ParserError::ParserError(format!(
1111411121
"`expect_one_of_keywords` further up allowed unhandled keyword: {unhandled:?}"

tests/sqlparser_common.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15155,6 +15155,12 @@ fn parse_pipeline_operator() {
1515515155
dialects.verified_stmt("SELECT * FROM users |> ORDER BY id DESC");
1515615156
dialects.verified_stmt("SELECT * FROM users |> ORDER BY id DESC, name ASC");
1515715157

15158+
// tablesample pipe operator
15159+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE BERNOULLI (50)");
15160+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE SYSTEM (50)");
15161+
// TODO: Technically, REPEATABLE is not available in BigQuery, but it is used with TABLESAMPLE in other dialects
15162+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE SYSTEM (50) REPEATABLE (10)");
15163+
1515815164
// many pipes
1515915165
dialects.verified_stmt(
1516015166
"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)