Skip to content

Commit

Permalink
Fix testing with WRITE_READ_PARSE_PLAN_TREES.
Browse files Browse the repository at this point in the history
Change RestrictInfo to AQOClause.
Add AQOConstNode to use it instead of useless nodes.
Serialize/deserialize all AQOPlanNode and AQOConstNode fields.
  • Loading branch information
Daniil Anisimov committed Jan 22, 2024
1 parent 52d586e commit a3e8286
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 141 deletions.
66 changes: 31 additions & 35 deletions aqo_pg13.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/contrib/Makefile b/contrib/Makefile
index 1846d415b6..95519ac11d 100644
index 1846d415b6f..95519ac11de 100644
--- a/contrib/Makefile
+++ b/contrib/Makefile
@@ -7,6 +7,7 @@ include $(top_builddir)/src/Makefile.global
Expand All @@ -11,7 +11,7 @@ index 1846d415b6..95519ac11d 100644
auto_explain \
bloom \
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index bc05c96b4c..b6a3abe0d2 100644
index bc05c96b4ce..b6a3abe0d2b 100644
--- a/src/backend/commands/explain.c
+++ b/src/backend/commands/explain.c
@@ -24,6 +24,7 @@
Expand Down Expand Up @@ -57,7 +57,7 @@ index bc05c96b4c..b6a3abe0d2 100644
if (es->format == EXPLAIN_FORMAT_TEXT)
appendStringInfoChar(es->str, '\n');
diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c
index 692b6c1559..580d04d784 100644
index 692b6c1559f..580d04d7844 100644
--- a/src/backend/nodes/copyfuncs.c
+++ b/src/backend/nodes/copyfuncs.c
@@ -132,6 +132,7 @@ CopyPlanFields(const Plan *from, Plan *newnode)
Expand All @@ -69,35 +69,31 @@ index 692b6c1559..580d04d784 100644

/*
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index 21ececf0c2..a0e7a7ebca 100644
index 21ececf0c2f..ebfd3ba86de 100644
--- a/src/backend/nodes/outfuncs.c
+++ b/src/backend/nodes/outfuncs.c
@@ -342,6 +342,7 @@ _outPlanInfo(StringInfo str, const Plan *node)
WRITE_NODE_FIELD(initPlan);
WRITE_BITMAPSET_FIELD(extParam);
WRITE_BITMAPSET_FIELD(allParam);
+ /*WRITE_NODE_FIELD(ext_nodes); */
+ WRITE_NODE_FIELD(ext_nodes);
}

/*
diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c
index 7976b369ba..604314e0b3 100644
index 7976b369ba8..2e47bd8d950 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -1580,6 +1580,11 @@ ReadCommonPlan(Plan *local_node)
@@ -1580,6 +1580,7 @@ ReadCommonPlan(Plan *local_node)
READ_NODE_FIELD(initPlan);
READ_BITMAPSET_FIELD(extParam);
READ_BITMAPSET_FIELD(allParam);
+ local_node->ext_nodes = NIL;
+ /* READ_NODE_FIELD(ext_nodes);
+ * Don't serialize this field. It is required to serialize RestrictInfo and
+ * EqualenceClass.
+ */
+ READ_NODE_FIELD(ext_nodes);
}

