Skip to content

Commit 1ea1063

Browse files
first version of IDENTIFIER
1 parent dbc83df commit 1ea1063

5 files changed

Lines changed: 42 additions & 3 deletions

File tree

sqlglot-integration-tests

sqlglot/expressions/core.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1789,7 +1789,13 @@ class JoinHint(Expression):
17891789

17901790

17911791
class Identifier(Expression):
1792-
arg_types = {"this": True, "quoted": False, "global_": False, "temporary": False}
1792+
arg_types = {
1793+
"this": True,
1794+
"quoted": False,
1795+
"global_": False,
1796+
"temporary": False,
1797+
"identifier_func": False,
1798+
}
17931799
is_primitive = True
17941800
_hash_raw_args = True
17951801

sqlglot/generators/duckdb.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,3 +4599,13 @@ def uuid_sql(self, expression: exp.Uuid) -> str:
45994599
return self.sql(result)
46004600

46014601
return super().uuid_sql(expression)
4602+
4603+
def identifier_sql(self, expression: exp.Identifier) -> str:
4604+
if expression.args.get("identifier_func"):
4605+
parts = expression.name.split(".")
4606+
if len(parts) > 1:
4607+
result: exp.Expr = exp.to_identifier(parts[0])
4608+
for part in parts[1:]:
4609+
result = exp.Dot(this=result, expression=exp.to_identifier(part))
4610+
return self.sql(result)
4611+
return super().identifier_sql(expression)

sqlglot/generators/snowflake.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,12 @@ class SnowflakeGenerator(generator.Generator):
616616
),
617617
}
618618

619+
def identifier_sql(self, expression: exp.Identifier) -> str:
620+
if expression.args.get("identifier_func"):
621+
name = f'"{expression.name}"' if expression.quoted else expression.name
622+
return self.func("IDENTIFIER", exp.Literal.string(name))
623+
return super().identifier_sql(expression)
624+
619625
def sortarray_sql(self, expression: exp.SortArray) -> str:
620626
asc = expression.args.get("asc")
621627
nulls_first = expression.args.get("nulls_first")

sqlglot/parsers/snowflake.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ class SnowflakeParser(parser.Parser):
733733
**parser.Parser.FUNCTION_PARSERS,
734734
"DATE_PART": lambda self: self._parse_date_part(),
735735
"DIRECTORY": lambda self: self._parse_directory(),
736+
"IDENTIFIER": lambda self: self._parse_identifier_function(),
736737
"OBJECT_CONSTRUCT_KEEP_NULL": lambda self: self._parse_json_object(),
737738
"LISTAGG": lambda self: self._parse_string_agg(),
738739
"SEMANTIC_VIEW": lambda self: self._parse_semantic_view(),
@@ -1142,6 +1143,22 @@ def _parse_table(
11421143

11431144
return table
11441145

1146+
def _fold_identifier_literal(self, arg: exp.Expr | None) -> exp.Expr:
1147+
if arg and arg.is_string:
1148+
inner = arg.to_py()
1149+
if len(inner) >= 2 and inner.startswith('"') and inner.endswith('"'):
1150+
return exp.Identifier(this=inner[1:-1], quoted=True, identifier_func=True)
1151+
return exp.Identifier(this=inner, identifier_func=True)
1152+
return self.expression(exp.Anonymous(this="IDENTIFIER", expressions=[arg]))
1153+
1154+
def _parse_identifier_function(self) -> exp.Expr:
1155+
arg = self._parse_string()
1156+
if not arg and (self._advance_any() or self._match_set(self.ID_VAR_TOKENS)):
1157+
quoted = self._prev.token_type == TokenType.STRING
1158+
arg = self._identifier_expression(quoted=quoted)
1159+
self._match_r_paren()
1160+
return self._fold_identifier_literal(arg)
1161+
11451162
def _parse_id_var(
11461163
self,
11471164
any_token: bool = True,
@@ -1152,7 +1169,7 @@ def _parse_id_var(
11521169
super()._parse_id_var(any_token=any_token, tokens=tokens) or self._parse_string()
11531170
)
11541171
self._match_r_paren()
1155-
return self.expression(exp.Anonymous(this="IDENTIFIER", expressions=[identifier]))
1172+
return self._fold_identifier_literal(identifier)
11561173

11571174
return super()._parse_id_var(any_token=any_token, tokens=tokens)
11581175

0 commit comments

Comments
 (0)