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.113 2006/12/06 19:40:01 tgl Exp $
10
+ * $PostgreSQL: pgsql/src/backend/optimizer/plan/subselect.c,v 1.114 2006/12/10 22:13:26 tgl Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -75,7 +75,6 @@ typedef struct convert_testexpr_context
75
75
{
76
76
int rtindex ;/* RT index for Vars, or 0 for Params */
77
77
List * righthandIds ;/* accumulated list of Vars or Param IDs */
78
- List * sub_tlist ;/* subselect targetlist (if given) */
79
78
}convert_testexpr_context ;
80
79
81
80
typedef struct finalize_primnode_context
@@ -87,8 +86,7 @@ typedef struct finalize_primnode_context
87
86
88
87
static Node * convert_testexpr (Node * testexpr ,
89
88
int rtindex ,
90
- List * * righthandIds ,
91
- List * sub_tlist );
89
+ List * * righthandIds );
92
90
static Node * convert_testexpr_mutator (Node * node ,
93
91
convert_testexpr_context * context );
94
92
static bool subplan_is_hashable (SubLink * slink ,SubPlan * node );
@@ -166,6 +164,7 @@ replace_outer_var(Var *var)
166
164
retval -> paramkind = PARAM_EXEC ;
167
165
retval -> paramid = i ;
168
166
retval -> paramtype = var -> vartype ;
167
+ retval -> paramtypmod = var -> vartypmod ;
169
168
170
169
return retval ;
171
170
}
@@ -204,6 +203,7 @@ replace_outer_agg(Aggref *agg)
204
203
retval -> paramkind = PARAM_EXEC ;
205
204
retval -> paramid = i ;
206
205
retval -> paramtype = agg -> aggtype ;
206
+ retval -> paramtypmod = -1 ;
207
207
208
208
return retval ;
209
209
}
@@ -212,8 +212,6 @@ replace_outer_agg(Aggref *agg)
212
212
* Generate a new Param node that will not conflict with any other.
213
213
*
214
214
* This is used to allocate PARAM_EXEC slots for subplan outputs.
215
- *
216
- * paramtypmod is currently unused but might be wanted someday.
217
215
*/
218
216
static Param *
219
217
generate_new_param (Oid paramtype ,int32 paramtypmod )
@@ -225,6 +223,7 @@ generate_new_param(Oid paramtype, int32 paramtypmod)
225
223
retval -> paramkind = PARAM_EXEC ;
226
224
retval -> paramid = list_length (PlannerParamList );
227
225
retval -> paramtype = paramtype ;
226
+ retval -> paramtypmod = paramtypmod ;
228
227
229
228
pitem = (PlannerParamItem * )palloc (sizeof (PlannerParamItem ));
230
229
pitem -> item = (Node * )retval ;
@@ -371,7 +370,7 @@ make_subplan(SubLink *slink, Node *testexpr, bool isTopQual)
371
370
if (!OidIsValid (arraytype ))
372
371
elog (ERROR ,"could not find array type for datatype %s" ,
373
372
format_type_be (exprType ((Node * )te -> expr )));
374
- prm = generate_new_param (arraytype ,-1 );
373
+ prm = generate_new_param (arraytype ,exprTypmod (( Node * ) te -> expr ) );
375
374
node -> setParam = list_make1_int (prm -> paramid );
376
375
PlannerInitPlan = lappend (PlannerInitPlan ,node );
377
376
result = (Node * )prm ;
@@ -381,8 +380,7 @@ make_subplan(SubLink *slink, Node *testexpr, bool isTopQual)
381
380
/* Adjust the Params */
382
381
result = convert_testexpr (testexpr ,
383
382
0 ,
384
- & node -> paramIds ,
385
- NIL );
383
+ & node -> paramIds );
386
384
node -> setParam = list_copy (node -> paramIds );
387
385
PlannerInitPlan = lappend (PlannerInitPlan ,node );
388
386
@@ -399,8 +397,7 @@ make_subplan(SubLink *slink, Node *testexpr, bool isTopQual)
399
397
/* Adjust the Params */
400
398
node -> testexpr = convert_testexpr (testexpr ,
401
399
0 ,
402
- & node -> paramIds ,
403
- NIL );
400
+ & node -> paramIds );
404
401
405
402
/*
406
403
* We can't convert subplans of ALL_SUBLINK or ANY_SUBLINK types to
@@ -474,10 +471,6 @@ make_subplan(SubLink *slink, Node *testexpr, bool isTopQual)
474
471
* of the Var nodes are returned in *righthandIds (this is a bit of a type
475
472
* cheat, but we can get away with it).
476
473
*
477
- * The subquery targetlist need be supplied only if rtindex is not 0.
478
- * We consult it to extract the correct typmods for the created Vars.
479
- * (XXX this is a kluge that could go away if Params carried typmod.)
480
- *
481
474
* The given testexpr has already been recursively processed by
482
475
* process_sublinks_mutator. Hence it can no longer contain any
483
476
* PARAM_SUBLINK Params for lower SubLink nodes; we can safely assume that
@@ -486,15 +479,13 @@ make_subplan(SubLink *slink, Node *testexpr, bool isTopQual)
486
479
static Node *
487
480
convert_testexpr (Node * testexpr ,
488
481
int rtindex ,
489
- List * * righthandIds ,
490
- List * sub_tlist )
482
+ List * * righthandIds )
491
483
{
492
484
Node * result ;
493
485
convert_testexpr_context context ;
494
486
495
487
context .rtindex = rtindex ;
496
488
context .righthandIds = NIL ;
497
- context .sub_tlist = sub_tlist ;
498
489
result = convert_testexpr_mutator (testexpr ,& context );
499
490
* righthandIds = context .righthandIds ;
500
491
return result ;
@@ -526,23 +517,10 @@ convert_testexpr_mutator(Node *node,
526
517
/* Make the Var node representing the subplan's result */
527
518
Var * newvar ;
528
519
529
- /*
530
- * XXX kluge: since Params don't carry typmod, we have to
531
- * look into the subquery targetlist to find out the right
532
- * typmod to assign to the Var.
533
- */
534
- TargetEntry * ste = get_tle_by_resno (context -> sub_tlist ,
535
- param -> paramid );
536
-
537
- if (ste == NULL || ste -> resjunk )
538
- elog (ERROR ,"subquery output %d not found" ,
539
- param -> paramid );
540
- Assert (param -> paramtype == exprType ((Node * )ste -> expr ));
541
-
542
520
newvar = makeVar (context -> rtindex ,
543
521
param -> paramid ,
544
522
param -> paramtype ,
545
- exprTypmod (( Node * ) ste -> expr ) ,
523
+ param -> paramtypmod ,
546
524
0 );
547
525
548
526
/*
@@ -558,7 +536,8 @@ convert_testexpr_mutator(Node *node,
558
536
/* Make the Param node representing the subplan's result */
559
537
Param * newparam ;
560
538
561
- newparam = generate_new_param (param -> paramtype ,-1 );
539
+ newparam = generate_new_param (param -> paramtype ,
540
+ param -> paramtypmod );
562
541
/* Record its ID */
563
542
context -> righthandIds = lappend_int (context -> righthandIds ,
564
543
newparam -> paramid );
@@ -775,8 +754,7 @@ convert_IN_to_join(PlannerInfo *root, SubLink *sublink)
775
754
*/
776
755
return convert_testexpr (sublink -> testexpr ,
777
756
rtindex ,
778
- & ininfo -> sub_targetlist ,
779
- subselect -> targetList );
757
+ & ininfo -> sub_targetlist );
780
758
}
781
759
782
760
/*