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

Commit 4a12853

Browse files
author
Nikita Glukhov
committed
Rename functions with two "Stats" in name, add two helper macros for jsonStatsGetPathByStr()
1 parent 2676385 commit 4a12853

File tree

2 files changed

+35
-40
lines changed

2 files changed

+35
-40
lines changed

src/backend/utils/adt/jsonb_selfuncs.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -233,13 +233,15 @@ jsonPathStatsCompare(const void *pv1, const void *pv2)
233233
}
234234

235235
/*
236-
* jsonStatsFindPathStats
236+
* jsonStatsFindPath
237237
* Find stats for a given path.
238238
*
239239
* The stats are sorted by path, so we can simply do bsearch().
240+
* This is low-level function and jsdata->prefix is not considered, the caller
241+
* should handle it by itself.
240242
*/
241243
static JsonPathStats
242-
jsonStatsFindPathStats(JsonStats jsdata, char *path, int pathlen)
244+
jsonStatsFindPath(JsonStats jsdata, char *path, int pathlen)
243245
{
244246
JsonPathStats stats;
245247
JsonbValue jbvkey;
@@ -264,15 +266,11 @@ jsonStatsFindPathStats(JsonStats jsdata, char *path, int pathlen)
264266
}
265267

266268
/*
267-
* jsonStatsGetPathStatsStr
268-
* ???
269-
*
270-
* XXX Seems to do essentially what jsonStatsFindPathStats, except that it also
271-
* considers jsdata->prefix. Seems fairly easy to combine those into a single
272-
* function.
269+
* jsonStatsGetPathByStr
270+
* Find stats for a given path string considering jsdata->prefix.
273271
*/
274272
JsonPathStats
275-
jsonStatsGetPathStatsStr(JsonStats jsdata, const char *subpath, int subpathlen)
273+
jsonStatsGetPathByStr(JsonStats jsdata, const char *subpath, int subpathlen)
276274
{
277275
JsonPathStats stats;
278276
char *path;
@@ -287,14 +285,20 @@ jsonStatsGetPathStatsStr(JsonStats jsdata, const char *subpath, int subpathlen)
287285
memcpy(path, jsdata->prefix, jsdata->prefixlen);
288286
memcpy(&path[jsdata->prefixlen], &subpath[1], subpathlen - 1);
289287

290-
stats = jsonStatsFindPathStats(jsdata, path, pathlen);
288+
stats = jsonStatsFindPath(jsdata, path, pathlen);
291289

292290
if (!stats)
293291
pfree(path);
294292

295293
return stats;
296294
}
297295

296+
#define jsonStatsGetRootPath(jsdata) \
297+
jsonStatsGetPathByStr(jsdata, JSON_PATH_ROOT, JSON_PATH_ROOT_LEN)
298+
299+
#define jsonStatsGetRootArrayPath(jsdata) \
300+
jsonStatsGetPathByStr(jsdata, JSON_PATH_ROOT_ARRAY, JSON_PATH_ROOT_ARRAY_LEN)
301+
298302
/*
299303
* jsonPathAppendEntry
300304
* Append entry (represented as simple string) to a path.
@@ -344,7 +348,7 @@ jsonPathStatsGetSubpath(JsonPathStats pstats, const char *key)
344348
path = str.data;
345349
pathlen = str.len;
346350

347-
spstats = jsonStatsFindPathStats(pstats->data, path, pathlen);
351+
spstats = jsonStatsFindPath(pstats->data, path, pathlen);
348352
if (!spstats)
349353
pfree(path);
350354

@@ -378,16 +382,15 @@ jsonPathStatsGetArrayIndexSelectivity(JsonPathStats pstats, int index)
378382
}
379383

380384
/*
381-
* jsonStatsGetPathStats
385+
* jsonStatsGetPath
382386
* Find JSON statistics for a given path.
383387
*
384388
* 'path' is an array of text datums of length 'pathlen' (can be zero).
385389
*/
386390
static JsonPathStats
387-
jsonStatsGetPathStats(JsonStats jsdata, Datum *path, int pathlen,
388-
float4 *nullfrac)
391+
jsonStatsGetPath(JsonStats jsdata, Datum *path, int pathlen, float4 *nullfrac)
389392
{
390-
JsonPathStats pstats = jsonStatsGetPathStatsStr(jsdata, JSON_PATH_ROOT, JSON_PATH_ROOT_LEN);
393+
JsonPathStats pstats = jsonStatsGetRootPath(jsdata);
391394
Selectivity sel = 1.0;
392395

393396
for (int i = 0; pstats && i < pathlen; i++)
@@ -840,16 +843,15 @@ jsonPathStatsFormTuple(JsonPathStats pstats, JsonStatType type, float4 nullfrac)
840843
}
841844

