Skip to content

Commit 84b58d7

Browse files
committed
Add unit tests for common
1 parent 06bf22d commit 84b58d7

File tree

4 files changed

+24
-7
lines changed

4 files changed

+24
-7
lines changed

src/ast/query.rs

+3-3
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10650,7 +10650,7 @@ impl<'a> Parser<'a> {
1065010650
};
1065110651
Ok(Some(TableSample::System(TableSampleSystem {
1065210652
probability,
10653-
seed,
10653+
repeatable: seed,
1065410654
})))
1065510655
} else if self.peek_token().token == Token::LParen {
1065610656
self.expect_token(&Token::LParen)?;

tests/sqlparser_common.rs

+16
Original file line numberDiff line numberDiff line change
@@ -12349,3 +12349,19 @@ fn parse_create_table_with_enum_types() {
1234912349
ParserError::ParserError("Expected: literal string, found: 2".to_string())
1235012350
);
1235112351
}
12352+
12353+
#[test]
12354+
fn test_table_sample() {
12355+
let dialects = all_dialects_where(|d| !d.supports_implicit_table_sample());
12356+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE BERNOULLI (50)");
12357+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE SYSTEM (50)");
12358+
dialects.verified_stmt("SELECT * FROM tbl AS t TABLESAMPLE SYSTEM (50) REPEATABLE (10)");
12359+
12360+
// The only dialect that supports implicit tablesample is Hive and it requires aliase after the table sample
12361+
let dialects = all_dialects_where(|d| {
12362+
d.supports_implicit_table_sample() && d.supports_table_sample_before_alias()
12363+
});
12364+
dialects.verified_stmt("SELECT * FROM tbl TABLESAMPLE (50) AS t");
12365+
dialects.verified_stmt("SELECT * FROM tbl TABLESAMPLE (50 ROWS) AS t");
12366+
dialects.verified_stmt("SELECT * FROM tbl TABLESAMPLE (50 PERCENT) AS t");
12367+
}

tests/sqlparser_snowflake.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2956,6 +2956,7 @@ fn test_table_sample() {
29562956
snowflake_and_generic()
29572957
.verified_stmt("SELECT * FROM testtable AS t TABLESAMPLE BERNOULLI (10)");
29582958

2959+
// In Snowflake we translate implicit table sample method to bernoulli
29592960
snowflake().one_statement_parses_to(
29602961
"SELECT * FROM testtable SAMPLE (10)",
29612962
"SELECT * FROM testtable TABLESAMPLE BERNOULLI (10)",
@@ -2968,12 +2969,12 @@ fn test_table_sample() {
29682969

29692970
snowflake_and_generic().one_statement_parses_to(
29702971
"SELECT * FROM testtable SAMPLE BLOCK (3) SEED (82)",
2971-
"SELECT * FROM testtable TABLESAMPLE SYSTEM (3) SEED (82)",
2972+
"SELECT * FROM testtable TABLESAMPLE SYSTEM (3) REPEATABLE (82)",
29722973
);
29732974

29742975
snowflake_and_generic().one_statement_parses_to(
2975-
"SELECT * FROM testtable SAMPLE BLOCK (0.012) REPEATABLE (99992)",
2976-
"SELECT * FROM testtable TABLESAMPLE SYSTEM (0.012) SEED (99992)",
2976+
"SELECT * FROM testtable SAMPLE BLOCK (0.012) SEED (99992)",
2977+
"SELECT * FROM testtable TABLESAMPLE SYSTEM (0.012) REPEATABLE (99992)",
29772978
);
29782979

29792980
snowflake_and_generic()

0 commit comments

Comments
 (0)