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
19 changes: 17 additions & 2 deletions sqlglot/dialects/e6.py
Original file line number Diff line number Diff line change
Expand Up @@ -2139,9 +2139,24 @@ def bracket_sql(self, expression: exp.Bracket) -> str:
Convert bracket access to ELEMENT_AT function for E6 dialect.

In E6:
- Map access: map['key'] -> ELEMENT_AT(map, 'key')
- Array access: arr[0] -> ELEMENT_AT(arr, 1) (1-indexed in E6)
- col['key'] -> ELEMENT_AT(col, 'key')
- arr[0] -> ELEMENT_AT(arr, 1) (1-indexed in E6)
- Inside VALUES: map[...] preserved as-is (Databricks uses MAP() with
parens for constructors, so map[...] in INSERT VALUES is not ELEMENT_AT)
"""
# Inside a VALUES clause, map[...] bracket syntax should be preserved
# as-is. In Databricks MAP() with parens is the constructor; map[...]
# in VALUES is a literal value that must not be rewritten to ELEMENT_AT.
this = expression.this
col_name = this.name if isinstance(this, exp.Column) else ""
if col_name.upper() == "MAP" and len(expression.expressions) > 1:
parent = expression.parent
while parent:
if isinstance(parent, exp.Values):
expressions_sql = ", ".join(self.sql(e) for e in expression.expressions)
return f"{self.sql(expression, 'this')}[{expressions_sql}]"
parent = parent.parent

func_name = (
"TRY_ELEMENT_AT"
if expression._meta and expression._meta.get("name", "").upper() == "TRY_ELEMENT_AT"
Expand Down
25 changes: 25 additions & 0 deletions tests/dialects/test_e6.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,31 @@ def test_E6(self):
},
)

# map[...] bracket syntax inside VALUES should be preserved as-is.
# Databricks uses MAP() with parens for map construction, so map[...]
# in INSERT VALUES is not an ELEMENT_AT operation.
self.validate_all(
"INSERT INTO t1 (selection) VALUES (map['report_id', '221be455', 'name', 'ED SHEERAN'])",
read={
"databricks": "INSERT INTO t1 (selection) VALUES (map['report_id', '221be455', 'name', 'ED SHEERAN'])",
},
)

self.validate_all(
"INSERT INTO t1 (selection, report_id) VALUES (map['k1', 'v1', 'k2', 'v2'], '123')",
read={
"databricks": "INSERT INTO t1 (selection, report_id) VALUES (map['k1', 'v1', 'k2', 'v2'], '123')",
},
)

# map['key'] in SELECT (not inside VALUES) should still convert to ELEMENT_AT
self.validate_all(
"SELECT ELEMENT_AT(map, 'key') FROM t1",
read={
"databricks": "SELECT map['key'] FROM t1",
},
)

self.validate_all(
"SELECT CONVERT_TIMEZONE('Asia/Seoul', 'UTC', CAST('2016-08-31' AS TIMESTAMP))",
read={"databricks": "SELECT to_utc_timestamp('2016-08-31', 'Asia/Seoul')"},
Expand Down