20
20
#include "access/sysattr.h"
21
21
#include "catalog/pg_class.h"
22
22
#include "catalog/pg_operator.h"
23
- #include "catalog/pg_type.h"
24
23
#include "foreign/fdwapi.h"
25
24
#include "nodes/makefuncs.h"
26
25
#include "nodes/nodeFuncs.h"
@@ -2117,6 +2116,7 @@ remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
2117
2116
foreach (lc , subquery -> targetList )
2118
2117
{
2119
2118
TargetEntry * tle = (TargetEntry * ) lfirst (lc );
2119
+ Node * texpr = (Node * ) tle -> expr ;
2120
2120
2121
2121
/*
2122
2122
* If it has a sortgroupref number, it's used in some sort/group
@@ -2140,28 +2140,24 @@ remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
2140
2140
* If it contains a set-returning function, we can't remove it since
2141
2141
* that could change the number of rows returned by the subquery.
2142
2142
*/
2143
- if (expression_returns_set (( Node * ) tle -> expr ))
2143
+ if (expression_returns_set (texpr ))
2144
2144
continue ;
2145
2145
2146
2146
/*
2147
2147
* If it contains volatile functions, we daren't remove it for fear
2148
2148
* that the user is expecting their side-effects to happen.
2149
2149
*/
2150
- if (contain_volatile_functions (( Node * ) tle -> expr ))
2150
+ if (contain_volatile_functions (texpr ))
2151
2151
continue ;
2152
2152
2153
2153
/*
2154
2154
* OK, we don't need it. Replace the expression with a NULL constant.
2155
- * We can just make the constant be of INT4 type, since nothing's
2156
- * going to look at it anyway .
2155
+ * Preserve the exposed type of the expression, in case something
2156
+ * looks at the rowtype of the subquery's result .
2157
2157
*/
2158
- tle -> expr = (Expr * ) makeConst (INT4OID ,
2159
- -1 ,
2160
- InvalidOid ,
2161
- sizeof (int32 ),
2162
- (Datum ) 0 ,
2163
- true, /* isnull */
2164
- true /* byval */ );
2158
+ tle -> expr = (Expr * ) makeNullConst (exprType (texpr ),
2159
+ exprTypmod (texpr ),
2160
+ exprCollation (texpr ));
2165
2161
}
2166
2162
}
2167
2163
0 commit comments