@@ -20,7 +20,7 @@ use sqlparser::ast::Ident;
20
20
use sqlparser:: ast:: SelectItem :: UnnamedExpr ;
21
21
use sqlparser:: ast:: TableFactor :: Table ;
22
22
use sqlparser:: ast:: * ;
23
- use sqlparser:: dialect:: ClickHouseDialect ;
23
+ use sqlparser:: dialect:: { ClickHouseDialect , GenericDialect } ;
24
24
use test_utils:: * ;
25
25
26
26
#[ macro_use]
@@ -455,6 +455,175 @@ fn parse_limit_by() {
455
455
) ;
456
456
}
457
457
458
+ #[ test]
459
+ fn parse_create_table_with_variant_default_expressions ( ) {
460
+ let sql = concat ! (
461
+ "CREATE TABLE table (" ,
462
+ "a DATETIME MATERIALIZED now()," ,
463
+ " b DATETIME EPHEMERAL now()," ,
464
+ " c DATETIME EPHEMERAL," ,
465
+ " d STRING ALIAS toString(c)" ,
466
+ ") ENGINE=MergeTree"
467
+ ) ;
468
+ match clickhouse ( ) . verified_stmt ( sql) {
469
+ Statement :: CreateTable { columns, .. } => {
470
+ assert_eq ! (
471
+ columns,
472
+ vec![
473
+ ColumnDef {
474
+ name: Ident :: new( "a" ) . empty_span( ) ,
475
+ data_type: DataType :: Custom (
476
+ ObjectName ( vec![ Ident :: new( "DATETIME" ) ] ) ,
477
+ vec![ ]
478
+ ) ,
479
+ collation: None ,
480
+ codec: None ,
481
+ options: vec![ ColumnOptionDef {
482
+ name: None ,
483
+ option: ColumnOption :: Materialized ( Expr :: Function ( Function {
484
+ name: ObjectName ( vec![ Ident :: new( "now" ) ] ) ,
485
+ args: vec![ ] ,
486
+ null_treatment: None ,
487
+ over: None ,
488
+ distinct: false ,
489
+ special: false ,
490
+ order_by: vec![ ] ,
491
+ limit: None ,
492
+ within_group: None ,
493
+ on_overflow: None ,
494
+ } ) )
495
+ } ] ,
496
+ column_options: vec![ ] ,
497
+ mask: None ,
498
+ column_location: None ,
499
+ } ,
500
+ ColumnDef {
501
+ name: Ident :: new( "b" ) . empty_span( ) ,
502
+ data_type: DataType :: Custom (
503
+ ObjectName ( vec![ Ident :: new( "DATETIME" ) ] ) ,
504
+ vec![ ]
505
+ ) ,
506
+ collation: None ,
507
+ codec: None ,
508
+ options: vec![ ColumnOptionDef {
509
+ name: None ,
510
+ option: ColumnOption :: Ephemeral ( Some ( Expr :: Function ( Function {
511
+ name: ObjectName ( vec![ Ident :: new( "now" ) ] ) ,
512
+ args: vec![ ] ,
513
+ null_treatment: None ,
514
+ over: None ,
515
+ distinct: false ,
516
+ special: false ,
517
+ order_by: vec![ ] ,
518
+ limit: None ,
519
+ within_group: None ,
520
+ on_overflow: None ,
521
+ } ) ) )
522
+ } ] ,
523
+ column_options: vec![ ] ,
524
+ mask: None ,
525
+ column_location: None ,
526
+ } ,
527
+ ColumnDef {
528
+ name: Ident :: new( "c" ) . empty_span( ) ,
529
+ data_type: DataType :: Custom (
530
+ ObjectName ( vec![ Ident :: new( "DATETIME" ) ] ) ,
531
+ vec![ ]
532
+ ) ,
533
+ collation: None ,
534
+ codec: None ,
535
+ options: vec![ ColumnOptionDef {
536
+ name: None ,
537
+ option: ColumnOption :: Ephemeral ( None )
538
+ } ] ,
539
+ column_options: vec![ ] ,
540
+ mask: None ,
541
+ column_location: None ,
542
+ } ,
543
+ ColumnDef {
544
+ name: Ident :: new( "d" ) . empty_span( ) ,
545
+ data_type: DataType :: Custom ( ObjectName ( vec![ Ident :: new( "STRING" ) ] ) , vec![ ] ) ,
546
+ collation: None ,
547
+ codec: None ,
548
+ options: vec![ ColumnOptionDef {
549
+ name: None ,
550
+ option: ColumnOption :: Alias ( Expr :: Function ( Function {
551
+ name: ObjectName ( vec![ Ident :: new( "toString" ) ] ) ,
552
+ args: vec![ FunctionArg :: Unnamed ( FunctionArgExpr :: Expr (
553
+ Expr :: Identifier ( Ident :: new( "c" ) . empty_span( ) )
554
+ ) ) ] ,
555
+ null_treatment: None ,
556
+ over: None ,
557
+ distinct: false ,
558
+ special: false ,
559
+ order_by: vec![ ] ,
560
+ limit: None ,
561
+ within_group: None ,
562
+ on_overflow: None ,
563
+ } ) )
564
+ } ] ,
565
+ column_options: vec![ ] ,
566
+ mask: None ,
567
+ column_location: None ,
568
+ }
569
+ ]
570
+ )
571
+ }
572
+ _ => unreachable ! ( ) ,
573
+ }
574
+ }
575
+
576
+ #[ test]
577
+ fn parse_create_view_with_fields_data_types ( ) {
578
+ match clickhouse ( ) . verified_stmt ( r#"CREATE VIEW v (i "int", f "String") AS SELECT * FROM t"# ) {
579
+ Statement :: CreateView {
580
+ name,
581
+ columns_with_types,
582
+ ..
583
+ } => {
584
+ assert_eq ! ( name, ObjectName ( vec![ "v" . into( ) ] ) ) ;
585
+ assert_eq ! (
586
+ columns_with_types,
587
+ vec![
588
+ ColumnDef {
589
+ name: Ident :: new( "i" ) . empty_span( ) ,
590
+ data_type: DataType :: Custom (
591
+ ObjectName ( vec![ Ident {
592
+ value: "int" . into( ) ,
593
+ quote_style: Some ( '"' )
594
+ } ] ) ,
595
+ vec![ ]
596
+ ) ,
597
+ collation: None ,
598
+ codec: None ,
599
+ options: vec![ ] ,
600
+ column_options: vec![ ] ,
601
+ mask: None ,
602
+ column_location: None ,
603
+ } ,
604
+ ColumnDef {
605
+ name: Ident :: new( "f" ) . empty_span( ) ,
606
+ data_type: DataType :: Custom (
607
+ ObjectName ( vec![ Ident {
608
+ value: "String" . into( ) ,
609
+ quote_style: Some ( '"' )
610
+ } ] ) ,
611
+ vec![ ]
612
+ ) ,
613
+ collation: None ,
614
+ codec: None ,
615
+ options: vec![ ] ,
616
+ column_options: vec![ ] ,
617
+ mask: None ,
618
+ column_location: None ,
619
+ } ,
620
+ ]
621
+ ) ;
622
+ }
623
+ _ => unreachable ! ( ) ,
624
+ }
625
+ }
626
+
458
627
#[ test]
459
628
fn parse_array_accessor ( ) {
460
629
clickhouse ( ) . one_statement_parses_to (
@@ -552,3 +721,10 @@ fn clickhouse() -> TestedDialects {
552
721
options : None ,
553
722
}
554
723
}
724
+
725
+ fn clickhouse_and_generic ( ) -> TestedDialects {
726
+ TestedDialects {
727
+ dialects : vec ! [ Box :: new( ClickHouseDialect { } ) , Box :: new( GenericDialect { } ) ] ,
728
+ options : None ,
729
+ }
730
+ }
0 commit comments