Skip to content

Commit 728645f

Browse files
Add all missing table options to be handled in any order (#1747)
Co-authored-by: Tomer Shani <[email protected]>
1 parent a464f8e commit 728645f

17 files changed

+767
-382
lines changed

src/ast/dml.rs

+18-57
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ pub use super::ddl::{ColumnDef, TableConstraint};
3333

3434
use super::{
3535
display_comma_separated, display_separated, query::InputFormatClause, Assignment, ClusteredBy,
36-
CommentDef, Expr, FileFormat, FromTable, HiveDistributionStyle, HiveFormat, HiveIOFormat,
37-
HiveRowFormat, Ident, IndexType, InsertAliases, MysqlInsertPriority, ObjectName, OnCommit,
38-
OnInsert, OneOrManyWithParens, OrderByExpr, Query, RowAccessPolicy, SelectItem, Setting,
39-
SqlOption, SqliteOnConflict, StorageSerializationPolicy, TableEngine, TableObject,
40-
TableWithJoins, Tag, WrappedCollection,
36+
CommentDef, CreateTableOptions, Expr, FileFormat, FromTable, HiveDistributionStyle, HiveFormat,
37+
HiveIOFormat, HiveRowFormat, Ident, IndexType, InsertAliases, MysqlInsertPriority, ObjectName,
38+
OnCommit, OnInsert, OneOrManyWithParens, OrderByExpr, Query, RowAccessPolicy, SelectItem,
39+
Setting, SqliteOnConflict, StorageSerializationPolicy, TableObject, TableWithJoins, Tag,
40+
WrappedCollection,
4141
};
4242

4343
/// Index column type.
@@ -146,19 +146,17 @@ pub struct CreateTable {
146146
pub constraints: Vec<TableConstraint>,
147147
pub hive_distribution: HiveDistributionStyle,
148148
pub hive_formats: Option<HiveFormat>,
149-
pub table_properties: Vec<SqlOption>,
150-
pub with_options: Vec<SqlOption>,
149+
pub table_options: CreateTableOptions,
151150
pub file_format: Option<FileFormat>,
152151
pub location: Option<String>,
153152
pub query: Option<Box<Query>>,
154153
pub without_rowid: bool,
155154
pub like: Option<ObjectName>,
156155
pub clone: Option<ObjectName>,
157-
pub engine: Option<TableEngine>,
156+
// For Hive dialect, the table comment is after the column definitions without `=`,
157+
// so the `comment` field is optional and different than the comment field in the general options list.
158+
// [Hive](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable)
158159
pub comment: Option<CommentDef>,
159-
pub auto_increment_offset: Option<u32>,
160-
pub default_charset: Option<String>,
161-
pub collation: Option<String>,
162160
pub on_commit: Option<OnCommit>,
163161
/// ClickHouse "ON CLUSTER" clause:
164162
/// <https://clickhouse.com/docs/en/sql-reference/distributed-ddl/>
@@ -179,9 +177,6 @@ pub struct CreateTable {
179177
/// Hive: Table clustering column list.
180178
/// <https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable>
181179
pub clustered_by: Option<ClusteredBy>,
182-
/// BigQuery: Table options list.
183-
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
184-
pub options: Option<Vec<SqlOption>>,
185180
/// Postgres `INHERITs` clause, which contains the list of tables from which
186181
/// the new table inherits.
187182
/// <https://www.postgresql.org/docs/current/ddl-inherit.html>
@@ -282,7 +277,7 @@ impl Display for CreateTable {
282277

283278
// Hive table comment should be after column definitions, please refer to:
284279
// [Hive](https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable)
285-
if let Some(CommentDef::AfterColumnDefsWithoutEq(comment)) = &self.comment {
280+
if let Some(comment) = &self.comment {
286281
write!(f, " COMMENT '{comment}'")?;
287282
}
288283

@@ -375,35 +370,14 @@ impl Display for CreateTable {
375370
}
376371
write!(f, " LOCATION '{}'", self.location.as_ref().unwrap())?;
377372
}
378-
if !self.table_properties.is_empty() {
379-
write!(
380-
f,
381-
" TBLPROPERTIES ({})",
382-
display_comma_separated(&self.table_properties)
383-
)?;
384-
}
385-
if !self.with_options.is_empty() {
386-
write!(f, " WITH ({})", display_comma_separated(&self.with_options))?;
387-
}
388-
if let Some(engine) = &self.engine {
389-
write!(f, " ENGINE={engine}")?;
390-
}
391-
if let Some(comment_def) = &self.comment {
392-
match comment_def {
393-
CommentDef::WithEq(comment) => {
394-
write!(f, " COMMENT = '{comment}'")?;
395-
}
396-
CommentDef::WithoutEq(comment) => {
397-
write!(f, " COMMENT '{comment}'")?;
398-
}
399-
// For CommentDef::AfterColumnDefsWithoutEq will be displayed after column definition
400-
CommentDef::AfterColumnDefsWithoutEq(_) => (),
401-
}
402-
}
403373

404-
if let Some(auto_increment_offset) = self.auto_increment_offset {
405-
write!(f, " AUTO_INCREMENT {auto_increment_offset}")?;
374+
match &self.table_options {
375+
options @ CreateTableOptions::With(_)
376+
| options @ CreateTableOptions::Plain(_)
377+
| options @ CreateTableOptions::TableProperties(_) => write!(f, " {}", options)?,
378+
_ => (),
406379
}
380+
407381
if let Some(primary_key) = &self.primary_key {
408382
write!(f, " PRIMARY KEY {}", primary_key)?;
409383
}
@@ -419,15 +393,9 @@ impl Display for CreateTable {
419393
if let Some(cluster_by) = self.cluster_by.as_ref() {
420394
write!(f, " CLUSTER BY {cluster_by}")?;
421395
}
422-
423-
if let Some(options) = self.options.as_ref() {
424-
write!(
425-
f,
426-
" OPTIONS({})",
427-
display_comma_separated(options.as_slice())
428-
)?;
396+
if let options @ CreateTableOptions::Options(_) = &self.table_options {
397+
write!(f, " {}", options)?;
429398
}
430-
431399
if let Some(external_volume) = self.external_volume.as_ref() {
432400
write!(f, " EXTERNAL_VOLUME = '{external_volume}'")?;
433401
}
@@ -503,13 +471,6 @@ impl Display for CreateTable {
503471
write!(f, " WITH TAG ({})", display_comma_separated(tag.as_slice()))?;
504472
}
505473

506-
if let Some(default_charset) = &self.default_charset {
507-
write!(f, " DEFAULT CHARSET={default_charset}")?;
508-
}
509-
if let Some(collation) = &self.collation {
510-
write!(f, " COLLATE={collation}")?;
511-
}
512-
513474
if self.on_commit.is_some() {
514475
let on_commit = match self.on_commit {
515476
Some(OnCommit::DeleteRows) => "ON COMMIT DELETE ROWS",

0 commit comments

Comments
 (0)