Skip to content

Commit 549115e

Browse files
author
Daniil Anisimov
committedFeb 20, 2024
Fix collecting eclasses routine.
1 parent 86ac7f5 commit 549115e

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed
 

‎aqo.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ aqo_free_callback(ResourceReleasePhase phase,
118118
{
119119
MemoryContextReset(AQOCacheMemCtx);
120120
cur_classes = NIL;
121+
aqo_eclass_collector = NIL;
121122
}
122123
}
123124

‎hash.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -623,18 +623,28 @@ get_clauselist_args(List *clauselist, int *nargs, int **args_hash)
623623
*args_hash = repalloc(*args_hash, (*nargs) * sizeof(**args_hash));
624624
p_sorted = repalloc(p_sorted, (*nargs) * sizeof(*p_sorted));
625625

626-
/* Compress the values of eclasses */
626+
/*
627+
* Compress the values of eclasses.
628+
* It is only sorted in order of args_hash.
629+
* Get the indexes in ascending order of the elements.
630+
*/
631+
idx = argsort(p_sorted, *nargs, sizeof(*p_sorted), int_cmp);
632+
633+
/*
634+
* Remove the holes from given array.
635+
* Later we can use it as indexes of args_hash.
636+
*/
627637
if (*nargs > 0)
628638
{
629-
int prev = p_sorted[0];
630-
p_sorted[0] = 0;
639+
int prev = p_sorted[idx[0]];
640+
p_sorted[idx[0]] = 0;
631641
for (i = 1; i < *nargs; i++)
632642
{
633-
int cur = p_sorted[i];
643+
int cur = p_sorted[idx[i]];
634644
if (cur == prev)
635-
p_sorted[i] = p_sorted[i-1];
645+
p_sorted[idx[i]] = p_sorted[idx[i-1]];
636646
else
637-
p_sorted[i] = p_sorted[i-1] + 1;
647+
p_sorted[idx[i]] = p_sorted[idx[i-1]] + 1;
638648
prev = cur;
639649
}
640650
}

‎path_utils.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ static AQOPlanNode DefaultAQOPlanNode =
4747
.prediction = -1.
4848
};
4949

50-
5150
/*
5251
* Auxiliary list for relabel equivalence classes
5352
* from pointers to the serial numbers - indexes of this list.
54-
* Maybe it's need to use some smart data structure such a HTAB?
53+
* XXX: Maybe it's need to use some smart data structure such a HTAB?
54+
* It must be allocated in AQOCacheMemCtx.
5555
*/
56-
List *eclass_collector = NIL;
56+
List *aqo_eclass_collector = NIL;
5757

5858
/*
5959
* Hook on creation of a plan node. We need to store AQO-specific data to
@@ -340,13 +340,6 @@ aqo_get_raw_clauses(PlannerInfo *root, List *restrictlist)
340340
return clauses;
341341
}
342342

343-
void
344-
eclass_collector_free(void)
345-
{
346-
list_free(eclass_collector);
347-
eclass_collector = NIL;
348-
}
349-
350343
static int
351344
get_eclass_index(EquivalenceClass *ec)
352345
{
@@ -361,16 +354,16 @@ get_eclass_index(EquivalenceClass *ec)
361354
while(ec->ec_merged)
362355
ec = ec->ec_merged;
363356

364-
foreach (lc, eclass_collector)
357+
foreach (lc, aqo_eclass_collector)
365358
{
366359
if (lfirst(lc) == ec)
367360
break;
368361
i++;
369362
}
370363

371364
old_ctx = MemoryContextSwitchTo(AQOCacheMemCtx);
372-
if (i == list_length(eclass_collector))
373-
eclass_collector = lappend(eclass_collector, ec);
365+
if (i == list_length(aqo_eclass_collector))
366+
aqo_eclass_collector = lappend(aqo_eclass_collector, ec);
374367
MemoryContextSwitchTo(old_ctx);
375368

376369
return i;

‎path_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define AQO_PLAN_NODE "AQOPlanNode"
99
#define AQO_CONST_NODE "AQOConstNode"
1010

11+
extern List *aqo_eclass_collector;
12+
1113
/*
1214
* Find and sort out relations that used in the query:
1315
* Use oids of relations to store dependency of ML row on a set of tables.
@@ -114,6 +116,5 @@ extern void RegisterAQOPlanNodeMethods(void);
114116
extern List *aqo_get_clauses(PlannerInfo *root, List *restrictlist);
115117

116118
void aqo_path_utils_init(void);
117-
void eclass_collector_free(void);
118119

119120
#endif /* PATH_UTILS_H */

‎postprocessing.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,6 @@ aqo_ExecutorEnd(QueryDesc *queryDesc)
848848
end:
849849
/* Release all AQO-specific memory, allocated during learning procedure */
850850
selectivity_cache_clear();
851-
eclass_collector_free();
852851
MemoryContextSwitchTo(oldctx);
853852
MemoryContextReset(AQOLearnMemCtx);
854853

0 commit comments

Comments
 (0)
Please sign in to comment.