aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/executor/execExpr.c19
-rw-r--r--src/backend/executor/execExprInterp.c46
2 files changed, 53 insertions, 12 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);
}
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,