Skip to content

Commit b4fe194

Browse files
Encapsulate CreateTable
1 parent d5faf3c commit b4fe194

9 files changed

+166
-155
lines changed

src/ast/dml.rs

+61-3
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,76 @@
1111
// limitations under the License.
1212

1313
#[cfg(not(feature = "std"))]
14-
use alloc::{boxed::Box, vec::Vec};
14+
use alloc::{boxed::Box, string::String, vec::Vec};
1515

1616
#[cfg(feature = "serde")]
1717
use serde::{Deserialize, Serialize};
1818
#[cfg(feature = "visitor")]
1919
use sqlparser_derive::{Visit, VisitMut};
2020

21+
pub use super::ddl::{ColumnDef, TableConstraint};
22+
2123
use super::{
22-
Expr, FromTable, Ident, InsertAliases, MysqlInsertPriority, ObjectName, OnInsert, OrderByExpr,
23-
Query, SelectItem, SqliteOnConflict, TableWithJoins,
24+
Expr, FileFormat, FromTable, HiveDistributionStyle, HiveFormat, Ident, InsertAliases,
25+
MysqlInsertPriority, ObjectName, OnCommit, OnInsert, OrderByExpr, Query, SelectItem, SqlOption,
26+
SqliteOnConflict, TableWithJoins,
2427
};
2528

29+
/// CREATE TABLE statement.
30+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
31+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
32+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
33+
pub struct CreateTable {
34+
pub or_replace: bool,
35+
pub temporary: bool,
36+
pub external: bool,
37+
pub global: Option<bool>,
38+
pub if_not_exists: bool,
39+
pub transient: bool,
40+
/// Table name
41+
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
42+
pub name: ObjectName,
43+
/// Optional schema
44+
pub columns: Vec<ColumnDef>,
45+
pub constraints: Vec<TableConstraint>,
46+
pub hive_distribution: HiveDistributionStyle,
47+
pub hive_formats: Option<HiveFormat>,
48+
pub table_properties: Vec<SqlOption>,
49+
pub with_options: Vec<SqlOption>,
50+
pub file_format: Option<FileFormat>,
51+
pub location: Option<String>,
52+
pub query: Option<Box<Query>>,
53+
pub without_rowid: bool,
54+
pub like: Option<ObjectName>,
55+
pub clone: Option<ObjectName>,
56+
pub engine: Option<String>,
57+
pub comment: Option<String>,
58+
pub auto_increment_offset: Option<u32>,
59+
pub default_charset: Option<String>,
60+
pub collation: Option<String>,
61+
pub on_commit: Option<OnCommit>,
62+
/// ClickHouse "ON CLUSTER" clause:
63+
/// <https://clickhouse.com/docs/en/sql-reference/distributed-ddl/>
64+
pub on_cluster: Option<String>,
65+
/// ClickHouse "ORDER BY " clause. Note that omitted ORDER BY is different
66+
/// than empty (represented as ()), the latter meaning "no sorting".
67+
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/>
68+
pub order_by: Option<Vec<Ident>>,
69+
/// BigQuery: A partition expression for the table.
70+
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#partition_expression>
71+
pub partition_by: Option<Box<Expr>>,
72+
/// BigQuery: Table clustering column list.
73+
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
74+
pub cluster_by: Option<Vec<Ident>>,
75+
/// BigQuery: Table options list.
76+
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
77+
pub options: Option<Vec<SqlOption>>,
78+
/// SQLite "STRICT" clause.
79+
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
80+
/// then strict typing rules apply to that table.
81+
pub strict: bool,
82+
}
83+
2684
/// INSERT statement.
2785
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2886
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]

src/ast/helpers/stmt_create_table.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize};
77
#[cfg(feature = "visitor")]
88
use sqlparser_derive::{Visit, VisitMut};
99

