1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-07 19:06:32 +03:00

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
This commit is contained in:
Amit Langote
2024-06-28 21:58:13 +09:00
parent c2d93c3802
commit 716bd12d22
15 changed files with 213 additions and 309 deletions

View File

@@ -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);
}

View File

@@ -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)

View File

@@ -2010,7 +2010,7 @@ llvm_compile_expr(ExprState *state)
v_jump_coercion = l_int32_const(lc, jsestate->jump_eval_coercion);
LLVMAddCase(v_switch, v_jump_coercion, b_coercion);
}
/* coercion_expr code */
/* jump_eval_coercion code */
LLVMPositionBuilderAtEnd(b, b_coercion);
if (jsestate->jump_eval_coercion >= 0)
LLVMBuildBr(b, opblocks[jsestate->jump_eval_coercion]);

View File

@@ -1006,10 +1006,7 @@ exprCollation(const Node *expr)
{
const JsonExpr *jsexpr = (JsonExpr *) expr;
if (jsexpr->coercion_expr)
coll = exprCollation(jsexpr->coercion_expr);
else
coll = jsexpr->collation;
coll = jsexpr->collation;
}
break;
case T_JsonBehavior:
@@ -1265,10 +1262,7 @@ exprSetCollation(Node *expr, Oid collation)
{
JsonExpr *jexpr = (JsonExpr *) expr;
if (jexpr->coercion_expr)
exprSetCollation((Node *) jexpr->coercion_expr, collation);
else
jexpr->collation = collation;
jexpr->collation = collation;
}
break;
case T_JsonBehavior:
@@ -2368,8 +2362,6 @@ expression_tree_walker_impl(Node *node,
return true;
if (WALK(jexpr->path_spec))
return true;
if (WALK(jexpr->coercion_expr))
return true;
if (WALK(jexpr->passing_values))
return true;
/* we assume walker doesn't care about passing_names */
@@ -3411,7 +3403,6 @@ expression_tree_mutator_impl(Node *node,
FLATCOPY(newnode, jexpr, JsonExpr);
MUTATE(newnode->formatted_expr, jexpr->formatted_expr, Node *);
MUTATE(newnode->path_spec, jexpr->path_spec, Node *);
MUTATE(newnode->coercion_expr, jexpr->coercion_expr, Node *);
MUTATE(newnode->passing_values, jexpr->passing_values, List *);
/* assume mutator does not care about passing_names */
MUTATE(newnode->on_empty, jexpr->on_empty, JsonBehavior *);

View File

@@ -96,7 +96,6 @@ static Node *transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func);
static void transformJsonPassingArgs(ParseState *pstate, const char *constructName,
JsonFormatType format, List *args,
List **passing_values, List **passing_names);
static void coerceJsonExprOutput(ParseState *pstate, JsonExpr *jsexpr);
static JsonBehavior *transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior,
JsonBehaviorType default_behavior,
JsonReturning *returning);
@@ -4492,39 +4491,7 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
/* JSON_TABLE() COLUMNS can specify a non-boolean type. */
if (jsexpr->returning->typid != BOOLOID)
{
Node *coercion_expr;
CaseTestExpr *placeholder = makeNode(CaseTestExpr);
int location = exprLocation((Node *) jsexpr);
/*
* We abuse CaseTestExpr here as placeholder to pass the
* result of evaluating JSON_EXISTS to the coercion
* expression.
*/
placeholder->typeId = BOOLOID;
placeholder->typeMod = -1;
placeholder->collation = InvalidOid;
coercion_expr =
coerce_to_target_type(pstate, (Node *) placeholder, BOOLOID,
jsexpr->returning->typid,
jsexpr->returning->typmod,
COERCION_EXPLICIT,
COERCE_IMPLICIT_CAST,
location);
if (coercion_expr == NULL)
ereport(ERROR,
(errcode(ERRCODE_CANNOT_COERCE),
errmsg("cannot cast type %s to %s",
format_type_be(BOOLOID),
format_type_be(jsexpr->returning->typid)),
parser_coercion_errposition(pstate, location, (Node *) jsexpr)));
if (coercion_expr != (Node *) placeholder)
jsexpr->coercion_expr = coercion_expr;
}
jsexpr->use_json_coercion = true;
jsexpr->on_error = transformJsonBehavior(pstate, func->on_error,
JSON_BEHAVIOR_FALSE,
@@ -4548,7 +4515,13 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
jsexpr->omit_quotes = (func->quotes == JS_QUOTES_OMIT);
jsexpr->wrapper = func->wrapper;
coerceJsonExprOutput(pstate, jsexpr);
/*
* Set up to coerce the result value of JsonPathValue() to the
* RETURNING type (default or user-specified), if needed. Also if
* OMIT QUOTES is specified.
*/
if (jsexpr->returning->typid != JSONBOID || jsexpr->omit_quotes)
jsexpr->use_json_coercion = true;
/* Assume NULL ON EMPTY when ON EMPTY is not specified. */
jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
@@ -4578,7 +4551,18 @@ transformJsonFuncExpr(ParseState *pstate, JsonFuncExpr *func)
/* Always omit quotes from scalar strings. */
jsexpr->omit_quotes = true;
coerceJsonExprOutput(pstate, jsexpr);
/*
* Set up to coerce the result value of JsonPathValue() to the
* RETURNING type (default or user-specified), if needed.
*/
if (jsexpr->returning->typid != TEXTOID)
{
if (get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN &&
DomainHasConstraints(jsexpr->returning->typid))
jsexpr->use_json_coercion = true;
else
jsexpr->use_io_coercion = true;
}
/* Assume NULL ON EMPTY when ON EMPTY is not specified. */
jsexpr->on_empty = transformJsonBehavior(pstate, func->on_empty,
@@ -4641,121 +4625,6 @@ transformJsonPassingArgs(ParseState *pstate, const char *constructName,
}
}
/*
* Set up to coerce the result value of JSON_VALUE() / JSON_QUERY() to the
* RETURNING type (default or user-specified), if needed.
*/
static void
coerceJsonExprOutput(ParseState *pstate, JsonExpr *jsexpr)
{
JsonReturning *returning = jsexpr->returning;
Node *context_item = jsexpr->formatted_expr;
int default_typmod;
Oid default_typid;
bool omit_quotes =
jsexpr->op == JSON_QUERY_OP && jsexpr->omit_quotes;
Node *coercion_expr = NULL;
Assert(returning);
/*
* Check for cases where the coercion should be handled at runtime, that
* is, without using a cast expression.
*/
if (jsexpr->op == JSON_VALUE_OP)
{
/*
* Use cast expressions for types with typmod and domain types.
*/
if (returning->typmod == -1 &&
get_typtype(returning->typid) != TYPTYPE_DOMAIN)
{
jsexpr->use_io_coercion = true;
return;
}
}
else if (jsexpr->op == JSON_QUERY_OP)
{
/*
* Cast functions from jsonb to the following types (jsonb_bool() et
* al) don't handle errors softly, so coerce either by calling
* json_populate_type() or the type's input function so that any
* errors are handled appropriately. The latter only if OMIT QUOTES is
* true.
*/
switch (returning->typid)
{
case BOOLOID:
case NUMERICOID:
case INT2OID:
case INT4OID:
case INT8OID:
case FLOAT4OID:
case FLOAT8OID:
if (jsexpr->omit_quotes)
jsexpr->use_io_coercion = true;
else
jsexpr->use_json_coercion = true;
return;
default:
break;
}
}
/* Look up a cast expression. */
/*
* For JSON_VALUE() and for JSON_QUERY() when OMIT QUOTES is true,
* ExecEvalJsonExprPath() will convert a quote-stripped source value to
* its text representation, so use TEXTOID as the source type.
*/
if (omit_quotes || jsexpr->op == JSON_VALUE_OP)
{
default_typid = TEXTOID;
default_typmod = -1;
}
else
{
default_typid = exprType(context_item);
default_typmod = exprTypmod(context_item);
}
if (returning->typid != default_typid ||
returning->typmod != default_typmod)
{
/*
* We abuse CaseTestExpr here as placeholder to pass the result of
* jsonpath evaluation as input to the coercion expression.
*/
CaseTestExpr *placeholder = makeNode(CaseTestExpr);
placeholder->typeId = default_typid;
placeholder->typeMod = default_typmod;
coercion_expr = coerceJsonFuncExpr(pstate, (Node *) placeholder,
returning, false);
if (coercion_expr == (Node *) placeholder)
coercion_expr = NULL;
}
jsexpr->coercion_expr = coercion_expr;
if (coercion_expr == NULL)
{
/*
* Either no cast was found or coercion is unnecessary but still must
* convert the string value to the output type.
*/
if (omit_quotes || jsexpr->op == JSON_VALUE_OP)
jsexpr->use_io_coercion = true;
else
jsexpr->use_json_coercion = true;
}
Assert(jsexpr->coercion_expr != NULL ||
(jsexpr->use_io_coercion != jsexpr->use_json_coercion));
}
/*
* Recursively checks if the given expression, or its sub-node in some cases,
* is valid for using as an ON ERROR / ON EMPTY DEFAULT expression.
@@ -4848,11 +4717,24 @@ transformJsonBehavior(ParseState *pstate, JsonBehavior *behavior,
btype == default_behavior))
coerce_at_runtime = true;
else
{
int32 baseTypmod = returning->typmod;
if (get_typtype(returning->typid) == TYPTYPE_DOMAIN)
(void) getBaseTypeAndTypmod(returning->typid, &baseTypmod);
if (baseTypmod > 0)
expr = coerce_to_specific_type(pstate, expr, TEXTOID,
"JSON_FUNCTION()");
coerced_expr =
coerce_to_target_type(pstate, expr, exprType(expr),
returning->typid, returning->typmod,
COERCION_EXPLICIT, COERCE_EXPLICIT_CAST,
returning->typid, baseTypmod,
baseTypmod > 0 ? COERCION_IMPLICIT :
COERCION_EXPLICIT,
baseTypmod > 0 ? COERCE_IMPLICIT_CAST :
COERCE_EXPLICIT_CAST,
exprLocation((Node *) behavior));
}
if (coerced_expr == NULL)
ereport(ERROR,

View File

@@ -447,12 +447,13 @@ static Datum populate_composite(CompositeIOData *io, Oid typid,
HeapTupleHeader defaultval, JsValue *jsv, bool *isnull,
Node *escontext);
static Datum populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv,
bool *isnull, Node *escontext);
bool *isnull, Node *escontext, bool omit_quotes);
static void prepare_column_cache(ColumnIOData *column, Oid typid, int32 typmod,
MemoryContext mcxt, bool need_scalar);
static Datum populate_record_field(ColumnIOData *col, Oid typid, int32 typmod,
const char *colname, MemoryContext mcxt, Datum defaultval,
JsValue *jsv, bool *isnull, Node *escontext);
JsValue *jsv, bool *isnull, Node *escontext,
bool omit_quotes);
static RecordIOData *allocate_record_info(MemoryContext mcxt, int ncolumns);
static bool JsObjectGetField(JsObject *obj, char *field, JsValue *jsv);
static void populate_recordset_record(PopulateRecordsetState *state, JsObject *obj);
@@ -2622,7 +2623,8 @@ populate_array_element(PopulateArrayContext *ctx, int ndim, JsValue *jsv)
ctx->aio->element_type,
ctx->aio->element_typmod,
NULL, ctx->mcxt, PointerGetDatum(NULL),
jsv, &element_isnull, ctx->escontext);
jsv, &element_isnull, ctx->escontext,
false);
/* Nothing to do on an error. */
if (SOFT_ERROR_OCCURRED(ctx->escontext))
return false;
@@ -3119,7 +3121,7 @@ populate_composite(CompositeIOData *io,
*/
static Datum
populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv,
bool *isnull, Node *escontext)
bool *isnull, Node *escontext, bool omit_quotes)
{
Datum res;
char *str = NULL;
@@ -3162,7 +3164,9 @@ populate_scalar(ScalarIOData *io, Oid typid, int32 typmod, JsValue *jsv,
{
JsonbValue *jbv = jsv->val.jsonb;
if (typid == JSONBOID)
if (jbv->type == jbvString && omit_quotes)
str = pnstrdup(jbv->val.string.val, jbv->val.string.len);
else if (typid == JSONBOID)
{
Jsonb *jsonb = JsonbValueToJsonb(jbv); /* directly use jsonb */
@@ -3225,7 +3229,7 @@ populate_domain(DomainIOData *io,
res = populate_record_field(io->base_io,
io->base_typid, io->base_typmod,
colname, mcxt, PointerGetDatum(NULL),
jsv, isnull, escontext);
jsv, isnull, escontext, false);
Assert(!*isnull || SOFT_ERROR_OCCURRED(escontext));
}
@@ -3338,7 +3342,7 @@ Datum
json_populate_type(Datum json_val, Oid json_type,
Oid typid, int32 typmod,
void **cache, MemoryContext mcxt,
bool *isnull,
bool *isnull, bool omit_quotes,
Node *escontext)
{
JsValue jsv = {0};
@@ -3368,10 +3372,22 @@ json_populate_type(Datum json_val, Oid json_type,
jsv.val.jsonb = &jbv;
/* fill binary jsonb value pointing to jb */
jbv.type = jbvBinary;
jbv.val.binary.data = &jsonb->root;
jbv.val.binary.len = VARSIZE(jsonb) - VARHDRSZ;
if (omit_quotes)
{
char *str = JsonbUnquote(DatumGetJsonbP(json_val));
/* fill the quote-stripped string */
jbv.type = jbvString;
jbv.val.string.len = strlen(str);
jbv.val.string.val = str;
}
else
{
/* fill binary jsonb value pointing to jb */
jbv.type = jbvBinary;
jbv.val.binary.data = &jsonb->root;
jbv.val.binary.len = VARSIZE(jsonb) - VARHDRSZ;
}
}
if (*cache == NULL)
@@ -3379,7 +3395,7 @@ json_populate_type(Datum json_val, Oid json_type,
return populate_record_field(*cache, typid, typmod, NULL, mcxt,
PointerGetDatum(NULL), &jsv, isnull,
escontext);
escontext, omit_quotes);
}
/* recursively populate a record field or an array element from a json/jsonb value */
@@ -3392,7 +3408,8 @@ populate_record_field(ColumnIOData *col,
Datum defaultval,
JsValue *jsv,
bool *isnull,
Node *escontext)
Node *escontext,
bool omit_scalar_quotes)
{
TypeCat typcat;
@@ -3426,7 +3443,7 @@ populate_record_field(ColumnIOData *col,
{
case TYPECAT_SCALAR:
return populate_scalar(&col->scalar_io, typid, typmod, jsv,
isnull, escontext);
isnull, escontext, omit_scalar_quotes);
case TYPECAT_ARRAY:
return populate_array(&col->io.array, colname, mcxt, jsv,
@@ -3595,7 +3612,8 @@ populate_record(TupleDesc tupdesc,
nulls[i] ? (Datum) 0 : values[i],
&field,
&nulls[i],
escontext);
escontext,
false);
}
res = heap_form_tuple(tupdesc, values, nulls);