Skip to content

Commit ac0367c

Browse files
committed
create table parsing - consolidate SqlOption::Union & SqlOption::TableEngine into SqlOption::NamedParenthesizedList
1 parent d91b70e commit ac0367c

File tree

5 files changed

+96
-73
lines changed

5 files changed

+96
-73
lines changed

src/ast/mod.rs

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7315,9 +7315,11 @@ pub enum SqlOption {
73157315

73167316
TableSpace(TablespaceOption),
73177317

7318-
Union(Vec<Ident>),
7319-
7320-
TableEngine(TableEngine),
7318+
// Advanced parameter formations
7319+
// UNION = (tbl_name[,tbl_name]...)
7320+
// ENGINE = ReplicatedMergeTree('/table_name','{replica}', ver)
7321+
// ENGINE = SummingMergeTree([columns])
7322+
NamedParenthesizedList(NamedParenthesizedList),
73217323
}
73227324

73237325
impl fmt::Display for SqlOption {
@@ -7357,18 +7359,7 @@ impl fmt::Display for SqlOption {
73577359
_ => Ok(()),
73587360
}
73597361
}
7360-
SqlOption::Union(tables) => {
7361-
write!(
7362-
f,
7363-
"UNION = ({})",
7364-
tables
7365-
.iter()
7366-
.map(|table| table.to_string())
7367-
.collect::<Vec<String>>()
7368-
.join(", ")
7369-
)
7370-
}
7371-
SqlOption::TableEngine(table_engine) => write!(f, "ENGINE = {}", table_engine),
7362+
73727363
SqlOption::Comment(comment) => match comment {
73737364
CommentDef::WithEq(comment) => {
73747365
write!(f, "COMMENT = '{comment}'")
@@ -7377,6 +7368,16 @@ impl fmt::Display for SqlOption {
73777368
write!(f, "COMMENT '{comment}'")
73787369
}
73797370
},
7371+
SqlOption::NamedParenthesizedList(value) => {
7372+
write!(f, "{} = ", value.key)?;
7373+
if let Some(key) = &value.value {
7374+
write!(f, "{}", key)?;
7375+
}
7376+
if !value.parameters.is_empty() {
7377+
write!(f, "({})", display_comma_separated(&value.parameters))?
7378+
}
7379+
Ok(())
7380+
}
73807381
}
73817382
}
73827383
}
@@ -8616,27 +8617,13 @@ impl Display for CreateViewParams {
86168617
}
86178618
}
86188619

8619-
/// Engine of DB. Some warehouse has parameters of engine, e.g. [clickhouse]
8620-
///
8621-
/// [clickhouse]: https://clickhouse.com/docs/en/engines/table-engines
86228620
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
86238621
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
86248622
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
8625-
pub struct TableEngine {
8626-
pub name: String,
8627-
pub parameters: Option<Vec<Ident>>,
8628-
}
8629-
8630-
impl Display for TableEngine {
8631-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8632-
write!(f, "{}", self.name)?;
8633-
8634-
if let Some(parameters) = self.parameters.as_ref() {
8635-
write!(f, "({})", display_comma_separated(parameters))?;
8636-
}
8637-
8638-
Ok(())
8639-
}
8623+
pub struct NamedParenthesizedList {
8624+
pub key: Ident,
8625+
pub value: Option<Ident>,
8626+
pub parameters: Vec<Ident>,
86408627
}
86418628

86428629
/// Snowflake `WITH ROW ACCESS POLICY policy_name ON (identifier, ...)`

