Skip to content

Commit e380494

Browse files
authored
Only set hive_formats on CreateTable if formats are present (#2105)
1 parent 2a2abc8 commit e380494

File tree

6 files changed

+48
-46
lines changed

6 files changed

+48
-46
lines changed

src/dialect/snowflake.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl Dialect for SnowflakeDialect {
393393

394394
fn is_column_alias(&self, kw: &Keyword, parser: &mut Parser) -> bool {
395395
match kw {
396-
// The following keywords can be considered an alias as long as
396+
// The following keywords can be considered an alias as long as
397397
// they are not followed by other tokens that may change their meaning
398398
// e.g. `SELECT * EXCEPT (col1) FROM tbl`
399399
Keyword::EXCEPT
@@ -408,7 +408,7 @@ impl Dialect for SnowflakeDialect {
408408
Keyword::LIMIT | Keyword::OFFSET if peek_for_limit_options(parser) => false,
409409

410410
// `FETCH` can be considered an alias as long as it's not followed by `FIRST`` or `NEXT`
411-
// which would give it a different meanings, for example:
411+
// which would give it a different meanings, for example:
412412
// `SELECT 1 FETCH FIRST 10 ROWS` - not an alias
413413
// `SELECT 1 FETCH 10` - not an alias
414414
Keyword::FETCH if parser.peek_one_of_keywords(&[Keyword::FIRST, Keyword::NEXT]).is_some()
@@ -417,8 +417,8 @@ impl Dialect for SnowflakeDialect {
417417
false
418418
}
419419

420-
// Reserved keywords by the Snowflake dialect, which seem to be less strictive
421-
// than what is listed in `keywords::RESERVED_FOR_COLUMN_ALIAS`. The following
420+
// Reserved keywords by the Snowflake dialect, which seem to be less strictive
421+
// than what is listed in `keywords::RESERVED_FOR_COLUMN_ALIAS`. The following
422422
// keywords were tested with the this statement: `SELECT 1 <KW>`.
423423
Keyword::FROM
424424
| Keyword::GROUP
@@ -688,7 +688,7 @@ pub fn parse_create_table(
688688
.iceberg(iceberg)
689689
.global(global)
690690
.dynamic(dynamic)
691-
.hive_formats(Some(Default::default()));
691+
.hive_formats(None);
692692

693693
// Snowflake does not enforce order of the parameters in the statement. The parser needs to
694694
// parse the statement in a loop.

src/parser/mod.rs

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5831,15 +5831,19 @@ impl<'a> Parser<'a> {
58315831
let hive_distribution = self.parse_hive_distribution()?;
58325832
let hive_formats = self.parse_hive_formats()?;
58335833

5834-
let file_format = if let Some(ff) = &hive_formats.storage {
5835-
match ff {
5836-
HiveIOFormat::FileFormat { format } => Some(*format),
5837-
_ => None,
5834+
let file_format = if let Some(ref hf) = hive_formats {
5835+
if let Some(ref ff) = hf.storage {
5836+
match ff {
5837+
HiveIOFormat::FileFormat { format } => Some(*format),
5838+
_ => None,
5839+
}
5840+
} else {
5841+
None
58385842
}
58395843
} else {
58405844
None
58415845
};
5842-
let location = hive_formats.location.clone();
5846+
let location = hive_formats.as_ref().and_then(|hf| hf.location.clone());
58435847
let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
58445848
let table_options = if !table_properties.is_empty() {
58455849
CreateTableOptions::TableProperties(table_properties)
@@ -5850,7 +5854,7 @@ impl<'a> Parser<'a> {
58505854
.columns(columns)
58515855
.constraints(constraints)
58525856
.hive_distribution(hive_distribution)
5853-
.hive_formats(Some(hive_formats))
5857+
.hive_formats(hive_formats)
58545858
.table_options(table_options)
58555859
.or_replace(or_replace)
58565860
.if_not_exists(if_not_exists)
@@ -7579,8 +7583,8 @@ impl<'a> Parser<'a> {
75797583
}
75807584
}
75817585

7582-
pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
7583-
let mut hive_format = HiveFormat::default();
7586+
pub fn parse_hive_formats(&mut self) -> Result<Option<HiveFormat>, ParserError> {
7587+
let mut hive_format: Option<HiveFormat> = None;
75847588
loop {
75857589
match self.parse_one_of_keywords(&[
75867590
Keyword::ROW,
@@ -7589,32 +7593,39 @@ impl<'a> Parser<'a> {
75897593
Keyword::WITH,
75907594
]) {
75917595
Some(Keyword::ROW) => {
7592-
hive_format.row_format = Some(self.parse_row_format()?);
7596+
hive_format
7597+
.get_or_insert_with(HiveFormat::default)
7598+
.row_format = Some(self.parse_row_format()?);
75937599
}
75947600
Some(Keyword::STORED) => {
75957601
self.expect_keyword_is(Keyword::AS)?;
75967602
if self.parse_keyword(Keyword::INPUTFORMAT) {
75977603
let input_format = self.parse_expr()?;
75987604
self.expect_keyword_is(Keyword::OUTPUTFORMAT)?;
75997605
let output_format = self.parse_expr()?;
7600-
hive_format.storage = Some(HiveIOFormat::IOF {
7601-
input_format,
7602-
output_format,
7603-
});
7606+
hive_format.get_or_insert_with(HiveFormat::default).storage =
7607+
Some(HiveIOFormat::IOF {
7608+
input_format,
7609+
output_format,
7610+
});
76047611
} else {
76057612
let format = self.parse_file_format()?;
7606-
hive_format.storage = Some(HiveIOFormat::FileFormat { format });
7613+
hive_format.get_or_insert_with(HiveFormat::default).storage =
7614+
Some(HiveIOFormat::FileFormat { format });
76077615
}
76087616
}
76097617
Some(Keyword::LOCATION) => {
7610-
hive_format.location = Some(self.parse_literal_string()?);
7618+
hive_format.get_or_insert_with(HiveFormat::default).location =
7619+
Some(self.parse_literal_string()?);
76117620
}
76127621
Some(Keyword::WITH) => {
76137622
self.prev_token();
76147623
let properties = self
76157624
.parse_options_with_keywords(&[Keyword::WITH, Keyword::SERDEPROPERTIES])?;
76167625
if !properties.is_empty() {
7617-
hive_format.serde_properties = Some(properties);
7626+
hive_format
7627+
.get_or_insert_with(HiveFormat::default)
7628+
.serde_properties = Some(properties);
76187629
} else {
76197630
break;
76207631
}
@@ -7829,7 +7840,7 @@ impl<'a> Parser<'a> {
78297840
.if_not_exists(if_not_exists)
78307841
.transient(transient)
78317842
.hive_distribution(hive_distribution)
7832-
.hive_formats(Some(hive_formats))
7843+
.hive_formats(hive_formats)
78337844
.global(global)
78347845
.query(query)
78357846
.without_rowid(without_rowid)

tests/sqlparser_common.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4724,6 +4724,17 @@ fn parse_create_external_table_lowercase() {
47244724
assert_matches!(ast, Statement::CreateTable(CreateTable { .. }));
47254725
}
47264726

4727+
#[test]
4728+
fn parse_create_table_hive_formats_none_when_no_options() {
4729+
let sql = "CREATE TABLE simple_table (id INT, name VARCHAR(100))";
4730+
match verified_stmt(sql) {
4731+
Statement::CreateTable(CreateTable { hive_formats, .. }) => {
4732+
assert_eq!(hive_formats, None);
4733+
}
4734+
_ => unreachable!(),
4735+
}
4736+
}
4737+
47274738
#[test]
47284739
fn parse_alter_table() {
47294740
let add_column = "ALTER TABLE tab ADD COLUMN foo TEXT;";

tests/sqlparser_duckdb.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -739,12 +739,7 @@ fn test_duckdb_union_datatype() {
739739
],
740740
constraints: Default::default(),
741741
hive_distribution: HiveDistributionStyle::NONE,
742-
hive_formats: Some(HiveFormat {
743-
row_format: Default::default(),
744-
serde_properties: Default::default(),
745-
storage: Default::default(),
746-
location: Default::default()
747-
}),
742+
hive_formats: None,
748743
file_format: Default::default(),
749744
location: Default::default(),
750745
query: Default::default(),

tests/sqlparser_mssql.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,12 +1881,7 @@ fn parse_create_table_with_valid_options() {
18811881
],
18821882
constraints: vec![],
18831883
hive_distribution: HiveDistributionStyle::NONE,
1884-
hive_formats: Some(HiveFormat {
1885-
row_format: None,
1886-
serde_properties: None,
1887-
storage: None,
1888-
location: None,
1889-
},),
1884+
hive_formats: None,
18901885
file_format: None,
18911886
location: None,
18921887
query: None,
@@ -2053,12 +2048,7 @@ fn parse_create_table_with_identity_column() {
20532048
},],
20542049
constraints: vec![],
20552050
hive_distribution: HiveDistributionStyle::NONE,
2056-
hive_formats: Some(HiveFormat {
2057-
row_format: None,
2058-
serde_properties: None,
2059-
storage: None,
2060-
location: None,
2061-
},),
2051+
hive_formats: None,
20622052
file_format: None,
20632053
location: None,
20642054
query: None,

tests/sqlparser_postgres.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6016,12 +6016,7 @@ fn parse_trigger_related_functions() {
60166016
],
60176017
constraints: vec![],
60186018
hive_distribution: HiveDistributionStyle::NONE,
6019-
hive_formats: Some(HiveFormat {
6020-
row_format: None,
6021-
serde_properties: None,
6022-
storage: None,
6023-
location: None
6024-
}),
6019+
hive_formats: None,
60256020
file_format: None,
60266021
location: None,
60276022
query: None,

0 commit comments

Comments
 (0)