Skip to content

Commit 5b79539

Browse files
committed
Fix a lint, add CREATE TABLE ... LIKE ...
1 parent 64b3953 commit 5b79539

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

src/ast/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,7 @@ pub enum Statement {
515515
location: Option<String>,
516516
query: Option<Box<Query>>,
517517
without_rowid: bool,
518+
like: Option<ObjectName>
518519
},
519520
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
520521
CreateVirtualTable {
@@ -817,6 +818,7 @@ impl fmt::Display for Statement {
817818
location,
818819
query,
819820
without_rowid,
821+
like
820822
} => {
821823
// We want to allow the following options
822824
// Empty column list, allowed by PostgreSQL:
@@ -839,7 +841,7 @@ impl fmt::Display for Statement {
839841
write!(f, ", ")?;
840842
}
841843
write!(f, "{})", display_comma_separated(constraints))?;
842-
} else if query.is_none() {
844+
} else if query.is_none() && like.is_none() {
843845
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
844846
write!(f, " ()")?;
845847
}
@@ -848,9 +850,13 @@ impl fmt::Display for Statement {
848850
write!(f, " WITHOUT ROWID")?;
849851
}
850852

853+
// Only for Hive
854+
if let Some(l) = like {
855+
write!(f, " LIKE {}", l)?;
856+
}
851857
match hive_distribution {
852858
HiveDistributionStyle::PARTITIONED { columns } => {
853-
write!(f, " PARTITIONED BY ({})", display_comma_separated(&columns))?
859+
write!(f, " PARTITIONED BY ({})", display_comma_separated(&columns))?;
854860
}
855861
HiveDistributionStyle::CLUSTERED {
856862
columns,

src/parser.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ impl Parser {
11921192
location: Some(location),
11931193
query: None,
11941194
without_rowid: false,
1195+
like: None
11951196
})
11961197
}
11971198

@@ -1337,6 +1338,9 @@ impl Parser {
13371338
pub fn parse_create_table(&mut self, or_replace: bool) -> Result<Statement, ParserError> {
13381339
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
13391340
let table_name = self.parse_object_name()?;
1341+
let like = if self.parse_keyword(Keyword::LIKE) {
1342+
self.parse_object_name().ok()
1343+
} else { None };
13401344
// parse optional column list (schema)
13411345
let (columns, constraints) = self.parse_columns()?;
13421346

@@ -1369,6 +1373,7 @@ impl Parser {
13691373
location: None,
13701374
query,
13711375
without_rowid,
1376+
like
13721377
})
13731378
}
13741379

tests/sqlparser_hive.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ fn drop_table_purge() {
7575
hive().verified_stmt(purge);
7676
}
7777

78-
fn hive() -> TestedDialects {
79-
TestedDialects {
80-
dialects: vec![Box::new(HiveDialect {})],
81-
}
78+
#[test]
79+
fn create_table_like() {
80+
let like = "CREATE TABLE db.table_name LIKE db.other_table";
81+
hive().verified_stmt(like);
8282
}
8383

84-
fn hive_and_generic() -> TestedDialects {
84+
fn hive() -> TestedDialects {
8585
TestedDialects {
86-
dialects: vec![Box::new(HiveDialect {}), Box::new(GenericDialect {})],
86+
dialects: vec![Box::new(HiveDialect {})],
8787
}
8888
}

0 commit comments

Comments
 (0)