Skip to content

Commit 70c6f3e

Browse files
first version of IDENTIFIER
1 parent a288150 commit 70c6f3e

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
@@ -4600,3 +4600,13 @@ def uuid_sql(self, expression: exp.Uuid) -> str:
46004600
return self.sql(result)
46014601

46024602
return super().uuid_sql(expression)
4603+
4604+
def identifier_sql(self, expression: exp.Identifier) -> str:
4605+
if expression.args.get("identifier_func"):
4606+
parts = expression.name.split(".")
4607+
if len(parts) > 1:
4608+
result: exp.Expr = exp.to_identifier(parts[0])
4609+
for part in parts[1:]:
4610+
result = exp.Dot(this=result, expression=exp.to_identifier(part))
4611+
return self.sql(result)
4612+
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(),
@@ -1121,6 +1122,22 @@ def _parse_table(
11211122

11221123
return table
11231124

1125+
def _fold_identifier_literal(self, arg: exp.Expr | None) -> exp.Expr:
1126+
if arg and arg.is_string:
1127+
inner = arg.to_py()
1128+
if len(inner) >= 2 and inner.startswith('"') and inner.endswith('"'):
1129+
return exp.Identifier(this=inner[1:-1], quoted=True, identifier_func=True)
1130+
return exp.Identifier(this=inner, identifier_func=True)
1131+
return self.expression(exp.Anonymous(this="IDENTIFIER", expressions=[arg]))
1132+
1133+
def _parse_identifier_function(self) -> exp.Expr:
1134+
arg = self._parse_string()
1135+
if not arg and (self._advance_any() or self._match_set(self.ID_VAR_TOKENS)):
1136+
quoted = self._prev.token_type == TokenType.STRING
1137+
arg = self._identifier_expression(quoted=quoted)
1138+
self._match_r_paren()
1139+
return self._fold_identifier_literal(arg)
1140+
11241141
def _parse_id_var(
11251142
self,
11261143
any_token: bool = True,
@@ -1131,7 +1148,7 @@ def _parse_id_var(
11311148
super()._parse_id_var(any_token=any_token, tokens=tokens) or self._parse_string()
11321149
)
11331150
self._match_r_paren()
1134-
return self.expression(exp.Anonymous(this="IDENTIFIER", expressions=[identifier]))
1151+
return self._fold_identifier_literal(identifier)
11351152

11361153
return super()._parse_id_var(any_token=any_token, tokens=tokens)
11371154

0 commit comments

Comments
 (0)