Skip to content

Commit e9c4bbb

Browse files
authored
feat: Support for ORDER BY ALL (#3756)
1 parent 0363fef commit e9c4bbb

File tree

5 files changed

+18
-0
lines changed

5 files changed

+18
-0
lines changed

sqlglot/dialects/dialect.py

+5
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ class Dialect(metaclass=_Dialect):
356356
EXPAND_ALIAS_REFS_EARLY_ONLY_IN_GROUP_BY = False
357357
"""Whether alias reference expansion before qualification should only happen for the GROUP BY clause."""
358358

359+
SUPPORTS_ORDER_BY_ALL = False
360+
"""
361+
Whether ORDER BY ALL is supported (expands to all the selected columns) as in DuckDB, Spark3/Databricks
362+
"""
363+
359364
# --- Autofilled ---
360365

361366
tokenizer_class = Tokenizer

sqlglot/dialects/duckdb.py

+1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ class DuckDB(Dialect):
172172
SAFE_DIVISION = True
173173
INDEX_OFFSET = 1
174174
CONCAT_COALESCE = True
175+
SUPPORTS_ORDER_BY_ALL = True
175176

176177
# https://duckdb.org/docs/sql/introduction.html#creating-a-new-table
177178
NORMALIZATION_STRATEGY = NormalizationStrategy.CASE_INSENSITIVE

sqlglot/dialects/spark.py

+2
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def _dateadd_sql(self: Spark.Generator, expression: exp.TsOrDsAdd | exp.Timestam
9090

9191

9292
class Spark(Spark2):
93+
SUPPORTS_ORDER_BY_ALL = True
94+
9395
class Tokenizer(Spark2.Tokenizer):
9496
STRING_ESCAPES_ALLOWED_IN_RAW_STRINGS = False
9597

sqlglot/parser.py

+3
Original file line numberDiff line numberDiff line change
@@ -3845,6 +3845,9 @@ def _parse_ordered(
38453845
if not this:
38463846
return None
38473847

3848+
if this.name.upper() == "ALL" and self.dialect.SUPPORTS_ORDER_BY_ALL:
3849+
this = exp.var("ALL")
3850+
38483851
asc = self._match(TokenType.ASC)
38493852
desc = self._match(TokenType.DESC) or (asc and False)
38503853

tests/dialects/test_dialect.py

+7
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,13 @@ def test_order_by(self):
11641164
},
11651165
)
11661166

1167+
order_by_all_sql = "SELECT * FROM t ORDER BY ALL"
1168+
self.validate_identity(order_by_all_sql).find(exp.Ordered).this.assert_is(exp.Column)
1169+
1170+
for dialect in ("duckdb", "spark", "databricks"):
1171+
with self.subTest(f"Testing ORDER BY ALL in {dialect}"):
1172+
parse_one(order_by_all_sql, read=dialect).find(exp.Ordered).this.assert_is(exp.Var)
1173+
11671174
def test_json(self):
11681175
self.validate_all(
11691176
"""JSON_EXTRACT(x, '$["a b"]')""",

0 commit comments

Comments
 (0)