|
7 | 7 | * Portions Copyright (c) 1994, Regents of the University of California |
8 | 8 | * |
9 | 9 | * IDENTIFICATION |
10 | | - * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.86 2003/11/29 19:51:50 pgsql Exp $ |
| 10 | + * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.87 2004/01/12 22:20:28 tgl Exp $ |
11 | 11 | * |
12 | 12 | *------------------------------------------------------------------------- |
13 | 13 | */ |
@@ -848,13 +848,58 @@ process_sublinks_mutator(Node *node, bool *isTopQual) |
848 | 848 | Assert(!IsA(node,Query)); |
849 | 849 |
|
850 | 850 | /* |
851 | | - * If we recurse down through anything other than a List node, we are |
852 | | - * definitely not at top qual level anymore. |
| 851 | + * Because make_subplan() could return an AND or OR clause, we have to |
| 852 | + * take steps to preserve AND/OR flatness of a qual. We assume the input |
| 853 | + * has been AND/OR flattened and so we need no recursion here. |
| 854 | + * |
| 855 | + * If we recurse down through anything other than an AND node, |
| 856 | + * we are definitely not at top qual level anymore. (Due to the coding |
| 857 | + * here, we will not get called on the List subnodes of an AND, so no |
| 858 | + * check is needed for List.) |
853 | 859 | */ |
854 | | -if (IsA(node,List)) |
| 860 | +if (and_clause(node)) |
| 861 | +{ |
| 862 | +List*newargs=NIL; |
| 863 | +List*l; |
| 864 | + |
| 865 | +/* Still at qual top-level */ |
855 | 866 | locTopQual=*isTopQual; |
856 | | -else |
857 | | -locTopQual= false; |
| 867 | + |
| 868 | +foreach(l, ((BoolExpr*)node)->args) |
| 869 | +{ |
| 870 | +Node*newarg; |
| 871 | + |
| 872 | +newarg=process_sublinks_mutator(lfirst(l), |
| 873 | + (void*)&locTopQual); |
| 874 | +if (and_clause(newarg)) |
| 875 | +newargs=nconc(newargs, ((BoolExpr*)newarg)->args); |
| 876 | +else |
| 877 | +newargs=lappend(newargs,newarg); |
| 878 | +} |
| 879 | +return (Node*)make_andclause(newargs); |
| 880 | +} |
| 881 | + |
| 882 | +/* otherwise not at qual top-level */ |
| 883 | +locTopQual= false; |
| 884 | + |
| 885 | +if (or_clause(node)) |
| 886 | +{ |
| 887 | +List*newargs=NIL; |
| 888 | +List*l; |
| 889 | + |
| 890 | +foreach(l, ((BoolExpr*)node)->args) |
| 891 | +{ |
| 892 | +Node*newarg; |
| 893 | + |
| 894 | +newarg=process_sublinks_mutator(lfirst(l), |
| 895 | + (void*)&locTopQual); |
| 896 | +if (or_clause(newarg)) |
| 897 | +newargs=nconc(newargs, ((BoolExpr*)newarg)->args); |
| 898 | +else |
| 899 | +newargs=lappend(newargs,newarg); |
| 900 | +} |
| 901 | +return (Node*)make_orclause(newargs); |
| 902 | +} |
858 | 903 |
|
859 | 904 | returnexpression_tree_mutator(node, |
860 | 905 | process_sublinks_mutator, |
|