Skip to content

Commit ab18ff4

Browse files
committed
Fix SQL serialization
1 parent 709039c commit ab18ff4

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
@@ -257,7 +257,7 @@ impl Display for CreateTable {
257257
// `CREATE TABLE t (a INT) AS SELECT a from t2`
258258
write!(
259259
f,
260-
"CREATE {or_replace}{external}{global}{temporary}{transient}{volatile}{iceberg}{dynamic}TABLE {if_not_exists}{name}",
260+
"CREATE {or_replace}{external}{global}{temporary}{transient}{volatile}{dynamic}{iceberg}TABLE {if_not_exists}{name}",
261261
or_replace = if self.or_replace { "OR REPLACE " } else { "" },
262262
external = if self.external { "EXTERNAL " } else { "" },
263263
global = self.global
@@ -317,6 +317,10 @@ impl Display for CreateTable {
317317
write!(f, " CLONE {c}")?;
318318
}
319319

320+
if let Some(version) = &self.version {
321+
write!(f, " {version}")?;
322+
}
323+
320324
match &self.hive_distribution {
321325
HiveDistributionStyle::PARTITIONED { columns } => {
322326
write!(f, " PARTITIONED BY ({})", display_comma_separated(columns))?;
@@ -419,27 +423,27 @@ impl Display for CreateTable {
419423
write!(f, " {options}")?;
420424
}
421425
if let Some(external_volume) = self.external_volume.as_ref() {
422-
write!(f, " EXTERNAL_VOLUME = '{external_volume}'")?;
426+
write!(f, " EXTERNAL_VOLUME='{external_volume}'")?;
423427
}
424428

425429
if let Some(catalog) = self.catalog.as_ref() {
426-
write!(f, " CATALOG = '{catalog}'")?;
430+
write!(f, " CATALOG='{catalog}'")?;
427431
}
428432

429433
if self.iceberg {
430434
if let Some(base_location) = self.base_location.as_ref() {
431-
write!(f, " BASE_LOCATION = '{base_location}'")?;
435+
write!(f, " BASE_LOCATION='{base_location}'")?;
432436
}
433437
}
434438

435439
if let Some(catalog_sync) = self.catalog_sync.as_ref() {
436-
write!(f, " CATALOG_SYNC = '{catalog_sync}'")?;
440+
write!(f, " CATALOG_SYNC='{catalog_sync}'")?;
437441
}
438442

439443
if let Some(storage_serialization_policy) = self.storage_serialization_policy.as_ref() {
440444
write!(
441445
f,
442-
" STORAGE_SERIALIZATION_POLICY = {storage_serialization_policy}"
446+
" STORAGE_SERIALIZATION_POLICY={storage_serialization_policy}"
443447
)?;
444448
}
445449

src/ast/query.rs

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

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+
));
10991089

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();
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+
));
1095+
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]
@@ -4528,9 +4532,6 @@ fn test_snowflake_identifier_function() {
45284532
.is_err(),
45294533
true
45304534
);
4531-
4532-
snowflake().verified_stmt("GRANT ROLE IDENTIFIER('AAA') TO USER IDENTIFIER('AAA')");
4533-
snowflake().verified_stmt("REVOKE ROLE IDENTIFIER('AAA') FROM USER IDENTIFIER('AAA')");
45344535
}
45354536

45364537
#[test]

0 commit comments

Comments
 (0)