Skip to content

Commit e4c3081

Browse files
committed
Model implicit table sample logic in dialect
1 parent aa5df54 commit e4c3081

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/dialect/hive.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,9 @@ impl Dialect for HiveDialect {
6666
fn supports_table_sample_before_alias(&self) -> bool {
6767
true
6868
}
69+
70+
/// See Hive <https://cwiki.apache.org/confluence/display/hive/languagemanual+sampling>
71+
fn supports_implicit_table_sample(&self) -> bool {
72+
true
73+
}
6974
}

src/dialect/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,12 +708,18 @@ pub trait Dialect: Debug + Any {
708708
keywords::RESERVED_FOR_IDENTIFIER.contains(&kw)
709709
}
710710

711-
/// Returns true if the dialect supports the `TABLESAMPLE` option
711+
/// Returns true if this dialect supports the `TABLESAMPLE` option
712712
/// before the table alias option.
713713
/// <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#_7_6_table_reference>
714714
fn supports_table_sample_before_alias(&self) -> bool {
715715
false
716716
}
717+
718+
/// Returns true if this dialect support not specifying a table sample method.
719+
/// <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#sample-clause>
720+
fn supports_implicit_table_sample(&self) -> bool {
721+
false
722+
}
717723
}
718724

719725
/// This represents the operators for which precedence must be defined

src/parser/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10491,8 +10491,10 @@ impl<'a> Parser<'a> {
1049110491
let mut sample = None;
1049210492
let mut sample_before_alias = false;
1049310493
if self.dialect.supports_table_sample_before_alias() {
10494-
sample = self.parse_optional_table_sample()?;
10494+
sample = self.maybe_parse_table_sample()?;
1049510495
if sample.is_some() {
10496+
// No need to modify the default is no sample option
10497+
// exists on the statement
1049610498
sample_before_alias = true;
1049710499
}
1049810500
}
@@ -10512,7 +10514,7 @@ impl<'a> Parser<'a> {
1051210514
};
1051310515

1051410516
if !self.dialect.supports_table_sample_before_alias() {
10515-
sample = self.parse_optional_table_sample()?;
10517+
sample = self.maybe_parse_table_sample()?;
1051610518
sample_before_alias = false;
1051710519
}
1051810520

@@ -10547,7 +10549,7 @@ impl<'a> Parser<'a> {
1054710549
}
1054810550
}
1054910551

10550-
fn parse_optional_table_sample(&mut self) -> Result<Option<TableSample>, ParserError> {
10552+
fn maybe_parse_table_sample(&mut self) -> Result<Option<TableSample>, ParserError> {
1055110553
if self
1055210554
.parse_one_of_keywords(&[Keyword::SAMPLE, Keyword::TABLESAMPLE])
1055310555
.is_none()
@@ -10628,7 +10630,8 @@ impl<'a> Parser<'a> {
1062810630
}
1062910631
}
1063010632
};
10631-
if self.peek_token().token == Token::RParen && dialect_of!(self is SnowflakeDialect)
10633+
if self.peek_token().token == Token::RParen
10634+
&& !self.dialect.supports_implicit_table_sample()
1063210635
{
1063310636
self.expect_token(&Token::RParen)?;
1063410637
Ok(Some(TableSample::Bernoulli(TableSampleBernoulli {

0 commit comments

Comments
 (0)