Skip to content

Commit 7fce610

Browse files
fix: support SHOW (ALL) TABLES for duckdb (#4961)
* fix: support SHOW (ALL) TABLES for duckdb * Fix styling * Fix set difference * Fix styling * Preserve ALL prefix * Fix test * Fix formatting --------- Co-authored-by: Jo <46752250+georgesittas@users.noreply.github.com>
1 parent b011ee2 commit 7fce610

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

sqlglot/dialects/duckdb.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ def _build_make_timestamp(args: t.List) -> exp.Expression:
156156
)
157157

158158

159+
def _show_parser(*args: t.Any, **kwargs: t.Any) -> t.Callable[[DuckDB.Parser], exp.Show]:
160+
def _parse(self: DuckDB.Parser) -> exp.Show:
161+
return self._parse_show_duckdb(*args, **kwargs)
162+
163+
return _parse
164+
165+
159166
def _struct_sql(self: DuckDB.Generator, expression: exp.Struct) -> str:
160167
args: t.List[str] = []
161168

@@ -348,6 +355,8 @@ class Tokenizer(tokens.Tokenizer):
348355
"$": TokenType.PARAMETER,
349356
}
350357

358+
COMMANDS = tokens.Tokenizer.COMMANDS - {TokenType.SHOW}
359+
351360
class Parser(parser.Parser):
352361
BITWISE = {
353362
**parser.Parser.BITWISE,
@@ -369,6 +378,11 @@ class Parser(parser.Parser):
369378

370379
FUNCTIONS_WITH_ALIASED_ARGS = {*parser.Parser.FUNCTIONS_WITH_ALIASED_ARGS, "STRUCT_PACK"}
371380

381+
SHOW_PARSERS = {
382+
"TABLES": _show_parser("TABLES"),
383+
"ALL TABLES": _show_parser("ALL TABLES"),
384+
}
385+
372386
FUNCTIONS = {
373387
**parser.Parser.FUNCTIONS,
374388
"ARRAY_REVERSE_SORT": _build_sort_array_desc,
@@ -467,6 +481,7 @@ class Parser(parser.Parser):
467481
**parser.Parser.STATEMENT_PARSERS,
468482
TokenType.ATTACH: lambda self: self._parse_attach_detach(),
469483
TokenType.DETACH: lambda self: self._parse_attach_detach(is_attach=False),
484+
TokenType.SHOW: lambda self: self._parse_show(),
470485
}
471486

472487
def _parse_expression(self) -> t.Optional[exp.Expression]:
@@ -581,6 +596,9 @@ def _parse_attach_option() -> exp.AttachOption:
581596
else self.expression(exp.Detach, this=this, exists=exists)
582597
)
583598

599+
def _parse_show_duckdb(self, this: str) -> exp.Show:
600+
return self.expression(exp.Show, this=this)
601+
584602
class Generator(generator.Generator):
585603
PARAMETER_TOKEN = "$"
586604
NAMED_PLACEHOLDER_TOKEN = "$"

tests/dialects/test_duckdb.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,3 +1598,7 @@ def test_at_sign_to_abs(self):
15981598
"SELECT (@-1) + 1",
15991599
"SELECT (ABS(-1)) + 1",
16001600
)
1601+
1602+
def test_show_tables(self):
1603+
self.validate_identity("SHOW TABLES").assert_is(exp.Show)
1604+
self.validate_identity("SHOW ALL TABLES").assert_is(exp.Show)

0 commit comments

Comments
 (0)