Skip to content

Commit 6587780

Browse files
author
Daniil Anisimov
committed
Fix collecting eclasses routine.
1 parent 33a64e0 commit 6587780

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
@@ -641,18 +641,28 @@ get_clauselist_args(List *clauselist, int *nargs, int **args_hash)
641641
*args_hash = repalloc(*args_hash, (*nargs) * sizeof(**args_hash));
642642
p_sorted = repalloc(p_sorted, (*nargs) * sizeof(*p_sorted));
643643

644-
/* Compress the values of eclasses */
644+
/*
645+
* Compress the values of eclasses.
646+
* It is only sorted in order of args_hash.
647+
* Get the indexes in ascending order of the elements.
648+
*/
649+
idx = argsort(p_sorted, *nargs, sizeof(*p_sorted), int_cmp);
650+
651+
/*
652+
* Remove the holes from given array.
653+
* Later we can use it as indexes of args_hash.
654+
*/
645655
if (*nargs > 0)
646656
{
647-
int prev = p_sorted[0];
648-
p_sorted[0] = 0;
657+
int prev = p_sorted[idx[0]];
658+
p_sorted[idx[0]] = 0;
649659
for (i = 1; i < *nargs; i++)
650660
{
651-
int cur = p_sorted[i];
661+
int cur = p_sorted[idx[i]];
652662
if (cur == prev)
653-
p_sorted[i] = p_sorted[i-1];
663+
p_sorted[idx[i]] = p_sorted[idx[i-1]];
654664
else
655-
p_sorted[i] = p_sorted[i-1] + 1;
665+
p_sorted[idx[i]] = p_sorted[idx[i-1]] + 1;
656666
prev = cur;
657667
}
658668
}

path_utils.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ static AQOPlanNode DefaultAQOPlanNode =
5252
.prediction = -1.
5353
};
5454

55-
5655
/*
5756
* Auxiliary list for relabel equivalence classes
5857
* from pointers to the serial numbers - indexes of this list.
59-
* Maybe it's need to use some smart data structure such a HTAB?
58+
* XXX: Maybe it's need to use some smart data structure such a HTAB?
59+
* It must be allocated in AQOCacheMemCtx.
6060
*/
61-
List *eclass_collector = NIL;
61+
List *aqo_eclass_collector = NIL;
6262

6363
/*
6464
* Hook on creation of a plan node. We need to store AQO-specific data to
@@ -345,13 +345,6 @@ aqo_get_raw_clauses(PlannerInfo *root, List *restrictlist)
345345
return clauses;
346346
}
347347

348-
void
349-
eclass_collector_free(void)
350-
{
351-
list_free(eclass_collector);
352-
eclass_collector = NIL;
353-
}
354-
355348
static int
356349
get_eclass_index(EquivalenceClass *ec)
357350
{
@@ -366,16 +359,16 @@ get_eclass_index(EquivalenceClass *ec)
366359
while(ec->ec_merged)
367360
ec = ec->ec_merged;
368361

369-
foreach (lc, eclass_collector)
362+
foreach (lc, aqo_eclass_collector)
370363
{
371364
if (lfirst(lc) == ec)
372365
break;
373366
i++;
374367
}
375368

376369
old_ctx = MemoryContextSwitchTo(AQOCacheMemCtx);
377-
if (i == list_length(eclass_collector))
378-
eclass_collector = lappend(eclass_collector, ec);
370+
if (i == list_length(aqo_eclass_collector))
371+
aqo_eclass_collector = lappend(aqo_eclass_collector, ec);
379372
MemoryContextSwitchTo(old_ctx);
380373

381374
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)