Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f59dbcc

Browse files
author
Nikita Glukhov
committed
Fix possible interpretation of object key as array index in -> operator
1 parent e1b2e58 commit f59dbcc

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/backend/utils/adt/jsonb_selfuncs.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ jsonPathStatsGetArrayIndexSelectivity(JsonPathStats pstats, int index)
455455
* 'path' is an array of text datums of length 'pathlen' (can be zero).
456456
*/
457457
static JsonPathStats
458-
jsonStatsGetPath(JsonStats jsdata, Datum *path, int pathlen, float4 *nullfrac)
458+
jsonStatsGetPath(JsonStats jsdata, Datum *path, int pathlen,
459+
bool try_arrays_indexes, float4 *nullfrac)
459460
{
460461
JsonPathStats pstats = jsonStatsGetRootPath(jsdata);
461462
Selectivity sel = 1.0;
@@ -466,6 +467,14 @@ jsonStatsGetPath(JsonStats jsdata, Datum *path, int pathlen, float4 *nullfrac)
466467
char *tail;
467468
int index;
468469

470+
if (!try_arrays_indexes)
471+
{
472+
/* Find object key stats */
473+
pstats = jsonPathStatsGetSubpath(pstats, key);
474+
pfree(key);
475+
continue;
476+
}
477+
469478
/* Try to interpret path entry as integer array index */
470479
errno = 0;
471480
index = strtoint(key, &tail, 10);
@@ -939,14 +948,15 @@ jsonPathStatsFormTuple(JsonPathStats pstats, JsonStatType type, float4 nullfrac)
939948

940949
/*
941950
* jsonStatsGetPathTuple
942-
* Extract JSON statistics for a path and form pg_statistics tuple.
951+
* Extract JSON statistics for a text[] path and form pg_statistics tuple.
943952
*/
944953
static HeapTuple
945954
jsonStatsGetPathTuple(JsonStats jsdata, JsonStatType type,
946-
Datum *path, int pathlen)
955+
Datum *path, int pathlen, bool try_arrays_indexes)
947956
{
948957
float4 nullfrac;
949-
JsonPathStats pstats = jsonStatsGetPath(jsdata, path, pathlen, &nullfrac);
958+
JsonPathStats pstats = jsonStatsGetPath(jsdata, path, pathlen,
959+
try_arrays_indexes, &nullfrac);
950960

951961
return jsonPathStatsFormTuple(pstats, type, nullfrac);
952962
}
@@ -1030,8 +1040,9 @@ jsonbStatsVarOpConst(Oid opid, VariableStatData *resdata,
10301040
return false;
10311041
}
10321042

1033-
resdata->statsTuple =
1034-
jsonStatsGetPathTuple(&jsdata, statype, &cnst->constvalue, 1);
1043+
resdata->statsTuple = jsonStatsGetPathTuple(&jsdata, statype,
1044+
&cnst->constvalue, 1,
1045+
false);
10351046
break;
10361047
}
10371048

@@ -1081,8 +1092,9 @@ jsonbStatsVarOpConst(Oid opid, VariableStatData *resdata,
10811092
}
10821093

10831094
if (!have_nulls)
1084-
resdata->statsTuple =
1085-
jsonStatsGetPathTuple(&jsdata, statype, path, pathlen);
1095+
resdata->statsTuple = jsonStatsGetPathTuple(&jsdata, statype,
1096+
path, pathlen,
1097+
true);
10861098

10871099
pfree(path);
10881100
pfree(nulls);
@@ -1431,7 +1443,7 @@ jsonSelectivityExists(JsonStats stats, Datum key)
14311443

14321444
jbkey = JsonbPGetDatum(JsonbValueToJsonb(&jbvkey));
14331445

1434-
keysel = jsonStatsGetPathFreq(stats, &key, 1);
1446+
keysel = jsonStatsGetPathFreq(stats, &key, 1, false);
14351447

14361448
rootstats = jsonStatsGetRootPath(stats);
14371449
scalarsel = jsonSelectivity(rootstats, jbkey, JsonbEqOperator);

0 commit comments

Comments
 (0)