Skip to content

Commit 3aa09e6

Browse files
committed
fix: gate INSERT BY NAME by dialect capability
1 parent 68ff95d commit 3aa09e6

5 files changed

Lines changed: 47 additions & 32 deletions

File tree

src/dialect/databricks.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ impl Dialect for DatabricksDialect {
9595
true
9696
}
9797

98+
fn supports_insert_by_name(&self) -> bool {
99+
true
100+
}
101+
98102
/// See <https://docs.databricks.com/aws/en/sql/language-manual/functions/bangsign>
99103
fn supports_bang_not_operator(&self) -> bool {
100104
true

src/dialect/generic.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,8 @@ impl Dialect for GenericDialect {
320320
fn supports_aliased_function_args(&self) -> bool {
321321
true
322322
}
323+
324+
fn supports_insert_by_name(&self) -> bool {
325+
true
326+
}
323327
}

src/dialect/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,13 @@ pub trait Dialect: Debug + Any {
13531353
false
13541354
}
13551355

1356+
/// Returns true if this dialect supports `INSERT INTO ... BY NAME ...`.
1357+
///
1358+
/// Databricks: <https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-dml-insert-into>
1359+
fn supports_insert_by_name(&self) -> bool {
1360+
false
1361+
}
1362+
13561363
/// Returns true if this dialect supports `SET` statements without an explicit
13571364
/// assignment operator such as `=`. For example: `SET SHOWPLAN_XML ON`.
13581365
fn supports_set_stmt_without_operator(&self) -> bool {

src/parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18350,7 +18350,7 @@ impl<'a> Parser<'a> {
1835018350

1835118351
let partitioned = self.parse_insert_partition()?;
1835218352
by_name = columns.is_empty()
18353-
&& dialect_of!(self is DatabricksDialect | GenericDialect)
18353+
&& self.dialect.supports_insert_by_name()
1835418354
&& self.parse_keywords(&[Keyword::BY, Keyword::NAME]);
1835518355
// Hive allows you to specify columns after partitions as well if you want.
1835618356
let after_columns = if dialect_of!(self is HiveDialect) {

tests/sqlparser_databricks.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -168,37 +168,6 @@ fn test_databricks_lambdas() {
168168
databricks().verified_expr("transform(array(1, 2, 3), x -> x + 1)");
169169
}
170170

171-
#[test]
172-
fn test_databricks_insert_by_name() {
173-
match databricks().verified_stmt("INSERT INTO target BY NAME SELECT 1 AS a") {
174-
Statement::Insert(Insert {
175-
by_name,
176-
columns,
177-
has_table_keyword,
178-
..
179-
}) => {
180-
assert!(by_name);
181-
assert!(columns.is_empty());
182-
assert!(!has_table_keyword);
183-
}
184-
_ => unreachable!(),
185-
}
186-
187-
match databricks().verified_stmt(
188-
"INSERT INTO TABLE lakehouse.dwd.dwd_event_quality_sla_metric_di BY NAME WITH day AS (SELECT 1 AS event_data_id) SELECT event_data_id FROM day",
189-
) {
190-
Statement::Insert(Insert {
191-
by_name,
192-
has_table_keyword,
193-
..
194-
}) => {
195-
assert!(by_name);
196-
assert!(has_table_keyword);
197-
}
198-
_ => unreachable!(),
199-
}
200-
}
201-
202171
#[test]
203172
fn test_values_clause() {
204173
let values = Values {
@@ -769,3 +738,34 @@ fn parse_cte_without_as() {
769738
.parse_sql_statements("WITH cte (SELECT 1) SELECT * FROM cte")
770739
.is_err());
771740
}
741+
742+
#[test]
743+
fn test_databricks_insert_by_name() {
744+
match databricks_and_generic().verified_stmt("INSERT INTO target BY NAME SELECT 1 AS a") {
745+
Statement::Insert(Insert {
746+
by_name,
747+
columns,
748+
has_table_keyword,
749+
..
750+
}) => {
751+
assert!(by_name);
752+
assert!(columns.is_empty());
753+
assert!(!has_table_keyword);
754+
}
755+
_ => unreachable!(),
756+
}
757+
758+
match databricks_and_generic().verified_stmt(
759+
"INSERT INTO TABLE lakehouse.dwd.dwd_event_quality_sla_metric_di BY NAME WITH day AS (SELECT 1 AS event_data_id) SELECT event_data_id FROM day",
760+
) {
761+
Statement::Insert(Insert {
762+
by_name,
763+
has_table_keyword,
764+
..
765+
}) => {
766+
assert!(by_name);
767+
assert!(has_table_keyword);
768+
}
769+
_ => unreachable!(),
770+
}
771+
}

0 commit comments

Comments
 (0)