@@ -78,17 +78,20 @@ typedef struct JsonPathEntry JsonPathEntry;
78
78
* Element of a path in the JSON document (i.e. not jsonpath). Elements
79
79
* are linked together to build longer paths.
80
80
*
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).
83
84
*/
84
85
typedef struct JsonPathEntry
85
86
{
86
87
JsonPathEntry * parent ;
87
88
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 ) */
89
90
uint32 hash ; /* hash of the whole path (with parent) */
90
91
} JsonPathEntry ;
91
92
93
+ #define JsonPathEntryIsArray (entry ) ((entry)->len == -1)
94
+
92
95
/* An array containing a dynamic number of JSON values. */
93
96
typedef struct JsonValues
94
97
{
@@ -518,7 +521,7 @@ jsonAnalyzeCollectSubpath(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats,
518
521
if (type != jbvBinary )
519
522
return ;
520
523
521
- if (entry -> len == -1 )
524
+ if (JsonPathEntryIsArray ( entry ) )
522
525
{
523
526
JsonbIterator * it ;
524
527
JsonbIteratorToken r ;
@@ -817,7 +820,7 @@ jsonAnalyzeBuildPathStats(JsonPathAnlStats *pstats)
817
820
if (pstats -> vstats .lens .values .count )
818
821
jsonAnalyzeMakeScalarStats (& ps , "length" , & vstats -> lens .stats );
819
822
820
- if (pstats -> path . len == -1 )
823
+ if (JsonPathEntryIsArray ( & pstats -> path ) )
821
824
{
822
825
JsonPathAnlStats * parent = (JsonPathAnlStats * ) pstats -> path .parent ;
823
826
@@ -854,10 +857,10 @@ jsonAnalyzeCalcPathFreq(JsonAnalyzeContext *ctx, JsonPathAnlStats *pstats)
854
857
855
858
if (parent )
856
859
{
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 ;
861
864
862
865
CLAMP_PROBABILITY (pstats -> freq );
863
866
}
0 commit comments