diff --git a/sqlglot/dialects/e6.py b/sqlglot/dialects/e6.py index f3c420e8c9..5aaad64c1d 100644 --- a/sqlglot/dialects/e6.py +++ b/sqlglot/dialects/e6.py @@ -2785,7 +2785,11 @@ def json_extract_sql(self, e: exp.JSONExtract | exp.JSONExtractScalar): if not is_placeholder: if not path_sql.startswith("'$."): - path = add_single_quotes("$." + path_sql) + if path_sql.startswith("["): + escaped = ("$" + path_sql).replace("'", "\\'") + path = f"'{escaped}'" + else: + path = add_single_quotes("$." + path_sql) else: path = path_sql diff --git a/tests/dialects/test_e6.py b/tests/dialects/test_e6.py index 8eaa1ec093..4ac9aa245e 100644 --- a/tests/dialects/test_e6.py +++ b/tests/dialects/test_e6.py @@ -1301,6 +1301,30 @@ def test_json_extract(self): read={"databricks": "SELECT FROM_JSON(GET_JSON_OBJECT(ra1_0.attributesJSON, ?), ?)"}, ) + # Bracket notation: get_json_object with $['key'] path (map/array access) + self.validate_all( + r"""SELECT JSON_EXTRACT(ra1_0.attributesWithLinkTypeJSON, '$[\'DestinationAccessLinkType:assets:dataRetention\']')""", + read={ + "databricks": r"""SELECT GET_JSON_OBJECT(ra1_0.attributesWithLinkTypeJSON, '$[\'DestinationAccessLinkType:assets:dataRetention\']')""", + }, + ) + + # Dot notation: get_json_object with $.field path (struct access) + self.validate_all( + "SELECT JSON_EXTRACT(endpoints, '$.youtube')", + read={ + "databricks": "SELECT GET_JSON_OBJECT(endpoints, '$.youtube')", + }, + ) + + # Dot notation: nested path + self.validate_all( + "SELECT JSON_EXTRACT(col, '$.foo.bar')", + read={ + "databricks": "SELECT GET_JSON_OBJECT(col, '$.foo.bar')", + }, + ) + def test_array_slice(self): self.validate_all( "SELECT ARRAY_SLICE(A, B, C + B)",