Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions src/ast/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ pub struct CreateTable {
/// BigQuery: Table options list.
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
pub options: Option<Vec<SqlOption>>,
/// Postgres `INHERITs` clause, which contains the list of tables from which
/// the new table inherits.
/// <https://www.postgresql.org/docs/current/ddl-inherit.html>
/// <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-INHERITS>
pub inherits: Option<Vec<ObjectName>>,
/// SQLite "STRICT" clause.
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
/// then strict typing rules apply to that table.
Expand Down Expand Up @@ -405,6 +410,9 @@ impl Display for CreateTable {
if let Some(order_by) = &self.order_by {
write!(f, " ORDER BY {}", order_by)?;
}
if let Some(inherits) = &self.inherits {
write!(f, " INHERITS ({})", display_comma_separated(inherits))?;
}
if let Some(partition_by) = self.partition_by.as_ref() {
write!(f, " PARTITION BY {partition_by}")?;
}
Expand Down
11 changes: 11 additions & 0 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ pub struct CreateTableBuilder {
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
pub clustered_by: Option<ClusteredBy>,
pub options: Option<Vec<SqlOption>>,
pub inherits: Option<Vec<ObjectName>>,
pub strict: bool,
pub copy_grants: bool,
pub enable_schema_evolution: Option<bool>,
Expand Down Expand Up @@ -151,6 +152,7 @@ impl CreateTableBuilder {
cluster_by: None,
clustered_by: None,
options: None,
inherits: None,
strict: false,
copy_grants: false,
enable_schema_evolution: None,
Expand Down Expand Up @@ -331,6 +333,11 @@ impl CreateTableBuilder {
self
}

pub fn inherits(mut self, inherits: Option<Vec<ObjectName>>) -> Self {
self.inherits = inherits;
self
}

pub fn strict(mut self, strict: bool) -> Self {
self.strict = strict;
self
Expand Down Expand Up @@ -451,6 +458,7 @@ impl CreateTableBuilder {
cluster_by: self.cluster_by,
clustered_by: self.clustered_by,
options: self.options,
inherits: self.inherits,
strict: self.strict,
copy_grants: self.copy_grants,
enable_schema_evolution: self.enable_schema_evolution,
Expand Down Expand Up @@ -512,6 +520,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
cluster_by,
clustered_by,
options,
inherits,
strict,
copy_grants,
enable_schema_evolution,
Expand Down Expand Up @@ -560,6 +569,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
cluster_by,
clustered_by,
options,
inherits,
strict,
iceberg,
copy_grants,
Expand Down Expand Up @@ -591,6 +601,7 @@ pub(crate) struct CreateTableConfiguration {
pub partition_by: Option<Box<Expr>>,
pub cluster_by: Option<WrappedCollection<Vec<Ident>>>,
pub options: Option<Vec<SqlOption>>,
pub inherits: Option<Vec<ObjectName>>,
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ impl Spanned for CreateTable {
cluster_by: _, // todo, BigQuery specific
clustered_by: _, // todo, Hive specific
options: _, // todo, BigQuery specific
inherits: _, // todo, PostgreSQL specific
strict: _, // bool
copy_grants: _, // bool
enable_schema_evolution: _, // bool
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ define_keywords!(
INDEX,
INDICATOR,
INHERIT,
INHERITS,
INITIALLY,
INNER,
INOUT,
Expand Down
15 changes: 13 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7050,6 +7050,7 @@ impl<'a> Parser<'a> {
.partition_by(create_table_config.partition_by)
.cluster_by(create_table_config.cluster_by)
.options(create_table_config.options)
.inherits(create_table_config.inherits)
.primary_key(primary_key)
.strict(strict)
.build())
Expand All @@ -7070,13 +7071,22 @@ impl<'a> Parser<'a> {
}
}

/// Parse configuration like partitioning, clustering information during the table creation.
/// Parse configuration like inheritance, partitioning, clustering information during the table creation.
///
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#syntax_2)
/// [PostgreSQL](https://www.postgresql.org/docs/current/ddl-partitioning.html)
/// [PostgreSQL Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html)
/// [PostgreSQL Inheritance](https://www.postgresql.org/docs/current/ddl-inherit.html)
fn parse_optional_create_table_config(
&mut self,
) -> Result<CreateTableConfiguration, ParserError> {
let inherits = if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
&& self.parse_keyword(Keyword::INHERITS)
{
Some(self.parse_parenthesized_qualified_column_list(IsOptional::Mandatory, false)?)
} else {
None
};

let partition_by = if dialect_of!(self is BigQueryDialect | PostgreSqlDialect | GenericDialect)
&& self.parse_keywords(&[Keyword::PARTITION, Keyword::BY])
{
Expand Down Expand Up @@ -7105,6 +7115,7 @@ impl<'a> Parser<'a> {
partition_by,
cluster_by,
options,
inherits,
})
}

Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,7 @@ fn test_duckdb_union_datatype() {
cluster_by: Default::default(),
clustered_by: Default::default(),
options: Default::default(),
inherits: Default::default(),
strict: Default::default(),
copy_grants: Default::default(),
enable_schema_evolution: Default::default(),
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,7 @@ fn parse_create_table_with_valid_options() {
cluster_by: None,
clustered_by: None,
options: None,
inherits: None,
strict: false,
iceberg: false,
copy_grants: false,
Expand Down Expand Up @@ -1764,6 +1765,7 @@ fn parse_create_table_with_identity_column() {
cluster_by: None,
clustered_by: None,
options: None,
inherits: None,
strict: false,
copy_grants: false,
enable_schema_evolution: None,
Expand Down
34 changes: 34 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2733,6 +2733,39 @@ fn parse_create_brin() {
}
}

#[test]
fn parse_create_table_with_inherits() {
let single_inheritance_sql =
"CREATE TABLE child_table (child_column INT) INHERITS (public.parent_table)";
match pg().verified_stmt(single_inheritance_sql) {
Statement::CreateTable(CreateTable {
inherits: Some(inherits),
..
}) => {
assert_eq_vec(&["public", "parent_table"], &inherits[0].0);
}
_ => unreachable!(),
}

let double_inheritance_sql = "CREATE TABLE child_table (child_column INT) INHERITS (public.parent_table, pg_catalog.pg_settings)";
match pg().verified_stmt(double_inheritance_sql) {
Statement::CreateTable(CreateTable {
inherits: Some(inherits),
..
}) => {
assert_eq_vec(&["public", "parent_table"], &inherits[0].0);
assert_eq_vec(&["pg_catalog", "pg_settings"], &inherits[1].0);
}
_ => unreachable!(),
}
}

#[test]
#[should_panic]
fn parse_create_table_with_empty_inherits_fails() {
pg().verified_stmt("CREATE TABLE child_table (child_column INT) INHERITS ()");
}

#[test]
fn parse_create_index_concurrently() {
let sql = "CREATE INDEX CONCURRENTLY IF NOT EXISTS my_index ON my_table(col1,col2)";
Expand Down Expand Up @@ -5426,6 +5459,7 @@ fn parse_trigger_related_functions() {
cluster_by: None,
clustered_by: None,
options: None,
inherits: None,
strict: false,
copy_grants: false,
enable_schema_evolution: None,
Expand Down