Skip to content

Commit d1bfdaa

Browse files
craig[bot]normanchenn
andcommitted
Merge #143839
143839: jsonpath: add division by zero check for modulo operation r=normanchenn a=normanchenn This commit adds a check for division by zero in the modulo operation to match Postgres' behaviour and maintain consistency with other modulo/division operations in the codebase. Epic: None Release note: None Co-authored-by: Norman Chen <[email protected]>
2 parents 1c2d48d + e778c3a commit d1bfdaa

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pkg/sql/logictest/testdata/logic_test/jsonb_path_query

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,9 @@ SELECT jsonb_path_query('{}', '3 % 2');
729729
----
730730
1
731731

732+
statement error pgcode 22012 pq: division by zero
733+
SELECT jsonb_path_query('{}', '1 % 0');
734+
732735
query T
733736
SELECT jsonb_path_query('{"a": 4, "b": 5}', '$.a + $.b');
734737
----

pkg/util/jsonpath/eval/operation.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@ func (ctx *jsonpathCtx) evalArithmetic(
389389
return nil, tree.ErrDivByZero
390390
}
391391
case jsonpath.OpMod:
392+
// In other places where apd.Context.Rem() is called, we first check if
393+
// the left value is NaN and the right value is 0, then return ErrDivByZero.
394+
// In this case, NaN shouldn't happen because JSON numbers cannot be NaN.
395+
// We assert this here, and then check if the right value is 0.
396+
if leftNum.Form == apd.NaN || rightNum.Form == apd.NaN {
397+
return nil, errors.AssertionFailedf("numbers in jsonpath queries cannot be NaN")
398+
}
399+
if rightNum.IsZero() {
400+
return nil, tree.ErrDivByZero
401+
}
392402
_, err = tree.DecimalCtx.Rem(&res, leftNum, rightNum)
393403
default:
394404
panic(errors.AssertionFailedf("unhandled jsonpath arithmetic type"))

0 commit comments

Comments
 (0)