Skip to content

Commit

Permalink
create table: add clone syntax (sqlparser-rs#542)
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Obuchowski <obuchowski.maciej@gmail.com>
  • Loading branch information
mobuchowski committed Aug 3, 2022
1 parent b984fc3 commit a70d87b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/ast/mod.rs
Expand Up @@ -891,6 +891,7 @@ pub enum Statement {
query: Option<Box<Query>>,
without_rowid: bool,
like: Option<ObjectName>,
clone: Option<ObjectName>,
engine: Option<String>,
default_charset: Option<String>,
collation: Option<String>,
Expand Down Expand Up @@ -1512,6 +1513,7 @@ impl fmt::Display for Statement {
query,
without_rowid,
like,
clone,
default_charset,
engine,
collation,
Expand Down Expand Up @@ -1548,7 +1550,7 @@ impl fmt::Display for Statement {
write!(f, ", ")?;
}
write!(f, "{})", display_comma_separated(constraints))?;
} else if query.is_none() && like.is_none() {
} else if query.is_none() && like.is_none() && clone.is_none() {
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
write!(f, " ()")?;
}
Expand All @@ -1561,6 +1563,11 @@ impl fmt::Display for Statement {
if let Some(l) = like {
write!(f, " LIKE {}", l)?;
}

if let Some(c) = clone {
write!(f, " CLONE {}", c)?;
}

match hive_distribution {
HiveDistributionStyle::PARTITIONED { columns } => {
write!(f, " PARTITIONED BY ({})", display_comma_separated(columns))?;
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Expand Up @@ -126,6 +126,7 @@ define_keywords!(
CHAR_LENGTH,
CHECK,
CLOB,
CLONE,
CLOSE,
CLUSTER,
COALESCE,
Expand Down
9 changes: 9 additions & 0 deletions src/parser.rs
Expand Up @@ -1800,6 +1800,7 @@ impl<'a> Parser<'a> {
query: None,
without_rowid: false,
like: None,
clone: None,
default_charset: None,
engine: None,
collation: None,
Expand Down Expand Up @@ -2090,6 +2091,13 @@ impl<'a> Parser<'a> {
} else {
None
};

let clone = if self.parse_keyword(Keyword::CLONE) {
self.parse_object_name().ok()
} else {
None
};

// parse optional column list (schema)
let (columns, constraints) = self.parse_columns()?;

Expand Down Expand Up @@ -2173,6 +2181,7 @@ impl<'a> Parser<'a> {
query,
without_rowid,
like,
clone,
engine,
default_charset,
collation,
Expand Down
12 changes: 12 additions & 0 deletions tests/sqlparser_common.rs
Expand Up @@ -1992,6 +1992,18 @@ fn parse_create_table_with_options() {
}
}

#[test]
fn parse_create_table_clone() {
let sql = "CREATE OR REPLACE TABLE a CLONE a_tmp";
match verified_stmt(sql) {
Statement::CreateTable { name, clone, .. } => {
assert_eq!(ObjectName(vec![Ident::new("a")]), name);
assert_eq!(Some(ObjectName(vec![(Ident::new("a_tmp"))])), clone)
}
_ => unreachable!(),
}
}

#[test]
fn parse_create_table_trailing_comma() {
let sql = "CREATE TABLE foo (bar int,)";
Expand Down

0 comments on commit a70d87b

Please sign in to comment.