Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/ast/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ pub struct Insert {
pub table_alias: Option<TableAliasWithoutColumns>,
/// COLUMNS
pub columns: Vec<ObjectName>,
/// BY NAME
pub by_name: bool,
/// Overwrite (Hive)
pub overwrite: bool,
/// A SQL query that specifies what to insert
Expand Down Expand Up @@ -201,6 +203,11 @@ impl Display for Insert {
}
}

if self.by_name {
write!(f, "BY NAME")?;
SpaceOrNewline.fmt(f)?;
}

if !self.after_columns.is_empty() {
write!(f, "({})", display_comma_separated(&self.after_columns))?;
SpaceOrNewline.fmt(f)?;
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,7 @@ impl Spanned for Insert {
table,
table_alias,
columns,
by_name: _, // bool
overwrite: _, // bool
source,
partitioned,
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ impl Dialect for DatabricksDialect {
true
}

fn supports_insert_by_name(&self) -> bool {
true
}

/// See <https://docs.databricks.com/aws/en/sql/language-manual/functions/bangsign>
fn supports_bang_not_operator(&self) -> bool {
true
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,8 @@ impl Dialect for GenericDialect {
fn supports_aliased_function_args(&self) -> bool {
true
}

fn supports_insert_by_name(&self) -> bool {
true
}
}
7 changes: 7 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,13 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if this dialect supports `INSERT INTO ... BY NAME ...`.
///
/// Databricks: <https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-dml-insert-into>
fn supports_insert_by_name(&self) -> bool {
false
}

/// Returns true if this dialect supports `SET` statements without an explicit
/// assignment operator such as `=`. For example: `SET SHOWPLAN_XML ON`.
fn supports_set_stmt_without_operator(&self) -> bool {
Expand Down
1 change: 1 addition & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1866,6 +1866,7 @@ fn parse_multi_table_insert(
table: TableObject::TableName(ObjectName(vec![])), // Not used for multi-table insert
table_alias: None,
columns: vec![],
by_name: false,
overwrite,
source: Some(source),
assignments: vec![],
Expand Down
5 changes: 5 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18338,6 +18338,7 @@ impl<'a> Parser<'a> {

let is_mysql = dialect_of!(self is MySqlDialect);

let mut by_name = false;
let (columns, partitioned, after_columns, output, source, assignments) = if self
.parse_keywords(&[Keyword::DEFAULT, Keyword::VALUES])
{
Expand All @@ -18348,6 +18349,9 @@ impl<'a> Parser<'a> {
self.parse_parenthesized_qualified_column_list(Optional, is_mysql)?;

let partitioned = self.parse_insert_partition()?;
by_name = columns.is_empty()
&& self.dialect.supports_insert_by_name()
&& self.parse_keywords(&[Keyword::BY, Keyword::NAME]);
// Hive allows you to specify columns after partitions as well if you want.
let after_columns = if dialect_of!(self is HiveDialect) {
self.parse_parenthesized_column_list(Optional, false)?
Expand Down Expand Up @@ -18472,6 +18476,7 @@ impl<'a> Parser<'a> {
ignore,
into,
overwrite,
by_name,
partitioned,
columns,
after_columns,
Expand Down
31 changes: 31 additions & 0 deletions tests/sqlparser_databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,3 +738,34 @@ fn parse_cte_without_as() {
.parse_sql_statements("WITH cte (SELECT 1) SELECT * FROM cte")
.is_err());
}

#[test]
fn test_databricks_insert_by_name() {
match databricks_and_generic().verified_stmt("INSERT INTO target BY NAME SELECT 1 AS a") {
Statement::Insert(Insert {
by_name,
columns,
has_table_keyword,
..
}) => {
assert!(by_name);
assert!(columns.is_empty());
assert!(!has_table_keyword);
}
_ => unreachable!(),
}

match databricks_and_generic().verified_stmt(
"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",
) {
Statement::Insert(Insert {
by_name,
has_table_keyword,
..
}) => {
assert!(by_name);
assert!(has_table_keyword);
}
_ => unreachable!(),
}
}
3 changes: 3 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6027,6 +6027,7 @@ fn test_simple_postgres_insert_with_alias() {
span: Span::empty(),
})
],
by_name: false,
overwrite: false,
source: Some(Box::new(Query {
with: None,
Expand Down Expand Up @@ -6107,6 +6108,7 @@ fn test_simple_postgres_insert_with_alias() {
span: Span::empty(),
})
],
by_name: false,
overwrite: false,
source: Some(Box::new(Query {
with: None,
Expand Down Expand Up @@ -6189,6 +6191,7 @@ fn test_simple_insert_with_quoted_alias() {
span: Span::empty(),
})
],
by_name: false,
overwrite: false,
source: Some(Box::new(Query {
with: None,
Expand Down
Loading