Skip to content

Commit fb7d4d4

Browse files
jonathanlehtoalamb
andauthored
Support serdeproperties for CREATE TABLE with HIVE (#1152)
Co-authored-by: Andrew Lamb <[email protected]>
1 parent 68b52a4 commit fb7d4d4

File tree

4 files changed

+28
-1
lines changed

4 files changed

+28
-1
lines changed

src/ast/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,7 @@ impl fmt::Display for Statement {
32053205

32063206
if let Some(HiveFormat {
32073207
row_format,
3208+
serde_properties,
32083209
storage,
32093210
location,
32103211
}) = hive_formats
@@ -3229,6 +3230,13 @@ impl fmt::Display for Statement {
32293230
}
32303231
_ => (),
32313232
}
3233+
if let Some(serde_properties) = serde_properties.as_ref() {
3234+
write!(
3235+
f,
3236+
" WITH SERDEPROPERTIES ({})",
3237+
display_comma_separated(serde_properties)
3238+
)?;
3239+
}
32323240
if !*external {
32333241
if let Some(loc) = location {
32343242
write!(f, " LOCATION '{loc}'")?;
@@ -4886,6 +4894,7 @@ pub enum HiveIOFormat {
48864894
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
48874895
pub struct HiveFormat {
48884896
pub row_format: Option<HiveRowFormat>,
4897+
pub serde_properties: Option<Vec<SqlOption>>,
48894898
pub storage: Option<HiveIOFormat>,
48904899
pub location: Option<String>,
48914900
}

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ define_keywords!(
596596
SEQUENCEFILE,
597597
SEQUENCES,
598598
SERDE,
599+
SERDEPROPERTIES,
599600
SERIALIZABLE,
600601
SESSION,
601602
SESSION_USER,

src/parser/mod.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4353,7 +4353,12 @@ impl<'a> Parser<'a> {
43534353
pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
43544354
let mut hive_format = HiveFormat::default();
43554355
loop {
4356-
match self.parse_one_of_keywords(&[Keyword::ROW, Keyword::STORED, Keyword::LOCATION]) {
4356+
match self.parse_one_of_keywords(&[
4357+
Keyword::ROW,
4358+
Keyword::STORED,
4359+
Keyword::LOCATION,
4360+
Keyword::WITH,
4361+
]) {
43574362
Some(Keyword::ROW) => {
43584363
hive_format.row_format = Some(self.parse_row_format()?);
43594364
}
@@ -4375,6 +4380,16 @@ impl<'a> Parser<'a> {
43754380
Some(Keyword::LOCATION) => {
43764381
hive_format.location = Some(self.parse_literal_string()?);
43774382
}
4383+
Some(Keyword::WITH) => {
4384+
self.prev_token();
4385+
let properties = self
4386+
.parse_options_with_keywords(&[Keyword::WITH, Keyword::SERDEPROPERTIES])?;
4387+
if !properties.is_empty() {
4388+
hive_format.serde_properties = Some(properties);
4389+
} else {
4390+
break;
4391+
}
4392+
}
43784393
None => break,
43794394
_ => break,
43804395
}

tests/sqlparser_hive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ use sqlparser::test_utils::*;
2727
fn parse_table_create() {
2828
let sql = r#"CREATE TABLE IF NOT EXISTS db.table (a BIGINT, b STRING, c TIMESTAMP) PARTITIONED BY (d STRING, e TIMESTAMP) STORED AS ORC LOCATION 's3://...' TBLPROPERTIES ("prop" = "2", "asdf" = '1234', 'asdf' = "1234", "asdf" = 2)"#;
2929
let iof = r#"CREATE TABLE IF NOT EXISTS db.table (a BIGINT, b STRING, c TIMESTAMP) PARTITIONED BY (d STRING, e TIMESTAMP) STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.orc.OrcOutputFormat' LOCATION 's3://...'"#;
30+
let serdeproperties = r#"CREATE EXTERNAL TABLE IF NOT EXISTS db.table (a STRING, b STRING, c STRING) PARTITIONED BY (d STRING, e STRING) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde.config' WITH SERDEPROPERTIES ('prop_a' = 'a', 'prop_b' = 'b') STORED AS TEXTFILE LOCATION 's3://...' TBLPROPERTIES ('prop_c' = 'c')"#;
3031

3132
hive().verified_stmt(sql);
3233
hive().verified_stmt(iof);
34+
hive().verified_stmt(serdeproperties);
3335
}
3436

3537
fn generic(options: Option<ParserOptions>) -> TestedDialects {

0 commit comments

Comments
 (0)