summaryrefslogtreecommitdiff
path: root/src/backend/executor/execExpr.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/execExpr.c
parent74c96699be3f53c058c8794c07952221b9625b4f (diff)
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/execExpr.c')
-rw-r--r--src/backend/executor/execExpr.c19
1 files changed, 15 insertions, 4 deletions
diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c
index f1caf48036b..9b52bab52fc 100644
--- a/src/backend/executor/execExpr.c
+++ b/src/backend/executor/execExpr.c
@@ -93,6 +93,7 @@ static void ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
ExprEvalStep *scratch);
static void ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
ErrorSaveContext *escontext, bool omit_quotes,
+ bool exists_coerce,
Datum *resv, bool *resnull);
@@ -4329,7 +4330,9 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
jsestate->jump_eval_coercion = state->steps_len;
ExecInitJsonCoercion(state, jsexpr->returning, escontext,
- jsexpr->omit_quotes, resv, resnull);
+ jsexpr->omit_quotes,
+ jsexpr->op == JSON_EXISTS_OP,
+ resv, resnull);
}
else if (jsexpr->use_io_coercion)
{
@@ -4410,7 +4413,8 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
/* Step to coerce the ON ERROR expression if needed */
if (jsexpr->on_error->coerce)
ExecInitJsonCoercion(state, jsexpr->returning, escontext,
- jsexpr->omit_quotes, resv, resnull);
+ jsexpr->omit_quotes, false,
+ resv, resnull);
/*
* Add a COERCION_FINISH step to check for errors that may occur when
@@ -4466,7 +4470,8 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
/* Step to coerce the ON EMPTY expression if needed */
if (jsexpr->on_empty->coerce)
ExecInitJsonCoercion(state, jsexpr->returning, escontext,
- jsexpr->omit_quotes, resv, resnull);
+ jsexpr->omit_quotes, false,
+ resv, resnull);
/*
* Add a COERCION_FINISH step to check for errors that may occur when
@@ -4502,6 +4507,7 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
static void
ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
ErrorSaveContext *escontext, bool omit_quotes,
+ bool exists_coerce,
Datum *resv, bool *resnull)
{
ExprEvalStep scratch = {0};
@@ -4512,8 +4518,13 @@ ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
scratch.resnull = resnull;
scratch.d.jsonexpr_coercion.targettype = returning->typid;
scratch.d.jsonexpr_coercion.targettypmod = returning->typmod;
- scratch.d.jsonexpr_coercion.json_populate_type_cache = NULL;
+ scratch.d.jsonexpr_coercion.json_coercion_cache = NULL;
scratch.d.jsonexpr_coercion.escontext = escontext;
scratch.d.jsonexpr_coercion.omit_quotes = omit_quotes;
+ scratch.d.jsonexpr_coercion.exists_coerce = exists_coerce;
+ scratch.d.jsonexpr_coercion.exists_cast_to_int = exists_coerce &&
+ getBaseType(returning->typid) == INT4OID;
+ scratch.d.jsonexpr_coercion.exists_check_domain = exists_coerce &&
+ DomainHasConstraints(returning->typid);
ExprEvalPushStep(state, &scratch);
}