-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathorca.c
More file actions
1320 lines (1134 loc) · 36.6 KB
/
orca.c
File metadata and controls
1320 lines (1134 loc) · 36.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* orca.c
* entrypoint to the ORCA planner and supporting routines
*
* This contains the entrypoint to the ORCA planner which is invoked via the
* standard_planner function when the optimizer GUC is set to on. Additionally,
* some supporting routines for planning with ORCA are contained herein.
*
* Portions Copyright (c) 2010-Present, VMware, Inc. or its affiliates
* Portions Copyright (c) 2005-2010, Greenplum inc
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/optimizer/plan/orca.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "cdb/cdbmutate.h" /* apply_shareinput */
#include "cdb/cdbplan.h"
#include "cdb/cdbvars.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "optimizer/clauses.h"
#include "optimizer/optimizer.h"
#include "optimizer/orca.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/planner.h"
#include "optimizer/tlist.h"
#include "optimizer/transform.h"
#include "parser/parse_collate.h"
#include "parser/parsetree.h"
#include "rewrite/rewriteManip.h"
#include "portability/instr_time.h"
#include "utils/elog.h"
#include "utils/guc.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_namespace.h"
/* GPORCA entry point */
extern PlannedStmt * GPOPTOptimizedPlan(Query *parse, bool *had_unexpected_failure, OptimizerOptions *opts);
static Plan *remove_redundant_results(PlannerInfo *root, Plan *plan);
static Node *remove_redundant_results_mutator(Node *node, void *);
static bool can_replace_tlist(Plan *plan);
static Node *push_down_expr_mutator(Node *node, List *child_tlist);
/*
* Logging of optimization outcome
*/
static void
log_optimizer(PlannedStmt *plan, bool fUnexpectedFailure)
{
/* optimizer logging is not enabled */
if (!optimizer_log)
return;
if (plan != NULL)
{
elog(DEBUG1, "GPORCA produced plan");
return;
}
/* optimizer failed to produce a plan, log failure */
if ((OPTIMIZER_ALL_FAIL == optimizer_log_failure) ||
(fUnexpectedFailure && OPTIMIZER_UNEXPECTED_FAIL == optimizer_log_failure) || /* unexpected fall back */
(!fUnexpectedFailure && OPTIMIZER_EXPECTED_FAIL == optimizer_log_failure)) /* expected fall back */
{
if (fUnexpectedFailure)
{
elog(LOG, "GPORCA failed to produce plan (unexpected)");
}
else
{
elog(LOG, "GPORCA failed to produce plan");
}
return;
}
}
/*
* support_functions_check_context
* Context structure for checking support functions, similar to eval_const_expressions_context
*/
typedef struct
{
PlannerInfo *root;
bool recurse_queries; /* recurse into query structures */
bool recurse_sublink_testexpr; /* recurse into sublink test expressions */
} support_functions_check_context;
/*
* check_support_functions_walker
* Walker function to check if a query tree contains functions with support functions
* Uses the same traversal pattern as eval_const_expressions_mutator but as a walker
*/
static bool
check_support_functions_walker(Node *node, support_functions_check_context *context)
{
if (node == NULL)
return false;
if (IsA(node, Query))
{
Query *query = (Query *) node;
if (context->recurse_queries)
{
/* Recurse into Query structures like fold_constants does */
return query_tree_walker(query, check_support_functions_walker, context, 0);
}
return false;
}
/* Handle SubLink like eval_const_expressions_mutator does */
if (IsA(node, SubLink) && !context->recurse_sublink_testexpr)
{
SubLink *sublink = (SubLink *) node;
/*
* Also invoke the walker on the sublink's Query node, so it
* can recurse into the sub-query if it wants to.
*/
return query_tree_walker((Query *) sublink->subselect, check_support_functions_walker, context, 0);
}
/* Check both FuncExpr and OpExpr nodes for prosupport functions */
if (IsA(node, FuncExpr) || IsA(node, OpExpr))
{
HeapTuple proctup;
Form_pg_proc procform;
bool has_support = false;
Oid funcid;
const char *exprtype;
/* Extract function OID from either FuncExpr or OpExpr */
if (IsA(node, FuncExpr))
{
FuncExpr *funcexpr = (FuncExpr *) node;
funcid = funcexpr->funcid;
exprtype = "FuncExpr";
}
else /* OpExpr */
{
OpExpr *opexpr = (OpExpr *) node;
funcid = opexpr->opfuncid;
exprtype = "OpExpr";
}
proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcid));
if (HeapTupleIsValid(proctup))
{
procform = (Form_pg_proc) GETSTRUCT(proctup);
has_support = OidIsValid(procform->prosupport);
/* Skip pg_catalog namespace support functions - they are safe */
if (has_support && procform->pronamespace != PG_CATALOG_NAMESPACE)
{
elog(DEBUG1, "Found %s with non-pg_catalog prosupport function, falling back to standard planner", exprtype);
ReleaseSysCache(proctup);
return true;
}
ReleaseSysCache(proctup);
}
}
return expression_tree_walker(node, check_support_functions_walker, context);
}
/*
* query_contains_support_functions
* Check if a query contains function calls that have support functions
*/
static bool
query_contains_support_functions(Query *query)
{
support_functions_check_context context;
context.root = NULL; /* We don't need PlannerInfo for this check */
context.recurse_queries = true; /* recurse into query structures */
context.recurse_sublink_testexpr = false; /* do not recurse into sublink test expressions */
/* Use the same traversal pattern as fold_constants but with a walker */
return query_or_expression_tree_walker((Node *) query, check_support_functions_walker, &context, 0);
}
/*
* optimize_query
* Plan the query using the GPORCA planner
*
* This is the main entrypoint for invoking Orca.
*/
PlannedStmt *
optimize_query(Query *parse, int cursorOptions, ParamListInfo boundParams, OptimizerOptions *options)
{
/* flag to check if optimizer unexpectedly failed to produce a plan */
bool fUnexpectedFailure = false;
PlannerInfo *root;
PlannerGlobal *glob;
Query *pqueryCopy;
PlannedStmt *result;
List *relationOids;
List *invalItems;
ListCell *lc;
ListCell *lp;
/*
* Fall back for updatable cursor
*/
if ((cursorOptions & CURSOR_OPT_UPDATABLE) != 0)
return NULL;
/*
* Initialize a dummy PlannerGlobal struct. ORCA doesn't use it, but the
* pre- and post-processing steps do.
*/
glob = makeNode(PlannerGlobal);
glob->subplans = NIL;
glob->subroots = NIL;
glob->rewindPlanIDs = NULL;
glob->transientPlan = false;
glob->oneoffPlan = false;
glob->share.shared_inputs = NULL;
glob->share.shared_input_count = 0;
glob->share.motStack = NIL;
glob->share.qdShares = NULL;
/* these will be filled in below, in the pre- and post-processing steps */
glob->finalrtable = NIL;
glob->relationOids = NIL;
glob->invalItems = NIL;
root = makeNode(PlannerInfo);
root->parse = parse;
root->glob = glob;
root->query_level = 1;
root->planner_cxt = CurrentMemoryContext;
root->wt_param_id = -1;
/* create a local copy to hand to the optimizer */
pqueryCopy = (Query *) copyObject(parse);
/*
* Pre-process the Query tree before calling optimizer.
*
* Check if rtable is NULL and query contains support functions.
* If both conditions are true, fall back to standard planner to avoid
* crashes in extension support functions that expect valid rtable.
*
* Performance note: This check does not significantly impact performance
* since pqueryCopy->rtable is non-NULL for most queries. The rtable is
* typically only NULL when the query contains sublinks, which is uncommon.
* The query_contains_support_functions() traversal only occurs in these
* rare cases.
*/
if (pqueryCopy->rtable == NULL && query_contains_support_functions(pqueryCopy))
{
elog(DEBUG1, "Query rtable is NULL and contains support functions, falling back to standard planner");
return NULL;
}
/*
* Constant folding will add dependencies to functions or relations in
* glob->invalItems, for any functions that are inlined or eliminated
* away. (We will find dependencies to other objects later, after planning).
*/
pqueryCopy = fold_constants(root, pqueryCopy, boundParams, GPOPT_MAX_FOLDED_CONSTANT_SIZE);
/*
* If any Query in the tree mixes window functions and aggregates, we need to
* transform it such that the grouped query appears as a subquery
*/
pqueryCopy = (Query *) transformGroupedWindows((Node *) pqueryCopy, NULL);
/* Ok, invoke ORCA. */
result = GPOPTOptimizedPlan(pqueryCopy, &fUnexpectedFailure, options);
log_optimizer(result, fUnexpectedFailure);
CHECK_FOR_INTERRUPTS();
/*
* If ORCA didn't produce a plan, bail out and fall back to the Postgres
* planner.
*/
if (!result)
return NULL;
/*
* Post-process the plan.
*/
/*
* ORCA filled in the final range table and subplans directly in the
* PlannedStmt. We might need to modify them still, so copy them out to
* the PlannerGlobal struct.
*/
glob->finalrtable = result->rtable;
glob->subplans = result->subplans;
glob->subplan_sliceIds = result->subplan_sliceIds;
glob->numSlices = result->numSlices;
glob->slices = result->slices;
/*
* Fake a subroot for each subplan, so that postprocessing steps don't
* choke.
*/
glob->subroots = NIL;
foreach(lp, glob->subplans)
{
PlannerInfo *subroot = makeNode(PlannerInfo);
subroot->glob = glob;
glob->subroots = lappend(glob->subroots, subroot);
}
/*
* For optimizer, we already have share_id and the plan tree is already a
* tree. However, the apply_shareinput_dag_to_tree walker does more than
* DAG conversion. It will also populate column names for RTE_CTE entries
* that will be later used for readable column names in EXPLAIN, if
* needed.
*/
foreach(lp, glob->subplans)
{
Plan *subplan = (Plan *) lfirst(lp);
collect_shareinput_producers(root, subplan);
}
collect_shareinput_producers(root, result->planTree);
/* Post-process ShareInputScan nodes */
(void) apply_shareinput_xslice(result->planTree, root);
/*
* Fix ShareInputScans for EXPLAIN, like in standard_planner(). For all
* subplans first, and then for the main plan tree.
*/
foreach(lp, glob->subplans)
{
Plan *subplan = (Plan *) lfirst(lp);
lfirst(lp) = replace_shareinput_targetlists(root, subplan);
}
result->planTree = replace_shareinput_targetlists(root, result->planTree);
result->planTree = remove_redundant_results(root, result->planTree);
/*
* To save on memory, and on the network bandwidth when the plan is
* dispatched to QEs, strip all subquery RTEs of the original Query
* objects.
*/
remove_subquery_in_RTEs((Node *) glob->finalrtable);
/*
* For plan cache invalidation purposes, extract the OIDs of all
* relations in the final range table, and of all functions used in
* expressions in the plan tree. (In the regular planner, this is done
* in set_plan_references, see that for more comments.)
*/
foreach(lc, glob->finalrtable)
{
RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
if (rte->rtekind == RTE_RELATION)
glob->relationOids = lappend_oid(glob->relationOids,
rte->relid);
}
foreach(lp, glob->subplans)
{
Plan *subplan = (Plan *) lfirst(lp);
cdb_extract_plan_dependencies(root, subplan);
}
cdb_extract_plan_dependencies(root, result->planTree);
/*
* Also extract dependencies from the original Query tree. This is needed
* to capture dependencies to e.g. views, which have been expanded at
* planning to the underlying tables, and don't appear anywhere in the
* resulting plan.
*/
extract_query_dependencies((Node *) pqueryCopy,
&relationOids,
&invalItems,
&pqueryCopy->hasRowSecurity);
glob->relationOids = list_concat(glob->relationOids, relationOids);
glob->invalItems = list_concat(glob->invalItems, invalItems);
/*
* All done! Copy the PlannerGlobal fields that we modified back to the
* PlannedStmt before returning.
*/
result->rtable = glob->finalrtable;
result->subplans = glob->subplans;
result->relationOids = glob->relationOids;
result->invalItems = glob->invalItems;
result->oneoffPlan = glob->oneoffPlan;
result->transientPlan = glob->transientPlan;
result->queryId = parse->queryId;
result->stmt_location = parse->stmt_location;
result->stmt_len = parse->stmt_len;
return result;
}
/*
* ORCA tends to generate gratuitous Result nodes for various reasons. We
* try to clean it up here, as much as we can, by eliminating the Results
* that are not really needed.
*/
static Plan *
remove_redundant_results(PlannerInfo *root, Plan *plan)
{
plan_tree_base_prefix ctx;
ctx.node = (Node *) root;
return (Plan *) remove_redundant_results_mutator((Node *) plan, &ctx);
}
static Node *
remove_redundant_results_mutator(Node *node, void *ctx)
{
if (!node)
return NULL;
if (IsA(node, Result))
{
Result *result_plan = (Result *) node;
Plan *child_plan = result_plan->plan.lefttree;
/*
* If this Result doesn't contain quals, hash filter or anything else
* funny, and the child node is projection capable, we can let the
* child node do the projection, and eliminate this Result.
*
* (We could probably push down quals and some other stuff to the child
* node if we worked a bit harder.)
*/
if (result_plan->resconstantqual == NULL &&
result_plan->numHashFilterCols == 0 &&
result_plan->plan.initPlan == NIL &&
result_plan->plan.qual == NIL &&
!expression_returns_set((Node *) result_plan->plan.targetlist) &&
can_replace_tlist(child_plan))
{
List *tlist = result_plan->plan.targetlist;
ListCell *lc;
child_plan = (Plan *)
remove_redundant_results_mutator((Node *) child_plan, ctx);
foreach(lc, tlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(lc);
tle->expr = (Expr *) push_down_expr_mutator((Node *) tle->expr,
child_plan->targetlist);
}
child_plan->targetlist = tlist;
child_plan->flow = result_plan->plan.flow;
return (Node *) child_plan;
}
}
return plan_tree_mutator(node,
remove_redundant_results_mutator,
ctx,
true);
}
/*
* Can the target list of a Plan node safely be replaced?
*/
static bool
can_replace_tlist(Plan *plan)
{
if (!plan)
return false;
/*
* SRFs in targetlists are quite funky. Don't mess with them.
* We could probably be smarter about them, but doesn't seem
* worth the trouble.
*/
if (expression_returns_set((Node *) plan->targetlist))
return false;
if (!is_projection_capable_plan(plan))
return false;
/*
* The Hash Filter column indexes in a Result node are based on
* the output target list. Can't change the target list if there's
* a Hash Filter, or it would mess up the column indexes.
*/
if (IsA(plan, Result))
{
Result *rplan = (Result *) plan;
if (rplan->numHashFilterCols > 0)
return false;
}
/*
* Split Update node also calculates a hash based on the output
* targetlist, like a Result with a Hash Filter.
*/
if (IsA(plan, SplitUpdate))
return false;
return true;
}
/*
* Fix up a target list, by replacing outer-Vars with the exprs from
* the child target list, when we're stripping off a Result node.
*/
static Node *
push_down_expr_mutator(Node *node, List *child_tlist)
{
if (!node)
return NULL;
if (IsA(node, Var))
{
Var *var = (Var *) node;
if (var->varno == OUTER_VAR && var->varattno > 0)
{
TargetEntry *child_tle = (TargetEntry *)
list_nth(child_tlist, var->varattno - 1);
// The const expr pertatining to a column in a child result node
// has const.consttypmod set as default value.
// correct typmod can be found at var.vartypmod.
// const.consttypmod value needs to be fixed before replacing var with const.
if (IsA(child_tle->expr, Const))
{
((Const *) child_tle->expr)->consttypmod = ((Var *) node)->vartypmod;
}
else if (IsA(child_tle->expr, Var))
{
((Var *) child_tle->expr)->vartypmod = ((Var *) node)->vartypmod;
}
return (Node *) child_tle->expr;
}
}
return expression_tree_mutator(node, push_down_expr_mutator, child_tlist);
}
/*
* ORCA cannot deal with window functions in the same query with
* grouping. If a query contains both, transformGroupedWindows()
* transforms it into a a query with a subquer to avoid that:
*
* If an input query (Q) mixes window functions with aggregate
* functions or grouping, then (per SQL:2003) we need to divide
* it into an outer query, Q', that contains no aggregate calls
* or grouping and an inner query, Q'', that contains no window
* calls.
*
* Q' will have a 1-entry range table whose entry corresponds to
* the results of Q''.
*
* Q'' will have the same range as Q and will be pushed down into
* a subquery range table entry in Q'.
*
* As a result, the depth of outer references in Q'' and below
* will increase, so we need to adjust non-zero xxxlevelsup fields
* (Var, Aggref, and WindowFunc nodes) in Q'' and below. At the end,
* there will be no levelsup items referring to Q'. Prior references
* to Q will now refer to Q''; prior references to blocks above Q will
* refer to the same blocks above Q'.)
*
* We do all this by creating a new Query node, subq, for Q''. We
* modify the input Query node, qry, in place for Q'. (Since qry is
* also the input, Q, be careful not to destroy values before we're
* done with them.
*
* The function is structured as a mutator, so that we can transform
* all of the Query nodes in the entire tree, bottom-up.
*/
/* Context for transformGroupedWindows() which mutates components
* of a query that mixes windowing and aggregation or grouping. It
* accumulates context for eventual construction of a subquery (the
* grouping query) during mutation of components of the outer query
* (the windowing query).
*/
typedef struct
{
List *subtlist; /* target list for subquery */
List *subgroupClause; /* group clause for subquery */
List *subgroupingSets; /* grouping sets for subquery */
List *windowClause; /* window clause for outer query */
/*
* Scratch area for init_grouped_window context and map_sgr_mutator.
*/
Index *sgr_map;
int sgr_map_size;
/*
* Scratch area for grouped_window_mutator and var_for_grouped_window_expr.
*/
List *subrtable;
int call_depth;
TargetEntry *tle;
} grouped_window_ctx;
static void init_grouped_window_context(grouped_window_ctx * ctx, Query *qry);
static Var *var_for_grouped_window_expr(grouped_window_ctx * ctx, Node *expr, bool force);
static void discard_grouped_window_context(grouped_window_ctx * ctx);
static Node *map_sgr_mutator(Node *node, void *context);
static Node *grouped_window_mutator(Node *node, void *context);
static Alias *make_replacement_alias(Query *qry, const char *aname);
static char *generate_positional_name(AttrNumber attrno);
static List *generate_alternate_vars(Var *var, grouped_window_ctx * ctx);
Node *
transformGroupedWindows(Node *node, void *context)
{
if (node == NULL)
return NULL;
if (IsA(node, Query))
{
// do a depth-first recursion into any subqueries
Query *qry = (Query *) query_tree_mutator((Query *) node, transformGroupedWindows, context, 0);
Assert(IsA(qry, Query));
/*
* we are done if this query doesn't have both window functions and group by/aggregates
*/
if (!qry->hasWindowFuncs ||
!(qry->groupClause || qry->groupingSets || qry->hasAggs))
return (Node *) qry;
Query *subq;
RangeTblEntry *rte;
RangeTblRef *ref;
Alias *alias;
bool hadSubLinks = qry->hasSubLinks;
grouped_window_ctx ctx;
Assert(qry->commandType == CMD_SELECT);
Assert(!PointerIsValid(qry->utilityStmt));
Assert(qry->returningList == NIL);
/*
* Make the new subquery (Q''). Note that (per SQL:2003) there can't be
* any window functions called in the WHERE, GROUP BY, or HAVING clauses.
*/
subq = makeNode(Query);
subq->commandType = CMD_SELECT;
subq->querySource = QSRC_PARSER;
subq->canSetTag = true;
subq->utilityStmt = NULL;
subq->resultRelation = 0;
subq->hasAggs = qry->hasAggs;
subq->hasWindowFuncs = false; /* reevaluate later */
subq->hasSubLinks = qry->hasSubLinks; /* reevaluate later */
/* Core of subquery input table expression: */
subq->rtable = qry->rtable; /* before windowing */
subq->jointree = qry->jointree; /* before windowing */
subq->targetList = NIL; /* fill in later */
subq->returningList = NIL;
subq->groupClause = qry->groupClause; /* before windowing */
subq->groupingSets = qry->groupingSets; /* before windowing */
subq->havingQual = qry->havingQual; /* before windowing */
subq->windowClause = NIL; /* by construction */
subq->distinctClause = NIL; /* after windowing */
subq->sortClause = NIL; /* after windowing */
subq->limitOffset = NULL; /* after windowing */
subq->limitCount = NULL; /* after windowing */
subq->rowMarks = NIL;
subq->setOperations = NULL;
/*
* Check if there is a window function in the join tree. If so we must
* mark hasWindowFuncs in the sub query as well.
*/
if (contain_window_function((Node *) subq->jointree))
subq->hasWindowFuncs = true;
/*
* Make the single range table entry for the outer query Q' as a wrapper
* for the subquery (Q'') currently under construction.
*/
rte = makeNode(RangeTblEntry);
rte->rtekind = RTE_SUBQUERY;
rte->subquery = subq;
rte->alias = NULL; /* fill in later */
rte->eref = NULL; /* fill in later */
rte->inFromCl = true;
rte->requiredPerms = ACL_SELECT;
/*
* Default? rte->inh = 0; rte->checkAsUser = 0;
*/
/*
* Make a reference to the new range table entry .
*/
ref = makeNode(RangeTblRef);
ref->rtindex = 1;
/*
* Set up context for mutating the target list. Careful. This is trickier
* than it looks. The context will be "primed" with grouping targets.
*/
init_grouped_window_context(&ctx, qry);
/*
* Begin rewriting the outer query in place.
*/
qry->hasAggs = false; /* by construction */
/* qry->hasSubLinks -- reevaluate later. */
/* Core of outer query input table expression: */
qry->rtable = list_make1(rte);
qry->jointree = (FromExpr *) makeNode(FromExpr);
qry->jointree->fromlist = list_make1(ref);
qry->jointree->quals = NULL;
/* qry->targetList -- to be mutated from Q to Q' below */
qry->groupClause = NIL; /* by construction */
qry->groupingSets = NIL; /* by construction */
qry->havingQual = NULL; /* by construction */
/*
* Mutate the Q target list and windowClauses for use in Q' and, at the
* same time, update state with info needed to assemble the target list
* for the subquery (Q'').
*/
qry->targetList = (List *) grouped_window_mutator((Node *) qry->targetList, &ctx);
qry->windowClause = (List *) grouped_window_mutator((Node *) qry->windowClause, &ctx);
qry->hasSubLinks = checkExprHasSubLink((Node *) qry->targetList);
/*
* New subquery fields
*/
subq->targetList = ctx.subtlist;
subq->groupClause = ctx.subgroupClause;
subq->groupingSets = ctx.subgroupingSets;
/*
* We always need an eref, but we shouldn't really need a filled in alias.
* However, view deparse (or at least the fix for MPP-2189) wants one.
*/
alias = make_replacement_alias(subq, "Window");
rte->eref = copyObject(alias);
rte->alias = alias;
/*
* Accommodate depth change in new subquery, Q''.
*/
IncrementVarSublevelsUpInTransformGroupedWindows((Node *) subq, 1, 1);
/* Might have changed. */
subq->hasSubLinks = checkExprHasSubLink((Node *) subq);
Assert(PointerIsValid(qry->targetList));
Assert(IsA(qry->targetList, List));
/*
* Use error instead of assertion to "use" hadSubLinks and keep compiler
* happy.
*/
if (hadSubLinks != (qry->hasSubLinks || subq->hasSubLinks))
elog(ERROR, "inconsistency detected in internal grouped windows transformation");
discard_grouped_window_context(&ctx);
return (Node *) qry;
}
/*
* for all other node types, just keep walking the tree
*/
return expression_tree_mutator(node, transformGroupedWindows, context);
}
/* Helper for transformGroupedWindows:
*
* Prime the subquery target list in the context with the grouping
* and windowing attributes from the given query and adjust the
* subquery group clauses in the context to agree.
*
* Note that we arrange dense sortgroupref values and stash the
* referents on the front of the subquery target list. This may
* be over-kill, but the grouping extension code seems to like it
* this way.
*
* Note that we only transfer sortgroupref values associated with
* grouping and windowing to the subquery context. The subquery
* shouldn't care about ordering, etc. XXX
*/
static void
init_grouped_window_context(grouped_window_ctx * ctx, Query *qry)
{
List *grp_tles;
List *grp_sortops;
List *grp_eqops;
ListCell *lc = NULL;
Index maxsgr = 0;
get_sortgroupclauses_tles(qry->groupClause, qry->targetList,
&grp_tles, &grp_sortops, &grp_eqops);
list_free(grp_sortops);
maxsgr = maxSortGroupRef(grp_tles, true);
ctx->subtlist = NIL;
ctx->subgroupClause = NIL;
ctx->subgroupingSets = NIL;
/*
* Set up scratch space.
*/
ctx->subrtable = qry->rtable;
/*
* Map input = outer query sortgroupref values to subquery values while
* building the subquery target list prefix.
*/
ctx->sgr_map = palloc0((maxsgr + 1) * sizeof(ctx->sgr_map[0]));
ctx->sgr_map_size = maxsgr + 1;
foreach(lc, grp_tles)
{
TargetEntry *tle;
Index old_sgr;
tle = (TargetEntry *) copyObject(lfirst(lc));
old_sgr = tle->ressortgroupref;
ctx->subtlist = lappend(ctx->subtlist, tle);
tle->resno = list_length(ctx->subtlist);
tle->ressortgroupref = tle->resno;
tle->resjunk = false;
ctx->sgr_map[old_sgr] = tle->ressortgroupref;
}
/* Miscellaneous scratch area. */
ctx->call_depth = 0;
ctx->tle = NULL;
/* Revise grouping into ctx->subgroupClause */
ctx->subgroupClause = (List *) map_sgr_mutator((Node *) qry->groupClause, ctx);
ctx->subgroupingSets = (List *) map_sgr_mutator((Node *) qry->groupingSets, ctx);
}
/* Helper for transformGroupedWindows */
static void
discard_grouped_window_context(grouped_window_ctx * ctx)
{
ctx->subtlist = NIL;
ctx->subgroupClause = NIL;
ctx->subgroupingSets = NIL;
ctx->tle = NULL;
if (ctx->sgr_map)
pfree(ctx->sgr_map);
ctx->sgr_map = NULL;
ctx->subrtable = NULL;
}
/* Helper for transformGroupedWindows:
*
* Look for the given expression in the context's subtlist. If
* none is found and the force argument is true, add a target
* for it. Make and return a variable referring to the target
* with the matching expression, or return NULL, if no target
* was found/added.
*/
static Var *
var_for_grouped_window_expr(grouped_window_ctx * ctx, Node *expr, bool force)
{
Var *var = NULL;
TargetEntry *tle = tlist_member((Expr *) expr, ctx->subtlist);
if (tle == NULL && force)
{
tle = makeNode(TargetEntry);
ctx->subtlist = lappend(ctx->subtlist, tle);
tle->expr = (Expr *) expr;
tle->resno = list_length(ctx->subtlist);
/*
* See comment in grouped_window_mutator for why level 3 is
* appropriate.
*/
if (ctx->call_depth == 3 && ctx->tle != NULL && ctx->tle->resname != NULL)
{
tle->resname = pstrdup(ctx->tle->resname);
}
else
{
tle->resname = generate_positional_name(tle->resno);
}
tle->ressortgroupref = 0;
tle->resorigtbl = 0;
tle->resorigcol = 0;
tle->resjunk = false;
}
if (tle != NULL)
{
var = makeNode(Var);
var->varno = 1; /* one and only */
var->varattno = tle->resno; /* by construction */
var->vartype = exprType((Node *) tle->expr);
var->vartypmod = exprTypmod((Node *) tle->expr);
var->varcollid = exprCollation((Node *) tle->expr);
var->varlevelsup = 0;
var->varnosyn = 1;
var->varattnosyn = tle->resno;
var->location = 0;
}
return var;
}
/* Helper for transformGroupedWindows:
*
* Mutator for subquery groupingClause to adjust sortgroupref values
* based on map developed while priming context target list.
*/
static Node *
map_sgr_mutator(Node *node, void *context)
{
grouped_window_ctx *ctx = (grouped_window_ctx *) context;
if (!node)
return NULL;
if (IsA(node, List))
{
ListCell *lc;
List *new_lst = NIL;
foreach(lc, (List *) node)
{
Node *newnode = lfirst(lc);
newnode = map_sgr_mutator(newnode, ctx);
new_lst = lappend(new_lst, newnode);
}
return (Node *) new_lst;
}
else if (IsA(node, IntList))
{
ListCell *lc;
List *new_lst = NIL;
foreach(lc, (List *) node)
{
int sortgroupref = lfirst_int(lc);
if (sortgroupref < 0 || sortgroupref >= ctx->sgr_map_size)
elog(ERROR, "sortgroupref %d out of bounds", sortgroupref);
sortgroupref = ctx->sgr_map[sortgroupref];
new_lst = lappend_int(new_lst, sortgroupref);
}
return (Node *) new_lst;
}
else if (IsA(node, SortGroupClause))
{
SortGroupClause *g = (SortGroupClause *) node;
SortGroupClause *new_g = makeNode(SortGroupClause);
memcpy(new_g, g, sizeof(SortGroupClause));
new_g->tleSortGroupRef = ctx->sgr_map[g->tleSortGroupRef];
return (Node *) new_g;
}
else if (IsA(node, GroupingSet))