src/ast/spans.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ use super::{
3030
FunctionArgumentClause, FunctionArgumentList, FunctionArguments, GroupByExpr, HavingBound,
3131
IfStatement, IlikeSelectItem, Insert, Interpolate, InterpolateExpr, Join, JoinConstraint,
3232
JoinOperator, JsonPath, JsonPathElem, LateralView, LimitClause, MatchRecognizePattern, Measure,
33-
NamedWindowDefinition, ObjectName, ObjectNamePart, Offset, OnConflict, OnConflictAction,
34-
OnInsert, OrderBy, OrderByExpr, OrderByKind, Partition, PivotValueSource, ProjectionSelect,
35-
Query, RaiseStatement, RaiseStatementValue, ReferentialAction, RenameSelectItem,
36-
ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem, SetExpr, SqlOption,
37-
Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef, TableConstraint,
38-
TableFactor, TableObject, TableOptionsClustered, TableWithJoins, UpdateTableFromKind, Use,
39-
Value, Values, ViewColumnDef, WildcardAdditionalOptions, With, WithFill,
33+
NamedParenthesizedList, NamedWindowDefinition, ObjectName, ObjectNamePart, Offset, OnConflict,
34+
OnConflictAction, OnInsert, OrderBy, OrderByExpr, OrderByKind, Partition, PivotValueSource,
35+
ProjectionSelect, Query, RaiseStatement, RaiseStatementValue, ReferentialAction,
36+
RenameSelectItem, ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem,
37+
SetExpr, SqlOption, Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef,
38+
TableConstraint, TableFactor, TableObject, TableOptionsClustered, TableWithJoins,
39+
UpdateTableFromKind, Use, Value, Values, ViewColumnDef, WildcardAdditionalOptions, With,
40+
WithFill,
4041
};
4142

4243
/// Given an iterator of spans, return the [Span::union] of all spans.
@@ -965,10 +966,14 @@ impl Spanned for SqlOption {
965966
} => union_spans(
966967
core::iter::once(column_name.span).chain(for_values.iter().map(|i| i.span())),
967968
),
968-
SqlOption::Union(idents) => union_spans(idents.iter().map(|i| i.span)),
969969
SqlOption::TableSpace(_) => Span::empty(),
970-
SqlOption::TableEngine(_) => Span::empty(),
971970
SqlOption::Comment(_) => Span::empty(),
971+
SqlOption::NamedParenthesizedList(NamedParenthesizedList {
972+
key: name,
973+
value,
974+
parameters: values,
975+
}) => union_spans(core::iter::once(name.span).chain(values.iter().map(|i| i.span)))
976+
.union_opt(&value.as_ref().map(|i| i.span)),
972977
}
973978
}
974979
}

