Skip to content

Commit 4b3a5f7

Browse files
author
Nikita Glukhov
committed
Refactor transformJsonIsPredicate()
1 parent 62db6a7 commit 4b3a5f7

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

src/backend/parser/parse_expr.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4346,28 +4346,25 @@ transformJsonArrayConstructor(ParseState *pstate, JsonArrayConstructor *ctor)
43464346
ctor->location);
43474347
}
43484348

4349-
/*
4350-
* Transform IS JSON predicate into
4351-
* json[b]_is_valid(json, value_type [, check_key_uniqueness]) call.
4352-
*/
43534349
static Node *
4354-
transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
4350+
transformJsonParseArg(ParseState *pstate, Node *jsexpr, JsonFormat *format,
4351+
Oid *exprtype)
43554352
{
4356-
Node *raw_expr = transformExprRecurse(pstate, pred->expr);
4353+
Node *raw_expr = transformExprRecurse(pstate, jsexpr);
43574354
Node *expr = raw_expr;
4358-
Oid exprtype = exprType(expr);
4355+
4356+
*exprtype = exprType(expr);
43594357

43604358
/* prepare input document */
4361-
if (exprtype == BYTEAOID)
4359+
if (*exprtype == BYTEAOID)
43624360
{
43634361
JsonValueExpr *jve;
43644362

43654363
expr = makeCaseTestExpr(raw_expr);
4366-
expr = makeJsonByteaToTextConversion(expr, pred->format,
4367-
exprLocation(expr));
4368-
exprtype = TEXTOID;
4364+
expr = makeJsonByteaToTextConversion(expr, format, exprLocation(expr));
4365+
*exprtype = TEXTOID;
43694366

4370-
jve = makeJsonValueExpr((Expr *) raw_expr, pred->format);
4367+
jve = makeJsonValueExpr((Expr *) raw_expr, format);
43714368

43724369
jve->formatted_expr = (Expr *) expr;
43734370
expr = (Node *) jve;
@@ -4377,31 +4374,45 @@ transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
43774374
char typcategory;
43784375
bool typispreferred;
43794376

4380-
get_type_category_preferred(exprtype, &typcategory, &typispreferred);
4377+
get_type_category_preferred(*exprtype, &typcategory, &typispreferred);
43814378

4382-
if (exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
4379+
if (*exprtype == UNKNOWNOID || typcategory == TYPCATEGORY_STRING)
43834380
{
4384-
expr = coerce_to_target_type(pstate, (Node *) expr, exprtype,
4381+
expr = coerce_to_target_type(pstate, (Node *) expr, *exprtype,
43854382
TEXTOID, -1,
43864383
COERCION_IMPLICIT,
43874384
COERCE_IMPLICIT_CAST, -1);
4388-
exprtype = TEXTOID;
4385+
*exprtype = TEXTOID;
43894386
}
43904387

4391-
if (pred->format->encoding != JS_ENC_DEFAULT)
4388+
if (format->encoding != JS_ENC_DEFAULT)
43924389
ereport(ERROR,
43934390
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
4394-
parser_errposition(pstate, pred->format->location),
4391+
parser_errposition(pstate, format->location),
43954392
errmsg("cannot use JSON FORMAT ENCODING clause for non-bytea input types")));
43964393
}
43974394

4395+
return expr;
4396+
}
4397+
4398+
/*
4399+
* Transform IS JSON predicate into
4400+
* json[b]_is_valid(json, value_type [, check_key_uniqueness]) call.
4401+
*/
4402+
static Node *
4403+
transformJsonIsPredicate(ParseState *pstate, JsonIsPredicate *pred)
4404+
{
4405+
Oid exprtype;
4406+
Node *expr = transformJsonParseArg(pstate, pred->expr, pred->format,
4407+
&exprtype);
4408+
43984409
/* make resulting expression */
43994410
if (exprtype != TEXTOID && exprtype != JSONOID && exprtype != JSONBOID)
44004411
ereport(ERROR,
44014412
(errcode(ERRCODE_DATATYPE_MISMATCH),
44024413
errmsg("cannot use type %s in IS JSON predicate",
44034414
format_type_be(exprtype))));
44044415

4405-
return makeJsonIsPredicate((Node *) expr, NULL, pred->value_type,
4416+
return makeJsonIsPredicate(expr, NULL, pred->value_type,
44064417
pred->unique_keys, pred->location);
44074418
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy