Skip to content

Commit

Permalink
Feat(duckdb): by_name modifiers #2118
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Aug 28, 2023
1 parent c066df2 commit 5b14f52
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,7 @@ class Insert(DDL):
"alternative": False,
"where": False,
"ignore": False,
"by_name": False,
}

def with_(
Expand Down Expand Up @@ -2460,6 +2461,7 @@ class Union(Subqueryable):
"this": True,
"expression": True,
"distinct": False,
"by_name": False,
**QUERY_MODIFIERS,
}

Expand Down
6 changes: 4 additions & 2 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1193,14 +1193,15 @@ def insert_sql(self, expression: exp.Insert) -> str:
where = f"{self.sep()}REPLACE WHERE {where}" if where else ""
expression_sql = f"{self.sep()}{self.sql(expression, 'expression')}"
conflict = self.sql(expression, "conflict")
by_name = " BY NAME" if expression.args.get("by_name") else ""
returning = self.sql(expression, "returning")

if self.RETURNING_END:
expression_sql = f"{expression_sql}{conflict}{returning}"
else:
expression_sql = f"{returning}{expression_sql}{conflict}"

sql = f"INSERT{alternative}{ignore}{this}{exists}{partition_sql}{where}{expression_sql}"
sql = f"INSERT{alternative}{ignore}{this}{by_name}{exists}{partition_sql}{where}{expression_sql}"
return self.prepend_ctes(expression, sql)

def intersect_sql(self, expression: exp.Intersect) -> str:
Expand Down Expand Up @@ -1834,7 +1835,8 @@ def union_sql(self, expression: exp.Union) -> str:
def union_op(self, expression: exp.Union) -> str:
kind = " DISTINCT" if self.EXPLICIT_UNION else ""
kind = kind if expression.args.get("distinct") else " ALL"
return f"UNION{kind}"
by_name = " BY NAME" if expression.args.get("by_name") else ""
return f"UNION{kind}{by_name}"

def unnest_sql(self, expression: exp.Unnest) -> str:
args = self.expressions(expression, flat=True)
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,7 @@ def _parse_insert(self) -> exp.Insert:
exp.Insert,
comments=comments,
this=this,
by_name=self._match_text_seq("BY", "NAME"),
exists=self._parse_exists(),
partition=self._parse_partition(),
where=self._match_pair(TokenType.REPLACE, TokenType.WHERE)
Expand Down Expand Up @@ -2980,6 +2981,7 @@ def _parse_set_operations(self, this: t.Optional[exp.Expression]) -> t.Optional[
expression,
this=this,
distinct=self._match(TokenType.DISTINCT) or not self._match(TokenType.ALL),
by_name=self._match_text_seq("BY", "NAME"),
expression=self._parse_set_operations(self._parse_select(nested=True)),
)

Expand Down
2 changes: 2 additions & 0 deletions tests/dialects/test_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class TestDuckDB(Validator):
dialect = "duckdb"

def test_duckdb(self):
self.validate_identity("INSERT INTO x BY NAME SELECT 1 AS y")
self.validate_identity("SELECT 1 AS x UNION ALL BY NAME SELECT 2 AS x")
self.validate_identity("SELECT SUM(x) FILTER (x = 1)", "SELECT SUM(x) FILTER(WHERE x = 1)")

# https://github.com/duckdb/duckdb/releases/tag/v0.8.0
Expand Down

0 comments on commit 5b14f52

Please sign in to comment.