842845
/*
843-
* jsonStatsGetPathStatsTuple
846+
* jsonStatsGetPathTuple
844847
* Extract JSON statistics for a path and form pg_statistics tuple.
845848
*/
846849
static HeapTuple
847-
jsonStatsGetPathStatsTuple(JsonStats jsdata, JsonStatType type,
848-
Datum *path, int pathlen)
850+
jsonStatsGetPathTuple(JsonStats jsdata, JsonStatType type,
851+
Datum *path, int pathlen)
849852
{
850853
float4 nullfrac;
851-
JsonPathStats pstats = jsonStatsGetPathStats(jsdata, path, pathlen,
852-
&nullfrac);
854+
JsonPathStats pstats = jsonStatsGetPath(jsdata, path, pathlen, &nullfrac);
853855

854856
return jsonPathStatsFormTuple(pstats, type, nullfrac);
855857
}
@@ -862,9 +864,7 @@ static HeapTuple
862864
jsonStatsGetArrayIndexStatsTuple(JsonStats jsdata, JsonStatType type, int32 index)
863865
{
864866
/* Extract statistics for root array elements */
865-
JsonPathStats pstats = jsonStatsGetPathStatsStr(jsdata,
866-
JSON_PATH_ROOT_ARRAY,
867-
JSON_PATH_ROOT_ARRAY_LEN);
867+
JsonPathStats pstats = jsonStatsGetRootArrayPath(jsdata);
868868
Selectivity index_sel;
869869

870870
if (!pstats)
@@ -885,10 +885,9 @@ jsonStatsGetArrayIndexStatsTuple(JsonStats jsdata, JsonStatType type, int32 inde
885885
static float4
886886
jsonStatsGetPathFreq(JsonStats jsdata, Datum *path, int pathlen)
887887
{
888-
float4 nullfrac;
889-
JsonPathStats pstats = jsonStatsGetPathStats(jsdata, path, pathlen,
890-
&nullfrac);
891-
float4 freq = (1.0 - nullfrac) * jsonPathStatsGetFreq(pstats, 0.0);
888+
float4 nullfrac;
889+
JsonPathStats pstats = jsonStatsGetPath(jsdata, path, pathlen, &nullfrac);
890+
float4 freq = (1.0 - nullfrac) * jsonPathStatsGetFreq(pstats, 0.0);
892891

893892
CLAMP_PROBABILITY(freq);
894893
return freq;
@@ -933,8 +932,7 @@ jsonbStatsVarOpConst(Oid opid, VariableStatData *resdata,
933932
}
934933

935934
resdata->statsTuple =
936-
jsonStatsGetPathStatsTuple(&jsdata, statype,
937-
&cnst->constvalue, 1);
935+
jsonStatsGetPathTuple(&jsdata, statype, &cnst->constvalue, 1);
938936
break;
939937
}
940938

@@ -985,7 +983,7 @@ jsonbStatsVarOpConst(Oid opid, VariableStatData *resdata,
985983

986984
if (!have_nulls)
987985
resdata->statsTuple =
988-
jsonStatsGetPathStatsTuple(&jsdata, statype, path, pathlen);
986+
jsonStatsGetPathTuple(&jsdata, statype, path, pathlen);
989987

990988
pfree(path);
991989
pfree(nulls);
@@ -1151,7 +1149,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
11511149
/* Initialize root path entry */
11521150
root.parent = NULL;
11531151
root.len = pathstr.len;
1154-
root.stats = jsonStatsGetPathStatsStr(stats, pathstr.data, pathstr.len);
1152+
root.stats = jsonStatsGetPathByStr(stats, pathstr.data, pathstr.len);
11551153
root.freq = jsonPathStatsGetFreq(root.stats, 0.0);
11561154
root.sel = 1.0;
11571155

@@ -1209,8 +1207,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12091207

12101208
/* Appeend path string entry for array elements, get stats. */
12111209
jsonPathAppendEntry(&pathstr, NULL);
1212-
pstats = jsonStatsGetPathStatsStr(stats, pathstr.data,
1213-
pathstr.len);
1210+
pstats = jsonStatsGetPathByStr(stats, pathstr.data, pathstr.len);
12141211
freq = jsonPathStatsGetFreq(pstats, 0.0);
12151212

12161213
/* If there are no arrays, return 0 or scalar selectivity */
@@ -1269,7 +1266,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12691266
* same statistics that was extracted in WJB_BEGIN_ARRAY.
12701267
*/
12711268
JsonPathStats pstats = r == WJB_ELEM ? path->stats :
1272-
jsonStatsGetPathStatsStr(stats, pathstr.data, pathstr.len);
1269+
jsonStatsGetPathByStr(stats, pathstr.data, pathstr.len);
12731270
/* Make scalar jsonb datum */
12741271
Datum scalar = JsonbPGetDatum(JsonbValueToJsonb(&v));
12751272
/* Absolute selectivity of 'path == scalar' */
@@ -1317,12 +1314,10 @@ jsonSelectivityExists(JsonStats stats, Datum key)
13171314

13181315
keysel = jsonStatsGetPathFreq(stats, &key, 1);
13191316

1320-
rootstats = jsonStatsGetPathStatsStr(stats, JSON_PATH_ROOT,
1321-
JSON_PATH_ROOT_LEN);
1317+
rootstats = jsonStatsGetRootPath(stats);
13221318
scalarsel = jsonSelectivity(rootstats, jbkey, JsonbEqOperator);
13231319

1324-
arrstats = jsonStatsGetPathStatsStr(stats, JSON_PATH_ROOT_ARRAY,
1325-
JSON_PATH_ROOT_ARRAY_LEN);
1320+
arrstats = jsonStatsGetRootArrayPath(stats);
13261321
arraysel = jsonSelectivity(arrstats, jbkey, JsonbEqOperator);
13271322
arraysel = 1.0 - pow(1.0 - arraysel,
13281323
jsonPathStatsGetAvgArraySize(arrstats));

src/include/utils/json_selfuncs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ typedef enum JsonStatType
6969
extern bool jsonStatsInit(JsonStats stats, const VariableStatData *vardata);
7070
extern void jsonStatsRelease(JsonStats data);
7171

72-
extern JsonPathStats jsonStatsGetPathStatsStr(JsonStats stats,
73-
const char *path, int pathlen);
72+
extern JsonPathStats jsonStatsGetPathByStr(JsonStats stats,
73+
const char *path, int pathlen);
7474

7575
extern JsonPathStats jsonPathStatsGetSubpath(JsonPathStats stats,
7676
const char *subpath);

0 commit comments

Comments
 (0)