aboutsummaryrefslogtreecommitdiff
path: root/src/backend/executor
diff options
context:
space:
mode:
authorAmit Langote <amitlan@postgresql.org>2024-06-28 21:58:13 +0900
committerAmit Langote <amitlan@postgresql.org>2024-06-28 21:58:13 +0900
commit716bd12d22c53d1943d41309f2dd061ec601dd5e (patch)
tree93a8030f6b2c690795c0d8bdb405206f476ee7b7 /src/backend/executor
parentc2d93c3802b205d135d1ae1d7ac167d74e08a274 (diff)
downloadpostgresql-716bd12d22c53d1943d41309f2dd061ec601dd5e.tar.gz
postgresql-716bd12d22c53d1943d41309f2dd061ec601dd5e.zip
SQL/JSON: Always coerce JsonExpr result at runtime
Instead of looking up casts at parse time for converting the result of JsonPath* query functions to the specified or the default RETURNING type, always perform the conversion at runtime using either the target type's input function or the function json_populate_type(). There are two motivations for this change: 1. json_populate_type() coerces to types with typmod such that any string values that exceed length limit cause an error instead of silent truncation, which is necessary to be standard-conforming. 2. It was possible to end up with a cast expression that doesn't support soft handling of errors causing bugs in the of handling ON ERROR clause. JsonExpr.coercion_expr which would store the cast expression is no longer necessary, so remove. Bump catversion because stored rules change because of the above removal. Reported-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Reviewed-by: Jian He <jian.universality@gmail.com> Discussion: Discussion: https://postgr.es/m/202405271326.5a5rprki64aw%40alvherre.pgsql
Diffstat (limited to 'src/backend/executor')
-rw-r--r--src/backend/executor/execExpr.c52
-rw-r--r--src/backend/executor/execExprInterp.c51
2 files changed, 40 insertions, 63 deletions
diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c
index 2bf86d06ef5..ccd48637784 100644
--- a/src/backend/executor/execExpr.c
+++ b/src/backend/executor/execExpr.c
@@ -92,7 +92,7 @@ static void ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
Datum *resv, bool *resnull,
ExprEvalStep *scratch);
static void ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
- ErrorSaveContext *escontext,
+ ErrorSaveContext *escontext, bool omit_quotes,
Datum *resv, bool *resnull);
@@ -4313,13 +4313,15 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
ExprEvalPushStep(state, scratch);
/*
- * Jump to coerce the NULL using coercion_expr if present. Coercing NULL
- * is only interesting when the RETURNING type is a domain whose
- * constraints must be checked. jsexpr->coercion_expr containing a
- * CoerceToDomain node must have been set in that case.
+ * Jump to coerce the NULL using json_populate_type() if needed. Coercing
+ * NULL is only interesting when the RETURNING type is a domain whose
+ * constraints must be checked. jsexpr->use_json_coercion must have been
+ * set in that case.
*/
- if (jsexpr->coercion_expr)
+ if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
+ DomainHasConstraints(jsexpr->returning->typid))
{
+ Assert(jsexpr->use_json_coercion);
scratch->opcode = EEOP_JUMP;
scratch->d.jump.jumpdone = state->steps_len + 1;
ExprEvalPushStep(state, scratch);
@@ -4337,33 +4339,12 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
* NULL returned on NULL input as described above.
*/
jsestate->jump_eval_coercion = -1;
- if (jsexpr->coercion_expr)
- {
- Datum *save_innermost_caseval;
- bool *save_innermost_casenull;
- ErrorSaveContext *save_escontext;
-
- jsestate->jump_eval_coercion = state->steps_len;
-
- save_innermost_caseval = state->innermost_caseval;
- save_innermost_casenull = state->innermost_casenull;
- save_escontext = state->escontext;
-
- state->innermost_caseval = resv;
- state->innermost_casenull = resnull;
- state->escontext = escontext;
-
- ExecInitExprRec((Expr *) jsexpr->coercion_expr, state, resv, resnull);
-
- state->innermost_caseval = save_innermost_caseval;
- state->innermost_casenull = save_innermost_casenull;
- state->escontext = save_escontext;
- }
- else if (jsexpr->use_json_coercion)
+ if (jsexpr->use_json_coercion)
{
jsestate->jump_eval_coercion = state->steps_len;
- ExecInitJsonCoercion(state, jsexpr->returning, escontext, resv, resnull);
+ ExecInitJsonCoercion(state, jsexpr->returning, escontext,
+ jsexpr->omit_quotes, resv, resnull);
}
else if (jsexpr->use_io_coercion)
{
@@ -4435,8 +4416,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, resv,
- resnull);
+ ExecInitJsonCoercion(state, jsexpr->returning, escontext,
+ jsexpr->omit_quotes, resv, resnull);
/* JUMP to end to skip the ON EMPTY steps added below. */
jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
@@ -4468,8 +4449,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, resv,
- resnull);
+ ExecInitJsonCoercion(state, jsexpr->returning, escontext,
+ jsexpr->omit_quotes, resv, resnull);
}
foreach(lc, jumps_to_end)
@@ -4488,7 +4469,7 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
*/
static void
ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
- ErrorSaveContext *escontext,
+ ErrorSaveContext *escontext, bool omit_quotes,
Datum *resv, bool *resnull)
{
ExprEvalStep scratch = {0};
@@ -4501,5 +4482,6 @@ ExecInitJsonCoercion(ExprState *state, JsonReturning *returning,
scratch.d.jsonexpr_coercion.targettypmod = returning->typmod;
scratch.d.jsonexpr_coercion.json_populate_type_cache = NULL;
scratch.d.jsonexpr_coercion.escontext = escontext;
+ scratch.d.jsonexpr_coercion.omit_quotes = omit_quotes;
ExprEvalPushStep(state, &scratch);
}
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index 852186312c5..d8735286c4d 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -4303,8 +4303,14 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
if (!error)
{
- *op->resvalue = BoolGetDatum(exists);
*op->resnull = false;
+ if (jsexpr->use_json_coercion)
+ *op->resvalue = DirectFunctionCall1(jsonb_in,
+ BoolGetDatum(exists) ?
+ CStringGetDatum("true") :
+ CStringGetDatum("false"));
+ else
+ *op->resvalue = BoolGetDatum(exists);
}
}
break;
@@ -4316,22 +4322,6 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
jsexpr->column_name);
*op->resnull = (DatumGetPointer(*op->resvalue) == NULL);
-
- /* Handle OMIT QUOTES. */
- if (!*op->resnull && jsexpr->omit_quotes)
- {
- val_string = JsonbUnquote(DatumGetJsonbP(*op->resvalue));
-
- /*
- * Pass the string as a text value to the cast expression if
- * one present. If not, use the input function call below to
- * do the coercion.
- */
- if (jump_eval_coercion >= 0)
- *op->resvalue =
- DirectFunctionCall1(textin,
- PointerGetDatum(val_string));
- }
break;
case JSON_VALUE_OP:
@@ -4343,7 +4333,7 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
if (jbv == NULL)
{
- /* Will be coerced with coercion_expr, if any. */
+ /* Will be coerced with json_populate_type(), if needed. */
*op->resvalue = (Datum) 0;
*op->resnull = true;
}
@@ -4355,18 +4345,22 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
val_string = DatumGetCString(DirectFunctionCall1(jsonb_out,
JsonbPGetDatum(JsonbValueToJsonb(jbv))));
}
+ else if (jsexpr->use_json_coercion)
+ {
+ *op->resvalue = JsonbPGetDatum(JsonbValueToJsonb(jbv));
+ *op->resnull = false;
+ }
else
{
val_string = ExecGetJsonValueItemString(jbv, op->resnull);
/*
- * Pass the string as a text value to the cast
- * expression if one present. If not, use the input
- * function call below to do the coercion.
+ * Simply convert to the default RETURNING type (text)
+ * if no coercion needed.
*/
- *op->resvalue = PointerGetDatum(val_string);
- if (jump_eval_coercion >= 0)
- *op->resvalue = DirectFunctionCall1(textin, *op->resvalue);
+ if (!jsexpr->use_io_coercion)
+ *op->resvalue = DirectFunctionCall1(textin,
+ CStringGetDatum(val_string));
}
}
break;
@@ -4545,13 +4539,14 @@ ExecEvalJsonCoercion(ExprState *state, ExprEvalStep *op,
op->d.jsonexpr_coercion.targettypmod,
&op->d.jsonexpr_coercion.json_populate_type_cache,
econtext->ecxt_per_query_memory,
- op->resnull, (Node *) escontext);
+ op->resnull,
+ op->d.jsonexpr_coercion.omit_quotes,
+ (Node *) escontext);
}
/*
- * Checks if an error occurred either when evaluating JsonExpr.coercion_expr or
- * in ExecEvalJsonCoercion(). If so, this sets JsonExprState.error to trigger
- * the ON ERROR handling steps.
+ * Checks if an error occurred in ExecEvalJsonCoercion(). If so, this sets
+ * JsonExprState.error to trigger the ON ERROR handling steps.
*/
void
ExecEvalJsonCoercionFinish(ExprState *state, ExprEvalStep *op)