File tree 5 files changed +55
-14
lines changed
5 files changed +55
-14
lines changed Original file line number Diff line number Diff line change @@ -1385,7 +1385,7 @@ pub enum Statement {
1385
1385
/// Overwrite (Hive)
1386
1386
overwrite : bool ,
1387
1387
/// A SQL query that specifies what to insert
1388
- source : Box < Query > ,
1388
+ source : Option < Box < Query > > ,
1389
1389
/// partitioned insert (Hive)
1390
1390
partitioned : Option < Vec < Expr > > ,
1391
1391
/// Columns defined after PARTITION
@@ -2241,7 +2241,14 @@ impl fmt::Display for Statement {
2241
2241
if !after_columns. is_empty ( ) {
2242
2242
write ! ( f, "({}) " , display_comma_separated( after_columns) ) ?;
2243
2243
}
2244
- write ! ( f, "{source}" ) ?;
2244
+
2245
+ if let Some ( source) = source {
2246
+ write ! ( f, "{source}" ) ?;
2247
+ }
2248
+
2249
+ if source. is_none ( ) && columns. is_empty ( ) {
2250
+ write ! ( f, "DEFAULT VALUES" ) ?;
2251
+ }
2245
2252
2246
2253
if let Some ( on) = on {
2247
2254
write ! ( f, "{on}" ) ?;
Original file line number Diff line number Diff line change @@ -7090,7 +7090,16 @@ impl<'a> Parser<'a> {
7090
7090
let table = self . parse_keyword ( Keyword :: TABLE ) ;
7091
7091
let table_name = self . parse_object_name ( ) ?;
7092
7092
let is_mysql = dialect_of ! ( self is MySqlDialect ) ;
7093
- let columns = self . parse_parenthesized_column_list ( Optional , is_mysql) ?;
7093
+ let is_postgresql = dialect_of ! ( self is PostgreSqlDialect ) ;
7094
+
7095
+ let is_postgresql_default_values =
7096
+ is_postgresql && self . parse_keywords ( & [ Keyword :: DEFAULT , Keyword :: VALUES ] ) ;
7097
+
7098
+ let columns: Vec < Ident > = if !is_postgresql_default_values {
7099
+ self . parse_parenthesized_column_list ( Optional , is_mysql) ?
7100
+ } else {
7101
+ vec ! [ ]
7102
+ } ;
7094
7103
7095
7104
let partitioned = if self . parse_keyword ( Keyword :: PARTITION ) {
7096
7105
self . expect_token ( & Token :: LParen ) ?;
@@ -7104,7 +7113,12 @@ impl<'a> Parser<'a> {
7104
7113
// Hive allows you to specify columns after partitions as well if you want.
7105
7114
let after_columns = self . parse_parenthesized_column_list ( Optional , false ) ?;
7106
7115
7107
- let source = Box :: new ( self . parse_query ( ) ?) ;
7116
+ let source = if !is_postgresql_default_values {
7117
+ Some ( Box :: new ( self . parse_query ( ) ?) )
7118
+ } else {
7119
+ None
7120
+ } ;
7121
+
7108
7122
let on = if self . parse_keyword ( Keyword :: ON ) {
7109
7123
if self . parse_keyword ( Keyword :: CONFLICT ) {
7110
7124
let conflict_target =
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ fn parse_insert_values() {
93
93
for ( index, column) in columns. iter ( ) . enumerate ( ) {
94
94
assert_eq ! ( column, & Ident :: new( expected_columns[ index] . clone( ) ) ) ;
95
95
}
96
- match & * source. body {
96
+ match & * source. as_ref ( ) . unwrap ( ) . body {
97
97
SetExpr :: Values ( Values { rows, .. } ) => {
98
98
assert_eq ! ( rows. as_slice( ) , expected_rows)
99
99
}
Original file line number Diff line number Diff line change @@ -937,7 +937,7 @@ fn parse_simple_insert() {
937
937
assert_eq ! ( vec![ Ident :: new( "title" ) , Ident :: new( "priority" ) ] , columns) ;
938
938
assert ! ( on. is_none( ) ) ;
939
939
assert_eq ! (
940
- Box :: new( Query {
940
+ Some ( Box :: new( Query {
941
941
with: None ,
942
942
body: Box :: new( SetExpr :: Values ( Values {
943
943
explicit_row: false ,
@@ -964,7 +964,7 @@ fn parse_simple_insert() {
964
964
offset: None ,
965
965
fetch: None ,
966
966
locks: vec![ ] ,
967
- } ) ,
967
+ } ) ) ,
968
968
source
969
969
) ;
970
970
}
@@ -990,7 +990,7 @@ fn parse_ignore_insert() {
990
990
assert ! ( on. is_none( ) ) ;
991
991
assert ! ( ignore) ;
992
992
assert_eq ! (
993
- Box :: new( Query {
993
+ Some ( Box :: new( Query {
994
994
with: None ,
995
995
body: Box :: new( SetExpr :: Values ( Values {
996
996
explicit_row: false ,
@@ -1005,7 +1005,7 @@ fn parse_ignore_insert() {
1005
1005
offset: None ,
1006
1006
fetch: None ,
1007
1007
locks: vec![ ]
1008
- } ) ,
1008
+ } ) ) ,
1009
1009
source
1010
1010
) ;
1011
1011
}
@@ -1029,7 +1029,7 @@ fn parse_empty_row_insert() {
1029
1029
assert ! ( columns. is_empty( ) ) ;
1030
1030
assert ! ( on. is_none( ) ) ;
1031
1031
assert_eq ! (
1032
- Box :: new( Query {
1032
+ Some ( Box :: new( Query {
1033
1033
with: None ,
1034
1034
body: Box :: new( SetExpr :: Values ( Values {
1035
1035
explicit_row: false ,
@@ -1041,7 +1041,7 @@ fn parse_empty_row_insert() {
1041
1041
offset: None ,
1042
1042
fetch: None ,
1043
1043
locks: vec![ ] ,
1044
- } ) ,
1044
+ } ) ) ,
1045
1045
source
1046
1046
) ;
1047
1047
}
@@ -1077,7 +1077,7 @@ fn parse_insert_with_on_duplicate_update() {
1077
1077
columns
1078
1078
) ;
1079
1079
assert_eq ! (
1080
- Box :: new( Query {
1080
+ Some ( Box :: new( Query {
1081
1081
with: None ,
1082
1082
body: Box :: new( SetExpr :: Values ( Values {
1083
1083
explicit_row: false ,
@@ -1100,7 +1100,7 @@ fn parse_insert_with_on_duplicate_update() {
1100
1100
offset: None ,
1101
1101
fetch: None ,
1102
1102
locks: vec![ ] ,
1103
- } ) ,
1103
+ } ) ) ,
1104
1104
source
1105
1105
) ;
1106
1106
assert_eq ! (
Original file line number Diff line number Diff line change @@ -1383,7 +1383,7 @@ fn parse_prepare() {
1383
1383
Expr :: Identifier ( "a2" . into( ) ) ,
1384
1384
Expr :: Identifier ( "a3" . into( ) ) ,
1385
1385
] ] ;
1386
- match & * source. body {
1386
+ match & * source. as_ref ( ) . unwrap ( ) . body {
1387
1387
SetExpr :: Values ( Values { rows, .. } ) => {
1388
1388
assert_eq ! ( rows. as_slice( ) , & expected_values)
1389
1389
}
@@ -3502,3 +3502,23 @@ fn parse_join_constraint_unnest_alias() {
3502
3502
} ]
3503
3503
) ;
3504
3504
}
3505
+
3506
+ // TODO: more tests with `ON CONFLICT`, `RETURNING`, ... any others?
3507
+ #[ test]
3508
+ fn parse_insert_default_values ( ) {
3509
+ let stmt = pg ( ) . verified_stmt ( "INSERT INTO users DEFAULT VALUES" ) ;
3510
+
3511
+ match stmt {
3512
+ Statement :: Insert {
3513
+ columns,
3514
+ source,
3515
+ table_name,
3516
+ ..
3517
+ } => {
3518
+ assert_eq ! ( columns, vec![ ] ) ;
3519
+ assert_eq ! ( source, None ) ;
3520
+ assert_eq ! ( table_name, ObjectName ( vec![ "users" . into( ) ] ) ) ;
3521
+ }
3522
+ _ => unreachable ! ( ) ,
3523
+ }
3524
+ }
You can’t perform that action at this time.
0 commit comments