7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.79 2003/09/25 06:58:01 petere Exp $
10
+ * $Header: /cvsroot/pgsql/src/backend/rewrite/rewriteManip.c,v 1.80 2003/10/20 20:01:59 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
99
99
checkExprHasSubLink (Node * node )
100
100
{
101
101
/*
102
- * If a Query is passed, examine it --- but we will not recurse into
102
+ * If a Query is passed, examine it --- but we need not recurse into
103
103
* sub-Queries.
104
104
*/
105
105
return query_or_expression_tree_walker (node ,
@@ -755,10 +755,12 @@ AddQual(Query *parsetree, Node *qual)
755
755
/*
756
756
* Make sure query is marked correctly if added qual has sublinks or
757
757
* aggregates (not sure it can ever have aggs, but sublinks
758
- * definitely).
758
+ * definitely). Need not search qual when query is already marked.
759
759
*/
760
- parsetree -> hasAggs |= checkExprHasAggs (copy );
761
- parsetree -> hasSubLinks |= checkExprHasSubLink (copy );
760
+ if (!parsetree -> hasAggs )
761
+ parsetree -> hasAggs = checkExprHasAggs (copy );
762
+ if (!parsetree -> hasSubLinks )
763
+ parsetree -> hasSubLinks = checkExprHasSubLink (copy );
762
764
}
763
765
764
766
/*
@@ -809,10 +811,12 @@ AddHavingQual(Query *parsetree, Node *havingQual)
809
811
/*
810
812
* Make sure query is marked correctly if added qual has sublinks or
811
813
* aggregates (not sure it can ever have aggs, but sublinks
812
- * definitely).
814
+ * definitely). Need not search qual when query is already marked.
813
815
*/
814
- parsetree -> hasAggs |= checkExprHasAggs (copy );
815
- parsetree -> hasSubLinks |= checkExprHasSubLink (copy );
816
+ if (!parsetree -> hasAggs )
817
+ parsetree -> hasAggs = checkExprHasAggs (copy );
818
+ if (!parsetree -> hasSubLinks )
819
+ parsetree -> hasSubLinks = checkExprHasSubLink (copy );
816
820
}
817
821
818
822
@@ -845,6 +849,12 @@ AddInvertedQual(Query *parsetree, Node *qual)
845
849
* entry with matching resno from targetlist, if there is one.
846
850
* If not, we either change the unmatched Var's varno to update_varno
847
851
* (when event == CMD_UPDATE) or replace it with a constant NULL.
852
+ *
853
+ * Note: the business with inserted_sublink is needed to update hasSubLinks
854
+ * in subqueries when the replacement adds a subquery inside a subquery.
855
+ * Messy, isn't it? We do not need to do similar pushups for hasAggs,
856
+ * because it isn't possible for this transformation to insert a level-zero
857
+ * aggregate reference into a subquery --- it could only insert outer aggs.
848
858
*/
849
859
850
860
typedef struct
@@ -854,6 +864,7 @@ typedef struct
854
864
List * targetlist ;
855
865
int event ;
856
866
int update_varno ;
867
+ bool inserted_sublink ;
857
868
} ResolveNew_context ;
858
869
859
870
static Node *
@@ -904,6 +915,9 @@ ResolveNew_mutator(Node *node, ResolveNew_context *context)
904
915
/* Adjust varlevelsup if tlist item is from higher query */
905
916
if (this_varlevelsup > 0 )
906
917
IncrementVarSublevelsUp (n , this_varlevelsup , 0 );
918
+ /* Report it if we are adding a sublink to query */
919
+ if (!context -> inserted_sublink )
920
+ context -> inserted_sublink = checkExprHasSubLink (n );
907
921
return n ;
908
922
}
909
923
}
@@ -914,12 +928,17 @@ ResolveNew_mutator(Node *node, ResolveNew_context *context)
914
928
{
915
929
/* Recurse into RTE subquery or not-yet-planned sublink subquery */
916
930
Query * newnode ;
931
+ bool save_inserted_sublink ;
917
932
918
933
context -> sublevels_up ++ ;
934
+ save_inserted_sublink = context -> inserted_sublink ;
935
+ context -> inserted_sublink = false;
919
936
newnode = query_tree_mutator ((Query * ) node ,
920
937
ResolveNew_mutator ,
921
938
(void * ) context ,
922
939
0 );
940
+ newnode -> hasSubLinks |= context -> inserted_sublink ;
941
+ context -> inserted_sublink = save_inserted_sublink ;
923
942
context -> sublevels_up -- ;
924
943
return (Node * ) newnode ;
925
944
}
@@ -938,6 +957,7 @@ ResolveNew(Node *node, int target_varno, int sublevels_up,
938
957
context .targetlist = targetlist ;
939
958
context .event = event ;
940
959
context .update_varno = update_varno ;
960
+ context .inserted_sublink = false;
941
961
942
962
/*
943
963
* Must be prepared to start with a Query or a bare expression tree;
0 commit comments