aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2004-01-12 22:20:28 +0000
committerTom Lane <tgl@sss.pgh.pa.us>2004-01-12 22:20:28 +0000
commit67af5bace5df0b3b0126df71ec8007494901e80c (patch)
tree17cab876122c656871b0e8a2f4d1d9b20118e2fe /src
parent5590be0a9ed5427ebcb90d587c50e9244ec48f56 (diff)
downloadpostgresql-67af5bace5df0b3b0126df71ec8007494901e80c.tar.gz
postgresql-67af5bace5df0b3b0126df71ec8007494901e80c.zip
Fix another place that wasn't maintaining AND/OR flatness of an
already-canonicalized qual expression.
Diffstat (limited to 'src')
-rw-r--r--src/backend/optimizer/plan/planner.c23
-rw-r--r--src/backend/optimizer/plan/subselect.c57
2 files changed, 65 insertions, 15 deletions
diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c
index bf8a28d1254..7af46244bbb 100644
--- a/src/backend/optimizer/plan/planner.c
+++ b/src/backend/optimizer/plan/planner.c
@@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.163 2003/12/28 21:57:36 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/plan/planner.c,v 1.164 2004/01/12 22:20:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -380,6 +380,10 @@ preprocess_expression(Query *parse, Node *expr, int kind)
* If it's a qual or havingQual, canonicalize it. It seems most useful
* to do this before applying eval_const_expressions, since the latter
* can optimize flattened AND/ORs better than unflattened ones.
+ *
+ * Note: all processing of a qual expression after this point must be
+ * careful to maintain AND/OR flatness --- that is, do not generate a
+ * tree with AND directly under AND, nor OR directly under OR.
*/
if (kind == EXPRKIND_QUAL)
{
@@ -396,14 +400,6 @@ preprocess_expression(Query *parse, Node *expr, int kind)
*/
expr = eval_const_expressions(expr);
- /*
- * If it's a qual or havingQual, convert it to implicit-AND format.
- * (We don't want to do this before eval_const_expressions, since the
- * latter would be unable to simplify a top-level AND correctly.)
- */
- if (kind == EXPRKIND_QUAL)
- expr = (Node *) make_ands_implicit((Expr *) expr);
-
/* Expand SubLinks to SubPlans */
if (parse->hasSubLinks)
expr = SS_process_sublinks(expr, (kind == EXPRKIND_QUAL));
@@ -417,6 +413,15 @@ preprocess_expression(Query *parse, Node *expr, int kind)
if (PlannerQueryLevel > 1)
expr = SS_replace_correlation_vars(expr);
+ /*
+ * If it's a qual or havingQual, convert it to implicit-AND format.
+ * (We don't want to do this before eval_const_expressions, since the
+ * latter would be unable to simplify a top-level AND correctly. Also,
+ * SS_process_sublinks expects explicit-AND format.)
+ */
+ if (kind == EXPRKIND_QUAL)
+ expr = (Node *) make_ands_implicit((Expr *) expr);
+
return expr;
}
diff --git a/src/backend/optimizer/plan/subselect.c b/src/backend/optimizer/plan/subselect.c
index 5140fcb52a9..34dca0e5aca 100644
--- a/src/backend/optimizer/plan/subselect.c
+++ b/src/backend/optimizer/plan/subselect.c
@@ -7,7 +7,7 @@
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.86 2003/11/29 19:51:50 pgsql Exp $
+ * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.87 2004/01/12 22:20:28 tgl Exp $
*
*-------------------------------------------------------------------------
*/
@@ -848,13 +848,58 @@ process_sublinks_mutator(Node *node, bool *isTopQual)
Assert(!IsA(node, Query));
/*
- * If we recurse down through anything other than a List node, we are
- * definitely not at top qual level anymore.
+ * Because make_subplan() could return an AND or OR clause, we have to
+ * take steps to preserve AND/OR flatness of a qual. We assume the input
+ * has been AND/OR flattened and so we need no recursion here.
+ *
+ * If we recurse down through anything other than an AND node,
+ * we are definitely not at top qual level anymore. (Due to the coding
+ * here, we will not get called on the List subnodes of an AND, so no
+ * check is needed for List.)
*/
- if (IsA(node, List))
+ if (and_clause(node))
+ {
+ List *newargs = NIL;
+ List *l;
+
+ /* Still at qual top-level */
locTopQual = *isTopQual;
- else
- locTopQual = false;
+
+ foreach(l, ((BoolExpr *) node)->args)
+ {
+ Node *newarg;
+
+ newarg = process_sublinks_mutator(lfirst(l),
+ (void *) &locTopQual);
+ if (and_clause(newarg))
+ newargs = nconc(newargs, ((BoolExpr *) newarg)->args);
+ else
+ newargs = lappend(newargs, newarg);
+ }
+ return (Node *) make_andclause(newargs);
+ }
+
+ /* otherwise not at qual top-level */
+ locTopQual = false;
+
+ if (or_clause(node))
+ {
+ List *newargs = NIL;
+ List *l;
+
+ foreach(l, ((BoolExpr *) node)->args)
+ {
+ Node *newarg;
+
+ newarg = process_sublinks_mutator(lfirst(l),
+ (void *) &locTopQual);
+ if (or_clause(newarg))
+ newargs = nconc(newargs, ((BoolExpr *) newarg)->args);
+ else
+ newargs = lappend(newargs, newarg);
+ }
+ return (Node *) make_orclause(newargs);
+ }
return expression_tree_mutator(node,
process_sublinks_mutator,