Skip to content

Commit 8113dd6

Browse files
committed
Code review comments
1 parent 92091a9 commit 8113dd6

12 files changed

+53
-66
lines changed

src/ast/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ pub use self::query::{
6969
OrderBy, OrderByExpr, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
7070
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
7171
SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table,
72-
TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs, TableSample,
73-
TableSampleBernoulli, TableSampleBucket, TableSampleImplicit, TableSampleSystem,
72+
TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs, TableSampleBernoulli,
73+
TableSampleBucket, TableSampleImplicit, TableSampleKind, TableSampleMethod, TableSampleSystem,
7474
TableSampleUnit, TableVersion, TableWithJoins, Top, TopQuantity, ValueTableMode, Values,
7575
WildcardAdditionalOptions, With, WithFill,
7676
};

src/ast/query.rs

+21-13
Original file line numberDiff line numberDiff line change
@@ -1004,10 +1004,7 @@ pub enum TableFactor {
10041004
json_path: Option<JsonPath>,
10051005
/// Optional table sample modifier
10061006
/// See: <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#sample-clause>
1007-
sample: Option<Box<TableSample>>,
1008-
/// Position of the table sample modifier in the table factor. Default is after the table alias
1009-
/// e.g. `SELECT * FROM tbl t TABLESAMPLE (10 ROWS)`. See `Dialect::supports_table_sample_before_alias`.
1010-
sample_before_alias: bool,
1007+
sample: Option<TableSampleKind>,
10111008
},
10121009
Derived {
10131010
lateral: bool,
@@ -1156,7 +1153,19 @@ pub enum TableFactor {
11561153
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11571154
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11581155
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1159-
pub enum TableSample {
1156+
1157+
pub enum TableSampleKind {
1158+
/// Table sample located before the table alias option
1159+
BeforeTableAlias(Box<TableSampleMethod>),
1160+
/// Table sample located after the table alias option
1161+
AfterTableAlias(Box<TableSampleMethod>),
1162+
}
1163+
1164+
/// The table sample method options
1165+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1166+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1167+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1168+
pub enum TableSampleMethod {
11601169
Bernoulli(TableSampleBernoulli),
11611170
System(TableSampleSystem),
11621171
Bucket(TableSampleBucket),
@@ -1234,11 +1243,11 @@ impl fmt::Display for TableSampleImplicit {
12341243
}
12351244
}
12361245

1237-
impl fmt::Display for TableSample {
1246+
impl fmt::Display for TableSampleMethod {
12381247
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12391248
write!(f, " TABLESAMPLE")?;
12401249
match self {
1241-
TableSample::Bernoulli(sample) => {
1250+
TableSampleMethod::Bernoulli(sample) => {
12421251
write!(f, " BERNOULLI (")?;
12431252
if let Some(probability) = &sample.probability {
12441253
write!(f, "{})", probability)?;
@@ -1250,16 +1259,16 @@ impl fmt::Display for TableSample {
12501259
write!(f, ")")?;
12511260
}
12521261
}
1253-
TableSample::System(sample) => {
1262+
TableSampleMethod::System(sample) => {
12541263
write!(f, " SYSTEM ({})", sample.probability)?;
12551264
if let Some(repeatable) = &sample.repeatable {
12561265
write!(f, " REPEATABLE ({})", repeatable)?;
12571266
}
12581267
}
1259-
TableSample::Bucket(sample) => {
1268+
TableSampleMethod::Bucket(sample) => {
12601269
write!(f, " ({})", sample)?;
12611270
}
1262-
TableSample::Implicit(sample) => {
1271+
TableSampleMethod::Implicit(sample) => {
12631272
write!(f, " ({})", sample)?;
12641273
}
12651274
}
@@ -1526,7 +1535,6 @@ impl fmt::Display for TableFactor {
15261535
with_ordinality,
15271536
json_path,
15281537
sample,
1529-
sample_before_alias,
15301538
} => {
15311539
write!(f, "{name}")?;
15321540
if let Some(json_path) = json_path {
@@ -1549,7 +1557,7 @@ impl fmt::Display for TableFactor {
15491557
if *with_ordinality {
15501558
write!(f, " WITH ORDINALITY")?;
15511559
}
1552-
if let (Some(sample), true) = (sample, sample_before_alias) {
1560+
if let Some(TableSampleKind::BeforeTableAlias(sample)) = sample {
15531561
write!(f, "{sample}")?;
15541562
}
15551563
if let Some(alias) = alias {
@@ -1561,7 +1569,7 @@ impl fmt::Display for TableFactor {
15611569
if let Some(version) = version {
15621570
write!(f, "{version}")?;
15631571
}
1564-
if let (Some(sample), false) = (sample, sample_before_alias) {
1572+
if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
15651573
write!(f, "{sample}")?;
15661574
}
15671575
Ok(())

src/ast/spans.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1700,7 +1700,6 @@ impl Spanned for TableFactor {
17001700
partitions: _,
17011701
json_path: _,
17021702
sample: _,
1703-
sample_before_alias: _,
17041703
} => union_spans(
17051704
name.0
17061705
.iter()

src/dialect/hive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Dialect for HiveDialect {
6868
}
6969

7070
/// See Hive <https://cwiki.apache.org/confluence/display/hive/languagemanual+sampling>
71-
fn supports_implicit_table_sample(&self) -> bool {
71+
fn supports_implicit_table_sample_method(&self) -> bool {
7272
true
7373
}
7474
}

src/dialect/mod.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,23 @@ pub trait Dialect: Debug + Any {
709709
}
710710

711711
/// Returns true if this dialect supports the `TABLESAMPLE` option
712-
/// before the table alias option.
712+
/// before the table alias option. For example:
713+
///
714+
/// Table sample before alias: `SELECT * FROM tbl AS t TABLESAMPLE (10)`
715+
/// Table sample after alias: `SELECT * FROM tbl TABLESAMPLE (10) AS t`
716+
///
713717
/// <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#_7_6_table_reference>
714718
fn supports_table_sample_before_alias(&self) -> bool {
715719
false
716720
}
717721

718-
/// Returns true if this dialect support not specifying a table sample method.
722+
/// Returns true if this dialect support not specifying a table sample method. For example:
723+
///
724+
/// Implicit table sample method: `SELECT * FROM tbl TABLESAMPLE (10)`
725+
/// Explicit table sample method: `SELECT * FROM tbl TABLESAMPLE BERNOULLI (10)`
726+
///
719727
/// <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#sample-clause>
720-
fn supports_implicit_table_sample(&self) -> bool {
728+
fn supports_implicit_table_sample_method(&self) -> bool {
721729
false
722730
}
723731
}

src/parser/mod.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -10595,13 +10595,9 @@ impl<'a> Parser<'a> {
1059510595
let with_ordinality = self.parse_keywords(&[Keyword::WITH, Keyword::ORDINALITY]);
1059610596

1059710597
let mut sample = None;
10598-
let mut sample_before_alias = false;
1059910598
if self.dialect.supports_table_sample_before_alias() {
10600-
sample = self.maybe_parse_table_sample()?;
10601-
if sample.is_some() {
10602-
// No need to modify the default is no sample option
10603-
// exists on the statement
10604-
sample_before_alias = true;
10599+
if let Some(parsed_sample) = self.maybe_parse_table_sample()? {
10600+
sample = Some(TableSampleKind::BeforeTableAlias(parsed_sample));
1060510601
}
1060610602
}
1060710603

@@ -10620,8 +10616,9 @@ impl<'a> Parser<'a> {
1062010616
};
1062110617

1062210618
if !self.dialect.supports_table_sample_before_alias() {
10623-
sample = self.maybe_parse_table_sample()?;
10624-
sample_before_alias = false;
10619+
if let Some(parsed_sample) = self.maybe_parse_table_sample()? {
10620+
sample = Some(TableSampleKind::AfterTableAlias(parsed_sample));
10621+
}
1062510622
}
1062610623

1062710624
let mut table = TableFactor::Table {
@@ -10634,7 +10631,6 @@ impl<'a> Parser<'a> {
1063410631
with_ordinality,
1063510632
json_path,
1063610633
sample,
10637-
sample_before_alias,
1063810634
};
1063910635

1064010636
while let Some(kw) = self.parse_one_of_keywords(&[Keyword::PIVOT, Keyword::UNPIVOT]) {
@@ -10655,14 +10651,15 @@ impl<'a> Parser<'a> {
1065510651
}
1065610652
}
1065710653

10658-
fn maybe_parse_table_sample(&mut self) -> Result<Option<Box<TableSample>>, ParserError> {
10654+
fn maybe_parse_table_sample(&mut self) -> Result<Option<Box<TableSampleMethod>>, ParserError> {
1065910655
if self
1066010656
.parse_one_of_keywords(&[Keyword::SAMPLE, Keyword::TABLESAMPLE])
1066110657
.is_none()
1066210658
{
1066310659
return Ok(None);
1066410660
}
1066510661

10662+
// Try to parse based on an explicit table sample method keyword
1066610663
let sample = if self
1066710664
.parse_one_of_keywords(&[Keyword::BERNOULLI, Keyword::ROW])
1066810665
.is_some()
@@ -10678,7 +10675,7 @@ impl<'a> Parser<'a> {
1067810675
(Some(expr), None, None)
1067910676
};
1068010677
self.expect_token(&Token::RParen)?;
10681-
TableSample::Bernoulli(TableSampleBernoulli {
10678+
TableSampleMethod::Bernoulli(TableSampleBernoulli {
1068210679
probability,
1068310680
value,
1068410681
unit,
@@ -10701,10 +10698,11 @@ impl<'a> Parser<'a> {
1070110698
} else {
1070210699
None
1070310700
};
10704-
TableSample::System(TableSampleSystem {
10701+
TableSampleMethod::System(TableSampleSystem {
1070510702
probability,
1070610703
repeatable: seed,
1070710704
})
10705+
// Try to parse without an explicit table sample method keyword
1070810706
} else if self.peek_token().token == Token::LParen {
1070910707
self.expect_token(&Token::LParen)?;
1071010708
if self.parse_keyword(Keyword::BUCKET) {
@@ -10717,10 +10715,10 @@ impl<'a> Parser<'a> {
1071710715
None
1071810716
};
1071910717
self.expect_token(&Token::RParen)?;
10720-
TableSample::Bucket(TableSampleBucket { bucket, total, on })
10718+
TableSampleMethod::Bucket(TableSampleBucket { bucket, total, on })
1072110719
} else {
10722-
let value = match self.try_parse(|p| p.parse_number_value()) {
10723-
Ok(num) => num,
10720+
let value = match self.maybe_parse(|p| p.parse_number_value()) {
10721+
Ok(Some(num)) => num,
1072410722
_ => {
1072510723
if let Token::Word(w) = self.next_token().token {
1072610724
Value::Placeholder(w.value)
@@ -10733,10 +10731,10 @@ impl<'a> Parser<'a> {
1073310731
}
1073410732
};
1073510733
if self.peek_token().token == Token::RParen
10736-
&& !self.dialect.supports_implicit_table_sample()
10734+
&& !self.dialect.supports_implicit_table_sample_method()
1073710735
{
1073810736
self.expect_token(&Token::RParen)?;
10739-
TableSample::Bernoulli(TableSampleBernoulli {
10737+
TableSampleMethod::Bernoulli(TableSampleBernoulli {
1074010738
probability: Some(Expr::Value(value)),
1074110739
unit: None,
1074210740
value: None,
@@ -10750,7 +10748,7 @@ impl<'a> Parser<'a> {
1075010748
None
1075110749
};
1075210750
self.expect_token(&Token::RParen)?;
10753-
TableSample::Implicit(TableSampleImplicit { value, unit })
10751+
TableSampleMethod::Implicit(TableSampleImplicit { value, unit })
1075410752
}
1075510753
}
1075610754
} else {

src/test_utils.rs

-3
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ pub fn table(name: impl Into<String>) -> TableFactor {
347347
with_ordinality: false,
348348
json_path: None,
349349
sample: None,
350-
sample_before_alias: false,
351350
}
352351
}
353352

@@ -362,7 +361,6 @@ pub fn table_from_name(name: ObjectName) -> TableFactor {
362361
with_ordinality: false,
363362
json_path: None,
364363
sample: None,
365-
sample_before_alias: false,
366364
}
367365
}
368366

@@ -380,7 +378,6 @@ pub fn table_with_alias(name: impl Into<String>, alias: impl Into<String>) -> Ta
380378
with_ordinality: false,
381379
json_path: None,
382380
sample: None,
383-
sample_before_alias: false,
384381
}
385382
}
386383

tests/sqlparser_bigquery.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,6 @@ fn parse_table_time_travel() {
15451545
with_ordinality: false,
15461546
json_path: None,
15471547
sample: None,
1548-
sample_before_alias: false,
15491548
},
15501549
joins: vec![]
15511550
},]
@@ -1646,7 +1645,6 @@ fn parse_merge() {
16461645
with_ordinality: false,
16471646
json_path: None,
16481647
sample: None,
1649-
sample_before_alias: false,
16501648
},
16511649
table
16521650
);
@@ -1664,7 +1662,6 @@ fn parse_merge() {
16641662
with_ordinality: false,
16651663
json_path: None,
16661664
sample: None,
1667-
sample_before_alias: false,
16681665
},
16691666
source
16701667
);

0 commit comments

Comments
 (0)