Skip to content
Permalink
Browse files
HAWQ-485. Wrong results in rollup with plan_append_aggs_with_rewrite.
  • Loading branch information
ztao1987 committed Mar 4, 2016
1 parent 5ba4e7b commit 99c370b4f82be50b86d11ccb88a9c6a4179497a5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 12 deletions.
@@ -2281,6 +2281,8 @@ typedef struct ReplaceGroupColsContext
* appear inside an Aggref.
*/
bool in_aggref;

int ressortgroupref;
} ReplaceGroupColsContext;


@@ -2293,13 +2295,26 @@ replace_grouping_columns_mutator(Node *node, void *v_cxt)
if (node == NULL)
return NULL;

if (IsA(node, TargetEntry))
{
cxt->ressortgroupref = ((TargetEntry *) node)->ressortgroupref;
}

Assert(IsA(cxt->grpcols, List));

foreach (lc, cxt->grpcols)
{
Node *grpcol = lfirst(lc);
if (equal(node, grpcol))
break;

Assert(IsA(grpcol, TargetEntry));

TargetEntry *grpcoltle = (TargetEntry *) grpcol;

if (equal(node, (Node *)(grpcoltle->expr)))
{
if (cxt->ressortgroupref == 0 || cxt->ressortgroupref == grpcoltle->ressortgroupref)
break;
}
}

if (IsA(node, Aggref))
@@ -2323,10 +2338,11 @@ replace_grouping_columns_mutator(Node *node, void *v_cxt)
/* Generate a NULL constant to replace the node. */
Const *null;
Node *grpcol = lfirst(lc);
TargetEntry *grpcoltle = (TargetEntry *) grpcol;

if (!cxt->in_aggref)
{
null = makeNullConst(exprType((Node *)grpcol), -1);
null = makeNullConst(exprType((Node *)(grpcoltle->expr)), -1);
return (Node *)null;
}
}
@@ -2361,11 +2377,12 @@ replace_grouping_columns(Node *node,
{
TargetEntry *te = get_tle_by_resno(sub_tlist, grpColIdx[attno]);
Assert(te != NULL);
grpcols = lappend(grpcols, te->expr);
grpcols = lappend(grpcols, te);
}

cxt.grpcols = grpcols;
cxt.in_aggref = false;
cxt.ressortgroupref = 0;

new_node = replace_grouping_columns_mutator((Node *)node, (void *)&cxt);
list_free(grpcols);
@@ -2070,7 +2070,9 @@ grouping_planner(PlannerInfo *root, double tuple_fraction)

if ( IsA(tle->expr, Var) && tle->resname == NULL )
{
TargetEntry *vartle = tlist_member((Node*)tle->expr, tlist);
TargetEntry *vartle = tlist_member_with_ressortgroupref((Node*)tle->expr,
tlist,
tle->ressortgroupref);

if ( vartle != NULL && vartle->resname != NULL )
tle->resname = pstrdup(vartle->resname);
@@ -88,6 +88,7 @@ typedef struct
indexed_tlist *subplan_itlist;
int rtoffset;
bool use_scan_slot;
int ressortgroupref;
} fix_upper_expr_context;

/*
@@ -125,7 +126,8 @@ static Var *search_indexed_tlist_for_var(Var *var,
int rtoffset);
static Var *search_indexed_tlist_for_non_var(Node *node,
indexed_tlist *itlist,
Index newvarno);
Index newvarno,
int ressortgroupref);
static List *fix_join_expr(PlannerGlobal *glob,
List *clauses,
indexed_tlist *outer_itlist,
@@ -1590,7 +1592,7 @@ set_upper_references(PlannerGlobal *glob, Plan *plan,
newexpr = copyObject(tle->expr);
else
newexpr = fix_upper_expr(glob,
(Node *) tle->expr,
(Node *) tle,
subplan_itlist,
rtoffset,
use_scan_slot);
@@ -1845,11 +1847,13 @@ search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
*/
static Var *
search_indexed_tlist_for_non_var(Node *node,
indexed_tlist *itlist, Index newvarno)
indexed_tlist *itlist,
Index newvarno,
int ressortgroupref)
{
TargetEntry *tle;

tle = tlist_member(node, itlist->tlist);
tle = tlist_member_with_ressortgroupref(node, itlist->tlist, ressortgroupref);
if (tle)
{
/* Found a matching subplan output expression */
@@ -2067,7 +2071,8 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
{
newvar = search_indexed_tlist_for_non_var(node,
context->outer_itlist,
OUTER);
OUTER,
0);
if (newvar)
return (Node *) newvar;
}
@@ -2076,7 +2081,8 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
{
newvar = search_indexed_tlist_for_non_var(node,
context->inner_itlist,
INNER);
INNER,
0);
if (newvar)
return (Node *) newvar;
}
@@ -2134,6 +2140,17 @@ fix_upper_expr(PlannerGlobal *glob,
context.rtoffset = rtoffset;
context.use_scan_slot = use_scan_slot;

if (node != NULL && IsA(node, TargetEntry))
{
TargetEntry *tle = (TargetEntry *) node;
node = (Node *) tle->expr;
context.ressortgroupref = tle->ressortgroupref;
}
else
{
context.ressortgroupref = 0;
}

return fix_upper_expr_mutator(node, &context);
}

@@ -2169,7 +2186,8 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
{
newvar = search_indexed_tlist_for_non_var(node,
context->subplan_itlist,
slot_varno);
slot_varno,
context->ressortgroupref);
if (newvar)
return (Node *) newvar;
}

0 comments on commit 99c370b

Please sign in to comment.