Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 485bc16

Browse files
author
Nikita Glukhov
committed
Fix jsonpath handling of arithmetic errors
1 parent eb3ba15 commit 485bc16

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ static JsonPathExecResult
812812
executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
813813
JsonbValue *jb, JsonValueList *found)
814814
{
815+
MemoryContext mcxt = CurrentMemoryContext;
815816
JsonPathExecResult jper;
816817
JsonPathItem elem;
817818
JsonValueList lseq = { 0 };
@@ -820,6 +821,7 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
820821
JsonbValue *rval;
821822
JsonbValue lvalbuf;
822823
JsonbValue rvalbuf;
824+
PGFunction func;
823825
Datum ldatum;
824826
Datum rdatum;
825827
Datum res;
@@ -868,23 +870,43 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
868870
switch (jsp->type)
869871
{
870872
case jpiAdd:
871-
res = DirectFunctionCall2(numeric_add, ldatum, rdatum);
873+
func = numeric_add;
872874
break;
873875
case jpiSub:
874-
res = DirectFunctionCall2(numeric_sub, ldatum, rdatum);
876+
func = numeric_sub;
875877
break;
876878
case jpiMul:
877-
res = DirectFunctionCall2(numeric_mul, ldatum, rdatum);
879+
func = numeric_mul;
878880
break;
879881
case jpiDiv:
880-
res = DirectFunctionCall2(numeric_div, ldatum, rdatum);
882+
func = numeric_div;
881883
break;
882884
case jpiMod:
883-
res = DirectFunctionCall2(numeric_mod, ldatum, rdatum);
885+
func = numeric_mod;
884886
break;
885887
default:
886888
elog(ERROR, "unknown jsonpath arithmetic operation %d", jsp->type);
889+
func = NULL;
890+
break;
891+
}
892+
893+
PG_TRY();
894+
{
895+
res = DirectFunctionCall2(func, ldatum, rdatum);
896+
}
897+
PG_CATCH();
898+
{
899+
int errcode = geterrcode();
900+
901+
if (ERRCODE_TO_CATEGORY(errcode) != ERRCODE_DATA_EXCEPTION)
902+
PG_RE_THROW();
903+
904+
FlushErrorState();
905+
MemoryContextSwitchTo(mcxt);
906+
907+
return jperMakeError(errcode);
887908
}
909+
PG_END_TRY();
888910

889911
lval = palloc(sizeof(*lval));
890912
lval->type = jbvNumeric;

src/test/regress/expected/jsonb_jsonpath.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,23 @@ select jsonb '1' @? '$ ? ($ > 0)';
777777
t
778778
(1 row)
779779

780+
-- arithmetic errors
781+
select jsonb '[1,2,0,3]' @* '$[*] ? (2 / @ > 0)';
782+
?column?
783+
----------
784+
1
785+
2
786+
3
787+
(3 rows)
788+
789+
select jsonb '[1,2,0,3]' @* '$[*] ? ((2 / @ > 0) is unknown)';
790+
?column?
791+
----------
792+
0
793+
(1 row)
794+
795+
select jsonb '0' @* '1 / $';
796+
ERROR: Unknown SQL/JSON error
780797
-- unwrapping of operator arguments in lax mode
781798
select jsonb '{"a": [2]}' @* 'lax $.a * 3';
782799
?column?

src/test/regress/sql/jsonb_jsonpath.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ select jsonb '[1,2,3]' @? '$ ? (-@[*] < -2)';
156156
select jsonb '[1,2,3]' @? '$ ? (-@[*] < -3)';
157157
select jsonb '1' @? '$ ? ($ > 0)';
158158

159+
-- arithmetic errors
160+
select jsonb '[1,2,0,3]' @* '$[*] ? (2 / @ > 0)';
161+
select jsonb '[1,2,0,3]' @* '$[*] ? ((2 / @ > 0) is unknown)';
162+
select jsonb '0' @* '1 / $';
163+
159164
-- unwrapping of operator arguments in lax mode
160165
select jsonb '{"a": [2]}' @* 'lax $.a * 3';
161166
select jsonb '{"a": [2]}' @* 'lax $.a + 3';

0 commit comments

Comments
 (0)