Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVjeL-0003X8-KK for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Jun 2018 20:24:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fVjeJ-0004ic-7l for pgsql-hackers@arkaria.postgresql.org; Wed, 20 Jun 2018 20:24:15 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVjeI-0004iO-Ow for pgsql-hackers@lists.postgresql.org; Wed, 20 Jun 2018 20:24:15 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fVjeF-0001Aj-LJ for pgsql-hackers@postgresql.org; Wed, 20 Jun 2018 20:24:13 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w5KKO3dF028463; Wed, 20 Jun 2018 16:24:04 -0400 From: Tom Lane To: Rajkumar Raghuwanshi cc: pgsql-hackers , Andres Freund Subject: Re: ERROR: ORDER/GROUP BY expression not found in targetlist In-reply-to: <20180.1529510397@sss.pgh.pa.us> References: <20180.1529510397@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 20 Jun 2018 11:59:57 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <28441.1529526222.0@sss.pgh.pa.us> Date: Wed, 20 Jun 2018 16:24:03 -0400 Message-ID: <28462.1529526243@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <28441.1529526222.1@sss.pgh.pa.us> I wrote: > Thanks for the report. I traced through this, and the problem seems to > be that split_pathtarget_at_srfs() is neglecting to attach sortgroupref > labeling to the extra PathTargets it constructs. I don't remember > whether that code is my fault or Andres', but I'll take a look at > fixing it. Here's a proposed patch for that. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-sortgroupref-handling-while-splitting-SRFs.patch"; charset="us-ascii" Content-ID: <28441.1529526222.2@sss.pgh.pa.us> Content-Description: fix-sortgroupref-handling-while-splitting-SRFs.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/optimizer/util/tlist.c b/src/backend/optimizer/ut= il/tlist.c index 32160d5..5500f33 100644 *** a/src/backend/optimizer/util/tlist.c --- b/src/backend/optimizer/util/tlist.c *************** *** 25,44 **** ((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \ (IsA(node, OpExpr) && ((OpExpr *) (node))->opretset)) = ! /* Workspace for split_pathtarget_walker */ typedef struct { List *input_target_exprs; /* exprs available from input */ ! List *level_srfs; /* list of lists of SRF exprs */ ! List *level_input_vars; /* vars needed by SRFs of each level */ ! List *level_input_srfs; /* SRFs needed by SRFs of each level */ List *current_input_vars; /* vars needed in current subexpr */ List *current_input_srfs; /* SRFs needed in current subexpr */ int current_depth; /* max SRF depth in current subexpr */ } split_pathtarget_context; = static bool split_pathtarget_walker(Node *node, split_pathtarget_context *context); = = /***********************************************************************= ****** --- 25,62 ---- ((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \ (IsA(node, OpExpr) && ((OpExpr *) (node))->opretset)) = ! /* ! * Data structures for split_pathtarget_at_srfs(). To preserve the iden= tity ! * of sortgroupref items even if they are textually equal(), what we tra= ck is ! * not just bare expressions but expressions plus their sortgroupref ind= exes. ! */ typedef struct { + Node *expr; /* some subexpression of a PathTarget */ + Index sortgroupref; /* its sortgroupref, or 0 if none */ + } split_pathtarget_item; + = + typedef struct + { + /* This is a List of bare expressions: */ List *input_target_exprs; /* exprs available from input */ ! /* These are Lists of Lists of split_pathtarget_items: */ ! List *level_srfs; /* SRF exprs to evaluate at each level */ ! List *level_input_vars; /* input vars needed at each level */ ! List *level_input_srfs; /* input SRFs needed at each level */ ! /* These are Lists of split_pathtarget_items: */ List *current_input_vars; /* vars needed in current subexpr */ List *current_input_srfs; /* SRFs needed in current subexpr */ + /* Auxiliary data for current split_pathtarget_walker traversal: */ int current_depth; /* max SRF depth in current subexpr */ + Index current_sgref; /* current subexpr's sortgroupref, or 0 */ } split_pathtarget_context; = static bool split_pathtarget_walker(Node *node, split_pathtarget_context *context); + static void add_sp_item_to_pathtarget(PathTarget *target, + split_pathtarget_item *item); + static void add_sp_items_to_pathtarget(PathTarget *target, List *items); = = /***********************************************************************= ****** *************** apply_pathtarget_labeling_to_tlist(List = *** 822,827 **** --- 840,848 ---- * already meant as a reference to a lower subexpression). So, don't ex= pand * any tlist expressions that appear in input_target, if that's not NULL= . * + * It's also important that we preserve any sortgroupref annotation appe= aring + * in the given target, especially on expressions matching input_target = items. + * * The outputs of this function are two parallel lists, one a list of * PathTargets and the other an integer list of bool flags indicating * whether the corresponding PathTarget contains any evaluatable SRFs. *************** split_pathtarget_at_srfs(PlannerInfo *ro *** 845,850 **** --- 866,872 ---- int max_depth; bool need_extra_projection; List *prev_level_tlist; + int lci; ListCell *lc, *lc1, *lc2, *************** split_pathtarget_at_srfs(PlannerInfo *ro *** 884,893 **** --- 906,920 ---- need_extra_projection =3D false; = /* Scan each expression in the PathTarget looking for SRFs */ + lci =3D 0; foreach(lc, target->exprs) { Node *node =3D (Node *) lfirst(lc); = + /* Tell split_pathtarget_walker about this expr's sortgroupref */ + context.current_sgref =3D get_pathtarget_sortgroupref(target, lci); + lci++; + = /* * Find all SRFs and Vars (and Var-like nodes) in this expression, and * enter them into appropriate lists within the context struct. *************** split_pathtarget_at_srfs(PlannerInfo *ro *** 981,996 **** * This target should actually evaluate any SRFs of the current * level, and it needs to propagate forward any Vars needed by * later levels, as well as SRFs computed earlier and needed by ! * later levels. We rely on add_new_columns_to_pathtarget() to ! * remove duplicate items. Also, for safety, make a separate copy ! * of each item for each PathTarget. */ ! add_new_columns_to_pathtarget(ntarget, copyObject(level_srfs)); for_each_cell(lc, lnext(lc2)) { List *input_vars =3D (List *) lfirst(lc); = ! add_new_columns_to_pathtarget(ntarget, copyObject(input_vars)); } for_each_cell(lc, lnext(lc3)) { --- 1008,1021 ---- * This target should actually evaluate any SRFs of the current * level, and it needs to propagate forward any Vars needed by * later levels, as well as SRFs computed earlier and needed by ! * later levels. */ ! add_sp_items_to_pathtarget(ntarget, level_srfs); for_each_cell(lc, lnext(lc2)) { List *input_vars =3D (List *) lfirst(lc); = ! add_sp_items_to_pathtarget(ntarget, input_vars); } for_each_cell(lc, lnext(lc3)) { *************** split_pathtarget_at_srfs(PlannerInfo *ro *** 999,1008 **** = foreach(lcx, input_srfs) { ! Expr *srf =3D (Expr *) lfirst(lcx); = ! if (list_member(prev_level_tlist, srf)) ! add_new_column_to_pathtarget(ntarget, copyObject(srf)); } } set_pathtarget_cost_width(root, ntarget); --- 1024,1033 ---- = foreach(lcx, input_srfs) { ! split_pathtarget_item *item =3D lfirst(lcx); = ! if (list_member(prev_level_tlist, item->expr)) ! add_sp_item_to_pathtarget(ntarget, item); } } set_pathtarget_cost_width(root, ntarget); *************** split_pathtarget_walker(Node *node, spli *** 1037,1048 **** * input_target can be treated like a Var (which indeed it will be afte= r * setrefs.c gets done with it), even if it's actually a SRF. Record i= t * as being needed for the current expression, and ignore any ! * substructure. */ if (list_member(context->input_target_exprs, node)) { context->current_input_vars =3D lappend(context->current_input_vars, ! node); return false; } = --- 1062,1078 ---- * input_target can be treated like a Var (which indeed it will be afte= r * setrefs.c gets done with it), even if it's actually a SRF. Record i= t * as being needed for the current expression, and ignore any ! * substructure. (Note in particular that this preserves the identity = of ! * any expressions that appear as sortgrouprefs in input_target.) */ if (list_member(context->input_target_exprs, node)) { + split_pathtarget_item *item =3D palloc(sizeof(split_pathtarget_item)); + = + item->expr =3D node; + item->sortgroupref =3D context->current_sgref; context->current_input_vars =3D lappend(context->current_input_vars, ! item); return false; } = *************** split_pathtarget_walker(Node *node, spli *** 1057,1064 **** IsA(node, GroupingFunc) || IsA(node, WindowFunc)) { context->current_input_vars =3D lappend(context->current_input_vars, ! node); return false; } = --- 1087,1098 ---- IsA(node, GroupingFunc) || IsA(node, WindowFunc)) { + split_pathtarget_item *item =3D palloc(sizeof(split_pathtarget_item)); + = + item->expr =3D node; + item->sortgroupref =3D context->current_sgref; context->current_input_vars =3D lappend(context->current_input_vars, ! item); return false; } = *************** split_pathtarget_walker(Node *node, spli *** 1068,1082 **** --- 1102,1121 ---- */ if (IS_SRF_CALL(node)) { + split_pathtarget_item *item =3D palloc(sizeof(split_pathtarget_item)); List *save_input_vars =3D context->current_input_vars; List *save_input_srfs =3D context->current_input_srfs; int save_current_depth =3D context->current_depth; int srf_depth; ListCell *lc; = + item->expr =3D node; + item->sortgroupref =3D context->current_sgref; + = context->current_input_vars =3D NIL; context->current_input_srfs =3D NIL; context->current_depth =3D 0; + context->current_sgref =3D 0; /* subexpressions are not sortgroup item= s */ = (void) expression_tree_walker(node, split_pathtarget_walker, (void *) context); *************** split_pathtarget_walker(Node *node, spli *** 1094,1100 **** = /* Record this SRF as needing to be evaluated at appropriate level */ lc =3D list_nth_cell(context->level_srfs, srf_depth); ! lfirst(lc) =3D lappend(lfirst(lc), node); = /* Record its inputs as being needed at the same level */ lc =3D list_nth_cell(context->level_input_vars, srf_depth); --- 1133,1139 ---- = /* Record this SRF as needing to be evaluated at appropriate level */ lc =3D list_nth_cell(context->level_srfs, srf_depth); ! lfirst(lc) =3D lappend(lfirst(lc), item); = /* Record its inputs as being needed at the same level */ lc =3D list_nth_cell(context->level_input_vars, srf_depth); *************** split_pathtarget_walker(Node *node, spli *** 1108,1114 **** * surrounding expression. */ context->current_input_vars =3D save_input_vars; ! context->current_input_srfs =3D lappend(save_input_srfs, node); context->current_depth =3D Max(save_current_depth, srf_depth); = /* We're done here */ --- 1147,1153 ---- * surrounding expression. */ context->current_input_vars =3D save_input_vars; ! context->current_input_srfs =3D lappend(save_input_srfs, item); context->current_depth =3D Max(save_current_depth, srf_depth); = /* We're done here */ *************** split_pathtarget_walker(Node *node, spli *** 1119,1124 **** --- 1158,1236 ---- * Otherwise, the node is a scalar (non-set) expression, so recurse to * examine its inputs. */ + context->current_sgref =3D 0; /* subexpressions are not sortgroup items= */ return expression_tree_walker(node, split_pathtarget_walker, (void *) context); } + = + /* + * Add a split_pathtarget_item to the PathTarget, unless a matching item= is + * already present. This is like add_new_column_to_pathtarget, but allo= ws + * for sortgrouprefs to be handled. An item having zero sortgroupref ca= n + * be merged with one that has a sortgroupref, acquiring the latter's + * sortgroupref. + * + * Note that we don't worry about possibly adding duplicate sortgroupref= s + * to the PathTarget. That would be bad, but it should be impossible un= less + * the target passed to split_pathtarget_at_srfs already had duplicates. + * As long as it didn't, we can have at most one split_pathtarget_item w= ith + * any particular nonzero sortgroupref. + */ + static void + add_sp_item_to_pathtarget(PathTarget *target, split_pathtarget_item *ite= m) + { + int lci; + ListCell *lc; + = + /* + * Look for a pre-existing entry that is equal() and does not have a + * conflicting sortgroupref already. + */ + lci =3D 0; + foreach(lc, target->exprs) + { + Node *node =3D (Node *) lfirst(lc); + Index sgref =3D get_pathtarget_sortgroupref(target, lci); + = + if ((item->sortgroupref =3D=3D sgref || + item->sortgroupref =3D=3D 0 || + sgref =3D=3D 0) && + equal(item->expr, node)) + { + /* Found a match. Assign item's sortgroupref if it has one. */ + if (item->sortgroupref) + { + if (target->sortgrouprefs =3D=3D NULL) + { + target->sortgrouprefs =3D (Index *) + palloc0(list_length(target->exprs) * sizeof(Index)); + } + target->sortgrouprefs[lci] =3D item->sortgroupref; + } + return; + } + lci++; + } + = + /* + * No match, so add item to PathTarget. Copy the expr for safety. + */ + add_column_to_pathtarget(target, (Expr *) copyObject(item->expr), + item->sortgroupref); + } + = + /* + * Apply add_sp_item_to_pathtarget to each element of list. + */ + static void + add_sp_items_to_pathtarget(PathTarget *target, List *items) + { + ListCell *lc; + = + foreach(lc, items) + { + split_pathtarget_item *item =3D lfirst(lc); + = + add_sp_item_to_pathtarget(target, item); + } + } diff --git a/src/test/regress/expected/select_parallel.out b/src/test/regr= ess/expected/select_parallel.out index e48e394..cd0b945 100644 *** a/src/test/regress/expected/select_parallel.out --- b/src/test/regress/expected/select_parallel.out *************** explain (costs off, verbose) *** 659,664 **** --- 659,726 ---- (11 rows) = drop function sp_simple_func(integer); + -- test handling of SRFs in targetlist (bug in 10.0) + explain (costs off) + select count(*), generate_series(1,2) from tenk1 group by twenty; + QUERY PLAN = + ---------------------------------------------------------- + ProjectSet + -> Finalize GroupAggregate + Group Key: twenty + -> Gather Merge + Workers Planned: 4 + -> Partial GroupAggregate + Group Key: twenty + -> Sort + Sort Key: twenty + -> Parallel Seq Scan on tenk1 + (10 rows) + = + select count(*), generate_series(1,2) from tenk1 group by twenty; + count | generate_series = + -------+----------------- + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + 500 | 1 + 500 | 2 + (40 rows) + = -- test gather merge with parallel leader participation disabled set parallel_leader_participation =3D off; explain (costs off) diff --git a/src/test/regress/sql/select_parallel.sql b/src/test/regress/s= ql/select_parallel.sql index 31045d7..7771e05 100644 *** a/src/test/regress/sql/select_parallel.sql --- b/src/test/regress/sql/select_parallel.sql *************** explain (costs off, verbose) *** 261,266 **** --- 261,273 ---- = drop function sp_simple_func(integer); = + -- test handling of SRFs in targetlist (bug in 10.0) + = + explain (costs off) + select count(*), generate_series(1,2) from tenk1 group by twenty; + = + select count(*), generate_series(1,2) from tenk1 group by twenty; + = -- test gather merge with parallel leader participation disabled set parallel_leader_participation =3D off; = ------- =_aaaaaaaaaa0--