address review

pull/4470/head
Sait Talha Nisanci 2021-01-06 21:59:39 +03:00
parent dbc571478a
commit cc1d3fddf0
2 changed files with 44 additions and 35 deletions

View File

@ -147,7 +147,8 @@ static bool ShouldRecursivelyPlanNonColocatedSubqueries(Query *subquery,
RecursivePlanningContext * RecursivePlanningContext *
context); context);
static bool ShouldConvertLocalTableJoinsToSubqueries(List *rangeTableList, static bool ShouldConvertLocalTableJoinsToSubqueries(List *rangeTableList,
List *tableList, List *rtableList); List *referencedRteList,
List *rtableList);
static bool ContainsSubquery(Query *query); static bool ContainsSubquery(Query *query);
static void RecursivelyPlanNonColocatedSubqueries(Query *subquery, static void RecursivelyPlanNonColocatedSubqueries(Query *subquery,
@ -200,9 +201,7 @@ static Query * CreateOuterSubquery(RangeTblEntry *rangeTableEntry,
List *outerSubqueryTargetList); List *outerSubqueryTargetList);
static List * GenerateRequiredColNamesFromTargetList(List *targetList); static List * GenerateRequiredColNamesFromTargetList(List *targetList);
static char * GetRelationNameAndAliasName(RangeTblEntry *rangeTablentry); static char * GetRelationNameAndAliasName(RangeTblEntry *rangeTablentry);
static void ContainsLocalTableDistributedTableVars(List *tableList, List *rtableList, static bool ContainsDistributedTable(List *rteList);
bool *containsLocalTable,
bool *containsDistributedTable);
/* /*
* GenerateSubplansForSubqueriesAndCTEs is a wrapper around RecursivelyPlanSubqueriesAndCTEs. * GenerateSubplansForSubqueriesAndCTEs is a wrapper around RecursivelyPlanSubqueriesAndCTEs.
@ -361,8 +360,8 @@ RecursivelyPlanSubqueriesAndCTEs(Query *query, RecursivePlanningContext *context
RecursivelyPlanNonColocatedSubqueries(query, context); RecursivelyPlanNonColocatedSubqueries(query, context);
} }
List *tableList = PullAllRangeTablesEntries(query, context->rtableList); List *rteList = PullAllRangeTablesEntries(query, context->rtableList);
if (ShouldConvertLocalTableJoinsToSubqueries(query->rtable, tableList, if (ShouldConvertLocalTableJoinsToSubqueries(query->rtable, rteList,
context->rtableList)) context->rtableList))
{ {
/* /*
@ -383,7 +382,7 @@ RecursivelyPlanSubqueriesAndCTEs(Query *query, RecursivePlanningContext *context
* convert local-dist table joins to subqueries. * convert local-dist table joins to subqueries.
*/ */
static bool static bool
ShouldConvertLocalTableJoinsToSubqueries(List *rangeTableList, List *tableList, ShouldConvertLocalTableJoinsToSubqueries(List *rangeTableList, List *referencedRteList,
List *rtableList) List *rtableList)
{ {
if (LocalTableJoinPolicy == LOCAL_JOIN_POLICY_NEVER) if (LocalTableJoinPolicy == LOCAL_JOIN_POLICY_NEVER)
@ -401,33 +400,41 @@ ShouldConvertLocalTableJoinsToSubqueries(List *rangeTableList, List *tableList,
return true; return true;
} }
/* if we have vars that references local or distributed tables, it means we need to do subquery-conversion */ if (!containsLocalTable)
ContainsLocalTableDistributedTableVars(tableList, rtableList, &containsLocalTable, {
&containsDistributedTable); /* if there is no local table, we don't need to do any conversion */
return false;
}
return containsDistributedTable && containsLocalTable; /*
* if referenced table list of this query contains a distributed table
* we need to do a convertion
*/
if (ContainsDistributedTable(referencedRteList))
{
return true;
}
return false;
} }
static void /*
ContainsLocalTableDistributedTableVars(List *tableList, List *rtableList, * ContainsDistributedTable returns true if the given rteList contains
bool *containsLocalTable, * a distributed table rte.
bool *containsDistributedTable) */
static bool
ContainsDistributedTable(List *rteList)
{ {
RangeTblEntry *rte = NULL; RangeTblEntry *rte = NULL;
foreach_ptr(rte, tableList) foreach_ptr(rte, rteList)
{ {
if (IsDistributedOrReferenceTableRTE((Node *) rte)) if (IsDistributedOrReferenceTableRTE((Node *) rte))
{ {
*containsDistributedTable = true; return true;
}
else if (IsRecursivelyPlannableRelation(rte) &&
IsLocalTableRteOrMatView((Node *) rte))
{
/* we consider citus local tables as local table */
*containsLocalTable = true;
} }
} }
return false;
} }
@ -1797,7 +1804,6 @@ TransformFunctionRTE(RangeTblEntry *rangeTblEntry)
subquery->targetList = lappend(subquery->targetList, targetEntry); subquery->targetList = lappend(subquery->targetList, targetEntry);
} }
} }
/* /*
* If tupleDesc is NULL we have 2 different cases: * If tupleDesc is NULL we have 2 different cases:
* *
@ -1847,7 +1853,6 @@ TransformFunctionRTE(RangeTblEntry *rangeTblEntry)
columnType = list_nth_oid(rangeTblFunction->funccoltypes, columnType = list_nth_oid(rangeTblFunction->funccoltypes,
targetColumnIndex); targetColumnIndex);
} }
/* use the types in the function definition otherwise */ /* use the types in the function definition otherwise */
else else
{ {

View File

@ -31,8 +31,8 @@
typedef struct PullTableContext typedef struct PullTableContext
{ {
List *rteList; List *referencedRteList; /* stores all the RTEs that are referenced by a var */
List *rtableList; List *rtableList; /* contains rtable's of upper queries, including the current query */
}PullTableContext; }PullTableContext;
@ -51,14 +51,14 @@ PullAllRangeTablesEntries(Query *query, List *rtableList)
{ {
PullTableContext p; PullTableContext p;
p.rtableList = rtableList; p.rtableList = rtableList;
p.rteList = NIL; p.referencedRteList = NIL;
query_or_expression_tree_walker((Node *) query, query_or_expression_tree_walker((Node *) query,
PullAllRangeTablesEntriesWalker, PullAllRangeTablesEntriesWalker,
(void *) &p, (void *) &p,
0); 0);
return p.rteList; return p.referencedRteList;
} }
@ -73,7 +73,7 @@ PullAllRangeTablesEntriesWalker(Node *node, PullTableContext *pullTableContext)
{ {
return false; return false;
} }
if (IsA(node, Var)) else if (IsA(node, Var))
{ {
Var *var = (Var *) node; Var *var = (Var *) node;
@ -86,17 +86,18 @@ PullAllRangeTablesEntriesWalker(Node *node, PullTableContext *pullTableContext)
} }
List *rtable = (List *) list_nth(rtableList, index); List *rtable = (List *) list_nth(rtableList, index);
RangeTblEntry *rte = (RangeTblEntry *) list_nth(rtable, var->varno - 1); RangeTblEntry *rte = (RangeTblEntry *) list_nth(rtable, var->varno - 1);
pullTableContext->rteList = lappend(pullTableContext->rteList, rte); pullTableContext->referencedRteList = lappend(pullTableContext->referencedRteList,
rte);
return false; return false;
} }
if (IsA(node, PlaceHolderVar)) else if (IsA(node, PlaceHolderVar))
{ {
/* PlaceHolderVar *phv = (PlaceHolderVar *) node; */ /* PlaceHolderVar *phv = (PlaceHolderVar *) node; */
/* we don't want to look into the contained expression */ /* we don't want to look into the contained expression */
return false; return false;
} }
if (IsA(node, Query)) else if (IsA(node, Query))
{ {
/* Recurse into RTE subquery or not-yet-planned sublink subquery */ /* Recurse into RTE subquery or not-yet-planned sublink subquery */
@ -107,6 +108,9 @@ PullAllRangeTablesEntriesWalker(Node *node, PullTableContext *pullTableContext)
pullTableContext->rtableList = list_delete_first(pullTableContext->rtableList); pullTableContext->rtableList = list_delete_first(pullTableContext->rtableList);
return result; return result;
} }
return expression_tree_walker(node, PullAllRangeTablesEntriesWalker, else
(void *) pullTableContext); {
return expression_tree_walker(node, PullAllRangeTablesEntriesWalker,
(void *) pullTableContext);
}
} }