Skip to content

Commit f9a2cb7

Browse files
committed
Fix a lint, add CREATE TABLE ... LIKE ...
1 parent 2ea1d84 commit f9a2cb7

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
@@ -513,6 +513,7 @@ pub enum Statement {
513513
location: Option<String>,
514514
query: Option<Box<Query>>,
515515
without_rowid: bool,
516+
like: Option<ObjectName>
516517
},
517518
/// SQLite's `CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
518519
CreateVirtualTable {
@@ -808,6 +809,7 @@ impl fmt::Display for Statement {
808809
location,
809810
query,
810811
without_rowid,
812+
like
811813
} => {
812814
// We want to allow the following options
813815
// Empty column list, allowed by PostgreSQL:
@@ -829,7 +831,7 @@ impl fmt::Display for Statement {
829831
write!(f, ", ")?;
830832
}
831833
write!(f, "{})", display_comma_separated(constraints))?;
832-
} else if query.is_none() {
834+
} else if query.is_none() && like.is_none() {
833835
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
834836
write!(f, " ()")?;
835837
}
@@ -838,9 +840,13 @@ impl fmt::Display for Statement {
838840
write!(f, " WITHOUT ROWID")?;
839841
}
840842

843+
// Only for Hive
844+
if let Some(l) = like {
845+
write!(f, " LIKE {}", l)?;
846+
}
841847
match hive_distribution {
842848
HiveDistributionStyle::PARTITIONED { columns } => {
843-
write!(f, " PARTITIONED BY ({})", display_comma_separated(&columns))?
849+
write!(f, " PARTITIONED BY ({})", display_comma_separated(&columns))?;
844850
}
845851
HiveDistributionStyle::CLUSTERED {
846852
columns,

src/parser.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ impl Parser {
11831183
location: Some(location),
11841184
query: None,
11851185
without_rowid: false,
1186+
like: None
11861187
})
11871188
}
11881189

@@ -1327,6 +1328,9 @@ impl Parser {
13271328
pub fn parse_create_table(&mut self) -> Result<Statement, ParserError> {
13281329
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
13291330
let table_name = self.parse_object_name()?;
1331+
let like = if self.parse_keyword(Keyword::LIKE) {
1332+
self.parse_object_name().ok()
1333+
} else { None };
13301334
// parse optional column list (schema)
13311335
let (columns, constraints) = self.parse_columns()?;
13321336

@@ -1358,6 +1362,7 @@ impl Parser {
13581362
location: None,
13591363
query,
13601364
without_rowid,
1365+
like
13611366
})
13621367
}
13631368

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)