/*
diff --git a/src/backend/optimizer/path/costsize.c b/src/backend/optimizer/path/costsize.c
index 4edc859cb5..988f2e6ab7 100644
index 4edc859cb57..988f2e6ab75 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -98,6 +98,12 @@
Expand Down Expand Up @@ -363,7 +359,7 @@ index 4edc859cb5..988f2e6ab7 100644
{
double parallel_divisor = path->parallel_workers;
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 917713c163..5b7bf1cec6 100644
index 917713c1633..5b7bf1cec69 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -70,6 +70,7 @@
Expand Down Expand Up @@ -394,7 +390,7 @@ index 917713c163..5b7bf1cec6 100644

/*
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 27c665ac12..f599fba755 100644
index 821693c60ee..fa627f472f9 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -145,7 +145,8 @@ static List *extract_rollup_sets(List *groupingSets);
Expand All @@ -407,7 +403,7 @@ index 27c665ac12..f599fba755 100644
grouping_sets_data *gd,
List *target_list);
static RelOptInfo *create_grouping_paths(PlannerInfo *root,
@@ -3686,7 +3687,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
@@ -3682,7 +3683,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
*/
static double
get_number_of_groups(PlannerInfo *root,
Expand All @@ -417,7 +413,7 @@ index 27c665ac12..f599fba755 100644
grouping_sets_data *gd,
List *target_list)
{
@@ -3723,7 +3725,7 @@ get_number_of_groups(PlannerInfo *root,
@@ -3719,7 +3721,7 @@ get_number_of_groups(PlannerInfo *root,
GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
double numGroups = estimate_num_groups(root,
groupExprs,
Expand All @@ -426,7 +422,7 @@ index 27c665ac12..f599fba755 100644
&gset);

gs->numGroups = numGroups;
@@ -3748,7 +3750,7 @@ get_number_of_groups(PlannerInfo *root,
@@ -3744,7 +3746,7 @@ get_number_of_groups(PlannerInfo *root,
GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
double numGroups = estimate_num_groups(root,
groupExprs,
Expand All @@ -435,7 +431,7 @@ index 27c665ac12..f599fba755 100644
&gset);

gs->numGroups = numGroups;
@@ -3764,8 +3766,8 @@ get_number_of_groups(PlannerInfo *root,
@@ -3760,8 +3762,8 @@ get_number_of_groups(PlannerInfo *root,
groupExprs = get_sortgrouplist_exprs(parse->groupClause,
target_list);

Expand All @@ -446,7 +442,7 @@ index 27c665ac12..f599fba755 100644
}
}
else if (parse->groupingSets)
@@ -4151,7 +4153,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
@@ -4147,7 +4149,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
* Estimate number of groups.
*/
dNumGroups = get_number_of_groups(root,
Expand All @@ -456,7 +452,7 @@ index 27c665ac12..f599fba755 100644
gd,
extra->targetList);

@@ -6935,13 +6938,15 @@ create_partial_grouping_paths(PlannerInfo *root,
@@ -6931,13 +6934,15 @@ create_partial_grouping_paths(PlannerInfo *root,
if (cheapest_total_path != NULL)
dNumPartialGroups =
get_number_of_groups(root,
Expand All @@ -475,7 +471,7 @@ index 27c665ac12..f599fba755 100644
extra->targetList);

diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index a203e6f1ff..d31bf5bae6 100644
index a203e6f1ff5..d31bf5bae63 100644
--- a/src/backend/optimizer/util/relnode.c
+++ b/src/backend/optimizer/util/relnode.c
@@ -258,6 +258,7 @@ build_simple_rel(PlannerInfo *root, int relid, RelOptInfo *parent)
Expand Down Expand Up @@ -541,7 +537,7 @@ index a203e6f1ff..d31bf5bae6 100644

return ppi;
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 37458da096..248a1875a1 100644
index 37458da096d..248a1875a18 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -147,6 +147,7 @@
Expand Down Expand Up @@ -573,7 +569,7 @@ index 37458da096..248a1875a1 100644
* estimate_num_groups - Estimate number of groups in a grouped query
*
diff --git a/src/include/commands/explain.h b/src/include/commands/explain.h
index ba661d32a6..09d0abe58b 100644
index ba661d32a63..09d0abe58be 100644
--- a/src/include/commands/explain.h
+++ b/src/include/commands/explain.h
@@ -75,6 +75,18 @@ extern PGDLLIMPORT ExplainOneQuery_hook_type ExplainOneQuery_hook;
Expand All @@ -596,7 +592,7 @@ index ba661d32a6..09d0abe58b 100644
extern void ExplainQuery(ParseState *pstate, ExplainStmt *stmt,
ParamListInfo params, DestReceiver *dest);
diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h
index 5ebf070979..5b2acd7de2 100644
index d2b4271de9d..559b9db7121 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -739,6 +739,10 @@ typedef struct RelOptInfo
Expand Down Expand Up @@ -635,7 +631,7 @@ index 5ebf070979..5b2acd7de2 100644


diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 90f02ce6fd..88c332164d 100644
index 90f02ce6fdd..88c332164dd 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -159,6 +159,9 @@ typedef struct Plan
Expand All @@ -649,7 +645,7 @@ index 90f02ce6fd..88c332164d 100644

/* ----------------
diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
index 6141654e47..e6b28cbb05 100644
index 6141654e478..e6b28cbb05f 100644
--- a/src/include/optimizer/cost.h
+++ b/src/include/optimizer/cost.h
@@ -39,6 +39,37 @@ typedef enum
Expand Down Expand Up @@ -733,7 +729,7 @@ index 6141654e47..e6b28cbb05 100644

#endif /* COST_H */
diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h
index 3bd7072ae8..21bbaba11c 100644
index 3bd7072ae8c..21bbaba11c8 100644
--- a/src/include/optimizer/pathnode.h
+++ b/src/include/optimizer/pathnode.h
@@ -18,6 +18,10 @@
Expand All @@ -748,7 +744,7 @@ index 3bd7072ae8..21bbaba11c 100644
* prototypes for pathnode.c
*/
diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h
index 8ce60e202e..75415102c2 100644
index 8ce60e202e5..75415102c2e 100644
--- a/src/include/optimizer/planmain.h
+++ b/src/include/optimizer/planmain.h
@@ -24,6 +24,12 @@ extern double cursor_tuple_fraction;
Expand All @@ -765,7 +761,7 @@ index 8ce60e202e..75415102c2 100644
* prototypes for plan/planmain.c
*/
diff --git a/src/include/utils/selfuncs.h b/src/include/utils/selfuncs.h
index 7ac4a06391..def3522881 100644
index 7ac4a063915..74fe91b89f1 100644
--- a/src/include/utils/selfuncs.h
+++ b/src/include/utils/selfuncs.h
@@ -127,6 +127,12 @@ typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
Expand All @@ -781,13 +777,13 @@ index 7ac4a06391..def3522881 100644

/* Functions in selfuncs.c */

@@ -195,6 +201,9 @@ extern void mergejoinscansel(PlannerInfo *root, Node *clause,
@@ -193,6 +199,9 @@ extern void mergejoinscansel(PlannerInfo *root, Node *clause,
Selectivity *leftstart, Selectivity *leftend,
Selectivity *rightstart, Selectivity *rightend);

extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
double input_rows, List **pgset);
+extern double estimate_num_groups_ext(PlannerInfo *root, List *groupExprs,
+ Path *subpath, RelOptInfo *grouped_rel,
+ List **pgset);
extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
double input_rows, List **pgset);

extern void estimate_hash_bucket_stats(PlannerInfo *root,
Node *hashkey, double nbuckets,
3 changes: 1 addition & 2 deletions cardinality_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ aqo_get_parameterized_baserel_size(PlannerInfo *root,

forboth(l, allclauses, l2, selectivities)
{
current_hash = get_clause_hash(
((RestrictInfo *) lfirst(l))->clause,
current_hash = get_clause_hash(((AQOClause *) lfirst(l))->clause,
nargs, args_hash, eclass_hash);
cache_selectivity(current_hash, rel->relid, rte->relid,
*((double *) lfirst(l2)));
Expand Down
33 changes: 17 additions & 16 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "aqo.h"
#include "hash.h"
#include "path_utils.h"

static int get_str_hash(const char *str);
static int get_node_hash(Node *node);
Expand Down Expand Up @@ -236,11 +237,11 @@ get_fss_for_object(List *relsigns, List *clauselist,
i = 0;
foreach(lc, clauselist)
{
RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
AQOClause *clause = (AQOClause *) lfirst(lc);

clause_hashes[i] = get_clause_hash(rinfo->clause,
clause_hashes[i] = get_clause_hash(clause->clause,
nargs, args_hash, eclass_hash);
args = get_clause_args_ptr(rinfo->clause);
args = get_clause_args_ptr(clause->clause);
clause_has_consts[i] = (args != NULL && has_consts(*args));
i++;
}
Expand Down Expand Up @@ -335,14 +336,14 @@ get_clause_hash(Expr *clause, int nargs, int *args_hash, int *eclass_hash)

cclause = copyObject(clause);
args = get_clause_args_ptr(cclause);
/* XXX: Why does it work even if this loop is removed? */
foreach(l, *args)
{
arg_eclass = get_arg_eclass(get_node_hash(lfirst(l)),
nargs, args_hash, eclass_hash);
if (arg_eclass != 0)
{
lfirst(l) = makeNode(Param);
((Param *) lfirst(l))->paramid = arg_eclass;
lfirst(l) = create_aqo_const_node(AQO_NODE_EXPR, arg_eclass);
}
}
if (!clause_is_eq_clause(clause) || has_consts(*args))
Expand Down Expand Up @@ -572,7 +573,7 @@ get_arg_eclass(int arg_hash, int nargs, int *args_hash, int *eclass_hash)
static void
get_clauselist_args(List *clauselist, int *nargs, int **args_hash)
{
RestrictInfo *rinfo;
AQOClause *clause;
List **args;
ListCell *l;
ListCell *l2;
Expand All @@ -582,9 +583,9 @@ get_clauselist_args(List *clauselist, int *nargs, int **args_hash)

foreach(l, clauselist)
{
rinfo = (RestrictInfo *) lfirst(l);
args = get_clause_args_ptr(rinfo->clause);
if (args != NULL && clause_is_eq_clause(rinfo->clause))
clause = (AQOClause *) lfirst(l);
args = get_clause_args_ptr(clause->clause);
if (args != NULL && clause_is_eq_clause(clause->clause))
foreach(l2, *args)
if (!IsA(lfirst(l2), Const))
cnt++;
Expand All @@ -593,9 +594,9 @@ get_clauselist_args(List *clauselist, int *nargs, int **args_hash)
*args_hash = palloc(cnt * sizeof(**args_hash));
foreach(l, clauselist)
{
rinfo = (RestrictInfo *) lfirst(l);
args = get_clause_args_ptr(rinfo->clause);
if (args != NULL && clause_is_eq_clause(rinfo->clause))
clause = (AQOClause *) lfirst(l);
args = get_clause_args_ptr(clause->clause);
if (args != NULL && clause_is_eq_clause(clause->clause))
foreach(l2, *args)
if (!IsA(lfirst(l2), Const))
(*args_hash)[i++] = get_node_hash(lfirst(l2));
Expand Down Expand Up @@ -650,7 +651,7 @@ disjoint_set_merge_eclasses(int *p, int v1, int v2)
static int *
perform_eclasses_join(List *clauselist, int nargs, int *args_hash)
{
RestrictInfo *rinfo;
AQOClause *clause;
int *p;
ListCell *l,
*l2;
Expand All @@ -664,9 +665,9 @@ perform_eclasses_join(List *clauselist, int nargs, int *args_hash)

foreach(l, clauselist)
{
rinfo = (RestrictInfo *) lfirst(l);
args = get_clause_args_ptr(rinfo->clause);
if (args != NULL && clause_is_eq_clause(rinfo->clause))
clause = (AQOClause *) lfirst(l);
args = get_clause_args_ptr(clause->clause);
if (args != NULL && clause_is_eq_clause(clause->clause))
{
i3 = -1;
foreach(l2, *args)
Expand Down
Loading

0 comments on commit a3e8286

Please sign in to comment.