Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
11 changes: 11 additions & 0 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,17 @@ 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 not in (
"",
"postgres",
"duckdb",
"presto",
"trino",
):
no_paren_functions = klass.parser_class.NO_PAREN_FUNCTIONS.copy()
no_paren_functions.pop(TokenType.CURRENT_CATALOG, None)
klass.parser_class.NO_PAREN_FUNCTIONS = no_paren_functions

klass.VALID_INTERVAL_UNITS = {
*klass.VALID_INTERVAL_UNITS,
*klass.DATE_PART_MAPPING.keys(),
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@ class Generator(generator.Generator):
exp.JSONObjectAgg: rename_func("JSON_GROUP_OBJECT"),
exp.JSONBObjectAgg: rename_func("JSON_GROUP_OBJECT"),
exp.DateBin: rename_func("TIME_BUCKET"),
exp.CurrentCatalog: lambda *_: "CURRENT_CATALOG",
}

SUPPORTED_JSON_PATH_PARTS = {
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ class Generator(generator.Generator):
exp.JSONObjectAgg: rename_func("JSON_OBJECT_AGG"),
exp.JSONBObjectAgg: rename_func("JSONB_OBJECT_AGG"),
exp.CountIf: count_if_to_sum,
exp.CurrentCatalog: lambda *_: "CURRENT_CATALOG",
}

TRANSFORMS.pop(exp.CommentColumnConstraint)
Expand Down
1 change: 1 addition & 0 deletions sqlglot/dialects/trino.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Generator(Presto.Generator):
),
exp.TimeStrToTime: lambda self, e: timestrtotime_sql(self, e, include_precision=True),
exp.Trim: trim_sql,
exp.CurrentCatalog: lambda *_: "CURRENT_CATALOG",
}

SUPPORTED_JSON_PATH_PARTS = {
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
2 changes: 2 additions & 0 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ class Parser(metaclass=_Parser):
TokenType.CURRENT_TIME: exp.CurrentTime,
TokenType.CURRENT_TIMESTAMP: exp.CurrentTimestamp,
TokenType.CURRENT_USER: exp.CurrentUser,
TokenType.CURRENT_CATALOG: exp.CurrentCatalog,
TokenType.LOCALTIME: exp.Localtime,
TokenType.LOCALTIMESTAMP: exp.Localtimestamp,
TokenType.CURRENT_ROLE: exp.CurrentRole,
Expand Down Expand Up @@ -638,6 +639,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
35 changes: 35 additions & 0 deletions tests/dialects/test_dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4765,3 +4765,38 @@ 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",
]

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)