Skip to content

Commit af50c1c

Browse files
kyle-cheungKyle Cheung
andauthored
fix(duckdb): Fix NOT precedence for JSON extractions (#6670)
* fix(duckdb): Fix NOT precedence for JSON extractions by wrapping in parantheses * added snowflake to duckdb tests * updated Snowflake to DuckDB to use scalar extract, tests will fail until ->> PR merges * formatting * added more tests for json extraction precedence * --amend * removed unequivalent tests --------- Co-authored-by: Kyle Cheung <kylecheung@Kyles-MacBook-Pro.local>
1 parent 26c16f2 commit af50c1c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

sqlglot/dialects/duckdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def _unix_to_time_sql(self: DuckDB.Generator, expression: exp.UnixToTime) -> str
423423
return self.sql(to_timestamp)
424424

425425

426-
WRAPPED_JSON_EXTRACT_EXPRESSIONS = (exp.Binary, exp.Bracket, exp.In)
426+
WRAPPED_JSON_EXTRACT_EXPRESSIONS = (exp.Binary, exp.Bracket, exp.In, exp.Not)
427427

428428

429429
def _arrow_json_extract_sql(self: DuckDB.Generator, expression: JSON_EXTRACT_TYPE) -> str:

tests/dialects/test_duckdb.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,24 @@ def test_duckdb(self):
548548
"JSON_EXTRACT_PATH_TEXT(x, '$.family')",
549549
"x ->> '$.family'",
550550
)
551+
self.validate_all(
552+
"SELECT NOT (data -> '$.value')",
553+
read={
554+
"snowflake": "SELECT NOT data:value",
555+
},
556+
)
557+
self.validate_all(
558+
"SELECT NOT (data -> '$.value.nested')",
559+
read={
560+
"snowflake": "SELECT NOT data:value:nested",
561+
},
562+
)
563+
self.validate_all(
564+
"SELECT (data -> '$.value') = 1",
565+
read={
566+
"snowflake": "SELECT data:value = 1",
567+
},
568+
)
551569
self.validate_identity(
552570
"SELECT {'yes': 'duck', 'maybe': 'goose', 'huh': NULL, 'no': 'heron'}"
553571
)

0 commit comments

Comments
 (0)