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

Commit e0f1434

Browse files
author
Nikita Glukhov
committed
Use symbolic names for root paths "$" and "$[*]"
1 parent 2a2622c commit e0f1434

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/backend/utils/adt/jsonb_selfuncs.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ static JsonPathStats
387387
jsonStatsGetPathStats(JsonStats jsdata, Datum *path, int pathlen,
388388
float4 *nullfrac)
389389
{
390-
JsonPathStats pstats = jsonStatsGetPathStatsStr(jsdata, "$", 1);
390+
JsonPathStats pstats = jsonStatsGetPathStatsStr(jsdata, JSON_PATH_ROOT, JSON_PATH_ROOT_LEN);
391391
Selectivity sel = 1.0;
392392

393393
for (int i = 0; pstats && i < pathlen; i++)
@@ -862,7 +862,9 @@ static HeapTuple
862862
jsonStatsGetArrayIndexStatsTuple(JsonStats jsdata, JsonStatType type, int32 index)
863863
{
864864
/* Extract statistics for root array elements */
865-
JsonPathStats pstats = jsonStatsGetPathStatsStr(jsdata, "$[*]", 4);
865+
JsonPathStats pstats = jsonStatsGetPathStatsStr(jsdata,
866+
JSON_PATH_ROOT_ARRAY,
867+
JSON_PATH_ROOT_ARRAY_LEN);
866868
Selectivity index_sel;
867869

868870
if (!pstats)
@@ -1144,7 +1146,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
11441146

11451147
/* Initialize root path string */
11461148
initStringInfo(&pathstr);
1147-
appendStringInfo(&pathstr, "$");
1149+
appendStringInfo(&pathstr, JSON_PATH_ROOT);
11481150

11491151
/* Initialize root path entry */
11501152
root.parent = NULL;
@@ -1299,6 +1301,7 @@ jsonSelectivityContains(JsonStats stats, Jsonb *jb)
12991301
static Selectivity
13001302
jsonSelectivityExists(JsonStats stats, Datum key)
13011303
{
1304+
JsonPathStats rootstats;
13021305
JsonPathStats arrstats;
13031306
JsonbValue jbvkey;
13041307
Datum jbkey;
@@ -1314,10 +1317,12 @@ jsonSelectivityExists(JsonStats stats, Datum key)
13141317

13151318
keysel = jsonStatsGetPathFreq(stats, &key, 1);
13161319

1317-
scalarsel = jsonSelectivity(jsonStatsGetPathStatsStr(stats, "$", 1),
1318-
jbkey, JsonbEqOperator);
1320+
rootstats = jsonStatsGetPathStatsStr(stats, JSON_PATH_ROOT,
1321+
JSON_PATH_ROOT_LEN);
1322+
scalarsel = jsonSelectivity(rootstats, jbkey, JsonbEqOperator);
13191323

1320-
arrstats = jsonStatsGetPathStatsStr(stats, "$[*]", 4);
1324+
arrstats = jsonStatsGetPathStatsStr(stats, JSON_PATH_ROOT_ARRAY,
1325+
JSON_PATH_ROOT_ARRAY_LEN);
13211326
arraysel = jsonSelectivity(arrstats, jbkey, JsonbEqOperator);
13221327
arraysel = 1.0 - pow(1.0 - arraysel,
13231328
jsonPathStatsGetAvgArraySize(arrstats));

src/backend/utils/adt/jsonb_typanalyze.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,8 @@ jsonAnalyzeMakeStats(JsonAnalyzeContext *ctx, int *numvalues)
10161016
Datum *values;
10171017
MemoryContext oldcxt = MemoryContextSwitchTo(ctx->stats->anl_context);
10181018

1019-
values = jsonAnalyzeBuildPathStatsArray(ctx->paths, ctx->npaths,
1020-
numvalues, "$", 1);
1019+
values = jsonAnalyzeBuildPathStatsArray(ctx->paths, ctx->npaths, numvalues,
1020+
JSON_PATH_ROOT, JSON_PATH_ROOT_LEN);
10211021

10221022
MemoryContextSwitchTo(oldcxt);
10231023

@@ -1119,7 +1119,7 @@ jsonAnalyzeInit(JsonAnalyzeContext *ctx, VacAttrStats *stats,
11191119
HASH_ELEM | HASH_FUNCTION | HASH_COMPARE | HASH_CONTEXT);
11201120

11211121
ctx->root = MemoryContextAllocZero(ctx->mcxt, sizeof(JsonPathAnlStats));
1122-
ctx->root->pathstr = "$";
1122+
ctx->root->pathstr = JSON_PATH_ROOT;
11231123
}
11241124

11251125
/*

src/include/utils/json_selfuncs.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
#include "utils/lsyscache.h"
2222
#include "utils/selfuncs.h"
2323

24+
#define JSON_PATH_ROOT "$"
25+
#define JSON_PATH_ROOT_LEN 1
26+
27+
#define JSON_PATH_ROOT_ARRAY "$[*]"
28+
#define JSON_PATH_ROOT_ARRAY_LEN 4
29+
2430
typedef struct JsonStatData
2531
{
2632
AttStatsSlot attslot;

0 commit comments

Comments
 (0)