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

Commit d7ad180

Browse files
author
Nikita Glukhov
committed
Fix jsonpath handling of arithmetic errors
1 parent 8fa4e08 commit d7ad180

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
@@ -843,6 +843,7 @@ static JsonPathExecResult
843843
executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
844844
JsonbValue *jb, JsonValueList *found)
845845
{
846+
MemoryContext mcxt = CurrentMemoryContext;
846847
JsonPathExecResult jper;
847848
JsonPathItem elem;
848849
JsonValueList lseq = { 0 };
@@ -851,6 +852,7 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
851852
JsonbValue *rval;
852853
JsonbValue lvalbuf;
853854
JsonbValue rvalbuf;
855+
PGFunction func;
854856
Datum ldatum;
855857
Datum rdatum;
856858
Datum res;
@@ -899,23 +901,43 @@ executeBinaryArithmExpr(JsonPathExecContext *cxt, JsonPathItem *jsp,
899901
switch (jsp->type)
900902
{
901903
case jpiAdd:
902-
res = DirectFunctionCall2(numeric_add, ldatum, rdatum);
904+
func = numeric_add;
903905
break;
904906
case jpiSub:
905-
res = DirectFunctionCall2(numeric_sub, ldatum, rdatum);
907+
func = numeric_sub;
906908
break;
907909
case jpiMul:
908-
res = DirectFunctionCall2(numeric_mul, ldatum, rdatum);
910+
func = numeric_mul;
909911
break;
910912
case jpiDiv:
911-
res = DirectFunctionCall2(numeric_div, ldatum, rdatum);
913+
func = numeric_div;
912914
break;
913915
case jpiMod:
914-
res = DirectFunctionCall2(numeric_mod, ldatum, rdatum);
916+
func = numeric_mod;
915917
break;
916918
default:
917919
elog(ERROR, "unknown jsonpath arithmetic operation %d", jsp->type);
920+
func = NULL;
921+
break;
922+
}
923+
924+
PG_TRY();
925+
{
926+
res = DirectFunctionCall2(func, ldatum, rdatum);
927+
}
928+
PG_CATCH();
929+
{
930+
int errcode = geterrcode();
931+
932+
if (ERRCODE_TO_CATEGORY(errcode) != ERRCODE_DATA_EXCEPTION)
933+
PG_RE_THROW();
934+
935+
FlushErrorState();
936+
MemoryContextSwitchTo(mcxt);
937+
938+
return jperMakeError(errcode);
918939
}
940+
PG_END_TRY();
919941

920942
lval = palloc(sizeof(*lval));
921943
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)