Skip to content

Commit f5d96c5

Browse files
author
Nikita Glukhov
committed
Fix json subscriptions: add coercion to text type
1 parent 5c5fa63 commit f5d96c5

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

src/backend/utils/adt/jsonfuncs.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mb/pg_wchar.h"
2525
#include "miscadmin.h"
2626
#include "nodes/nodeFuncs.h"
27+
#include "parser/parse_coerce.h"
2728
#include "utils/array.h"
2829
#include "utils/builtins.h"
2930
#include "utils/hsearch.h"
@@ -4044,7 +4045,7 @@ jsonb_subscription_evaluate(PG_FUNCTION_ARGS)
40444045

40454046
path = (text **) palloc(sbsdata->indexprNumber * sizeof(text*));
40464047
for (i = 0; i < sbsdata->indexprNumber; i++)
4047-
path[i] = cstring_to_text((char *) DatumGetPointer(sbsdata->upper[i]));
4048+
path[i] = DatumGetTextP(sbsdata->upper[i]);
40484049

40494050
if (is_assignment)
40504051
{
@@ -4119,13 +4120,45 @@ jsonb_subscription_prepare(PG_FUNCTION_ARGS)
41194120
{
41204121
SubscriptionRef *sbsref = (SubscriptionRef *) PG_GETARG_POINTER(0);
41214122
ParseState *pstate = (ParseState *) PG_GETARG_POINTER(1);
4123+
List *upperIndexpr = NIL;
4124+
ListCell *l;
41224125

41234126
if (sbsref->reflowerindexpr != NIL)
41244127
ereport(ERROR,
41254128
(errcode(ERRCODE_DATATYPE_MISMATCH),
41264129
errmsg("jsonb subscript does not support slices"),
41274130
parser_errposition(pstate, exprLocation(
4128-
((Node *)lfirst(sbsref->reflowerindexpr->head))) )));
4131+
((Node *)lfirst(sbsref->reflowerindexpr->head))))));
4132+
4133+
foreach(l, sbsref->refupperindexpr)
4134+
{
4135+
Node *subexpr = (Node *) lfirst(l);
4136+
4137+
Assert(subexpr != NULL);
4138+
4139+
if (subexpr == NULL)
4140+
ereport(ERROR,
4141+
(errcode(ERRCODE_DATATYPE_MISMATCH),
4142+
errmsg("jsonb subscript does not support slices"),
4143+
parser_errposition(pstate, exprLocation(
4144+
((Node *) lfirst(sbsref->refupperindexpr->head))))));
4145+
4146+
subexpr = coerce_to_target_type(pstate,
4147+
subexpr, exprType(subexpr),
4148+
TEXTOID, -1,
4149+
COERCION_ASSIGNMENT,
4150+
COERCE_IMPLICIT_CAST,
4151+
-1);
4152+
if (subexpr == NULL)
4153+
ereport(ERROR,
4154+
(errcode(ERRCODE_DATATYPE_MISMATCH),
4155+
errmsg("jsonb subscript must have text type"),
4156+
parser_errposition(pstate, exprLocation(subexpr))));
4157+
4158+
upperIndexpr = lappend(upperIndexpr, subexpr);
4159+
}
4160+
4161+
sbsref->refupperindexpr = upperIndexpr;
41294162

41304163
PG_RETURN_POINTER(sbsref);
41314164
}

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