Skip to content

Commit 464fca3

Browse files
committed
Keep source syntax
1 parent 353ce58 commit 464fca3

File tree

4 files changed

+164
-103
lines changed

4 files changed

+164
-103
lines changed

src/ast/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ pub use self::query::{
7070
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
7171
SelectInto, SelectItem, SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table,
7272
TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs, TableSampleBernoulli,
73-
TableSampleBucket, TableSampleImplicit, TableSampleKind, TableSampleMethod, TableSampleSystem,
73+
TableSampleBucket, TableSampleImplicit, TableSampleKind, TableSampleMethod,
74+
TableSampleMethodName, TableSampleSeed, TableSampleSeedModifier, TableSampleSystem,
7475
TableSampleUnit, TableVersion, TableWithJoins, Top, TopQuantity, ValueTableMode, Values,
7576
WildcardAdditionalOptions, With, WithFill,
7677
};

src/ast/query.rs

+81-18
Original file line numberDiff line numberDiff line change
@@ -1172,21 +1172,96 @@ pub enum TableSampleMethod {
11721172
Implicit(TableSampleImplicit),
11731173
}
11741174

1175+
/// The table sample method names
1176+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1177+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1178+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1179+
pub enum TableSampleMethodName {
1180+
Row,
1181+
Bernoulli,
1182+
System,
1183+
Block,
1184+
}
1185+
1186+
impl fmt::Display for TableSampleMethodName {
1187+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1188+
match self {
1189+
TableSampleMethodName::Bernoulli => write!(f, "BERNOULLI"),
1190+
TableSampleMethodName::Row => write!(f, "ROW"),
1191+
TableSampleMethodName::System => write!(f, "SYSTEM"),
1192+
TableSampleMethodName::Block => write!(f, "BLOCK"),
1193+
}
1194+
}
1195+
}
1196+
11751197
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11761198
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11771199
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11781200
pub struct TableSampleBernoulli {
1179-
pub probability: Option<Expr>,
1180-
pub value: Option<Expr>,
1201+
pub name: TableSampleMethodName,
1202+
pub probability: Option<Value>,
1203+
pub value: Option<Value>,
11811204
pub unit: Option<TableSampleUnit>,
11821205
}
11831206

1207+
impl fmt::Display for TableSampleBernoulli {
1208+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1209+
write!(f, " {} (", self.name)?;
1210+
if let Some(probability) = &self.probability {
1211+
write!(f, "{})", probability)?;
1212+
} else if let Some(value) = &self.value {
1213+
write!(f, "{}", value)?;
1214+
if let Some(unit) = &self.unit {
1215+
write!(f, " {}", unit)?;
1216+
}
1217+
write!(f, ")")?;
1218+
}
1219+
Ok(())
1220+
}
1221+
}
1222+
11841223
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11851224
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11861225
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11871226
pub struct TableSampleSystem {
1188-
pub probability: Expr,
1189-
pub repeatable: Option<Expr>,
1227+
pub name: TableSampleMethodName,
1228+
pub probability: Value,
1229+
pub seed: Option<TableSampleSeed>,
1230+
}
1231+
1232+
impl fmt::Display for TableSampleSystem {
1233+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1234+
write!(f, " {} ({})", self.name, self.probability)?;
1235+
if let Some(seed) = &self.seed {
1236+
write!(f, " {} ({})", seed.modifier, seed.value)?;
1237+
}
1238+
Ok(())
1239+
}
1240+
}
1241+
1242+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1243+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1244+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1245+
pub struct TableSampleSeed {
1246+
pub modifier: TableSampleSeedModifier,
1247+
pub value: Value,
1248+
}
1249+
1250+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1251+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1252+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1253+
pub enum TableSampleSeedModifier {
1254+
Repeatable,
1255+
Seed,
1256+
}
1257+
1258+
impl fmt::Display for TableSampleSeedModifier {
1259+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1260+
match self {
1261+
TableSampleSeedModifier::Repeatable => write!(f, "REPEATABLE"),
1262+
TableSampleSeedModifier::Seed => write!(f, "SEED"),
1263+
}
1264+
}
11901265
}
11911266

