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

Commit 4a6a004

Browse files
author
Nikita Glukhov
committed
Add JsonPathEntryIsArray(), fix XXX comment about JsonPathEntry.len
1 parent f3f3fba commit 4a6a004

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/backend/utils/adt/jsonb_typanalyze.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,20 @@ typedef struct JsonPathEntry JsonPathEntry;
7878
* Element of a path in the JSON document (i.e. not jsonpath). Elements
7979
* are linked together to build longer paths.
8080
*
81-
* XXX We need entry+lenth because JSON path elements may contain null
82-
* bytes, I guess?
81+
* 'entry' can be not zero-terminated when it is pointing to JSONB keys, so
82+
* 'len' is necessary. 'len' is also used for faster entry comparison, to
83+
* distinguish array entries ('len' == -1).
8384
*/
8485
typedef struct JsonPathEntry
8586
{
8687
JsonPathEntry *parent;
8788
const char *entry; /* element of the path as a string */
88-
int len; /* length of entry string (may be 0) */
89+
int len; /* length of entry string (may be 0 or -1) */
8990
uint32 hash; /* hash of the whole path (with parent) */
9091
} JsonPathEntry;
9192

93+
#define JsonPathEntryIsArray(entry) ((entry)->len == -1)
94+
9295
/* An array containing a dynamic number of JSON values. */
9396
typedef struct JsonValues
9497
{
@@ -518,7 +521,7 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
518521
if (type != jbvBinary)
519522
return;
520523

521-
if (entry->len == -1)
524+
if (JsonPathEntryIsArray(entry))
522525
{
523526
JsonbIterator *it;
524527
JsonbIteratorToken r;
@@ -817,7 +820,7 @@ jsonAnalyzeBuildPathStats(JsonPathAnlStats *pstats)
817820
if (pstats->vstats.lens.values.count)
818821
jsonAnalyzeMakeScalarStats(&ps, "length", &vstats->lens.stats);
819822

820-
if (pstats->path.len == -1)
823+
if (JsonPathEntryIsArray(&pstats->path))
821824
{
822825
JsonPathAnlStats *parent = (JsonPathAnlStats *) pstats->path.parent;
823826

@@ -854,10 +857,10 @@ jsonAnalyzeCalcPathFreq(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats)
854857

855858
if (parent)
856859
{
857-
pstats->freq = parent->freq *
858-
(pstats->path.len == -1 ? parent->vstats.narrays
859-
: pstats->vstats.jsons.values.count) /
860-
parent->vstats.jsons.values.count;
860+
int count = JsonPathEntryIsArray(&pstats->path) ?
861+
parent->vstats.narrays : pstats->vstats.jsons.values.count;
862+
863+
pstats->freq = parent->freq * count / parent->vstats.jsons.values.count;
861864

862865
CLAMP_PROBABILITY(pstats->freq);
863866
}

0 commit comments

Comments
 (0)