@@ -211,12 +211,9 @@ fn parse_delimited_identifiers() {
211
211
#[ test]
212
212
fn parse_create_table ( ) {
213
213
clickhouse ( ) . verified_stmt ( r#"CREATE TABLE "x" ("a" "int") ENGINE=MergeTree ORDER BY ("x")"# ) ;
214
- clickhouse ( ) . one_statement_parses_to (
215
- r#"CREATE TABLE "x" ("a" "int") ENGINE=MergeTree ORDER BY "x""# ,
216
- r#"CREATE TABLE "x" ("a" "int") ENGINE=MergeTree ORDER BY ("x")"# ,
217
- ) ;
214
+ clickhouse ( ) . verified_stmt ( r#"CREATE TABLE "x" ("a" "int") ENGINE=MergeTree ORDER BY "x""# ) ;
218
215
clickhouse ( ) . verified_stmt (
219
- r#"CREATE TABLE "x" ("a" "int") ENGINE=MergeTree ORDER BY ( "x") AS SELECT * FROM "t" WHERE true"# ,
216
+ r#"CREATE TABLE "x" ("a" "int") ENGINE=MergeTree ORDER BY "x" AS SELECT * FROM "t" WHERE true"# ,
220
217
) ;
221
218
}
222
219
@@ -248,7 +245,7 @@ fn parse_clickhouse_data_types() {
248
245
. replace ( " Float64" , " FLOAT64" ) ;
249
246
250
247
match clickhouse_and_generic ( ) . one_statement_parses_to ( sql, & canonical_sql) {
251
- Statement :: CreateTable { name, columns, .. } => {
248
+ Statement :: CreateTable ( CreateTable { name, columns, .. } ) => {
252
249
assert_eq ! ( name, ObjectName ( vec![ "table" . into( ) ] ) ) ;
253
250
assert_eq ! (
254
251
columns,
@@ -289,7 +286,7 @@ fn parse_create_table_with_nullable() {
289
286
let canonical_sql = sql. replace ( "String" , "STRING" ) ;
290
287
291
288
match clickhouse_and_generic ( ) . one_statement_parses_to ( sql, & canonical_sql) {
292
- Statement :: CreateTable { name, columns, .. } => {
289
+ Statement :: CreateTable ( CreateTable { name, columns, .. } ) => {
293
290
assert_eq ! ( name, ObjectName ( vec![ "table" . into( ) ] ) ) ;
294
291
assert_eq ! (
295
292
columns,
@@ -338,7 +335,7 @@ fn parse_create_table_with_nested_data_types() {
338
335
) ;
339
336
340
337
match clickhouse ( ) . one_statement_parses_to ( sql, "" ) {
341
- Statement :: CreateTable { name, columns, .. } => {
338
+ Statement :: CreateTable ( CreateTable { name, columns, .. } ) => {
342
339
assert_eq ! ( name, ObjectName ( vec![ "table" . into( ) ] ) ) ;
343
340
assert_eq ! (
344
341
columns,
@@ -410,6 +407,88 @@ fn parse_create_table_with_nested_data_types() {
410
407
}
411
408
}
412
409
410
+ #[ test]
411
+ fn parse_create_table_with_primary_key ( ) {
412
+ match clickhouse_and_generic ( ) . verified_stmt ( concat ! (
413
+ r#"CREATE TABLE db.table (`i` INT, `k` INT)"# ,
414
+ " ENGINE=SharedMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}')" ,
415
+ " PRIMARY KEY tuple(i)" ,
416
+ " ORDER BY tuple(i)" ,
417
+ ) ) {
418
+ Statement :: CreateTable ( CreateTable {
419
+ name,
420
+ columns,
421
+ engine,
422
+ primary_key,
423
+ order_by,
424
+ ..
425
+ } ) => {
426
+ assert_eq ! ( name. to_string( ) , "db.table" ) ;
427
+ assert_eq ! (
428
+ vec![
429
+ ColumnDef {
430
+ name: Ident :: with_quote( '`' , "i" ) ,
431
+ data_type: DataType :: Int ( None ) ,
432
+ collation: None ,
433
+ options: vec![ ] ,
434
+ } ,
435
+ ColumnDef {
436
+ name: Ident :: with_quote( '`' , "k" ) ,
437
+ data_type: DataType :: Int ( None ) ,
438
+ collation: None ,
439
+ options: vec![ ] ,
440
+ } ,
441
+ ] ,
442
+ columns
443
+ ) ;
444
+ assert_eq ! (
445
+ engine,
446
+ Some ( TableEngine {
447
+ name: "SharedMergeTree" . to_string( ) ,
448
+ parameters: Some ( vec![
449
+ Ident :: with_quote( '\'' , "/clickhouse/tables/{uuid}/{shard}" ) ,
450
+ Ident :: with_quote( '\'' , "{replica}" ) ,
451
+ ] ) ,
452
+ } )
453
+ ) ;
454
+ fn assert_function ( actual : & Function , name : & str , arg : & str ) -> bool {
455
+ assert_eq ! ( actual. name, ObjectName ( vec![ Ident :: new( name) ] ) ) ;
456
+ assert_eq ! (
457
+ actual. args,
458
+ FunctionArguments :: List ( FunctionArgumentList {
459
+ args: vec![ FunctionArg :: Unnamed ( FunctionArgExpr :: Expr ( Identifier (
460
+ Ident :: new( arg)
461
+ ) ) , ) ] ,
462
+ duplicate_treatment: None ,
463
+ clauses: vec![ ] ,
464
+ } )
465
+ ) ;
466
+ true
467
+ }
468
+ match primary_key. unwrap ( ) . as_ref ( ) {
469
+ Expr :: Function ( primary_key) => {
470
+ assert ! ( assert_function( primary_key, "tuple" , "i" ) ) ;
471
+ }
472
+ _ => panic ! ( "unexpected primary key type" ) ,
473
+ }
474
+ match order_by {
475
+ Some ( OneOrManyWithParens :: One ( Expr :: Function ( order_by) ) ) => {
476
+ assert ! ( assert_function( & order_by, "tuple" , "i" ) ) ;
477
+ }
478
+ _ => panic ! ( "unexpected order by type" ) ,
479
+ } ;
480
+ }
481
+ _ => unreachable ! ( ) ,
482
+ }
483
+
484
+ clickhouse_and_generic ( )
485
+ . parse_sql_statements ( concat ! (
486
+ r#"CREATE TABLE db.table (`i` Int, `k` Int)"# ,
487
+ " ORDER BY tuple(i), tuple(k)" ,
488
+ ) )
489
+ . expect_err ( "ORDER BY supports one expression with tuple" ) ;
490
+ }
491
+
413
492
#[ test]
414
493
fn parse_create_view_with_fields_data_types ( ) {
415
494
match clickhouse ( ) . verified_stmt ( r#"CREATE VIEW v (i "int", f "String") AS SELECT * FROM t"# ) {
0 commit comments