From d682452424551c828c39f2c71da8c14117d8d83a Mon Sep 17 00:00:00 2001 From: aykutbozkurt Date: Mon, 24 Apr 2023 17:50:42 +0300 Subject: [PATCH] refactor --- .../distributed/planner/distributed_planner.c | 83 ++++--------------- 1 file changed, 18 insertions(+), 65 deletions(-) diff --git a/src/backend/distributed/planner/distributed_planner.c b/src/backend/distributed/planner/distributed_planner.c index 60eb426fc..67b0dc790 100644 --- a/src/backend/distributed/planner/distributed_planner.c +++ b/src/backend/distributed/planner/distributed_planner.c @@ -92,15 +92,8 @@ static bool ListContainsDistributedTableRTE(List *rangeTableList, bool *maybeHasForeignDistributedTable); static PlannedStmt * CreateDistributedPlannedStmt( DistributedPlanningContext *planContext); -static PlannedStmt * InlineCtesAndCreateDistributedPlannedStmt(uint64 planId, - DistributedPlanningContext - *planContext); -static PlannedStmt * TryCreateDistributedPlannedStmt(PlannedStmt *localPlan, - Query *originalQuery, - Query *query, ParamListInfo - boundParams, - PlannerRestrictionContext * - plannerRestrictionContext); +static PlannedStmt * TryInlineCtesAndCreateDistributedPlannedStmt( + DistributedPlanningContext *planContext); static DeferredErrorMessage * DeferErrorIfPartitionTableNotSingleReplicated(Oid relationId); @@ -711,7 +704,7 @@ CreateDistributedPlannedStmt(DistributedPlanningContext *planContext) * if the distributed planning fails with inlined CTEs, because recursively * planning CTEs can provide full SQL coverage, although it might be slow. */ - resultPlan = InlineCtesAndCreateDistributedPlannedStmt(planId, planContext); + resultPlan = TryInlineCtesAndCreateDistributedPlannedStmt(planContext); if (resultPlan != NULL) { return resultPlan; @@ -796,7 +789,7 @@ CreateDistributedPlannedStmt(DistributedPlanningContext *planContext) /* - * InlineCtesAndCreateDistributedPlannedStmt gets all the parameters required + * TryInlineCtesAndCreateDistributedPlannedStmt gets all the parameters required * for creating a distributed planned statement. The function is primarily a * wrapper on top of CreateDistributedPlannedStmt(), by first inlining the * CTEs and calling CreateDistributedPlannedStmt() in PG_TRY() block. The @@ -804,68 +797,28 @@ CreateDistributedPlannedStmt(DistributedPlanningContext *planContext) * CTEs are inlined. */ static PlannedStmt * -InlineCtesAndCreateDistributedPlannedStmt(uint64 planId, - DistributedPlanningContext *planContext) -{ - /* - * We'll inline the CTEs and try distributed planning, preserve the original - * query ,the modified query, and planner context in case the planning fails - * and we fallback to recursive planning of CTEs. - */ - Query *copyOfOriginalQuery = copyObject(planContext->originalQuery); - - RecursivelyInlineCtesInQueryTree(copyOfOriginalQuery); - - /* after inlining, we shouldn't have any inlinable CTEs */ - Assert(!QueryTreeContainsInlinableCTE(copyOfOriginalQuery)); - - /* recompute modified query and planner context after we inlined the query */ - PlannerRestrictionContext *plannerContextForInlinedQuery = - CreateAndPushPlannerRestrictionContext(); - Query *copyOfInlinedOriginalQuery = copyObject(copyOfOriginalQuery); - standard_planner(copyOfInlinedOriginalQuery, NULL, 0, planContext->boundParams); - Query *modifiedInlinedQuery = copyOfInlinedOriginalQuery; - - /* simply recurse into CreateDistributedPlannedStmt() in a PG_TRY() block */ - PlannedStmt *result = TryCreateDistributedPlannedStmt(planContext->plan, - copyOfOriginalQuery, - modifiedInlinedQuery, - planContext->boundParams, - plannerContextForInlinedQuery); - - PopPlannerRestrictionContext(); - - return result; -} - - -/* - * TryCreateDistributedPlannedStmt is a wrapper around CreateDistributedPlannedStmt, simply - * calling it in PG_TRY()/PG_CATCH() block. The function returns a PlannedStmt if the input - * query can be planned by Citus. If not, the function returns NULL and generates a DEBUG4 - * message with the reason for the failure. - */ -static PlannedStmt * -TryCreateDistributedPlannedStmt(PlannedStmt *localPlan, - Query *originalQuery, - Query *query, ParamListInfo boundParams, - PlannerRestrictionContext *plannerRestrictionContext) +TryInlineCtesAndCreateDistributedPlannedStmt(DistributedPlanningContext *planContext) { MemoryContext savedContext = CurrentMemoryContext; PlannedStmt *result = NULL; - DistributedPlanningContext *planContext = palloc0(sizeof(DistributedPlanningContext)); - - planContext->plan = localPlan; - planContext->boundParams = boundParams; - planContext->originalQuery = originalQuery; - planContext->query = query; - planContext->plannerRestrictionContext = plannerRestrictionContext; + /* + * We'll inline the CTEs and try distributed planning, preserve the original + * query, the modified query, and planner context in case the planning fails + * and we fallback to recursive planning of CTEs. + */ + Query *inlinedQuery = copyObject(planContext->originalQuery); + RecursivelyInlineCtesInQueryTree(inlinedQuery); + /* after inlining, we shouldn't have any inlinable CTEs */ + Assert(!QueryTreeContainsInlinableCTE(inlinedQuery)); PG_TRY(); { - result = CreateDistributedPlannedStmt(planContext); + /* replan after we inlined the query */ + result = planner(inlinedQuery, + NULL, 0, + planContext->boundParams); } PG_CATCH(); {