src/parser/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7082,15 +7082,18 @@ impl<'a> Parser<'a> {
70827082
let engine = match value.token {
70837083
Token::Word(w) => {
70847084
let parameters = if self.peek_token() == Token::LParen {
7085-
Some(self.parse_parenthesized_identifiers()?)
7085+
self.parse_parenthesized_identifiers()?
70867086
} else {
7087-
None
7087+
vec![]
70887088
};
70897089

7090-
Ok(Some(SqlOption::TableEngine(TableEngine {
7091-
name: w.value,
7092-
parameters,
7093-
})))
7090+
Ok(Some(SqlOption::NamedParenthesizedList(
7091+
NamedParenthesizedList {
7092+
key: Ident::new("ENGINE"),
7093+
value: Some(Ident::new(w.value)),
7094+
parameters,
7095+
},
7096+
)))
70947097
}
70957098
_ => {
70967099
return self.expected("Token::Word", value)?;
@@ -7148,7 +7151,13 @@ impl<'a> Parser<'a> {
71487151
self.parse_comma_separated0(Parser::parse_identifier, Token::RParen)?;
71497152
self.expect_token(&Token::RParen)?;
71507153

7151-
return Ok(Some(SqlOption::Union(tables)));
7154+
return Ok(Some(SqlOption::NamedParenthesizedList(
7155+
NamedParenthesizedList {
7156+
key: Ident::new("UNION"),
7157+
value: None,
7158+
parameters: tables,
7159+
},
7160+
)));
71527161
}
71537162
_ => {
71547163
return self.expected("Token::LParen", value)?;

tests/sqlparser_clickhouse.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -748,13 +748,16 @@ fn parse_create_table_with_primary_key() {
748748
_ => unreachable!(),
749749
};
750750

751-
assert!(plain_options.contains(&SqlOption::TableEngine(TableEngine {
752-
name: "SharedMergeTree".to_owned(),
753-
parameters: Some(vec![
754-
Ident::with_quote('\'', "/clickhouse/tables/{uuid}/{shard}"),
755-
Ident::with_quote('\'', "{replica}"),
756-
])
757-
})));
751+
assert!(plain_options.contains(&SqlOption::NamedParenthesizedList(
752+
NamedParenthesizedList {
753+
key: Ident::new("ENGINE"),
754+
value: Some(Ident::new("SharedMergeTree")),
755+
parameters: vec![
756+
Ident::with_quote('\'', "/clickhouse/tables/{uuid}/{shard}"),
757+
Ident::with_quote('\'', "{replica}"),
758+
]
759+
}
760+
)));
758761

759762
fn assert_function(actual: &Function, name: &str, arg: &str) -> bool {
760763
assert_eq!(actual.name, ObjectName::from(vec![Ident::new(name)]));

tests/sqlparser_mysql.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,14 @@ fn parse_create_table_multiple_options_order_independent() {
924924
_ => unreachable!(),
925925
};
926926

927-
assert!(plain_options.contains(&SqlOption::TableEngine(TableEngine {
928-
name: "InnoDB".to_owned(),
929-
parameters: None
930-
})));
927+
assert!(plain_options.contains(&SqlOption::NamedParenthesizedList(
928+
NamedParenthesizedList {
929+
key: Ident::new("ENGINE"),
930+
value: Some(Ident::new("InnoDB")),
931+
parameters: vec![]
932+
}
933+
)));
934+
931935
assert!(plain_options.contains(&SqlOption::KeyValue {
932936
key: Ident::new("KEY_BLOCK_SIZE"),
933937
value: Expr::Value(
@@ -972,10 +976,14 @@ fn parse_create_table_with_all_table_options() {
972976
_ => unreachable!(),
973977
};
974978

975-
assert!(plain_options.contains(&SqlOption::TableEngine(TableEngine {
976-
name: "InnoDB".to_owned(),
977-
parameters: None
978-
})));
979+
assert!(plain_options.contains(&SqlOption::NamedParenthesizedList(
980+
NamedParenthesizedList {
981+
key: Ident::new("ENGINE"),
982+
value: Some(Ident::new("InnoDB")),
983+
parameters: vec![]
984+
}
985+
)));
986+
979987
assert!(plain_options.contains(&SqlOption::KeyValue {
980988
key: Ident::new("COLLATE"),
981989
value: Expr::Identifier(Ident::new("utf8mb4_0900_ai_ci".to_owned()))
@@ -1109,11 +1117,19 @@ fn parse_create_table_with_all_table_options() {
11091117
storage: Some(StorageType::Disk),
11101118
}))
11111119
);
1112-
assert!(plain_options.contains(&SqlOption::Union(vec![
1113-
Ident::new("table1".to_string()),
1114-
Ident::new("table2".to_string()),
1115-
Ident::new("table3".to_string())
1116-
])));
1120+
1121+
assert!(plain_options.contains(&SqlOption::NamedParenthesizedList(
1122+
NamedParenthesizedList {
1123+
key: Ident::new("UNION"),
1124+
value: None,
1125+
parameters: vec![
1126+
Ident::new("table1".to_string()),
1127+
Ident::new("table2".to_string()),
1128+
Ident::new("table3".to_string())
1129+
]
1130+
}
1131+
)));
1132+
11171133
assert!(plain_options.contains(&SqlOption::KeyValue {
11181134
key: Ident::new("DATA DIRECTORY"),
11191135
value: Expr::value(Value::SingleQuotedString("/var/lib/mysql/data".to_owned()))
@@ -1189,10 +1205,13 @@ fn parse_create_table_engine_default_charset() {
11891205
value: Expr::Identifier(Ident::new("utf8mb3".to_owned()))
11901206
}));
11911207

1192-
assert!(plain_options.contains(&SqlOption::TableEngine(TableEngine {
1193-
name: "InnoDB".to_owned(),
1194-
parameters: None
1195-
})));
1208+
assert!(plain_options.contains(&SqlOption::NamedParenthesizedList(
1209+
NamedParenthesizedList {
1210+
key: Ident::new("ENGINE"),
1211+
value: Some(Ident::new("InnoDB")),
1212+
parameters: vec![]
1213+
}
1214+
)));
11961215
}
11971216
_ => unreachable!(),
11981217
}

0 commit comments

Comments
 (0)