Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce function calls to var's during requested column computation #112

Merged
merged 1 commit into from Aug 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;