Skip to content

Commit 9ac68d1

Browse files
committed
[clickhouse] Add support for Engine parameters parsing
1 parent 8b3278b commit 9ac68d1

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

src/ast/helpers/stmt_create_table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
88
use sqlparser_derive::{Visit, VisitMut};
99

1010
use crate::ast::{
11-
ColumnDef, FileFormat, HiveDistributionStyle, HiveFormat, Ident, ObjectName, OnCommit, Query,
11+
ColumnDef, FileFormat, HiveDistributionStyle, HiveFormat, Ident, ObjectName, EngineSpec, OnCommit, Query,
1212
SqlOption, Statement, TableConstraint,
1313
};
1414
use crate::parser::ParserError;
@@ -64,7 +64,7 @@ pub struct CreateTableBuilder {
6464
pub without_rowid: bool,
6565
pub like: Option<ObjectName>,
6666
pub clone: Option<ObjectName>,
67-
pub engine: Option<String>,
67+
pub engine: Option<EngineSpec>,
6868
pub default_charset: Option<String>,
6969
pub collation: Option<String>,
7070
pub on_commit: Option<OnCommit>,
@@ -190,7 +190,7 @@ impl CreateTableBuilder {
190190
self
191191
}
192192

193-
pub fn engine(mut self, engine: Option<String>) -> Self {
193+
pub fn engine(mut self, engine: Option<EngineSpec>) -> Self {
194194
self.engine = engine;
195195
self
196196
}

src/ast/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,25 @@ impl fmt::Display for ObjectName {
176176
}
177177
}
178178

179+
180+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
181+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
182+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
183+
pub struct EngineSpec {
184+
pub name: String,
185+
pub options: Option<Vec<Expr>>,
186+
}
187+
188+
impl fmt::Display for EngineSpec {
189+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
190+
if let Some(opts) = &self.options {
191+
write!(f, "{}({})", self.name, display_comma_separated(&opts))
192+
} else {
193+
write!(f, "{}", self.name)
194+
}
195+
}
196+
}
197+
179198
/// Represents an Array Expression, either
180199
/// `ARRAY[..]`, or `[..]`
181200
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -1410,7 +1429,7 @@ pub enum Statement {
14101429
without_rowid: bool,
14111430
like: Option<ObjectName>,
14121431
clone: Option<ObjectName>,
1413-
engine: Option<String>,
1432+
engine: Option<EngineSpec>,
14141433
default_charset: Option<String>,
14151434
collation: Option<String>,
14161435
on_commit: Option<OnCommit>,

src/parser.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3474,10 +3474,22 @@ impl<'a> Parser<'a> {
34743474
let engine = if self.parse_keyword(Keyword::ENGINE) {
34753475
self.expect_token(&Token::Eq)?;
34763476
let next_token = self.next_token();
3477-
match next_token.token {
3478-
Token::Word(w) => Some(w.value),
3477+
let engine_name = match next_token.token {
3478+
Token::Word(w) => w.value,
34793479
_ => self.expected("identifier", next_token)?,
3480-
}
3480+
};
3481+
let engine_options = if self.consume_token(&Token::LParen) {
3482+
let columns = if self.peek_token() != Token::RParen {
3483+
self.parse_comma_separated(|p| p.parse_expr())?
3484+
} else {
3485+
vec![]
3486+
};
3487+
self.expect_token(&Token::RParen)?;
3488+
Some(columns)
3489+
} else {
3490+
None
3491+
};
3492+
Some(EngineSpec{name: engine_name, options: engine_options})
34813493
} else {
34823494
None
34833495
};

tests/sqlparser_clickhouse.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ fn parse_create_table() {
347347
clickhouse().verified_stmt(
348348
r#"CREATE TABLE "x" ("a" Nullable(DateTime64(8))) ENGINE=MergeTree ORDER BY ("x") AS SELECT * FROM "t" WHERE true"#,
349349
);
350+
clickhouse().verified_stmt(
351+
r#"CREATE TABLE default.runs_buffer (`workspace` LowCardinality(String), `id` String, `assets` Array(String), `asset_types` Array(Int32), `target` Array(String), `target_type` Array(Int32), `extra_references` Array(String) DEFAULT [], `extra_reference_types` Array(Int32) DEFAULT [], `run_type` Int32, `run_status` Int32, `message` String, `created_at` DateTime64(8,'UTC'), `started_at` DateTime64(8,'UTC'), `finished_at` DateTime64(8,'UTC'), `meta` String, `exclude_status_update` Bool, `ingested_at` DateTime64(8,'UTC'), `parent_ids` Array(String), `skipped` Bool DEFAULT false) ENGINE=Buffer('default', 'runs', 4, 2, 5, 10000, 1000000, 2500000, 10000000)"#,
352+
);
350353
}
351354

352355
#[test]

tests/sqlparser_mysql.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ fn parse_create_table_engine_default_charset() {
350350
},],
351351
columns
352352
);
353-
assert_eq!(engine, Some("InnoDB".to_string()));
353+
assert_eq!(engine.unwrap().name, "InnoDB".to_string());
354354
assert_eq!(default_charset, Some("utf8mb3".to_string()));
355355
}
356356
_ => unreachable!(),

0 commit comments

Comments
 (0)