Skip to content

Commit a70d87b

Browse files
committed
create table: add clone syntax (apache#542)
Signed-off-by: Maciej Obuchowski <[email protected]>
1 parent b984fc3 commit a70d87b

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/ast/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,7 @@ pub enum Statement {
891891
query: Option<Box<Query>>,
892892
without_rowid: bool,
893893
like: Option<ObjectName>,
894+
clone: Option<ObjectName>,
894895
engine: Option<String>,
895896
default_charset: Option<String>,
896897
collation: Option<String>,
@@ -1512,6 +1513,7 @@ impl fmt::Display for Statement {
15121513
query,
15131514
without_rowid,
15141515
like,
1516+
clone,
15151517
default_charset,
15161518
engine,
15171519
collation,
@@ -1548,7 +1550,7 @@ impl fmt::Display for Statement {
15481550
write!(f, ", ")?;
15491551
}
15501552
write!(f, "{})", display_comma_separated(constraints))?;
1551-
} else if query.is_none() && like.is_none() {
1553+
} else if query.is_none() && like.is_none() && clone.is_none() {
15521554
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
15531555
write!(f, " ()")?;
15541556
}
@@ -1561,6 +1563,11 @@ impl fmt::Display for Statement {
15611563
if let Some(l) = like {
15621564
write!(f, " LIKE {}", l)?;
15631565
}
1566+
1567+
if let Some(c) = clone {
1568+
write!(f, " CLONE {}", c)?;
1569+
}
1570+
15641571
match hive_distribution {
15651572
HiveDistributionStyle::PARTITIONED { columns } => {
15661573
write!(f, " PARTITIONED BY ({})", display_comma_separated(columns))?;

src/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ define_keywords!(
126126
CHAR_LENGTH,
127127
CHECK,
128128
CLOB,
129+
CLONE,
129130
CLOSE,
130131
CLUSTER,
131132
COALESCE,

src/parser.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,7 @@ impl<'a> Parser<'a> {
18001800
query: None,
18011801
without_rowid: false,
18021802
like: None,
1803+
clone: None,
18031804
default_charset: None,
18041805
engine: None,
18051806
collation: None,
@@ -2090,6 +2091,13 @@ impl<'a> Parser<'a> {
20902091
} else {
20912092
None
20922093
};
2094+
2095+
let clone = if self.parse_keyword(Keyword::CLONE) {
2096+
self.parse_object_name().ok()
2097+
} else {
2098+
None
2099+
};
2100+
20932101
// parse optional column list (schema)
20942102
let (columns, constraints) = self.parse_columns()?;
20952103

@@ -2173,6 +2181,7 @@ impl<'a> Parser<'a> {
21732181
query,
21742182
without_rowid,
21752183
like,
2184+
clone,
21762185
engine,
21772186
default_charset,
21782187
collation,

tests/sqlparser_common.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,18 @@ fn parse_create_table_with_options() {
19921992
}
19931993
}
19941994

1995+
#[test]
1996+
fn parse_create_table_clone() {
1997+
let sql = "CREATE OR REPLACE TABLE a CLONE a_tmp";
1998+
match verified_stmt(sql) {
1999+
Statement::CreateTable { name, clone, .. } => {
2000+
assert_eq!(ObjectName(vec![Ident::new("a")]), name);
2001+
assert_eq!(Some(ObjectName(vec![(Ident::new("a_tmp"))])), clone)
2002+
}
2003+
_ => unreachable!(),
2004+
}
2005+
}
2006+
19952007
#[test]
19962008
fn parse_create_table_trailing_comma() {
19972009
let sql = "CREATE TABLE foo (bar int,)";

0 commit comments

Comments
 (0)