Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b7efe19

Browse files
committed
Replace aqo_data table with shmem hash table + DSA + file storage.
1 parent b7374f1 commit b7efe19

File tree

5 files changed

+48
-30
lines changed

5 files changed

+48
-30
lines changed

cardinality_estimation.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ predict_for_relation(List *clauses, List *selectivities, List *relsigns,
6464
{
6565
double *features;
6666
double result;
67-
int i;
68-
OkNNrdata data;
67+
int ncols;
68+
OkNNrdata *data;
6969

7070
if (relsigns == NIL)
7171
/*
@@ -75,14 +75,11 @@ predict_for_relation(List *clauses, List *selectivities, List *relsigns,
7575
return -4.;
7676

7777
*fss = get_fss_for_object(relsigns, clauses, selectivities,
78-
&data.cols, &features);
78+
&ncols, &features);
79+
data = OkNNr_allocate(ncols);
7980

80-
if (data.cols > 0)
81-
for (i = 0; i < aqo_K; ++i)
82-
data.matrix[i] = palloc0(sizeof(double) * data.cols);
83-
84-
if (load_fss_ext(query_context.fspace_hash, *fss, &data, NULL, true))
85-
result = OkNNr_predict(&data, features);
81+
if (load_fss_ext(query_context.fspace_hash, *fss, data, NULL, true))
82+
result = OkNNr_predict(data, features);
8683
else
8784
{
8885
/*
@@ -93,25 +90,21 @@ predict_for_relation(List *clauses, List *selectivities, List *relsigns,
9390
*/
9491

9592
/* Try to search in surrounding feature spaces for the same node */
96-
if (!load_fss(query_context.fspace_hash, *fss, &data, NULL, false))
93+
if (!load_aqo_data(query_context.fspace_hash, *fss, data, NULL, true))
9794
result = -1;
9895
else
9996
{
10097
elog(DEBUG5, "[AQO] Make prediction for fss %d by a neighbour "
10198
"includes %d feature(s) and %d fact(s).",
102-
*fss, data.cols, data.rows);
103-
result = OkNNr_predict(&data, features);
99+
*fss, data->cols, data->rows);
100+
result = OkNNr_predict(data, features);
104101
}
105102
}
106103
#ifdef AQO_DEBUG_PRINT
107104
predict_debug_output(clauses, selectivities, relsigns, *fss, result);
108105
#endif
109106
pfree(features);
110-
if (data.cols > 0)
111-
{
112-
for (i = 0; i < aqo_K; ++i)
113-
pfree(data.matrix[i]);
114-
}
107+
OkNNr_free(data);
115108

116109
if (result < 0)
117110
return -1;

machine_learning.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,33 @@ static double fs_similarity(double dist);
4141
static double compute_weights(double *distances, int nrows, double *w, int *idx);
4242

4343

44+
OkNNrdata*
45+
OkNNr_allocate(int ncols)
46+
{
47+
OkNNrdata *data = palloc(sizeof(OkNNrdata));
48+
int i;
49+
50+
if (ncols > 0)
51+
for (i = 0; i < aqo_K; ++i)
52+
data->matrix[i] = palloc0(sizeof(double) * ncols);
53+
54+
data->cols = ncols;
55+
return data;
56+
}
57+
58+
void
59+
OkNNr_free(OkNNrdata *data)
60+
{
61+
int i;
62+
63+
if (data->cols > 0)
64+
{
65+
for (i = 0; i < aqo_K; ++i)
66+
pfree(data->matrix[i]);
67+
}
68+
pfree(data);
69+
}
70+
4471
/*
4572
* Computes L2-distance between two given vectors.
4673
*/

machine_learning.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ typedef struct OkNNrdata
2121
double rfactors[aqo_K];
2222
} OkNNrdata;
2323

24+
extern OkNNrdata* OkNNr_allocate(int ncols);
25+
extern void OkNNr_free(OkNNrdata *data);
26+
2427
/* Machine learning techniques */
2528
extern double OkNNr_predict(OkNNrdata *data, double *features);
2629
extern int OkNNr_learn(OkNNrdata *data,

postprocessing.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,13 @@ learn_sample(aqo_obj_stat *ctx, RelSortOut *rels,
146146
uint64 fs = query_context.fspace_hash;
147147
double *features;
148148
double target;
149-
OkNNrdata data;
149+
OkNNrdata *data;
150150
int fss;
151-
int i;
151+
int ncols;
152152

153-
memset(&data, 0, sizeof(OkNNrdata));
154153
target = log(learned);
155154
fss = get_fss_for_object(rels->signatures, ctx->clauselist,
156-
ctx->selectivities, &data.cols, &features);
155+
ctx->selectivities, &ncols, &features);
157156

158157
/* Only Agg nodes can have non-empty a grouping expressions list. */
159158
Assert(!IsA(plan, Agg) || aqo_node->grouping_exprs != NIL);
@@ -165,19 +164,14 @@ learn_sample(aqo_obj_stat *ctx, RelSortOut *rels,
165164
if (notExecuted && aqo_node->prediction > 0)
166165
return;
167166

168-
if (data.cols > 0)
169-
for (i = 0; i < aqo_K; ++i)
170-
data.matrix[i] = palloc(sizeof(double) * data.cols);
167+
data = OkNNr_allocate(ncols);
171168

172169
/* Critical section */
173-
atomic_fss_learn_step(fs, fss, &data, features, target, rfactor,
170+
atomic_fss_learn_step(fs, fss, data, features, target, rfactor,
174171
rels->hrels, ctx->isTimedOut);
175172
/* End of critical section */
176173

177-
if (data.cols > 0)
178-
for (i = 0; i < aqo_K; ++i)
179-
pfree(data.matrix[i]);
180-
174+
OkNNr_free(data);
181175
pfree(features);
182176
}
183177

storage.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ extern void aqo_qtexts_flush(void);
8888
extern void aqo_qtexts_load(void);
8989

9090
extern bool aqo_data_store(uint64 fs, int fss, OkNNrdata *data, List *reloids);
91-
extern bool load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids);
91+
extern bool load_aqo_data(uint64 fs, int fss, OkNNrdata *data, List **reloids,
92+
bool wideSearch);
9293
extern void aqo_data_flush(void);
9394
extern void aqo_data_load(void);
9495
/* Utility routines */

0 commit comments

Comments
 (0)