aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor/execExprInterp.c
diff options
context:
space:
mode:
authorAmit Langote <amitlan@postgresql.org>2024-07-30 10:12:23 +0900
committerAmit Langote <amitlan@postgresql.org>2024-07-30 10:34:17 +0900
commit7f56eaff2fb0f3541c5c6ef8c1099e7f0618b461 (patch)
treed03b8c513567f5a085ec15395bb57fff9677b596 /src/backend/executor/execExprInterp.c
parent74c96699be3f53c058c8794c07952221b9625b4f (diff)
downloadpostgresql-7f56eaff2fb0f3541c5c6ef8c1099e7f0618b461.tar.gz
postgresql-7f56eaff2fb0f3541c5c6ef8c1099e7f0618b461.zip
SQL/JSON: Fix casting for integer EXISTS columns in JSON_TABLE
The current method of coercing the boolean result value of JsonPathExists() to the target type specified for an EXISTS column, which is to call the type's input function via json_populate_type(), leads to an error when the target type is integer, because the integer input function doesn't recognize boolean literal values as valid. Instead use the boolean-to-integer cast function for coercion in that case so that using integer or domains thereof as type for EXISTS columns works. Note that coercion for ON ERROR values TRUE and FALSE already works like that because the parser creates a cast expression including the cast function, but the coercion of the actual result value is not handled by the parser. Tests by Jian He. Reported-by: Jian He <jian.universality@gmail.com> Author: Jian He <jian.universality@gmail.com> Author: Amit Langote <amitlangote09@gmail.com> Discussion: https://postgr.es/m/CACJufxEo4sUjKCYtda0_qt9tazqqKPmF1cqhW9KBOUeJFqQd2g@mail.gmail.com Backpatch-through: 17
Diffstat (limited to 'src/backend/executor/execExprInterp.c')
-rw-r--r--src/backend/executor/execExprInterp.c46
1 files changed, 38 insertions, 8 deletions
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index 430438f668e..1633ea83731 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -4303,13 +4303,7 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
if (!error)
{
*op->resnull = false;
- if (jsexpr->use_json_coercion)
- *op->resvalue = DirectFunctionCall1(jsonb_in,
- BoolGetDatum(exists) ?
- CStringGetDatum("true") :
- CStringGetDatum("false"));
- else
- *op->resvalue = BoolGetDatum(exists);
+ *op->resvalue = BoolGetDatum(exists);
}
}
break;
@@ -4550,10 +4544,46 @@ ExecEvalJsonCoercion(ExprState *state, ExprEvalStep *op,
{
ErrorSaveContext *escontext = op->d.jsonexpr_coercion.escontext;
+ /*
+ * Prepare to call json_populate_type() to coerce the boolean result of
+ * JSON_EXISTS_OP to the target type. If the the target type is integer
+ * or a domain over integer, call the boolean-to-integer cast function
+ * instead, because the integer's input function (which is what
+ * json_populate_type() calls to coerce to scalar target types) doesn't
+ * accept boolean literals as valid input. We only have a special case
+ * for integer and domains thereof as it seems common to use those types
+ * for EXISTS columns in JSON_TABLE().
+ */
+ if (op->d.jsonexpr_coercion.exists_coerce)
+ {
+ if (op->d.jsonexpr_coercion.exists_cast_to_int)
+ {
+ /* Check domain constraints if any. */
+ if (op->d.jsonexpr_coercion.exists_check_domain &&
+ !domain_check_safe(*op->resvalue, *op->resnull,
+ op->d.jsonexpr_coercion.targettype,
+ &op->d.jsonexpr_coercion.json_coercion_cache,
+ econtext->ecxt_per_query_memory,
+ (Node *) escontext))
+ {
+ *op->resnull = true;
+ *op->resvalue = (Datum) 0;
+ }
+ else
+ *op->resvalue = DirectFunctionCall1(bool_int4, *op->resvalue);
+ return;
+ }
+
+ *op->resvalue = DirectFunctionCall1(jsonb_in,
+ DatumGetBool(*op->resvalue) ?
+ CStringGetDatum("true") :
+ CStringGetDatum("false"));
+ }
+
*op->resvalue = json_populate_type(*op->resvalue, JSONBOID,
op->d.jsonexpr_coercion.targettype,
op->d.jsonexpr_coercion.targettypmod,
- &op->d.jsonexpr_coercion.json_populate_type_cache,
+ &op->d.jsonexpr_coercion.json_coercion_cache,
econtext->ecxt_per_query_memory,
op->resnull,
op->d.jsonexpr_coercion.omit_quotes,