Skip to content
Merged
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
1 change: 1 addition & 0 deletions sqlglot/dialects/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class Generator(Spark.Generator):
if e.args.get("is_numeric")
else self.function_fallback_sql(e)
),
exp.CurrentCatalog: lambda *_: "CURRENT_CATALOG()",
}

TRANSFORMS.pop(exp.RegexpLike)
Expand Down
16 changes: 16 additions & 0 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,22 @@ def get_start_end(token_type: TokenType) -> t.Tuple[t.Optional[str], t.Optional[
no_paren_functions.pop(TokenType.LOCALTIMESTAMP, None)
klass.parser_class.NO_PAREN_FUNCTIONS = no_paren_functions

if enum in (
"",
"postgres",
"duckdb",
"trino",
):
no_paren_functions = klass.parser_class.NO_PAREN_FUNCTIONS.copy()
no_paren_functions[TokenType.CURRENT_CATALOG] = exp.CurrentCatalog
klass.parser_class.NO_PAREN_FUNCTIONS = no_paren_functions
else:
# For dialects that don't support this keyword, treat it as a regular identifier
# This fixes the "Unexpected token" error in BQ, Spark, etc.
klass.parser_class.ID_VAR_TOKENS = klass.parser_class.ID_VAR_TOKENS | {
TokenType.CURRENT_CATALOG,
}

klass.VALID_INTERVAL_UNITS = {
*klass.VALID_INTERVAL_UNITS,
*klass.DATE_PART_MAPPING.keys(),
Expand Down
4 changes: 4 additions & 0 deletions sqlglot/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6356,6 +6356,10 @@ class CurrentUser(Func):
arg_types = {"this": False}


class CurrentCatalog(Func):
arg_types = {}


class CurrentRegion(Func):
arg_types = {}

Expand Down
1 change: 1 addition & 0 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Generator(metaclass=_Generator):
exp.CopyGrantsProperty: lambda *_: "COPY GRANTS",
exp.CredentialsProperty: lambda self,
e: f"CREDENTIALS=({self.expressions(e, 'expressions', sep=' ')})",
exp.CurrentCatalog: lambda *_: "CURRENT_CATALOG",
exp.DateFormatColumnConstraint: lambda self, e: f"FORMAT {self.sql(e, 'this')}",
exp.DefaultColumnConstraint: lambda self, e: f"DEFAULT {self.sql(e, 'this')}",
exp.DynamicProperty: lambda *_: "DYNAMIC",
Expand Down
1 change: 1 addition & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ class Parser(metaclass=_Parser):
TokenType.CURRENT_TIMESTAMP,
TokenType.CURRENT_TIME,
TokenType.CURRENT_USER,
TokenType.CURRENT_CATALOG,
TokenType.FILTER,
TokenType.FIRST,
TokenType.FORMAT,
Expand Down
2 changes: 2 additions & 0 deletions sqlglot/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ class TokenType(AutoName):
CURRENT_TIMESTAMP = auto()
CURRENT_USER = auto()
CURRENT_ROLE = auto()
CURRENT_CATALOG = auto()
DECLARE = auto()
DEFAULT = auto()
DELETE = auto()
Expand Down Expand Up @@ -758,6 +759,7 @@ class Tokenizer(metaclass=_Tokenizer):
"CURRENT_TIME": TokenType.CURRENT_TIME,
"CURRENT_TIMESTAMP": TokenType.CURRENT_TIMESTAMP,
"CURRENT_USER": TokenType.CURRENT_USER,
"CURRENT_CATALOG": TokenType.CURRENT_CATALOG,
"DATABASE": TokenType.DATABASE,
"DEFAULT": TokenType.DEFAULT,
"DELETE": TokenType.DELETE,
Expand Down
36 changes: 36 additions & 0 deletions tests/dialects/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4765,3 +4765,39 @@ def test_localtime_and_localtimestamp(self):
select = parse_one(sql, dialect=dialect)
select.selects[0].assert_is(exp.Column)
self.assertEqual(select.sql(dialect), sql)

def test_current_catalog(self):
sql = "SELECT CURRENT_CATALOG"

unsupported_dialects = [
"bigquery",
"mysql",
"oracle",
"clickhouse",
"snowflake",
"spark",
"databricks",
"presto",
]

for dialect in unsupported_dialects:
with self.subTest(f"Testing CURRENT_CATALOG as Column in {dialect}"):
select = parse_one(sql, dialect=dialect)
select.selects[0].assert_is(exp.Column)
self.assertEqual(select.sql(dialect), sql)

supported_dialects = [
"postgres",
"duckdb",
"trino",
"databricks",
]

for dialect in supported_dialects:
with self.subTest(f"Testing CURRENT_CATALOG expression in {dialect}"):
if dialect == "databricks":
sql = "SELECT CURRENT_CATALOG()"
select = parse_one(sql, dialect=dialect)
select.selects[0].assert_is(exp.CurrentCatalog)

self.assertEqual(select.sql(dialect), sql)