Skip to content

Support serdeproperties for CREATE TABLE with HIVE #1152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3201,6 +3201,7 @@ impl fmt::Display for Statement {

if let Some(HiveFormat {
row_format,
serde_properties,
storage,
location,
}) = hive_formats
Expand All @@ -3225,6 +3226,13 @@ impl fmt::Display for Statement {
}
_ => (),
}
if let Some(serde_properties) = serde_properties.as_ref() {
write!(
f,
" WITH SERDEPROPERTIES ({})",
display_comma_separated(serde_properties)
)?;
}
if !*external {
if let Some(loc) = location {
write!(f, " LOCATION '{loc}'")?;
Expand Down Expand Up @@ -4875,6 +4883,7 @@ pub enum HiveIOFormat {
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct HiveFormat {
pub row_format: Option<HiveRowFormat>,
pub serde_properties: Option<Vec<SqlOption>>,
pub storage: Option<HiveIOFormat>,
pub location: Option<String>,
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ define_keywords!(
SEQUENCEFILE,
SEQUENCES,
SERDE,
SERDEPROPERTIES,
SERIALIZABLE,
SESSION,
SESSION_USER,
Expand Down
17 changes: 16 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4353,7 +4353,12 @@ impl<'a> Parser<'a> {
pub fn parse_hive_formats(&mut self) -> Result<HiveFormat, ParserError> {
let mut hive_format = HiveFormat::default();
loop {
match self.parse_one_of_keywords(&[Keyword::ROW, Keyword::STORED, Keyword::LOCATION]) {
match self.parse_one_of_keywords(&[
Keyword::ROW,
Keyword::STORED,
Keyword::LOCATION,
Keyword::WITH,
]) {
Some(Keyword::ROW) => {
hive_format.row_format = Some(self.parse_row_format()?);
}
Expand All @@ -4375,6 +4380,16 @@ impl<'a> Parser<'a> {
Some(Keyword::LOCATION) => {
hive_format.location = Some(self.parse_literal_string()?);
}
Some(Keyword::WITH) => {
self.prev_token();
let properties = self
.parse_options_with_keywords(&[Keyword::WITH, Keyword::SERDEPROPERTIES])?;
if !properties.is_empty() {
hive_format.serde_properties = Some(properties);
} else {
break;
}
}
None => break,
_ => break,
}
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_hive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ use sqlparser::test_utils::*;
fn parse_table_create() {
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)"#;
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://...'"#;
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')"#;

hive().verified_stmt(sql);
hive().verified_stmt(iof);
hive().verified_stmt(serdeproperties);
}

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