Skip to content

Commit e6dbd03

Browse files
committed
Fix SQL serialization
1 parent c278957 commit e6dbd03

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

src/ast/dml.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl Display for CreateTable {
265265
// `CREATE TABLE t (a INT) AS SELECT a from t2`
266266
write!(
267267
f,
268-
"CREATE {or_replace}{external}{global}{temporary}{transient}{volatile}{iceberg}{dynamic}TABLE {if_not_exists}{name}",
268+
"CREATE {or_replace}{external}{global}{temporary}{transient}{volatile}{dynamic}{iceberg}TABLE {if_not_exists}{name}",
269269
or_replace = if self.or_replace { "OR REPLACE " } else { "" },
270270
external = if self.external { "EXTERNAL " } else { "" },
271271
global = self.global
@@ -325,6 +325,10 @@ impl Display for CreateTable {
325325
write!(f, " CLONE {c}")?;
326326
}
327327

328+
if let Some(version) = &self.version {
329+
write!(f, " {version}")?;
330+
}
331+
328332
match &self.hive_distribution {
329333
HiveDistributionStyle::PARTITIONED { columns } => {
330334
write!(f, " PARTITIONED BY ({})", display_comma_separated(columns))?;
@@ -427,27 +431,27 @@ impl Display for CreateTable {
427431
write!(f, " {options}")?;
428432
}
429433
if let Some(external_volume) = self.external_volume.as_ref() {
430-
write!(f, " EXTERNAL_VOLUME = '{external_volume}'")?;
434+
write!(f, " EXTERNAL_VOLUME='{external_volume}'")?;
431435
}
432436

433437
if let Some(catalog) = self.catalog.as_ref() {
434-
write!(f, " CATALOG = '{catalog}'")?;
438+
write!(f, " CATALOG='{catalog}'")?;
435439
}
436440

437441
if self.iceberg {
438442
if let Some(base_location) = self.base_location.as_ref() {
439-
write!(f, " BASE_LOCATION = '{base_location}'")?;
443+
write!(f, " BASE_LOCATION='{base_location}'")?;
440444
}
441445
}
442446

443447
if let Some(catalog_sync) = self.catalog_sync.as_ref() {
444-
write!(f, " CATALOG_SYNC = '{catalog_sync}'")?;
448+
write!(f, " CATALOG_SYNC='{catalog_sync}'")?;
445449
}
446450

447451
if let Some(storage_serialization_policy) = self.storage_serialization_policy.as_ref() {
448452
write!(
449453
f,
450-
" STORAGE_SERIALIZATION_POLICY = {storage_serialization_policy}"
454+
" STORAGE_SERIALIZATION_POLICY={storage_serialization_policy}"
451455
)?;
452456
}
453457

src/ast/query.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ impl fmt::Display for TableFactor {
18831883
write!(f, " WITH ({})", display_comma_separated(with_hints))?;
18841884
}
18851885
if let Some(version) = version {
1886-
write!(f, "{version}")?;
1886+
write!(f, " {version}")?;
18871887
}
18881888
if let Some(TableSampleKind::AfterTableAlias(sample)) = sample {
18891889
write!(f, " {sample}")?;
@@ -2178,8 +2178,8 @@ pub enum TableVersion {
21782178
impl Display for TableVersion {
21792179
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21802180
match self {
2181-
TableVersion::ForSystemTimeAsOf(e) => write!(f, " FOR SYSTEM_TIME AS OF {e}")?,
2182-
TableVersion::Function(func) => write!(f, " {func}")?,
2181+
TableVersion::ForSystemTimeAsOf(e) => write!(f, "FOR SYSTEM_TIME AS OF {e}")?,
2182+
TableVersion::Function(func) => write!(f, "{func}")?,
21832183
}
21842184
Ok(())
21852185
}

tests/sqlparser_snowflake.rs

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -906,8 +906,8 @@ fn test_snowflake_create_table_with_several_column_options() {
906906
#[test]
907907
fn test_snowflake_create_iceberg_table_all_options() {
908908
match snowflake().verified_stmt("CREATE ICEBERG TABLE my_table (a INT, b INT) \
909-
CLUSTER BY (a, b) EXTERNAL_VOLUME = 'volume' CATALOG = 'SNOWFLAKE' BASE_LOCATION = 'relative/path' CATALOG_SYNC = 'OPEN_CATALOG' \
910-
STORAGE_SERIALIZATION_POLICY = COMPATIBLE COPY GRANTS CHANGE_TRACKING=TRUE DATA_RETENTION_TIME_IN_DAYS=5 MAX_DATA_EXTENSION_TIME_IN_DAYS=10 \
909+
CLUSTER BY (a, b) EXTERNAL_VOLUME='volume' CATALOG='SNOWFLAKE' BASE_LOCATION='relative/path' CATALOG_SYNC='OPEN_CATALOG' \
910+
STORAGE_SERIALIZATION_POLICY=COMPATIBLE COPY GRANTS CHANGE_TRACKING=TRUE DATA_RETENTION_TIME_IN_DAYS=5 MAX_DATA_EXTENSION_TIME_IN_DAYS=10 \
911911
WITH AGGREGATION POLICY policy_name WITH ROW ACCESS POLICY policy_name ON (a) WITH TAG (A='TAG A', B='TAG B')") {
912912
Statement::CreateTable(CreateTable {
913913
name, cluster_by, base_location,
@@ -955,7 +955,7 @@ fn test_snowflake_create_iceberg_table_all_options() {
955955
#[test]
956956
fn test_snowflake_create_iceberg_table() {
957957
match snowflake()
958-
.verified_stmt("CREATE ICEBERG TABLE my_table (a INT) BASE_LOCATION = 'relative_path'")
958+
.verified_stmt("CREATE ICEBERG TABLE my_table (a INT) BASE_LOCATION='relative_path'")
959959
{
960960
Statement::CreateTable(CreateTable {
961961
name,
@@ -1069,51 +1069,55 @@ fn parse_sf_create_table_or_view_with_dollar_quoted_comment() {
10691069
#[test]
10701070
fn parse_create_dynamic_table() {
10711071
snowflake().verified_stmt(r#"CREATE OR REPLACE DYNAMIC TABLE my_dynamic_table TARGET_LAG='20 minutes' WAREHOUSE=mywh AS SELECT product_id, product_name FROM staging_table"#);
1072-
snowflake()
1073-
.parse_sql_statements(
1074-
r#"
1075-
CREATE DYNAMIC ICEBERG TABLE my_dynamic_table (date TIMESTAMP_NTZ, id NUMBER, content STRING)
1076-
TARGET_LAG = '20 minutes'
1077-
WAREHOUSE = mywh
1078-
EXTERNAL_VOLUME = 'my_external_volume'
1079-
CATALOG = 'SNOWFLAKE'
1080-
BASE_LOCATION = 'my_iceberg_table'
1081-
AS
1082-
SELECT product_id, product_name FROM staging_table;
1083-
"#,
1084-
)
1085-
.unwrap();
1072+
snowflake().verified_stmt(concat!(
1073+
"CREATE DYNAMIC ICEBERG TABLE my_dynamic_table (date TIMESTAMP_NTZ, id NUMBER, content STRING)",
1074+
" EXTERNAL_VOLUME='my_external_volume'",
1075+
" CATALOG='SNOWFLAKE'",
1076+
" BASE_LOCATION='my_iceberg_table'",
1077+
" TARGET_LAG='20 minutes'",
1078+
" WAREHOUSE=mywh",
1079+
" AS SELECT product_id, product_name FROM staging_table"
1080+
));
10861081

1087-
snowflake()
1088-
.parse_sql_statements(
1089-
r#"
1090-
CREATE DYNAMIC TABLE my_dynamic_table (date TIMESTAMP_NTZ, id NUMBER, content VARIANT)
1091-
TARGET_LAG = '20 minutes'
1092-
WAREHOUSE = mywh
1093-
CLUSTER BY (date, id)
1094-
AS
1095-
SELECT product_id, product_name FROM staging_table;
1096-
"#,
1097-
)
1098-
.unwrap();
1082+
snowflake().verified_stmt(concat!(
1083+
"CREATE DYNAMIC TABLE my_dynamic_table (date TIMESTAMP_NTZ, id NUMBER, content VARIANT)",
1084+
" CLUSTER BY (date, id)",
1085+
" TARGET_LAG='20 minutes'",
1086+
" WAREHOUSE=mywh",
1087+
" AS SELECT product_id, product_name FROM staging_table"
1088+
));
1089+
1090+
snowflake().verified_stmt(concat!(
1091+
"CREATE DYNAMIC TABLE my_cloned_dynamic_table",
1092+
" CLONE my_dynamic_table",
1093+
" AT(TIMESTAMP => TO_TIMESTAMP_TZ('04/05/2013 01:02:03', 'mm/dd/yyyy hh24:mi:ss'))"
1094+
));
10991095

1100-
snowflake().parse_sql_statements(r#"
1101-
CREATE DYNAMIC TABLE my_cloned_dynamic_table CLONE my_dynamic_table AT (TIMESTAMP => TO_TIMESTAMP_TZ('04/05/2013 01:02:03', 'mm/dd/yyyy hh24:mi:ss'));
1102-
"#).unwrap();
1096+
snowflake().verified_stmt(concat!(
1097+
"CREATE DYNAMIC TABLE my_cloned_dynamic_table",
1098+
" CLONE my_dynamic_table",
1099+
" BEFORE(OFFSET => TO_TIMESTAMP_TZ('04/05/2013 01:02:03', 'mm/dd/yyyy hh24:mi:ss'))"
1100+
));
1101+
1102+
snowflake().verified_stmt(concat!(
1103+
"CREATE DYNAMIC TABLE my_dynamic_table",
1104+
" TARGET_LAG='DOWNSTREAM'",
1105+
" WAREHOUSE=mywh",
1106+
" INITIALIZE=ON_SCHEDULE",
1107+
" REQUIRE USER",
1108+
" AS SELECT product_id, product_name FROM staging_table"
1109+
));
1110+
1111+
snowflake().verified_stmt(concat!(
1112+
"CREATE DYNAMIC TABLE my_dynamic_table",
1113+
" TARGET_LAG='DOWNSTREAM'",
1114+
" WAREHOUSE=mywh",
1115+
" REFRESH_MODE=AUTO",
1116+
" INITIALIZE=ON_SCHEDULE",
1117+
" REQUIRE USER",
1118+
" AS SELECT product_id, product_name FROM staging_table"
1119+
));
11031120

1104-
snowflake()
1105-
.parse_sql_statements(
1106-
r#"
1107-
CREATE DYNAMIC TABLE my_dynamic_table
1108-
TARGET_LAG = 'DOWNSTREAM'
1109-
WAREHOUSE = mywh
1110-
INITIALIZE = on_schedule
1111-
REQUIRE USER
1112-
AS
1113-
SELECT product_id, product_name FROM staging_table;
1114-
"#,
1115-
)
1116-
.unwrap();
11171121
}
11181122

11191123
#[test]
@@ -4512,7 +4516,4 @@ fn test_snowflake_identifier_function() {
45124516
.is_err(),
45134517
true
45144518
);
4515-
4516-
snowflake().verified_stmt("GRANT ROLE IDENTIFIER('AAA') TO USER IDENTIFIER('AAA')");
4517-
snowflake().verified_stmt("REVOKE ROLE IDENTIFIER('AAA') FROM USER IDENTIFIER('AAA')");
45184519
}

0 commit comments

Comments
 (0)