Skip to content

Commit 06bf22d

Browse files
committed
Model implicit table sample logic in dialect
1 parent d4a7ade commit 06bf22d

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
@@ -10544,8 +10544,10 @@ impl<'a> Parser<'a> {
1054410544
let mut sample = None;
1054510545
let mut sample_before_alias = false;
1054610546
if self.dialect.supports_table_sample_before_alias() {
10547-
sample = self.parse_optional_table_sample()?;
10547+
sample = self.maybe_parse_table_sample()?;
1054810548
if sample.is_some() {
10549+
// No need to modify the default is no sample option
10550+
// exists on the statement
1054910551
sample_before_alias = true;
1055010552
}
1055110553
}
@@ -10565,7 +10567,7 @@ impl<'a> Parser<'a> {
1056510567
};
1056610568

1056710569
if !self.dialect.supports_table_sample_before_alias() {
10568-
sample = self.parse_optional_table_sample()?;
10570+
sample = self.maybe_parse_table_sample()?;
1056910571
sample_before_alias = false;
1057010572
}
1057110573

@@ -10600,7 +10602,7 @@ impl<'a> Parser<'a> {
1060010602
}
1060110603
}
1060210604

10603-
fn parse_optional_table_sample(&mut self) -> Result<Option<TableSample>, ParserError> {
10605+
fn maybe_parse_table_sample(&mut self) -> Result<Option<TableSample>, ParserError> {
1060410606
if self
1060510607
.parse_one_of_keywords(&[Keyword::SAMPLE, Keyword::TABLESAMPLE])
1060610608
.is_none()
@@ -10681,7 +10683,8 @@ impl<'a> Parser<'a> {
1068110683
}
1068210684
}
1068310685
};
10684-
if self.peek_token().token == Token::RParen && dialect_of!(self is SnowflakeDialect)
10686+
if self.peek_token().token == Token::RParen
10687+
&& !self.dialect.supports_implicit_table_sample()
1068510688
{
1068610689
self.expect_token(&Token::RParen)?;
1068710690
Ok(Some(TableSample::Bernoulli(TableSampleBernoulli {

0 commit comments

Comments
 (0)