10+
use super::super::dml::CreateTable;
1011
use crate::ast::{
1112
ColumnDef, Expr, FileFormat, HiveDistributionStyle, HiveFormat, Ident, ObjectName, OnCommit,
1213
Query, SqlOption, Statement, TableConstraint,
@@ -263,7 +264,7 @@ impl CreateTableBuilder {
263264
}
264265

265266
pub fn build(self) -> Statement {
266-
Statement::CreateTable {
267+
Statement::CreateTable(CreateTable {
267268
or_replace: self.or_replace,
268269
temporary: self.temporary,
269270
external: self.external,
@@ -295,7 +296,7 @@ impl CreateTableBuilder {
295296
cluster_by: self.cluster_by,
296297
options: self.options,
297298
strict: self.strict,
298-
}
299+
})
299300
}
300301
}
301302

@@ -306,7 +307,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
306307
// ownership.
307308
fn try_from(stmt: Statement) -> Result<Self, Self::Error> {
308309
match stmt {
309-
Statement::CreateTable {
310+
Statement::CreateTable(CreateTable {
310311
or_replace,
311312
temporary,
312313
external,
@@ -338,7 +339,7 @@ impl TryFrom<Statement> for CreateTableBuilder {
338339
cluster_by,
339340
options,
340341
strict,
341-
} => Ok(Self {
342+
}) => Ok(Self {
342343
or_replace,
343344
temporary,
344345
external,

src/ast/mod.rs

+36-84
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub use self::ddl::{
3838
ReferentialAction, TableConstraint, UserDefinedTypeCompositeAttributeDef,
3939
UserDefinedTypeRepresentation, ViewColumnDef,
4040
};
41-
pub use self::dml::{Delete, Insert};
41+
pub use self::dml::{CreateTable, Delete, Insert};
4242
pub use self::operator::{BinaryOperator, UnaryOperator};
4343
pub use self::query::{
4444
AfterMatchSkip, ConnectBy, Cte, CteAsMaterialized, Distinct, EmptyMatchesMode,
@@ -1968,56 +1968,7 @@ pub enum Statement {
19681968
/// ```sql
19691969
/// CREATE TABLE
19701970
/// ```
1971-
CreateTable {
1972-
or_replace: bool,
1973-
temporary: bool,
1974-
external: bool,
1975-
global: Option<bool>,
1976-
if_not_exists: bool,
1977-
transient: bool,
1978-
/// Table name
1979-
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
1980-
name: ObjectName,
1981-
/// Optional schema
1982-
columns: Vec<ColumnDef>,
1983-
constraints: Vec<TableConstraint>,
1984-
hive_distribution: HiveDistributionStyle,
1985-
hive_formats: Option<HiveFormat>,
1986-
table_properties: Vec<SqlOption>,
1987-
with_options: Vec<SqlOption>,
1988-
file_format: Option<FileFormat>,
1989-
location: Option<String>,
1990-
query: Option<Box<Query>>,
1991-
without_rowid: bool,
1992-
like: Option<ObjectName>,
1993-
clone: Option<ObjectName>,
1994-
engine: Option<String>,
1995-
comment: Option<String>,
1996-
auto_increment_offset: Option<u32>,
1997-
default_charset: Option<String>,
1998-
collation: Option<String>,
1999-
on_commit: Option<OnCommit>,
2000-
/// ClickHouse "ON CLUSTER" clause:
2001-
/// <https://clickhouse.com/docs/en/sql-reference/distributed-ddl/>
2002-
on_cluster: Option<String>,
2003-
/// ClickHouse "ORDER BY " clause. Note that omitted ORDER BY is different
2004-
/// than empty (represented as ()), the latter meaning "no sorting".
2005-
/// <https://clickhouse.com/docs/en/sql-reference/statements/create/table/>
2006-
order_by: Option<Vec<Ident>>,
2007-
/// BigQuery: A partition expression for the table.
2008-
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#partition_expression>
2009-
partition_by: Option<Box<Expr>>,
2010-
/// BigQuery: Table clustering column list.
2011-
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
2012-
cluster_by: Option<Vec<Ident>>,
2013-
/// BigQuery: Table options list.
2014-
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#table_option_list>
2015-
options: Option<Vec<SqlOption>>,
2016-
/// SQLite "STRICT" clause.
2017-
/// if the "STRICT" table-option keyword is added to the end, after the closing ")",
2018-
/// then strict typing rules apply to that table.
2019-
strict: bool,
2020-
},
1971+
CreateTable(CreateTable),
20211972
/// ```sql
20221973
/// CREATE VIRTUAL TABLE .. USING <module_name> (<module_args>)`
20231974
/// ```
@@ -3257,39 +3208,40 @@ impl fmt::Display for Statement {
32573208
}
32583209
Ok(())
32593210
}
3260-
Statement::CreateTable {
3261-
name,
3262-
columns,
3263-
constraints,
3264-
table_properties,
3265-
with_options,
3266-
or_replace,
3267-
if_not_exists,
3268-
transient,
3269-
hive_distribution,
3270-
hive_formats,
3271-
external,
3272-
global,
3273-
temporary,
3274-
file_format,
3275-
location,
3276-
query,
3277-
without_rowid,
3278-
like,
3279-
clone,
3280-
default_charset,
3281-
engine,
3282-
comment,
3283-
auto_increment_offset,
3284-
collation,
3285-
on_commit,
3286-
on_cluster,
3287-
order_by,
3288-
partition_by,
3289-
cluster_by,
3290-
options,
3291-
strict,
3292-
} => {
3211+
Statement::CreateTable(create_table) => {
3212+
let CreateTable {
3213+
name,
3214+
columns,
3215+
constraints,
3216+
table_properties,
3217+
with_options,
3218+
or_replace,
3219+
if_not_exists,
3220+
transient,
3221+
hive_distribution,
3222+
hive_formats,
3223+
external,
3224+
global,
3225+
temporary,
3226+
file_format,
3227+
location,
3228+
query,
3229+
without_rowid,
3230+
like,
3231+
clone,
3232+
default_charset,
3233+
engine,
3234+
comment,
3235+
auto_increment_offset,
3236+
collation,
3237+
on_commit,
3238+
on_cluster,
3239+
order_by,
3240+
partition_by,
3241+
cluster_by,
3242+
options,
3243+
strict,
3244+
} = create_table;
32933245
// We want to allow the following options
32943246
// Empty column list, allowed by PostgreSQL:
32953247
// `CREATE TABLE t ()`

tests/sqlparser_bigquery.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ fn parse_create_view_with_unquoted_hyphen() {
350350
fn parse_create_table_with_unquoted_hyphen() {
351351
let sql = "CREATE TABLE my-pro-ject.mydataset.mytable (x INT64)";
352352
match bigquery().verified_stmt(sql) {
353-
Statement::CreateTable { name, columns, .. } => {
353+
Statement::CreateTable(CreateTable { name, columns, .. }) => {
354354
assert_eq!(
355355
name,
356356
ObjectName(vec![
@@ -384,14 +384,14 @@ fn parse_create_table_with_options() {
384384
r#"OPTIONS(partition_expiration_days = 1, description = "table option description")"#
385385
);
386386
match bigquery().verified_stmt(sql) {
387-
Statement::CreateTable {
387+
Statement::CreateTable(CreateTable {
388388
name,
389389
columns,
390390
partition_by,
391391
cluster_by,
392392
options,
393393
..
394-
} => {
394+
}) => {
395395
assert_eq!(
396396
name,
397397
ObjectName(vec!["mydataset".into(), "newtable".into()])
@@ -473,7 +473,7 @@ fn parse_create_table_with_options() {
473473
fn parse_nested_data_types() {
474474
let sql = "CREATE TABLE table (x STRUCT<a ARRAY<INT64>, b BYTES(42)>, y ARRAY<STRUCT<INT64>>)";
475475
match bigquery_and_generic().one_statement_parses_to(sql, sql) {
476-
Statement::CreateTable { name, columns, .. } => {
476+
Statement::CreateTable(CreateTable { name, columns, .. }) => {
477477
assert_eq!(name, ObjectName(vec!["table".into()]));
478478
assert_eq!(
479479
columns,

0 commit comments

Comments
 (0)