@@ -32,11 +32,11 @@ use sqlparser_derive::{Visit, VisitMut};
32
32
pub use super :: ddl:: { ColumnDef , TableConstraint } ;
33
33
34
34
use super :: {
35
- display_comma_separated, display_separated, Assignment , ClusteredBy , CommentDef , Expr ,
36
- FileFormat , FromTable , HiveDistributionStyle , HiveFormat , HiveIOFormat , HiveRowFormat , Ident ,
37
- InsertAliases , MysqlInsertPriority , ObjectName , OnCommit , OnInsert , OneOrManyWithParens ,
38
- OrderByExpr , Query , RowAccessPolicy , SelectItem , SqlOption , SqliteOnConflict , TableEngine ,
39
- TableWithJoins , Tag , WrappedCollection ,
35
+ display_comma_separated, display_separated, query :: InputFormatClause , Assignment , ClusteredBy ,
36
+ CommentDef , Expr , FileFormat , FromTable , HiveDistributionStyle , HiveFormat , HiveIOFormat ,
37
+ HiveRowFormat , Ident , InsertAliases , MysqlInsertPriority , ObjectName , OnCommit , OnInsert ,
38
+ OneOrManyWithParens , OrderByExpr , Query , RowAccessPolicy , SelectItem , Setting , SqlOption ,
39
+ SqliteOnConflict , TableEngine , TableObject , TableWithJoins , Tag , WrappedCollection ,
40
40
} ;
41
41
42
42
/// CREATE INDEX statement.
@@ -470,8 +470,7 @@ pub struct Insert {
470
470
/// INTO - optional keyword
471
471
pub into : bool ,
472
472
/// TABLE
473
- #[ cfg_attr( feature = "visitor" , visit( with = "visit_relation" ) ) ]
474
- pub table_name : ObjectName ,
473
+ pub table : TableObject ,
475
474
/// table_name as foo (for PostgreSQL)
476
475
pub table_alias : Option < Ident > ,
477
476
/// COLUMNS
@@ -488,7 +487,7 @@ pub struct Insert {
488
487
/// Columns defined after PARTITION
489
488
pub after_columns : Vec < Ident > ,
490
489
/// whether the insert has the table keyword (Hive)
491
- pub table : bool ,
490
+ pub has_table_keyword : bool ,
492
491
pub on : Option < OnInsert > ,
493
492
/// RETURNING
494
493
pub returning : Option < Vec < SelectItem > > ,
@@ -498,14 +497,27 @@ pub struct Insert {
498
497
pub priority : Option < MysqlInsertPriority > ,
499
498
/// Only for mysql
500
499
pub insert_alias : Option < InsertAliases > ,
500
+ /// Settings used for ClickHouse.
501
+ ///
502
+ /// ClickHouse syntax: `INSERT INTO tbl SETTINGS format_template_resultset = '/some/path/resultset.format'`
503
+ ///
504
+ /// [ClickHouse `INSERT INTO`](https://clickhouse.com/docs/en/sql-reference/statements/insert-into)
505
+ pub settings : Option < Vec < Setting > > ,
506
+ /// Format for `INSERT` statement when not using standard SQL format. Can be e.g. `CSV`,
507
+ /// `JSON`, `JSONAsString`, `LineAsString` and more.
508
+ ///
509
+ /// ClickHouse syntax: `INSERT INTO tbl FORMAT JSONEachRow {"foo": 1, "bar": 2}, {"foo": 3}`
510
+ ///
511
+ /// [ClickHouse formats JSON insert](https://clickhouse.com/docs/en/interfaces/formats#json-inserting-data)
512
+ pub format_clause : Option < InputFormatClause > ,
501
513
}
502
514
503
515
impl Display for Insert {
504
516
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
505
517
let table_name = if let Some ( alias) = & self . table_alias {
506
- format ! ( "{0} AS {alias}" , self . table_name )
518
+ format ! ( "{0} AS {alias}" , self . table )
507
519
} else {
508
- self . table_name . to_string ( )
520
+ self . table . to_string ( )
509
521
} ;
510
522
511
523
if let Some ( on_conflict) = self . or {
@@ -531,7 +543,7 @@ impl Display for Insert {
531
543
ignore = if self . ignore { " IGNORE" } else { "" } ,
532
544
over = if self . overwrite { " OVERWRITE" } else { "" } ,
533
545
int = if self . into { " INTO" } else { "" } ,
534
- tbl = if self . table { " TABLE" } else { "" } ,
546
+ tbl = if self . has_table_keyword { " TABLE" } else { "" } ,
535
547
) ?;
536
548
}
537
549
if !self . columns . is_empty ( ) {
@@ -546,12 +558,18 @@ impl Display for Insert {
546
558
write ! ( f, "({}) " , display_comma_separated( & self . after_columns) ) ?;
547
559
}
548
560
561
+ if let Some ( settings) = & self . settings {
562
+ write ! ( f, "SETTINGS {} " , display_comma_separated( settings) ) ?;
563
+ }
564
+
549
565
if let Some ( source) = & self . source {
550
566
write ! ( f, "{source}" ) ?;
551
567
} else if !self . assignments . is_empty ( ) {
552
568
write ! ( f, "SET " ) ?;
553
569
write ! ( f, "{}" , display_comma_separated( & self . assignments) ) ?;
554
- } else if self . source . is_none ( ) && self . columns . is_empty ( ) {
570
+ } else if let Some ( format_clause) = & self . format_clause {
571
+ write ! ( f, "{format_clause}" ) ?;
572
+ } else if self . columns . is_empty ( ) {
555
573
write ! ( f, "DEFAULT VALUES" ) ?;
556
574
}
557
575
0 commit comments