Skip to content

Redshift: CREATE TABLE ... (LIKE ..) #1967

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 9 additions & 5 deletions src/ast/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ use serde::{Deserialize, Serialize};
#[cfg(feature = "visitor")]
use sqlparser_derive::{Visit, VisitMut};

use crate::display_utils::{indented_list, DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
use crate::{
ast::CreateTableLikeKind,
display_utils::{indented_list, DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline},
};

pub use super::ddl::{ColumnDef, TableConstraint};

Expand Down Expand Up @@ -153,7 +156,7 @@ pub struct CreateTable {
pub location: Option<String>,
pub query: Option<Box<Query>>,
pub without_rowid: bool,
pub like: Option<ObjectName>,
pub like: Option<CreateTableLikeKind>,
pub clone: Option<ObjectName>,
// For Hive dialect, the table comment is after the column definitions without `=`,
// so the `comment` field is optional and different than the comment field in the general options list.
Expand Down Expand Up @@ -282,6 +285,8 @@ impl Display for CreateTable {
} else if self.query.is_none() && self.like.is_none() && self.clone.is_none() {
// PostgreSQL allows `CREATE TABLE t ();`, but requires empty parens
f.write_str(" ()")?;
} else if let Some(CreateTableLikeKind::Parenthesized(like_in_columns_list)) = &self.like {
write!(f, " ({like_in_columns_list})")?;
}

// Hive table comment should be after column definitions, please refer to:
Expand All @@ -295,9 +300,8 @@ impl Display for CreateTable {
write!(f, " WITHOUT ROWID")?;
}

// Only for Hive
if let Some(l) = &self.like {
write!(f, " LIKE {l}")?;
if let Some(CreateTableLikeKind::NotParenthesized(like)) = &self.like {
write!(f, " {like}")?;
}

if let Some(c) = &self.clone {
Expand Down
6 changes: 3 additions & 3 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use sqlparser_derive::{Visit, VisitMut};

use super::super::dml::CreateTable;
use crate::ast::{
ClusteredBy, ColumnDef, CommentDef, CreateTableOptions, Expr, FileFormat,
ClusteredBy, ColumnDef, CommentDef, CreateTableLikeKind, CreateTableOptions, Expr, FileFormat,
HiveDistributionStyle, HiveFormat, Ident, ObjectName, OnCommit, OneOrManyWithParens, Query,
RowAccessPolicy, Statement, StorageSerializationPolicy, TableConstraint, Tag,
WrappedCollection,
Expand Down Expand Up @@ -82,7 +82,7 @@ pub struct CreateTableBuilder {
pub location: Option<String>,
pub query: Option<Box<Query>>,
pub without_rowid: bool,
pub like: Option<ObjectName>,
pub like: Option<CreateTableLikeKind>,
pub clone: Option<ObjectName>,
pub comment: Option<CommentDef>,
pub on_commit: Option<OnCommit>,
Expand Down Expand Up @@ -238,7 +238,7 @@ impl CreateTableBuilder {
self
}

pub fn like(mut self, like: Option<ObjectName>) -> Self {
pub fn like(mut self, like: Option<CreateTableLikeKind>) -> Self {
self.like = like;
self
}
Expand Down
57 changes: 57 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10125,6 +10125,63 @@ impl fmt::Display for MemberOf {
}
}

/// Specifies how to create a new table based on an existing table's schema.
///
/// Not parenthesized:
/// '''sql
/// CREATE TABLE new LIKE old ...
/// '''
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-table#label-create-table-like)
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_like)
///
/// Parenthesized:
/// '''sql
/// CREATE TABLE new (LIKE old ...)
/// '''
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CreateTableLikeKind {
Parenthesized(CreateTableLike),
NotParenthesized(CreateTableLike),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NotParenthesized(CreateTableLike),
Plain(CreateTableLike),

Thinking we can do similar to e.g. CreateTableOptions, so that NotParenthesized doesn't become ambiguous if there happens to be a third variant in the future

Comment on lines +10128 to +10147
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Specifies how to create a new table based on an existing table's schema.
///
/// Not parenthesized:
/// '''sql
/// CREATE TABLE new LIKE old ...
/// '''
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-table#label-create-table-like)
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_like)
///
/// Parenthesized:
/// '''sql
/// CREATE TABLE new (LIKE old ...)
/// '''
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html)
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CreateTableLikeKind {
Parenthesized(CreateTableLike),
NotParenthesized(CreateTableLike),
/// Specifies how to create a new table based on an existing table's schema.
///
/// '''sql
/// CREATE TABLE new LIKE old ...
/// '''
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CreateTableLikeKind {
/// '''sql
/// CREATE TABLE new (LIKE old ...)
/// '''
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html)
Parenthesized(CreateTableLike),
/// '''sql
/// CREATE TABLE new LIKE old ...
/// '''
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-table#label-create-table-like)
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_like)
NotParenthesized(CreateTableLike),

Thinking something like this to document each variant with its example?

}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum CreateTableLikeDefaults {
Including,
Excluding,
}

impl fmt::Display for CreateTableLikeDefaults {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CreateTableLikeDefaults::Including => write!(f, "INCLUDING DEFAULTS"),
CreateTableLikeDefaults::Excluding => write!(f, "EXCLUDING DEFAULTS"),
}
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct CreateTableLike {
pub name: ObjectName,
pub defaults: Option<CreateTableLikeDefaults>,
}

impl fmt::Display for CreateTableLike {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "LIKE {}", self.name)?;
if let Some(defaults) = &self.defaults {
write!(f, " {defaults}")?;
}
Ok(())
}
}

#[cfg(test)]
mod tests {
use crate::tokenizer::Location;
Expand Down
3 changes: 1 addition & 2 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ impl Spanned for CreateTable {
location: _, // string, no span
query,
without_rowid: _, // bool
like,
like: _,
clone,
comment: _, // todo, no span
on_commit: _,
Expand Down Expand Up @@ -610,7 +610,6 @@ impl Spanned for CreateTable {
.chain(columns.iter().map(|i| i.span()))
.chain(constraints.iter().map(|i| i.span()))
.chain(query.iter().map(|i| i.span()))
.chain(like.iter().map(|i| i.span()))
.chain(clone.iter().map(|i| i.span())),
)
}
Expand Down
19 changes: 19 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,25 @@ pub trait Dialect: Debug + Any {
fn supports_notnull_operator(&self) -> bool {
false
}

/// Returns true if the dialect supports specifying which table to copy
/// the schema from inside parenthesis.
///
/// Not parenthesized:
/// '''sql
/// CREATE TABLE new LIKE old ...
/// '''
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/create-table#label-create-table-like)
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_like)
///
/// Parenthesized:
/// '''sql
/// CREATE TABLE new (LIKE old ...)
/// '''
/// [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html)
fn supports_create_table_like_in_parens(&self) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn supports_create_table_like_in_parens(&self) -> bool {
fn supports_create_table_like_parenthesized(&self) -> bool {

false
}
}

/// This represents the operators for which precedence must be defined
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@ impl Dialect for RedshiftSqlDialect {
fn supports_select_exclude(&self) -> bool {
true
}

fn supports_create_table_like_in_parens(&self) -> bool {
true
}
}
17 changes: 11 additions & 6 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use crate::ast::helpers::stmt_data_loading::{
FileStagingCommand, StageLoadSelectItem, StageLoadSelectItemKind, StageParamsObject,
};
use crate::ast::{
ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, DollarQuotedString,
Ident, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
IdentityPropertyOrder, ObjectName, ObjectNamePart, RowAccessPolicy, ShowObjects, SqlOption,
Statement, TagsColumnOption, WrappedCollection,
ColumnOption, ColumnPolicy, ColumnPolicyProperty, CopyIntoSnowflakeKind, CreateTableLikeKind,
DollarQuotedString, Ident, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
IdentityPropertyKind, IdentityPropertyOrder, ObjectName, ObjectNamePart, RowAccessPolicy,
ShowObjects, SqlOption, Statement, TagsColumnOption, WrappedCollection,
};
use crate::dialect::{Dialect, Precedence};
use crate::keywords::Keyword;
Expand Down Expand Up @@ -577,8 +577,13 @@ pub fn parse_create_table(
builder = builder.clone_clause(clone);
}
Keyword::LIKE => {
let like = parser.parse_object_name(false).ok();
builder = builder.like(like);
let name = parser.parse_object_name(false)?;
builder = builder.like(Some(CreateTableLikeKind::NotParenthesized(
crate::ast::CreateTableLike {
name,
defaults: None,
},
)));
}
Keyword::CLUSTER => {
parser.expect_keyword_is(Keyword::BY)?;
Expand Down
3 changes: 3 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ define_keywords!(
DECLARE,
DEDUPLICATE,
DEFAULT,
DEFAULTS,
DEFAULT_DDL_COLLATION,
DEFERRABLE,
DEFERRED,
Expand Down Expand Up @@ -336,6 +337,7 @@ define_keywords!(
EXCEPTION,
EXCHANGE,
EXCLUDE,
EXCLUDING,
EXCLUSIVE,
EXEC,
EXECUTE,
Expand Down Expand Up @@ -437,6 +439,7 @@ define_keywords!(
IN,
INCLUDE,
INCLUDE_NULL_VALUES,
INCLUDING,
INCREMENT,
INDEX,
INDICATOR,
Expand Down
31 changes: 29 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7220,8 +7220,35 @@ impl<'a> Parser<'a> {
// Clickhouse has `ON CLUSTER 'cluster'` syntax for DDLs
let on_cluster = self.parse_optional_on_cluster()?;

let like = if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
self.parse_object_name(allow_unquoted_hyphen).ok()
// Try to parse `CREATE TABLE new (LIKE old [{INCLUDING | EXCLUDING} DEFAULTS])` or `CREATE TABLE new LIKE old`
let like = if self.dialect.supports_create_table_like_in_parens()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we pull the logic out to a function like maybe_parse_create_table_like()? since it now adds a bit of code to the existing function

&& self.consume_token(&Token::LParen)
{
if self.parse_keyword(Keyword::LIKE) {
let name = self.parse_object_name(allow_unquoted_hyphen)?;
let defaults = if self.parse_keywords(&[Keyword::INCLUDING, Keyword::DEFAULTS]) {
Some(CreateTableLikeDefaults::Including)
} else if self.parse_keywords(&[Keyword::EXCLUDING, Keyword::DEFAULTS]) {
Some(CreateTableLikeDefaults::Excluding)
} else {
None
};
self.expect_token(&Token::RParen)?;
Some(CreateTableLikeKind::Parenthesized(CreateTableLike {
name,
defaults,
}))
} else {
// Rollback the '(' it's probably the columns list
self.prev_token();
None
}
} else if self.parse_keyword(Keyword::LIKE) || self.parse_keyword(Keyword::ILIKE) {
let name = self.parse_object_name(allow_unquoted_hyphen)?;
Some(CreateTableLikeKind::NotParenthesized(CreateTableLike {
name,
defaults: None,
}))
} else {
None
};
Expand Down
74 changes: 74 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16256,3 +16256,77 @@ fn parse_notnull() {
// for unsupported dialects, parsing should stop at `NOT NULL`
notnull_unsupported_dialects.expr_parses_to("NOT NULL NOTNULL", "NOT NULL");
}

#[test]
fn parse_create_table_like() {
let dialects = all_dialects_except(|d| d.supports_create_table_like_in_parens());
let sql = "CREATE TABLE new LIKE old";
match dialects.verified_stmt(sql) {
Statement::CreateTable(stmt) => {
assert_eq!(
stmt.name,
ObjectName::from(vec![Ident::new("new".to_string())])
);
assert_eq!(
stmt.like,
Some(CreateTableLikeKind::NotParenthesized(CreateTableLike {
name: ObjectName::from(vec![Ident::new("old".to_string())]),
defaults: None,
}))
)
}
_ => unreachable!(),
}
let dialects = all_dialects_where(|d| d.supports_create_table_like_in_parens());
let sql = "CREATE TABLE new (LIKE old)";
match dialects.verified_stmt(sql) {
Statement::CreateTable(stmt) => {
assert_eq!(
stmt.name,
ObjectName::from(vec![Ident::new("new".to_string())])
);
assert_eq!(
stmt.like,
Some(CreateTableLikeKind::Parenthesized(CreateTableLike {
name: ObjectName::from(vec![Ident::new("old".to_string())]),
defaults: None,
}))
)
}
_ => unreachable!(),
}
let sql = "CREATE TABLE new (LIKE old INCLUDING DEFAULTS)";
match dialects.verified_stmt(sql) {
Statement::CreateTable(stmt) => {
assert_eq!(
stmt.name,
ObjectName::from(vec![Ident::new("new".to_string())])
);
assert_eq!(
stmt.like,
Some(CreateTableLikeKind::Parenthesized(CreateTableLike {
name: ObjectName::from(vec![Ident::new("old".to_string())]),
defaults: Some(CreateTableLikeDefaults::Including),
}))
)
}
_ => unreachable!(),
}
let sql = "CREATE TABLE new (LIKE old EXCLUDING DEFAULTS)";
match dialects.verified_stmt(sql) {
Statement::CreateTable(stmt) => {
assert_eq!(
stmt.name,
ObjectName::from(vec![Ident::new("new".to_string())])
);
assert_eq!(
stmt.like,
Some(CreateTableLikeKind::Parenthesized(CreateTableLike {
name: ObjectName::from(vec![Ident::new("old".to_string())]),
defaults: Some(CreateTableLikeDefaults::Excluding),
}))
)
}
_ => unreachable!(),
}
}
Loading