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

Commit 033b18a

Browse files
author
Nikita Glukhov
committed
Refactor jsonAnalyzeSortPaths() to return paths
1 parent b1e77d0 commit 033b18a

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/backend/utils/adt/jsonb_typanalyze.c

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,6 @@ typedef struct JsonAnalyzeContext
169169
AnalyzeAttrFetchFunc fetchfunc;
170170
HTAB *pathshash;
171171
JsonPathAnlStats *root;
172-
JsonPathAnlStats **paths;
173-
int npaths;
174172
double totalrows;
175173
double total_width;
176174
int samplerows;
@@ -929,30 +927,29 @@ JsonPathStatsCompare(const void *pv1, const void *pv2)
929927
/*
930928
* jsonAnalyzeSortPaths
931929
* Reads all stats stored in the hash table and sorts them.
932-
*
933-
* XXX It's a bit strange we simply store the result in the context instead
934-
* of just returning it.
935930
*/
936-
static void
937-
jsonAnalyzeSortPaths(JsonAnalyzeContext *ctx)
931+
static JsonPathAnlStats **
932+
jsonAnalyzeSortPaths(JsonAnalyzeContext *ctx, int *p_npaths)
938933
{
939-
HASH_SEQ_STATUS hseq;
940-
JsonPathAnlStats *path;
941-
int i;
934+
HASH_SEQ_STATUS hseq;
935+
JsonPathAnlStats *path;
936+
JsonPathAnlStats **paths;
937+
int npaths;
942938

943-
ctx->npaths = hash_get_num_entries(ctx->pathshash) + 1;
944-
ctx->paths = MemoryContextAlloc(ctx->mcxt,
945-
sizeof(*ctx->paths) * ctx->npaths);
939+
npaths = hash_get_num_entries(ctx->pathshash) + 1;
940+
paths = MemoryContextAlloc(ctx->mcxt, sizeof(*paths) * npaths);
946941

947-
ctx->paths[0] = ctx->root;
942+
paths[0] = ctx->root;
948943

949944
hash_seq_init(&hseq, ctx->pathshash);
950945

951-
for (i = 1; (path = hash_seq_search(&hseq)); i++)
952-
ctx->paths[i] = path;
946+
for (int i = 1; (path = hash_seq_search(&hseq)) != NULL; i++)
947+
paths[i] = path;
953948

954-
pg_qsort(ctx->paths, ctx->npaths, sizeof(*ctx->paths),
955-
JsonPathStatsCompare);
949+
pg_qsort(paths, npaths, sizeof(*paths), JsonPathStatsCompare);
950+
951+
*p_npaths = npaths;
952+
return paths;
956953
}
957954

958955
/*
@@ -986,12 +983,13 @@ jsonAnalyzeBuildPathStatsArray(JsonPathAnlStats **paths, int npaths, int *nvals,
986983
* ???
987984
*/
988985
static Datum *
989-
jsonAnalyzeMakeStats(JsonAnalyzeContext *ctx, int *numvalues)
986+
jsonAnalyzeMakeStats(JsonAnalyzeContext *ctx, JsonPathAnlStats **paths,
987+
int npaths, int *numvalues)
990988
{
991989
Datum *values;
992990
MemoryContext oldcxt = MemoryContextSwitchTo(ctx->stats->anl_context);
993991

994-
values = jsonAnalyzeBuildPathStatsArray(ctx->paths, ctx->npaths, numvalues,
992+
values = jsonAnalyzeBuildPathStatsArray(paths, npaths, numvalues,
995993
JSON_PATH_ROOT, JSON_PATH_ROOT_LEN);
996994

997995
MemoryContextSwitchTo(oldcxt);
@@ -1175,6 +1173,8 @@ compute_json_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
11751173
int samplerows, double totalrows)
11761174
{
11771175
JsonAnalyzeContext ctx;
1176+
JsonPathAnlStats **paths;
1177+
int npaths;
11781178
bool sigle_pass = false; /* FIXME make GUC or simply remove */
11791179

11801180
jsonAnalyzeInit(&ctx, stats, fetchfunc, samplerows, totalrows);
@@ -1196,14 +1196,13 @@ compute_json_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
11961196
* XXX I wonder if we could do this in two phases, to maybe not collect
11971197
* (or even accumulate) values for paths that are not interesting.
11981198
*/
1199-
jsonAnalyzeSortPaths(&ctx);
1199+
paths = jsonAnalyzeSortPaths(&ctx, &npaths);
12001200

1201-
for (int i = 0; i < ctx.npaths; i++)
1202-
jsonAnalyzePath(&ctx, ctx.paths[i]);
1201+
for (int i = 0; i < npaths; i++)
1202+
jsonAnalyzePath(&ctx, paths[i]);
12031203
}
12041204
else
12051205
{
1206-
int i;
12071206
MemoryContext oldcxt;
12081207
MemoryContext tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
12091208
"Json Analyze Tmp Context",
@@ -1221,20 +1220,20 @@ compute_json_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
12211220

12221221
/* Collect all paths first without accumulating any Values, sort them */
12231222
jsonAnalyzePass(&ctx, jsonAnalyzeCollectPaths, (void *)(intptr_t) false);
1224-
jsonAnalyzeSortPaths(&ctx);
1223+
paths = jsonAnalyzeSortPaths(&ctx, &npaths);
12251224

12261225
/*
12271226
* Next, process each path independently to save memory (we don't want
12281227
* to accumulate all values for all paths, with a lot of duplicities).
12291228
*/
12301229
MemoryContextReset(tmpcxt);
12311230

1232-
for (i = 0; i < ctx.npaths; i++)
1231+
for (int i = 0; i < npaths; i++)
12331232
{
1234-
JsonPathAnlStats *path = ctx.paths[i];
1233+
JsonPathAnlStats *path = paths[i];
12351234

12361235
elog(DEBUG1, "analyzing json path (%d/%d) %s",
1237-
i + 1, ctx.npaths, path->pathstr);
1236+
i + 1, npaths, path->pathstr);
12381237

12391238
jsonAnalyzePass(&ctx, jsonAnalyzeCollectPath, path);
12401239
jsonAnalyzePath(&ctx, path);
@@ -1315,7 +1314,8 @@ compute_json_stats(VacAttrStats *stats, AnalyzeAttrFetchFunc fetchfunc,
13151314
sizeof(float4));
13161315
stats->stanumbers[empty_slot][0] = 0.0; /* nullfrac */
13171316
stats->stavalues[empty_slot] =
1318-
jsonAnalyzeMakeStats(&ctx, &stats->numvalues[empty_slot]);
1317+
jsonAnalyzeMakeStats(&ctx, paths, npaths,
1318+
&stats->numvalues[empty_slot]);
13191319

13201320
/* We are storing jsonb values */
13211321
stats->statypid[empty_slot] = JSONBOID;

0 commit comments

Comments
 (0)