Skip to content
Open
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
2 changes: 1 addition & 1 deletion sqlglot-integration-tests
8 changes: 4 additions & 4 deletions sqlglot/dialects/dialect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2285,15 +2285,15 @@ def _json_extract_segments(self: Generator, expression: JSON_EXTRACT_TYPE) -> st

segments = []
for segment in path.expressions:
escape = segment.args.get("quoted")
path = self.sql(segment)
if path:
if isinstance(segment, exp.JSONPathPart) and (
quoted_index or not isinstance(segment, exp.JSONPathSubscript)
):
if escape:
path = self.escape_str(path)

# Always escape path segments when wrapping them as SQL string literals,
# regardless of whether the key was quoted in the JSON path syntax.
# This ensures characters like single quotes are properly escaped for SQL.
path = self.escape_str(path)
path = f"{self.dialect.QUOTE_START}{path}{self.dialect.QUOTE_END}"

segments.append(path)
Expand Down
9 changes: 4 additions & 5 deletions sqlglot/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3840,7 +3840,7 @@ def json_path_part(self, expression: int | str | exp.JSONPathPart) -> str:

if self._quote_json_path_key_using_brackets and self.JSON_PATH_SINGLE_QUOTE_ESCAPE:
escaped = expression.replace("'", "\\'")
escaped = f"\\'{expression}\\'"
escaped = f"\\'{escaped}\\'"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be escaped = f"'{escaped}'", right?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove the backslashes there, we stop escaping the inner single quotes at the jsonpath layer. That changes the generated sql text.
Eg:
$[\'it\'s\'] will be changed to $['it\'s']

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing this entirely would cause other dialects like Postgres, SQLite, MySQL, Presto, Trino, Snowflake, DuckDB, ClickHouse, and Redshift to break. As this is literally the fix provided for the issue reported in #8251

else:
escaped = expression.replace('"', '\\"')
escaped = f'"{escaped}"'
Expand Down Expand Up @@ -5401,10 +5401,9 @@ def _jsonpathkey_sql(self, expression: exp.JSONPathKey) -> str:

this = self.json_path_part(this)

if quoted and self.QUOTE_JSON_PATH:
# The whole path is rendered as a single quoted string literal, so the bracketed key
# (which may itself contain backslash-escaped quotes, e.g. ["x \"y\"z"]) must be
# escaped again for the outer string literal (-> ["x \\"y\\"z"]).
if self.QUOTE_JSON_PATH and not (
self._quote_json_path_key_using_brackets and self.JSON_PATH_SINGLE_QUOTE_ESCAPE
):
this = self.escape_str(this)

return (
Expand Down
15 changes: 11 additions & 4 deletions sqlglot/generators/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,13 @@ def _levenshtein_sql(self: BigQueryGenerator, expression: exp.Levenshtein) -> st
def _json_extract_sql(self: BigQueryGenerator, expression: JSON_EXTRACT_TYPE) -> str:
name = expression.meta_get("name") or expression.sql_name()
upper = name.upper()

dquote_escaping = upper in DQUOTES_ESCAPING_JSON_FUNCTIONS

if dquote_escaping:
self._quote_json_path_key_using_brackets = False

sql = rename_func(upper)(self, expression)

if dquote_escaping:
self._quote_json_path_key_using_brackets = True

return sql


Expand Down Expand Up @@ -299,6 +295,17 @@ class BigQueryGenerator(generator.Generator):
exp.TsOrDsToDate,
)

def json_path_part(self, expression: int | str | exp.JSONPathPart) -> str:
if (
isinstance(expression, str)
and self._quote_json_path_key_using_brackets
and self.JSON_PATH_SINGLE_QUOTE_ESCAPE
):
escaped = expression.replace("'", "\\'")
return self.escape_str(f"'{escaped}'")

return super().json_path_part(expression)

TRANSFORMS = {
**generator.Generator.TRANSFORMS,
exp.AIEmbed: rename_func("EMBED"),
Expand Down
2 changes: 1 addition & 1 deletion sqlglot/generators/databricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def timeserieskey_sql(self, expression: exp.TimeseriesKey) -> str:
return f"{self.sql(expression, 'this')} TIMESERIES"

def jsonpath_sql(self, expression: exp.JSONPath) -> str:
expression.set("escape", None)
path = super().jsonpath_sql(expression)

if isinstance(expression.parent, exp.JSONExtractScalar):
path = self.escape_str(path)
return f"{self.dialect.QUOTE_START}{path}{self.dialect.QUOTE_END}"
Comment thread
geooo109 marked this conversation as resolved.

return path
Expand Down
11 changes: 10 additions & 1 deletion tests/test_jsonpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ def test_identity(self):
with self.subTest(f"{selector} -> {expected}"):
self.assertEqual(parse(selector).sql(), f"'{expected}'")

def test_apostrophe_escaping_is_scoped_to_keys(self):
for selector, expected in (
("$['it\\'s']", "$[\"it''s\"]"),
("$[?@.a=='b']", "$[?@.a=='b']"),
("$[?!(@.a=='b')]", "$[?!(@.a=='b')]"),
Comment thread
geooo109 marked this conversation as resolved.
):
with self.subTest(selector):
self.assertEqual(parse(selector).sql(), f"'{expected}'")

def test_union_preserves_falsey_members(self):
for selector, expected in (
("$[1,0]", exp.JSONPathUnion(expressions=[1, 0])),
Expand Down Expand Up @@ -74,7 +83,7 @@ def test_cts_file(self):
"""$['a']""": """$.a""",
"""$['c']""": """$.c""",
"""$[' ']""": """$[" "]""",
"""$['\\'']""": """$["\'"]""",
"""$['\\'']""": """$["''"]""",
"""$['\\\\']""": """$["\\\\"]""",
"""$['\\/']""": """$["\\/"]""",
"""$['\\b']""": """$["\\b"]""",
Expand Down
Loading