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

Merged
merged 2 commits into from
Aug 16, 2025

Conversation

yoavcloud
Copy link
Contributor

Add support for Redshift CREATE TABLE new_table (LIKE parent_table [ { INCLUDING | EXCLUDING } DEFAULTS ])

src/ast/mod.rs Outdated
#[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

src/ast/mod.rs Outdated
Comment on lines 10128 to 10147
/// 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
/// 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?

/// 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 {

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

@yoavcloud yoavcloud force-pushed the redshift_create_table_like branch from 257f7d3 to aedcce9 Compare August 14, 2025 09:43
@yoavcloud yoavcloud requested a review from iffyio August 14, 2025 09:48
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @yoavcloud!
cc @alamb

@iffyio iffyio merged commit b660a3b into apache:main Aug 16, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants