Skip to content

Commit caacef8

Browse files
committed
Implement tablesample
1 parent 4739c45 commit caacef8

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

src/ast/query.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl fmt::Display for Query {
104104
format.fmt(f)?;
105105
}
106106
for pipe_operator in &self.pipe_operators {
107-
f.write_str(" |> ")?;
107+
f.write_str(" |>")?;
108108
pipe_operator.fmt(f)?;
109109
}
110110
Ok(())
@@ -2690,22 +2690,22 @@ impl fmt::Display for PipeOperator {
26902690
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26912691
match self {
26922692
PipeOperator::Select { exprs } => {
2693-
write!(f, "SELECT {}", display_comma_separated(exprs.as_slice()))
2693+
write!(f, " SELECT {}", display_comma_separated(exprs.as_slice()))
26942694
}
26952695
PipeOperator::Extend { exprs } => {
2696-
write!(f, "EXTEND {}", display_comma_separated(exprs.as_slice()))
2696+
write!(f, " EXTEND {}", display_comma_separated(exprs.as_slice()))
26972697
}
26982698
PipeOperator::Set { assignments } => {
2699-
write!(f, "SET {}", display_comma_separated(assignments.as_slice()))
2699+
write!(f, " SET {}", display_comma_separated(assignments.as_slice()))
27002700
}
27012701
PipeOperator::Drop { columns } => {
2702-
write!(f, "DROP {}", display_comma_separated(columns.as_slice()))
2702+
write!(f, " DROP {}", display_comma_separated(columns.as_slice()))
27032703
}
27042704
PipeOperator::As { alias } => {
2705-
write!(f, "AS {}", alias)
2705+
write!(f, " AS {}", alias)
27062706
}
27072707
PipeOperator::Limit { expr, offset } => {
2708-
write!(f, "LIMIT {}", expr)?;
2708+
write!(f, " LIMIT {}", expr)?;
27092709
if let Some(offset) = offset {
27102710
write!(f, " OFFSET {}", offset)?;
27112711
}
@@ -2715,7 +2715,7 @@ impl fmt::Display for PipeOperator {
27152715
full_table_exprs,
27162716
group_by_expr,
27172717
} => {
2718-
write!(f, "AGGREGATE")?;
2718+
write!(f, " AGGREGATE")?;
27192719
if !full_table_exprs.is_empty() {
27202720
write!(
27212721
f,
@@ -2730,14 +2730,14 @@ impl fmt::Display for PipeOperator {
27302730
}
27312731

27322732
PipeOperator::Where { expr } => {
2733-
write!(f, "WHERE {}", expr)
2733+
write!(f, " WHERE {}", expr)
27342734
}
27352735
PipeOperator::OrderBy { exprs } => {
2736-
write!(f, "ORDER BY {}", display_comma_separated(exprs.as_slice()))
2736+
write!(f, " ORDER BY {}", display_comma_separated(exprs.as_slice()))
27372737
}
27382738

27392739
PipeOperator::TableSample { sample } => {
2740-
write!(f, " {}", sample)
2740+
write!(f, "{}", sample)
27412741
}
27422742
}
27432743
}

src/parser/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11111,10 +11111,8 @@ impl<'a> Parser<'a> {
1111111111
pipe_operators.push(PipeOperator::OrderBy { exprs })
1111211112
}
1111311113
Keyword::TABLESAMPLE => {
11114-
if let Some(sample) = self.maybe_parse_table_sample()? {
11115-
pipe_operators.push(PipeOperator::TableSample { sample });
11116-
};
11117-
11114+
let sample = self.parse_table_sample(TableSampleModifier::TableSample)?;
11115+
pipe_operators.push(PipeOperator::TableSample { sample });
1111811116
}
1111911117
unhandled => {
1112011118
return Err(ParserError::ParserError(format!(
@@ -12760,8 +12758,11 @@ impl<'a> Parser<'a> {
1276012758
} else {
1276112759
return Ok(None);
1276212760
};
12761+
self.parse_table_sample(modifier).map(|sample| Some(sample))
12762+
}
1276312763

12764-
let name = match self.parse_one_of_keywords(&[
12764+
fn parse_table_sample(&mut self, modifier: TableSampleModifier ) -> Result<Box<TableSample>, ParserError> {
12765+
let name = match self.parse_one_of_keywords(&[
1276512766
Keyword::BERNOULLI,
1276612767
Keyword::ROW,
1276712768
Keyword::SYSTEM,
@@ -12842,14 +12843,14 @@ impl<'a> Parser<'a> {
1284212843
None
1284312844
};
1284412845

12845-
Ok(Some(Box::new(TableSample {
12846+
Ok(Box::new(TableSample {
1284612847
modifier,
1284712848
name,
1284812849
quantity,
1284912850
seed,
1285012851
bucket,
1285112852
offset,
12852-
})))
12853+
}))
1285312854
}
1285412855

1285512856
fn parse_table_sample_seed(

tests/sqlparser_common.rs

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

1515815158
// 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)");
15159+
dialects.verified_stmt("SELECT * FROM tbl |> TABLESAMPLE BERNOULLI (50)");
15160+
dialects.verified_stmt("SELECT * FROM tbl |> TABLESAMPLE SYSTEM (50)");
1516115161
// 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)");
15162+
dialects.verified_stmt("SELECT * FROM tbl |> TABLESAMPLE SYSTEM (50) REPEATABLE (10)");
1516315163

1516415164
// many pipes
1516515165
dialects.verified_stmt(

0 commit comments

Comments
 (0)