1
0
Fork 0

Merging upstream version 21.0.1.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-13 21:20:36 +01:00
parent 3759c601a7
commit 96b10de29a
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
115 changed files with 66603 additions and 60920 deletions

View file

@ -2,7 +2,7 @@ import json
import os
import unittest
from sqlglot import jsonpath
from sqlglot import exp, jsonpath
from sqlglot.errors import ParseError, TokenError
from tests.helpers import FIXTURES_DIR
@ -11,24 +11,22 @@ class TestJsonpath(unittest.TestCase):
maxDiff = None
def test_jsonpath(self):
expected_expressions = [
exp.JSONPathRoot(),
exp.JSONPathKey(this=exp.JSONPathWildcard()),
exp.JSONPathKey(this="a"),
exp.JSONPathSubscript(this=0),
exp.JSONPathKey(this="x"),
exp.JSONPathUnion(expressions=[exp.JSONPathWildcard(), "y", 1]),
exp.JSONPathKey(this="z"),
exp.JSONPathSelector(this=exp.JSONPathFilter(this="(@.a == 'b'), 1:")),
exp.JSONPathSubscript(this=exp.JSONPathSlice(start=1, end=5, step=None)),
exp.JSONPathUnion(expressions=[1, exp.JSONPathFilter(this="@.a")]),
exp.JSONPathSelector(this=exp.JSONPathScript(this="@.x)")),
]
self.assertEqual(
jsonpath.parse("$.*.a[0]['x'][*, 'y', 1].z[?(@.a == 'b'), 1:][1:5][1,?@.a][(@.x)]"),
[
{"kind": "root"},
{"kind": "child", "value": "*"},
{"kind": "child", "value": "a"},
{"kind": "subscript", "value": 0},
{"kind": "key", "value": "x"},
{"kind": "union", "value": [{"kind": "wildcard"}, "y", 1]},
{"kind": "child", "value": "z"},
{"kind": "selector", "value": {"kind": "filter", "value": "(@.a == 'b'), 1:"}},
{
"kind": "subscript",
"value": {"end": 5, "kind": "slice", "start": 1, "step": None},
},
{"kind": "union", "value": [1, {"kind": "filter", "value": "@.a"}]},
{"kind": "selector", "value": {"kind": "script", "value": "@.x)"}},
],
exp.JSONPath(expressions=expected_expressions),
)
def test_identity(self):
@ -38,7 +36,7 @@ class TestJsonpath(unittest.TestCase):
("$[((@.length-1))]", "$[((@.length-1))]"),
):
with self.subTest(f"{selector} -> {expected}"):
self.assertEqual(jsonpath.generate(jsonpath.parse(selector)), expected)
self.assertEqual(jsonpath.parse(selector).sql(), f"'{expected}'")
def test_cts_file(self):
with open(os.path.join(FIXTURES_DIR, "jsonpath", "cts.json")) as file:
@ -46,6 +44,7 @@ class TestJsonpath(unittest.TestCase):
# sqlglot json path generator rewrites to a normal form
overrides = {
"$.☺": '$[""]',
"""$['a',1]""": """$["a",1]""",
"""$[*,'a']""": """$[*,"a"]""",
"""$..['a','d']""": """$..["a","d"]""",
@ -136,5 +135,5 @@ class TestJsonpath(unittest.TestCase):
except (ParseError, TokenError):
pass
else:
nodes = jsonpath.parse(selector)
self.assertEqual(jsonpath.generate(nodes), overrides.get(selector, selector))
path = jsonpath.parse(selector)
self.assertEqual(path.sql(), f"'{overrides.get(selector, selector)}'")