11921267
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -1248,22 +1323,10 @@ impl fmt::Display for TableSampleMethod {
12481323
write!(f, " TABLESAMPLE")?;
12491324
match self {
12501325
TableSampleMethod::Bernoulli(sample) => {
1251-
write!(f, " BERNOULLI (")?;
1252-
if let Some(probability) = &sample.probability {
1253-
write!(f, "{})", probability)?;
1254-
} else if let Some(value) = &sample.value {
1255-
write!(f, "{}", value)?;
1256-
if let Some(unit) = &sample.unit {
1257-
write!(f, " {}", unit)?;
1258-
}
1259-
write!(f, ")")?;
1260-
}
1326+
write!(f, "{}", sample)?;
12611327
}
12621328
TableSampleMethod::System(sample) => {
1263-
write!(f, " SYSTEM ({})", sample.probability)?;
1264-
if let Some(repeatable) = &sample.repeatable {
1265-
write!(f, " REPEATABLE ({})", repeatable)?;
1266-
}
1329+
write!(f, "{}", sample)?;
12671330
}
12681331
TableSampleMethod::Bucket(sample) => {
12691332
write!(f, " ({})", sample)?;

src/parser/mod.rs

+72-61
Original file line numberDiff line numberDiff line change
@@ -10660,48 +10660,14 @@ impl<'a> Parser<'a> {
1066010660
}
1066110661

1066210662
// Try to parse based on an explicit table sample method keyword
10663-
let sample = if self
10664-
.parse_one_of_keywords(&[Keyword::BERNOULLI, Keyword::ROW])
10665-
.is_some()
10666-
{
10667-
self.expect_token(&Token::LParen)?;
10668-
let expr = self.parse_expr()?;
10669-
10670-
let (probability, value, unit) = if self.parse_keyword(Keyword::ROWS) {
10671-
(None, Some(expr), Some(TableSampleUnit::Rows))
10672-
} else if self.parse_keyword(Keyword::PERCENT) {
10673-
(None, Some(expr), Some(TableSampleUnit::Percent))
10674-
} else {
10675-
(Some(expr), None, None)
10676-
};
10677-
self.expect_token(&Token::RParen)?;
10678-
TableSampleMethod::Bernoulli(TableSampleBernoulli {
10679-
probability,
10680-
value,
10681-
unit,
10682-
})
10683-
} else if self
10684-
.parse_one_of_keywords(&[Keyword::SYSTEM, Keyword::BLOCK])
10685-
.is_some()
10686-
{
10687-
self.expect_token(&Token::LParen)?;
10688-
let probability = self.parse_expr()?;
10689-
self.expect_token(&Token::RParen)?;
10690-
let seed = if self
10691-
.parse_one_of_keywords(&[Keyword::REPEATABLE, Keyword::SEED])
10692-
.is_some()
10693-
{
10694-
self.expect_token(&Token::LParen)?;
10695-
let seed = self.parse_expr()?;
10696-
self.expect_token(&Token::RParen)?;
10697-
Some(seed)
10698-
} else {
10699-
None
10700-
};
10701-
TableSampleMethod::System(TableSampleSystem {
10702-
probability,
10703-
repeatable: seed,
10704-
})
10663+
let sample = if self.parse_keyword(Keyword::BERNOULLI) {
10664+
self.parse_table_sample_bernoulli(TableSampleMethodName::Bernoulli)?
10665+
} else if self.parse_keyword(Keyword::ROW) {
10666+
self.parse_table_sample_bernoulli(TableSampleMethodName::Row)?
10667+
} else if self.parse_keyword(Keyword::SYSTEM) {
10668+
self.parse_table_sample_system(TableSampleMethodName::System)?
10669+
} else if self.parse_keyword(Keyword::BLOCK) {
10670+
self.parse_table_sample_system(TableSampleMethodName::Block)?
1070510671
// Try to parse without an explicit table sample method keyword
1070610672
} else if self.consume_token(&Token::LParen) {
1070710673
if self.parse_keyword(Keyword::BUCKET) {
@@ -10729,36 +10695,81 @@ impl<'a> Parser<'a> {
1072910695
}
1073010696
}
1073110697
};
10732-
if !self.dialect.supports_implicit_table_sample_method()
10733-
&& self.consume_token(&Token::RParen)
10734-
{
10735-
TableSampleMethod::Bernoulli(TableSampleBernoulli {
10736-
probability: Some(Expr::Value(value)),
10737-
unit: None,
10738-
value: None,
10739-
})
10698+
let unit = if self.parse_keyword(Keyword::ROWS) {
10699+
Some(TableSampleUnit::Rows)
10700+
} else if self.parse_keyword(Keyword::PERCENT) {
10701+
Some(TableSampleUnit::Percent)
1074010702
} else {
10741-
let unit = if self.parse_keyword(Keyword::ROWS) {
10742-
Some(TableSampleUnit::Rows)
10743-
} else if self.parse_keyword(Keyword::PERCENT) {
10744-
Some(TableSampleUnit::Percent)
10745-
} else {
10746-
None
10747-
};
10748-
self.expect_token(&Token::RParen)?;
10749-
TableSampleMethod::Implicit(TableSampleImplicit { value, unit })
10750-
}
10703+
None
10704+
};
10705+
self.expect_token(&Token::RParen)?;
10706+
TableSampleMethod::Implicit(TableSampleImplicit { value, unit })
1075110707
}
1075210708
} else {
1075310709
return parser_err!(
10754-
"Expecting BERNOULLI, ROW, SYSTEM or BLOCK",
10710+
"Expecting BERNOULLI, ROW, SYSTEM, BLOCK or a valid TABLESAMPLE expression in parenthesis",
1075510711
self.peek_token().span.start
1075610712
);
1075710713
};
1075810714

1075910715
Ok(Some(Box::new(sample)))
1076010716
}
1076110717

10718+
fn parse_table_sample_bernoulli(
10719+
&mut self,
10720+
name: TableSampleMethodName,
10721+
) -> Result<TableSampleMethod, ParserError> {
10722+
self.expect_token(&Token::LParen)?;
10723+
let value = self.parse_number_value()?;
10724+
let (probability, value, unit) = if self.parse_keyword(Keyword::ROWS) {
10725+
(None, Some(value), Some(TableSampleUnit::Rows))
10726+
} else if self.parse_keyword(Keyword::PERCENT) {
10727+
(None, Some(value), Some(TableSampleUnit::Percent))
10728+
} else {
10729+
(Some(value), None, None)
10730+
};
10731+
self.expect_token(&Token::RParen)?;
10732+
Ok(TableSampleMethod::Bernoulli(TableSampleBernoulli {
10733+
name,
10734+
probability,
10735+
value,
10736+
unit,
10737+
}))
10738+
}
10739+
10740+
fn parse_table_sample_system(
10741+
&mut self,
10742+
name: TableSampleMethodName,
10743+
) -> Result<TableSampleMethod, ParserError> {
10744+
self.expect_token(&Token::LParen)?;
10745+
let probability = self.parse_number_value()?;
10746+
self.expect_token(&Token::RParen)?;
10747+
10748+
let seed = if self.parse_keyword(Keyword::REPEATABLE) {
10749+
Some(self.parse_table_sample_seed(TableSampleSeedModifier::Repeatable)?)
10750+
} else if self.parse_keyword(Keyword::SEED) {
10751+
Some(self.parse_table_sample_seed(TableSampleSeedModifier::Seed)?)
10752+
} else {
10753+
None
10754+
};
10755+
10756+
Ok(TableSampleMethod::System(TableSampleSystem {
10757+
name,
10758+
probability,
10759+
seed,
10760+
}))
10761+
}
10762+
10763+
fn parse_table_sample_seed(
10764+
&mut self,
10765+
modifier: TableSampleSeedModifier,
10766+
) -> Result<TableSampleSeed, ParserError> {
10767+
self.expect_token(&Token::LParen)?;
10768+
let value = self.parse_number_value()?;
10769+
self.expect_token(&Token::RParen)?;
10770+
Ok(TableSampleSeed { modifier, value })
10771+
}
10772+
1076210773
/// Parses `OPENJSON( jsonExpression [ , path ] ) [ <with_clause> ]` clause,
1076310774
/// assuming the `OPENJSON` keyword was already consumed.
1076410775
fn parse_open_json_table_factor(&mut self) -> Result<TableFactor, ParserError> {

tests/sqlparser_snowflake.rs

+9-23
Original file line numberDiff line numberDiff line change
@@ -2961,30 +2961,16 @@ fn parse_insert_overwrite() {
29612961

29622962
#[test]
29632963
fn test_table_sample() {
2964-
snowflake_and_generic()
2965-
.verified_stmt("SELECT * FROM testtable AS t TABLESAMPLE BERNOULLI (10)");
2966-
2967-
// In Snowflake we translate implicit table sample method to bernoulli
2968-
snowflake().one_statement_parses_to(
2969-
"SELECT * FROM testtable SAMPLE (10)",
2970-
"SELECT * FROM testtable TABLESAMPLE BERNOULLI (10)",
2971-
);
2972-
2973-
snowflake_and_generic().one_statement_parses_to(
2974-
"SELECT * FROM testtable TABLESAMPLE ROW (20.3)",
2975-
"SELECT * FROM testtable TABLESAMPLE BERNOULLI (20.3)",
2976-
);
2977-
29782964
snowflake_and_generic().one_statement_parses_to(
2979-
"SELECT * FROM testtable SAMPLE BLOCK (3) SEED (82)",
2980-
"SELECT * FROM testtable TABLESAMPLE SYSTEM (3) REPEATABLE (82)",
2981-
);
2982-
2983-
snowflake_and_generic().one_statement_parses_to(
2984-
"SELECT * FROM testtable SAMPLE BLOCK (0.012) SEED (99992)",
2985-
"SELECT * FROM testtable TABLESAMPLE SYSTEM (0.012) REPEATABLE (99992)",
2965+
"SELECT * FROM testtable SAMPLE (10)",
2966+
"SELECT * FROM testtable TABLESAMPLE (10)",
29862967
);
2987-
29882968
snowflake_and_generic()
2989-
.verified_stmt("SELECT * FROM testtable TABLESAMPLE BERNOULLI (10 ROWS)");
2969+
.verified_stmt("SELECT * FROM testtable AS t TABLESAMPLE BERNOULLI (10)");
2970+
snowflake_and_generic().verified_stmt("SELECT * FROM testtable AS t TABLESAMPLE ROW (10)");
2971+
snowflake_and_generic().verified_stmt("SELECT * FROM testtable AS t TABLESAMPLE ROW (10 ROWS)");
2972+
snowflake_and_generic()
2973+
.verified_stmt("SELECT * FROM testtable TABLESAMPLE BLOCK (3) SEED (82)");
2974+
snowflake_and_generic()
2975+
.verified_stmt("SELECT * FROM testtable TABLESAMPLE SYSTEM (3) REPEATABLE (82)");
29902976
}

0 commit comments

Comments
 (0)