Skip to content

Commit a8ac13c

Browse files
mvzinkayman-sigma
authored andcommitted
Only set hive_formats on CreateTable if formats are present (apache#2105)
1 parent 9883296 commit a8ac13c

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
@@ -5862,15 +5862,19 @@ impl<'a> Parser<'a> {
58625862
let hive_distribution = self.parse_hive_distribution()?;
58635863
let hive_formats = self.parse_hive_formats()?;
58645864

5865-
let file_format = if let Some(ff) = &hive_formats.storage {
5866-
match ff {
5867-
HiveIOFormat::FileFormat { format } => Some(*format),
5868-
_ => None,
5865+
let file_format = if let Some(ref hf) = hive_formats {
5866+
if let Some(ref ff) = hf.storage {
5867+
match ff {
5868+
HiveIOFormat::FileFormat { format } => Some(*format),
5869+
_ => None,
5870+
}
5871+
} else {
5872+
None
58695873
}
58705874
} else {
58715875
None
58725876
};
5873-
let location = hive_formats.location.clone();
5877+
let location = hive_formats.as_ref().and_then(|hf| hf.location.clone());
58745878
let table_properties = self.parse_options(Keyword::TBLPROPERTIES)?;
58755879
let table_options = if !table_properties.is_empty() {
58765880
CreateTableOptions::TableProperties(table_properties)
@@ -5881,7 +5885,7 @@ impl<'a> Parser<'a> {
58815885
.columns(columns)
58825886
.constraints(constraints)
58835887
.hive_distribution(hive_distribution)
5884-
.hive_formats(Some(hive_formats))
5888+
.hive_formats(hive_formats)
58855889
.table_options(table_options)
58865890
.or_replace(or_replace)
58875891
.if_not_exists(if_not_exists)
@@ -7610,8 +7614,8 @@ impl<'a> Parser<'a> {
76107614
}
76117615
}
76127616

7613-
pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
7614-
let mut hive_format = HiveFormat::default();
7617+
pub fn parse_hive_formats(&mut self) -> Result<Option<HiveFormat>, ParserError> {
7618+
let mut hive_format: Option<HiveFormat> = None;
76157619
loop {
76167620
match self.parse_one_of_keywords(&[
76177621
Keyword::ROW,
@@ -7620,32 +7624,39 @@ impl<'a> Parser<'a> {
76207624
Keyword::WITH,
76217625
]) {
76227626
Some(Keyword::ROW) => {
7623-
hive_format.row_format = Some(self.parse_row_format()?);
7627+
hive_format
7628+
.get_or_insert_with(HiveFormat::default)
7629+
.row_format = Some(self.parse_row_format()?);
76247630
}
76257631
Some(Keyword::STORED) => {
76267632
self.expect_keyword_is(Keyword::AS)?;
76277633
if self.parse_keyword(Keyword::INPUTFORMAT) {
76287634
let input_format = self.parse_expr()?;
76297635
self.expect_keyword_is(Keyword::OUTPUTFORMAT)?;
76307636
let output_format = self.parse_expr()?;
7631-
hive_format.storage = Some(HiveIOFormat::IOF {
7632-
input_format,
7633-
output_format,
7634-
});
7637+
hive_format.get_or_insert_with(HiveFormat::default).storage =
7638+
Some(HiveIOFormat::IOF {
7639+
input_format,
7640+
output_format,
7641+
});
76357642
} else {
76367643
let format = self.parse_file_format()?;
7637-
hive_format.storage = Some(HiveIOFormat::FileFormat { format });
7644+
hive_format.get_or_insert_with(HiveFormat::default).storage =
7645+
Some(HiveIOFormat::FileFormat { format });
76387646
}
76397647
}
76407648
Some(Keyword::LOCATION) => {
7641-
hive_format.location = Some(self.parse_literal_string()?);
7649+
hive_format.get_or_insert_with(HiveFormat::default).location =
7650+
Some(self.parse_literal_string()?);
76427651
}
76437652
Some(Keyword::WITH) => {
76447653
self.prev_token();
76457654
let properties = self
76467655
.parse_options_with_keywords(&[Keyword::WITH, Keyword::SERDEPROPERTIES])?;
76477656
if !properties.is_empty() {
7648-
hive_format.serde_properties = Some(properties);
7657+
hive_format
7658+
.get_or_insert_with(HiveFormat::default)
7659+
.serde_properties = Some(properties);
76497660
} else {
76507661
break;
76517662
}
@@ -7860,7 +7871,7 @@ impl<'a> Parser<'a> {
78607871
.if_not_exists(if_not_exists)
78617872
.transient(transient)
78627873
.hive_distribution(hive_distribution)
7863-
.hive_formats(Some(hive_formats))
7874+
.hive_formats(hive_formats)
78647875
.global(global)
78657876
.query(query)
78667877
.without_rowid(without_rowid)

tests/sqlparser_common.rs

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

4728+
#[test]
4729+
fn parse_create_table_hive_formats_none_when_no_options() {
4730+
let sql = "CREATE TABLE simple_table (id INT, name VARCHAR(100))";
4731+
match verified_stmt(sql) {
4732+
Statement::CreateTable(CreateTable { hive_formats, .. }) => {
4733+
assert_eq!(hive_formats, None);
4734+
}
4735+
_ => unreachable!(),
4736+
}
4737+
}
4738+
47284739
#[test]
47294740
fn parse_alter_table() {
47304741
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)