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 5d9c6aa commit 758257a
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 142 deletions.
70 changes: 33 additions & 37 deletions aqo_pg14.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/contrib/Makefile b/contrib/Makefile
index f27e458482..0c62191904 100644
index f27e458482e..0c621919045 100644
--- a/contrib/Makefile
+++ b/contrib/Makefile
@@ -7,6 +7,7 @@ include $(top_builddir)/src/Makefile.global
Expand All @@ -11,7 +11,7 @@ index f27e458482..0c62191904 100644
auto_explain \
bloom \
diff --git a/src/backend/commands/explain.c b/src/backend/commands/explain.c
index 70551522da..958529fbab 100644
index 70551522dac..958529fbab4 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 70551522da..958529fbab 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 4d9746d54a..6fa85d1c71 100644
index 4d9746d54a0..6fa85d1c71f 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 4d9746d54a..6fa85d1c71 100644

/*
diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c
index 58c2590698..1e06738a13 100644
index 58c2590698c..b9f39d36e03 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 eaa51c5c06..6ad8b78c7d 100644
index eaa51c5c062..65741a86a05 100644
--- a/src/backend/nodes/readfuncs.c
+++ b/src/backend/nodes/readfuncs.c
@@ -1628,6 +1628,11 @@ ReadCommonPlan(Plan *local_node)
@@ -1628,6 +1628,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 006f91f0a8..ef9c8ec581 100644
index 006f91f0a87..ef9c8ec5817 100644
--- a/src/backend/optimizer/path/costsize.c
+++ b/src/backend/optimizer/path/costsize.c
@@ -98,6 +98,11 @@
Expand Down Expand Up @@ -362,7 +358,7 @@ index 006f91f0a8..ef9c8ec581 100644
{
double parallel_divisor = path->parallel_workers;
diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c
index 0ed858f305..9d4a6c5903 100644
index 0ed858f305a..9d4a6c59030 100644
--- a/src/backend/optimizer/plan/createplan.c
+++ b/src/backend/optimizer/plan/createplan.c
@@ -71,6 +71,7 @@
Expand Down Expand Up @@ -393,7 +389,7 @@ index 0ed858f305..9d4a6c5903 100644

/*
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index 70899e5430..34075cc87b 100644
index 5da863d85de..5b21ffd0667 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -143,7 +143,8 @@ static List *extract_rollup_sets(List *groupingSets);
Expand All @@ -406,7 +402,7 @@ index 70899e5430..34075cc87b 100644
grouping_sets_data *gd,
List *target_list);
static RelOptInfo *create_grouping_paths(PlannerInfo *root,
@@ -3151,7 +3152,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
@@ -3145,7 +3146,8 @@ standard_qp_callback(PlannerInfo *root, void *extra)
*/
static double
get_number_of_groups(PlannerInfo *root,
Expand All @@ -416,7 +412,7 @@ index 70899e5430..34075cc87b 100644
grouping_sets_data *gd,
List *target_list)
{
@@ -3188,7 +3190,7 @@ get_number_of_groups(PlannerInfo *root,
@@ -3182,7 +3184,7 @@ get_number_of_groups(PlannerInfo *root,
GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
double numGroups = estimate_num_groups(root,
groupExprs,
Expand All @@ -425,7 +421,7 @@ index 70899e5430..34075cc87b 100644
&gset,
NULL);

@@ -3214,7 +3216,7 @@ get_number_of_groups(PlannerInfo *root,
@@ -3208,7 +3210,7 @@ get_number_of_groups(PlannerInfo *root,
GroupingSetData *gs = lfirst_node(GroupingSetData, lc2);
double numGroups = estimate_num_groups(root,
groupExprs,
Expand All @@ -434,7 +430,7 @@ index 70899e5430..34075cc87b 100644
&gset,
NULL);

@@ -3231,8 +3233,8 @@ get_number_of_groups(PlannerInfo *root,
@@ -3225,8 +3227,8 @@ get_number_of_groups(PlannerInfo *root,
groupExprs = get_sortgrouplist_exprs(parse->groupClause,
target_list);

Expand All @@ -445,7 +441,7 @@ index 70899e5430..34075cc87b 100644
}
}
else if (parse->groupingSets)
@@ -3619,7 +3621,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
@@ -3613,7 +3615,8 @@ create_ordinary_grouping_paths(PlannerInfo *root, RelOptInfo *input_rel,
* Estimate number of groups.
*/
dNumGroups = get_number_of_groups(root,
Expand All @@ -455,7 +451,7 @@ index 70899e5430..34075cc87b 100644
gd,
extra->targetList);

@@ -6425,13 +6428,15 @@ create_partial_grouping_paths(PlannerInfo *root,
@@ -6419,13 +6422,15 @@ create_partial_grouping_paths(PlannerInfo *root,
if (cheapest_total_path != NULL)
dNumPartialGroups =
get_number_of_groups(root,
Expand All @@ -474,7 +470,7 @@ index 70899e5430..34075cc87b 100644
extra->targetList);

diff --git a/src/backend/optimizer/util/relnode.c b/src/backend/optimizer/util/relnode.c
index e105a4d5f1..c5bcc9d1d1 100644
index e105a4d5f1d..c5bcc9d1d15 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 @@ -540,7 +536,7 @@ index e105a4d5f1..c5bcc9d1d1 100644

return ppi;
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 87879c9ddc..1aad8c43d9 100644
index 87879c9ddc8..1aad8c43d92 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -143,6 +143,7 @@
Expand Down Expand Up @@ -573,7 +569,7 @@ index 87879c9ddc..1aad8c43d9 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 e94d9e49cf..49236ced77 100644
index e94d9e49cf6..49236ced77c 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 e94d9e49cf..49236ced77 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 e370a01141..9f2f1628f5 100644
index 3c034fa3c5e..e441674970c 100644
--- a/src/include/nodes/pathnodes.h
+++ b/src/include/nodes/pathnodes.h
@@ -756,6 +756,10 @@ typedef struct RelOptInfo
Expand Down Expand Up @@ -635,7 +631,7 @@ index e370a01141..9f2f1628f5 100644


diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h
index 2308c80dde..a933afa483 100644
index 1c9357f6a77..58c005c1a9b 100644
--- a/src/include/nodes/plannodes.h
+++ b/src/include/nodes/plannodes.h
@@ -158,6 +158,9 @@ typedef struct Plan
Expand All @@ -649,7 +645,7 @@ index 2308c80dde..a933afa483 100644

/* ----------------
diff --git a/src/include/optimizer/cost.h b/src/include/optimizer/cost.h
index 2113bc82de..bcc2520cec 100644
index 2113bc82de0..bcc2520cec5 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 2113bc82de..bcc2520cec 100644

#endif /* COST_H */
diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h
index 2922c0cdc1..c59dce6989 100644
index 2922c0cdc14..c59dce6989e 100644
--- a/src/include/optimizer/pathnode.h
+++ b/src/include/optimizer/pathnode.h
@@ -18,6 +18,10 @@
Expand All @@ -748,7 +744,7 @@ index 2922c0cdc1..c59dce6989 100644
* prototypes for pathnode.c
*/
diff --git a/src/include/optimizer/planmain.h b/src/include/optimizer/planmain.h
index bf1adfc52a..9c78e0f4e0 100644
index bf1adfc52ac..9c78e0f4e02 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 bf1adfc52a..9c78e0f4e0 100644
* prototypes for plan/planmain.c
*/
diff --git a/src/include/utils/selfuncs.h b/src/include/utils/selfuncs.h
index 9dd444e1ff..cfaae98aa2 100644
index 9dd444e1ff5..b0b5a656185 100644
--- a/src/include/utils/selfuncs.h
+++ b/src/include/utils/selfuncs.h
@@ -144,6 +144,13 @@ typedef bool (*get_index_stats_hook_type) (PlannerInfo *root,
Expand All @@ -782,13 +778,13 @@ index 9dd444e1ff..cfaae98aa2 100644

/* Functions in selfuncs.c */

@@ -213,6 +220,9 @@ extern void mergejoinscansel(PlannerInfo *root, Node *clause,
extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
double input_rows, List **pgset,
EstimationInfo *estinfo);
@@ -210,6 +217,9 @@ extern void mergejoinscansel(PlannerInfo *root, Node *clause,
Selectivity *leftstart, Selectivity *leftend,
Selectivity *rightstart, Selectivity *rightend);

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

extern void estimate_hash_bucket_stats(PlannerInfo *root,
Node *hashkey, double nbuckets,
extern double estimate_num_groups(PlannerInfo *root, List *groupExprs,
double input_rows, List **pgset,
EstimationInfo *estinfo);
3 changes: 1 addition & 2 deletions cardinality_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,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 @@ -218,11 +219,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 @@ -317,14 +318,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 @@ -554,7 +555,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 @@ -564,9 +565,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 @@ -575,9 +576,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 @@ -632,7 +633,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 @@ -646,9 +647,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 758257a

Please sign in to comment.