From f2f9176548d36d3b02bd3baf1d5afdf0a84a84b1 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Mon, 15 Jan 2024 14:19:22 +0100 Subject: [PATCH 2/2] fixup! Implement jsonpath .number(), .decimal([precision [, scale]]), .bigint(), and .integer() methods --- src/backend/utils/adt/jsonpath_exec.c | 48 +++++++++------------------ 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index a361c456c54..092bcda45ee 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -1118,8 +1118,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, char *numstr = NULL; if (unwrap && JsonbType(jb) == jbvArray) - return executeItemUnwrapTargetArray(cxt, jsp, jb, found, - false); + return executeItemUnwrapTargetArray(cxt, jsp, jb, found, false); if (jb->type == jbvNumeric) { @@ -1131,8 +1130,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, jspOperationName(jsp->type))))); if (jsp->type == jpiDecimal) - numstr = DatumGetCString(DirectFunctionCall1(numeric_out, - NumericGetDatum(num))); + numstr = DatumGetCString(DirectFunctionCall1(numeric_out, NumericGetDatum(num))); res = jperOk; } else if (jb->type == jbvString) @@ -1144,10 +1142,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, numstr = pnstrdup(jb->val.string.val, jb->val.string.len); - noerr = DirectInputFunctionCallSafe(numeric_in, numstr, - InvalidOid, -1, - (Node *) &escontext, - &datum); + noerr = DirectInputFunctionCallSafe(numeric_in, numstr, InvalidOid, -1, + (Node *) &escontext, &datum); if (!noerr || escontext.error_occurred) RETURN_ERROR(ereport(ERROR, @@ -1195,8 +1191,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, if (elem.type != jpiNumeric) elog(ERROR, "invalid jsonpath item type for .decimal() precision"); - precision = numeric_int4_opt_error(jspGetNumeric(&elem), - &have_error); + precision = numeric_int4_opt_error(jspGetNumeric(&elem), &have_error); if (have_error) RETURN_ERROR(ereport(ERROR, (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM), @@ -1209,8 +1204,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, if (elem.type != jpiNumeric) elog(ERROR, "invalid jsonpath item type for .decimal() scale"); - scale = numeric_int4_opt_error(jspGetNumeric(&elem), - &have_error); + scale = numeric_int4_opt_error(jspGetNumeric(&elem), &have_error); if (have_error) RETURN_ERROR(ereport(ERROR, (errcode(ERRCODE_NON_NUMERIC_SQL_JSON_ITEM), @@ -1228,8 +1222,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, datums[1] = CStringGetDatum(sstr); arrtypmod = construct_array_builtin(datums, 2, CSTRINGOID); - dtypmod = DirectFunctionCall1(numerictypmodin, - PointerGetDatum(arrtypmod)); + dtypmod = DirectFunctionCall1(numerictypmodin, PointerGetDatum(arrtypmod)); /* Convert numstr to Numeric with typmod */ Assert(numstr != NULL); @@ -1262,8 +1255,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, Datum datum; if (unwrap && JsonbType(jb) == jbvArray) - return executeItemUnwrapTargetArray(cxt, jsp, jb, found, - false); + return executeItemUnwrapTargetArray(cxt, jsp, jb, found, false); if (jb->type == jbvNumeric) { @@ -1288,10 +1280,8 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, ErrorSaveContext escontext = {T_ErrorSaveContext}; bool noerr; - noerr = DirectInputFunctionCallSafe(int8in, tmp, - InvalidOid, -1, - (Node *) &escontext, - &datum); + noerr = DirectInputFunctionCallSafe(int8in, tmp, InvalidOid, -1, + (Node *) &escontext, &datum); if (!noerr || escontext.error_occurred) RETURN_ERROR(ereport(ERROR, @@ -1309,8 +1299,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, jb = &jbv; jb->type = jbvNumeric; - jb->val.numeric = DatumGetNumeric(DirectFunctionCall1(int8_numeric, - datum)); + jb->val.numeric = DatumGetNumeric(DirectFunctionCall1(int8_numeric, datum)); res = executeNextItem(cxt, jsp, NULL, jb, found, true); } @@ -1322,8 +1311,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, Datum datum; if (unwrap && JsonbType(jb) == jbvArray) - return executeItemUnwrapTargetArray(cxt, jsp, jb, found, - false); + return executeItemUnwrapTargetArray(cxt, jsp, jb, found, false); if (jb->type == jbvNumeric) { @@ -1343,15 +1331,12 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, else if (jb->type == jbvString) { /* cast string as integer */ - char *tmp = pnstrdup(jb->val.string.val, - jb->val.string.len); + char *tmp = pnstrdup(jb->val.string.val, jb->val.string.len); ErrorSaveContext escontext = {T_ErrorSaveContext}; bool noerr; - noerr = DirectInputFunctionCallSafe(int4in, tmp, - InvalidOid, -1, - (Node *) &escontext, - &datum); + noerr = DirectInputFunctionCallSafe(int4in, tmp, InvalidOid, -1, + (Node *) &escontext, &datum); if (!noerr || escontext.error_occurred) RETURN_ERROR(ereport(ERROR, @@ -1369,8 +1354,7 @@ executeItemOptUnwrapTarget(JsonPathExecContext *cxt, JsonPathItem *jsp, jb = &jbv; jb->type = jbvNumeric; - jb->val.numeric = DatumGetNumeric(DirectFunctionCall1(int4_numeric, - datum)); + jb->val.numeric = DatumGetNumeric(DirectFunctionCall1(int4_numeric, datum)); res = executeNextItem(cxt, jsp, NULL, jb, found, true); } -- 2.43.0