Skip to content

Commit 9f47fdb

Browse files
author
aleksei.p
committed
update
1 parent c34b27b commit 9f47fdb

File tree

1 file changed

+87
-50
lines changed

1 file changed

+87
-50
lines changed

tests/sqlparser_clickhouse.rs

Lines changed: 87 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,68 @@ fn parse_create_table() {
220220
);
221221
}
222222

223+
fn column_def(name: Ident, data_type: DataType) -> ColumnDef {
224+
ColumnDef {
225+
name,
226+
data_type,
227+
collation: None,
228+
options: vec![],
229+
}
230+
}
231+
232+
#[test]
233+
fn parse_clickhouse_data_types() {
234+
let sql = concat!(
235+
"CREATE TABLE table (",
236+
"a1 UInt8, a2 UInt16, a3 UInt32, a4 UInt64, a5 UInt128, a6 UInt256,",
237+
" b1 Int8, b2 Int16, b3 Int32, b4 Int64, b5 Int128, b6 Int256,",
238+
" c1 Float32, c2 Float64,",
239+
" d1 Date32, d2 DateTime64(3), d3 DateTime64(3, 'UTC'),",
240+
" e1 FixedString(255),",
241+
" f1 LowCardinality(Int32)",
242+
") ORDER BY (a1)",
243+
);
244+
// ClickHouse has a case-sensitive definition of data type, but canonical representation is not
245+
let canonical_sql = sql
246+
.replace(" Int8", " INT8")
247+
.replace(" Int64", " INT64")
248+
.replace(" Float64", " FLOAT64");
249+
250+
match clickhouse_and_generic().one_statement_parses_to(sql, &canonical_sql) {
251+
Statement::CreateTable { name, columns, .. } => {
252+
assert_eq!(name, ObjectName(vec!["table".into()]));
253+
assert_eq!(
254+
columns,
255+
vec![
256+
column_def("a1".into(), DataType::UInt8),
257+
column_def("a2".into(), DataType::UInt16),
258+
column_def("a3".into(), DataType::UInt32),
259+
column_def("a4".into(), DataType::UInt64),
260+
column_def("a5".into(), DataType::UInt128),
261+
column_def("a6".into(), DataType::UInt256),
262+
column_def("b1".into(), DataType::Int8(None)),
263+
column_def("b2".into(), DataType::Int16),
264+
column_def("b3".into(), DataType::Int32),
265+
column_def("b4".into(), DataType::Int64),
266+
column_def("b5".into(), DataType::Int128),
267+
column_def("b6".into(), DataType::Int256),
268+
column_def("c1".into(), DataType::Float32),
269+
column_def("c2".into(), DataType::Float64),
270+
column_def("d1".into(), DataType::Date32),
271+
column_def("d2".into(), DataType::Datetime64(3, None)),
272+
column_def("d3".into(), DataType::Datetime64(3, Some("UTC".into()))),
273+
column_def("e1".into(), DataType::FixedString(255)),
274+
column_def(
275+
"f1".into(),
276+
DataType::LowCardinality(Box::new(DataType::Int32))
277+
),
278+
]
279+
);
280+
}
281+
_ => unreachable!(),
282+
}
283+
}
284+
223285
#[test]
224286
fn parse_create_table_with_nullable() {
225287
let sql = r#"CREATE TABLE table (k UInt8, `a` Nullable(String), `b` Nullable(DateTime64(9, 'UTC')), c Nullable(DateTime64(9)), d Date32 NULL) ENGINE=MergeTree ORDER BY (`k`)"#;
@@ -232,42 +294,23 @@ fn parse_create_table_with_nullable() {
232294
assert_eq!(
233295
columns,
234296
vec![
235-
ColumnDef {
236-
name: Ident::new("k"),
237-
data_type: DataType::UInt8,
238-
collation: None,
239-
options: vec![],
240-
},
241-
ColumnDef {
242-
name: Ident::with_quote('`', "a"),
243-
data_type: DataType::Nullable(Box::new(DataType::String(None))),
244-
collation: None,
245-
options: vec![],
246-
},
247-
ColumnDef {
248-
name: Ident::with_quote('`', "b"),
249-
data_type: DataType::Nullable(Box::new(DataType::Datetime64(
297+
column_def("k".into(), DataType::UInt8),
298+
column_def(
299+
Ident::with_quote('`', "a"),
300+
DataType::Nullable(Box::new(DataType::String(None)))
301+
),
302+
column_def(
303+
Ident::with_quote('`', "b"),
304+
DataType::Nullable(Box::new(DataType::Datetime64(
250305
9,
251306
Some("UTC".to_string())
252-
))),
253-
collation: None,
254-
options: vec![],
255-
},
256-
ColumnDef {
257-
name: Ident::new("c"),
258-
data_type: DataType::Nullable(Box::new(DataType::Datetime64(9, None))),
259-
collation: None,
260-
options: vec![],
261-
},
262-
ColumnDef {
263-
name: Ident::new("d"),
264-
data_type: DataType::Date32,
265-
collation: None,
266-
options: vec![ColumnOptionDef {
267-
name: None,
268-
option: ColumnOption::Null
269-
}],
270-
},
307+
)))
308+
),
309+
column_def(
310+
"c".into(),
311+
DataType::Nullable(Box::new(DataType::Datetime64(9, None)))
312+
),
313+
column_def("d".into(), DataType::Date32),
271314
]
272315
);
273316
}
@@ -295,22 +338,16 @@ fn parse_create_table_with_nested_data_types() {
295338
ColumnDef {
296339
name: Ident::new("i"),
297340
data_type: DataType::Nested(vec![
298-
ColumnDef {
299-
name: "a".into(),
300-
data_type: DataType::Array(ArrayElemTypeDef::Parenthesis(
301-
Box::new(DataType::Int16),
302-
)),
303-
collation: None,
304-
options: vec![],
305-
},
306-
ColumnDef {
307-
name: "b".into(),
308-
data_type: DataType::LowCardinality(Box::new(DataType::String(
309-
None
310-
))),
311-
collation: None,
312-
options: vec![],
313-
},
341+
column_def(
342+
"a".into(),
343+
DataType::Array(ArrayElemTypeDef::Parenthesis(Box::new(
344+
DataType::Int16
345+
),))
346+
),
347+
column_def(
348+
"b".into(),
349+
DataType::LowCardinality(Box::new(DataType::String(None)))
350+
)
314351
]),
315352
collation: None,
316353
options: vec![],

0 commit comments

Comments
 (0)