Skip to content

Commit

Permalink
Merge pull request #112 from citusdata/fix_95_non_var_target_list
Browse files Browse the repository at this point in the history
Reduce function calls to var's during requested column computation
  • Loading branch information
mtuncer committed Aug 25, 2016
2 parents 5739ebc + 3a192ff commit 1167f2f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
22 changes: 20 additions & 2 deletions cstore_fdw.c
Expand Up @@ -1513,7 +1513,8 @@ PageCount(const char *filename)
* ColumnList takes in the planner's information about this foreign table. The
* function then finds all columns needed for query execution, including those
* used in projections, joins, and filter clauses, de-duplicates these columns,
* and returns them in a new list. This function is unchanged from mongo_fdw.
* and returns them in a new list. This function is taken from mongo_fdw with
* slight modifications.
*/
static List *
ColumnList(RelOptInfo *baserel, Oid foreignTableId)
Expand All @@ -1527,6 +1528,7 @@ ColumnList(RelOptInfo *baserel, Oid foreignTableId)
#else
List *targetColumnList = baserel->reltargetlist;
#endif
ListCell *targetColumnCell = NULL;
List *restrictInfoList = baserel->baserestrictinfo;
ListCell *restrictInfoCell = NULL;
const AttrNumber wholeRow = 0;
Expand All @@ -1535,7 +1537,23 @@ ColumnList(RelOptInfo *baserel, Oid foreignTableId)
Form_pg_attribute *attributeFormArray = tupleDescriptor->attrs;

/* first add the columns used in joins and projections */
neededColumnList = list_copy(targetColumnList);
foreach(targetColumnCell, targetColumnList)
{
List *targetVarList = NIL;
Node *targetExpr = (Node *) lfirst(targetColumnCell);

#if PG_VERSION_NUM >= 90600
targetVarList = pull_var_clause(targetExpr,
PVC_RECURSE_AGGREGATES |
PVC_RECURSE_PLACEHOLDERS);
#else
targetVarList = pull_var_clause(targetExpr,
PVC_RECURSE_AGGREGATES,
PVC_RECURSE_PLACEHOLDERS);
#endif

neededColumnList = list_union(neededColumnList, targetVarList);
}

/* then walk over all restriction clauses, and pull up any used columns */
foreach(restrictInfoCell, restrictInfoList)
Expand Down
21 changes: 21 additions & 0 deletions expected/query.out
Expand Up @@ -82,3 +82,24 @@ SELECT to_json(v) FROM contestant v ORDER BY rating LIMIT 1;
{"handle":"g","birthdate":"1991-12-13","rating":1803,"percentile":85.1,"country":"XD ","achievements":["a","c"]}
(1 row)

-- Test variables used in expressions
CREATE FOREIGN TABLE union_first (a int, b int) SERVER cstore_server;
CREATE FOREIGN TABLE union_second (a int, b int) SERVER cstore_server;
INSERT INTO union_first SELECT a, a FROM generate_series(1, 5) a;
INSERT INTO union_second SELECT a, a FROM generate_series(11, 15) a;
(SELECT a*1, b FROM union_first) union all (SELECT a*1, b FROM union_second);
?column? | b
----------+----
1 | 1
2 | 2
3 | 3
4 | 4
5 | 5
11 | 11
12 | 12
13 | 13
14 | 14
15 | 15
(10 rows)

DROP FOREIGN TABLE union_first, union_second;
11 changes: 11 additions & 0 deletions sql/query.sql
Expand Up @@ -21,3 +21,14 @@ SELECT * FROM contestant_compressed ORDER BY handle;

-- Verify that we handle whole-row references correctly
SELECT to_json(v) FROM contestant v ORDER BY rating LIMIT 1;

-- Test variables used in expressions
CREATE FOREIGN TABLE union_first (a int, b int) SERVER cstore_server;
CREATE FOREIGN TABLE union_second (a int, b int) SERVER cstore_server;

INSERT INTO union_first SELECT a, a FROM generate_series(1, 5) a;
INSERT INTO union_second SELECT a, a FROM generate_series(11, 15) a;

(SELECT a*1, b FROM union_first) union all (SELECT a*1, b FROM union_second);

DROP FOREIGN TABLE union_first, union_second;

0 comments on commit 1167f2f

Please sign in to comment.