Skip to content

Commit 789e1db

Browse files
committed
Add unit tests for common
1 parent e4c3081 commit 789e1db

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/ast/query.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ pub struct TableSampleBernoulli {
11771177
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
11781178
pub struct TableSampleSystem {
11791179
pub probability: Expr,
1180-
pub seed: Option<Expr>,
1180+
pub repeatable: Option<Expr>,
11811181
}
11821182

11831183
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -1252,8 +1252,8 @@ impl fmt::Display for TableSample {
12521252
}
12531253
TableSample::System(sample) => {
12541254
write!(f, " SYSTEM ({})", sample.probability)?;
1255-
if let Some(seed) = &sample.seed {
1256-
write!(f, " SEED ({})", seed)?;
1255+
if let Some(repeatable) = &sample.repeatable {
1256+
write!(f, " REPEATABLE ({})", repeatable)?;
12571257
}
12581258
}
12591259
TableSample::Bucket(sample) => {

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10597,7 +10597,7 @@ impl<'a> Parser<'a> {
1059710597
};
1059810598
Ok(Some(TableSample::System(TableSampleSystem {
1059910599
probability,
10600-
seed,
10600+
repeatable: seed,
1060110601
})))
1060210602
} else if self.peek_token().token == Token::LParen {
1060310603
self.expect_token(&Token::LParen)?;

tests/sqlparser_common.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12268,3 +12268,19 @@ fn parse_create_table_with_bit_types() {
1226812268
_ => unreachable!(),
1226912269
}
1227012270
}
12271+
12272+
#[test]
12273+
fn test_table_sample() {
12274+
let dialects = all_dialects_where(|d| !d.supports_implicit_table_sample());
12275+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE BERNOULLI (50)");
12276+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE SYSTEM (50)");
12277+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE SYSTEM (50) REPEATABLE (10)");
12278+
12279+
// The only dialect that supports implicit tablesample is Hive and it requires aliase after the table sample
12280+
let dialects = all_dialects_where(|d| {
12281+
d.supports_implicit_table_sample() && d.supports_table_sample_before_alias()
12282+
});
12283+
dialects.verified_stmt("SELECT * FROM tbl TABLESAMPLE (50) AS t");
12284+
dialects.verified_stmt("SELECT * FROM tbl TABLESAMPLE (50 ROWS) AS t");
12285+
dialects.verified_stmt("SELECT * FROM tbl TABLESAMPLE (50 PERCENT) AS t");
12286+
}

tests/sqlparser_snowflake.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,7 @@ fn test_table_sample() {
29022902
snowflake_and_generic()
29032903
.verified_stmt("SELECT * FROM testtable AS t TABLESAMPLE BERNOULLI (10)");
29042904

2905+
// In Snowflake we translate implicit table sample method to bernoulli
29052906
snowflake().one_statement_parses_to(
29062907
"SELECT * FROM testtable SAMPLE (10)",
29072908
"SELECT * FROM testtable TABLESAMPLE BERNOULLI (10)",
@@ -2914,12 +2915,12 @@ fn test_table_sample() {
29142915

29152916
snowflake_and_generic().one_statement_parses_to(
29162917
"SELECT * FROM testtable SAMPLE BLOCK (3) SEED (82)",
2917-
"SELECT * FROM testtable TABLESAMPLE SYSTEM (3) SEED (82)",
2918+
"SELECT * FROM testtable TABLESAMPLE SYSTEM (3) REPEATABLE (82)",
29182919
);
29192920

29202921
snowflake_and_generic().one_statement_parses_to(
2921-
"SELECT * FROM testtable SAMPLE BLOCK (0.012) REPEATABLE (99992)",
2922-
"SELECT * FROM testtable TABLESAMPLE SYSTEM (0.012) SEED (99992)",
2922+
"SELECT * FROM testtable SAMPLE BLOCK (0.012) SEED (99992)",
2923+
"SELECT * FROM testtable TABLESAMPLE SYSTEM (0.012) REPEATABLE (99992)",
29232924
);
29242925

29252926
snowflake_and_generic()

0 commit comments

Comments
 (0)