Skip to content

Commit dbe2cf0

Browse files
committed
jsonpath: return unknown instead of error for exists, comparisons
This commit modifies JSONPath query evaluation to return unknown instead of propagating errors for invalid operations in exists and comparison functions. This matches postgres behaviour with these operations. Test cases were found from pg_regress. Release note: None
1 parent 5269684 commit dbe2cf0

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

pkg/sql/logictest/testdata/logic_test/jsonb_path_query

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,3 +1157,34 @@ query T
11571157
SELECT jsonb_path_query('["ab", 1]', 'strict $ starts with $var', '{"var": "abc"}');
11581158
----
11591159
null
1160+
1161+
query T
1162+
SELECT jsonb_path_query('{}', 'exists(2 + "3")');
1163+
----
1164+
null
1165+
1166+
query T
1167+
SELECT jsonb_path_query('{}', '2 / 0 > 0');
1168+
----
1169+
null
1170+
1171+
query T rowsort
1172+
SELECT jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'lax $.g ? ((exists (@.x + "3")) is unknown)');
1173+
----
1174+
{"x": 2}
1175+
{"y": 3}
1176+
1177+
query T
1178+
SELECT jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'strict $.g[*] ? ((exists (@.x)) is unknown)');
1179+
----
1180+
{"y": 3}
1181+
1182+
query T
1183+
SELECT jsonb_path_query('{"g": [{"x": 2}, {"y": 3}]}', 'strict $.g ? ((exists (@[*].x)) is unknown)');
1184+
----
1185+
[{"x": 2}, {"y": 3}]
1186+
1187+
query T
1188+
SELECT jsonb_path_query('[1,2,0,3]', '$[*] ? ((2 / @ > 0) is unknown)');
1189+
----
1190+
0

pkg/util/jsonpath/eval/operation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (ctx *jsonpathCtx) evalExists(
141141
// We can optimize this by short-circuiting in lax mode.
142142
l, err := ctx.evalAndUnwrapResult(op.Left, jsonValue, false /* unwrap */)
143143
if err != nil {
144-
return jsonpathBoolUnknown, err
144+
return jsonpathBoolUnknown, nil //nolint:returnerrcheck
145145
}
146146
if len(l) == 0 {
147147
return jsonpathBoolFalse, nil
@@ -255,14 +255,14 @@ func (ctx *jsonpathCtx) evalPredicate(
255255
// The left argument results are always auto-unwrapped.
256256
left, err := ctx.evalAndUnwrapResult(op.Left, jsonValue, true /* unwrap */)
257257
if err != nil {
258-
return jsonpathBoolUnknown, err
258+
return jsonpathBoolUnknown, nil //nolint:returnerrcheck
259259
}
260260
var right []json.JSON
261261
if evalRight {
262262
// The right argument results are conditionally evaluated and unwrapped.
263263
right, err = ctx.evalAndUnwrapResult(op.Right, jsonValue, unwrapRight)
264264
if err != nil {
265-
return jsonpathBoolUnknown, err
265+
return jsonpathBoolUnknown, nil //nolint:returnerrcheck
266266
}
267267
} else {
268268
// If we don't want to evaluate the right argument, we need to call

0 commit comments

Comments
 (0)