forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit247dea8

Richard Guo
Introduce an RTE for the grouping step
If there are subqueries in the grouping expressions, each of thesesubqueries in the targetlist and HAVING clause is expanded intodistinct SubPlan nodes. As a result, only one of these SubPlan nodeswould be converted to reference to the grouping key column output bythe Agg node; others would have to get evaluated afresh. This is notefficient, and with grouping sets this can cause wrong results issuesin cases where they should go to NULL because they are from the wronggrouping set. Furthermore, during re-evaluation, these SubPlan nodesmight use nulled column values from grouping sets, which is notcorrect.This issue is not limited to subqueries. For other types ofexpressions that are part of grouping items, if they are transformedinto another form during preprocessing, they may fail to match lowertarget items. This can also lead to wrong results with grouping sets.To fix this issue, we introduce a new kind of RTE representing theoutput of the grouping step, with columns that are the Vars orexpressions being grouped on. In the parser, we replace the groupingexpressions in the targetlist and HAVING clause with Vars referencingthis new RTE, so that the output of the parser directly expresses thesemantic requirement that the grouping expressions be gotten from thegrouping output rather than computed some other way. In the planner,we first preprocess all the columns of this new RTE and then replaceany Vars in the targetlist and HAVING clause that reference this newRTE with the underlying grouping expressions, so that we will haveonly one instance of a SubPlan node for each subquery contained in thegrouping expressions.Bump catversion because this changes the querytree produced by theparser.Thanks to Tom Lane for the idea to invent a new kind of RTE.Per reports from Geoff Winkless, Tobias Wendorff, Richard Guo fromvarious threads.Author: Richard GuoReviewed-by: Ashutosh Bapat, Sutou KouheiDiscussion:https://postgr.es/m/CAMbWs4_dp7e7oTwaiZeBX8+P1rXw4ThkZxh1QG81rhu9Z47VsQ@mail.gmail.com1 parentfba49d5 commit247dea8
File tree
25 files changed
+731
-86
lines changed- src
- backend
- commands
- nodes
- optimizer
- path
- plan
- prep
- util
- parser
- utils/adt
- include
- catalog
- commands
- nodes
- optimizer
- parser
- test/regress
- expected
- sql
- tools/pgindent
25 files changed
+731
-86
lines changedLines changed: 18 additions & 6 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
879 | 879 |
| |
880 | 880 |
| |
881 | 881 |
| |
| 882 | + | |
882 | 883 |
| |
883 | 884 |
| |
884 | 885 |
| |
| |||
889 | 890 |
| |
890 | 891 |
| |
891 | 892 |
| |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
892 | 904 |
| |
893 | 905 |
| |
894 | 906 |
| |
| |||
2474 | 2486 |
| |
2475 | 2487 |
| |
2476 | 2488 |
| |
2477 |
| - | |
| 2489 | + | |
2478 | 2490 |
| |
2479 | 2491 |
| |
2480 | 2492 |
| |
| |||
2558 | 2570 |
| |
2559 | 2571 |
| |
2560 | 2572 |
| |
2561 |
| - | |
| 2573 | + | |
2562 | 2574 |
| |
2563 | 2575 |
| |
2564 | 2576 |
| |
| |||
2648 | 2660 |
| |
2649 | 2661 |
| |
2650 | 2662 |
| |
2651 |
| - | |
| 2663 | + | |
2652 | 2664 |
| |
2653 | 2665 |
| |
2654 | 2666 |
| |
| |||
2788 | 2800 |
| |
2789 | 2801 |
| |
2790 | 2802 |
| |
2791 |
| - | |
| 2803 | + | |
2792 | 2804 |
| |
2793 | 2805 |
| |
2794 | 2806 |
| |
| |||
2900 | 2912 |
| |
2901 | 2913 |
| |
2902 | 2914 |
| |
2903 |
| - | |
| 2915 | + | |
2904 | 2916 |
| |
2905 | 2917 |
| |
2906 | 2918 |
| |
| |||
3386 | 3398 |
| |
3387 | 3399 |
| |
3388 | 3400 |
| |
3389 |
| - | |
| 3401 | + | |
3390 | 3402 |
| |
3391 | 3403 |
| |
3392 | 3404 |
| |
|
Lines changed: 14 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2854 | 2854 |
| |
2855 | 2855 |
| |
2856 | 2856 |
| |
| 2857 | + | |
| 2858 | + | |
| 2859 | + | |
| 2860 | + | |
| 2861 | + | |
2857 | 2862 |
| |
2858 | 2863 |
| |
2859 | 2864 |
| |
| |||
3891 | 3896 |
| |
3892 | 3897 |
| |
3893 | 3898 |
| |
| 3899 | + | |
| 3900 | + | |
| 3901 | + | |
| 3902 | + | |
| 3903 | + | |
| 3904 | + | |
| 3905 | + | |
| 3906 | + | |
| 3907 | + | |
3894 | 3908 |
| |
3895 | 3909 |
| |
3896 | 3910 |
| |
|
Lines changed: 3 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
562 | 562 |
| |
563 | 563 |
| |
564 | 564 |
| |
| 565 | + | |
| 566 | + | |
| 567 | + | |
565 | 568 |
| |
566 | 569 |
| |
567 | 570 |
| |
|
Lines changed: 4 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
300 | 300 |
| |
301 | 301 |
| |
302 | 302 |
| |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
303 | 307 |
| |
304 | 308 |
| |
305 | 309 |
| |
|
Lines changed: 3 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
422 | 422 |
| |
423 | 423 |
| |
424 | 424 |
| |
| 425 | + | |
| 426 | + | |
| 427 | + | |
425 | 428 |
| |
426 | 429 |
| |
427 | 430 |
| |
|
Lines changed: 4 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
731 | 731 |
| |
732 | 732 |
| |
733 | 733 |
| |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
734 | 738 |
| |
735 | 739 |
| |
736 | 740 |
| |
|
Lines changed: 61 additions & 9 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
88 | 88 |
| |
89 | 89 |
| |
90 | 90 |
| |
| 91 | + | |
91 | 92 |
| |
92 | 93 |
| |
93 | 94 |
| |
| |||
748 | 749 |
| |
749 | 750 |
| |
750 | 751 |
| |
| 752 | + | |
751 | 753 |
| |
752 | 754 |
| |
753 | 755 |
| |
| |||
781 | 783 |
| |
782 | 784 |
| |
783 | 785 |
| |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
784 | 790 |
| |
785 | 791 |
| |
786 | 792 |
| |
| |||
836 | 842 |
| |
837 | 843 |
| |
838 | 844 |
| |
839 |
| - | |
840 |
| - | |
841 |
| - | |
842 |
| - | |
843 | 845 |
| |
844 | 846 |
| |
845 | 847 |
| |
| |||
969 | 971 |
| |
970 | 972 |
| |
971 | 973 |
| |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
972 | 981 |
| |
973 | 982 |
| |
974 | 983 |
| |
| |||
1005 | 1014 |
| |
1006 | 1015 |
| |
1007 | 1016 |
| |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
| 1023 | + | |
| 1024 | + | |
| 1025 | + | |
| 1026 | + | |
| 1027 | + | |
| 1028 | + | |
| 1029 | + | |
| 1030 | + | |
| 1031 | + | |
| 1032 | + | |
| 1033 | + | |
| 1034 | + | |
| 1035 | + | |
| 1036 | + | |
| 1037 | + | |
1008 | 1038 |
| |
1009 | 1039 |
| |
1010 | 1040 |
| |
| |||
1032 | 1062 |
| |
1033 | 1063 |
| |
1034 | 1064 |
| |
| 1065 | + | |
| 1066 | + | |
| 1067 | + | |
| 1068 | + | |
| 1069 | + | |
| 1070 | + | |
| 1071 | + | |
| 1072 | + | |
| 1073 | + | |
| 1074 | + | |
1035 | 1075 |
| |
1036 | 1076 |
| |
1037 | 1077 |
| |
| |||
1051 | 1091 |
| |
1052 | 1092 |
| |
1053 | 1093 |
| |
1054 |
| - | |
| 1094 | + | |
| 1095 | + | |
| 1096 | + | |
| 1097 | + | |
| 1098 | + | |
| 1099 | + | |
1055 | 1100 |
| |
1056 |
| - | |
| 1101 | + | |
| 1102 | + | |
1057 | 1103 |
| |
1058 | 1104 |
| |
1059 | 1105 |
| |
1060 |
| - | |
| 1106 | + | |
| 1107 | + | |
| 1108 | + | |
| 1109 | + | |
| 1110 | + | |
| 1111 | + | |
1061 | 1112 |
| |
1062 |
| - | |
1063 |
| - | |
| 1113 | + | |
| 1114 | + | |
| 1115 | + | |
1064 | 1116 |
| |
1065 | 1117 |
| |
1066 | 1118 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
557 | 557 |
| |
558 | 558 |
| |
559 | 559 |
| |
| 560 | + | |
560 | 561 |
| |
561 | 562 |
| |
562 | 563 |
| |
|
Lines changed: 8 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1235 | 1235 |
| |
1236 | 1236 |
| |
1237 | 1237 |
| |
| 1238 | + | |
1238 | 1239 |
| |
1239 | 1240 |
| |
1240 | 1241 |
| |
| |||
2218 | 2219 |
| |
2219 | 2220 |
| |
2220 | 2221 |
| |
2221 |
| - | |
| 2222 | + | |
| 2223 | + | |
2222 | 2224 |
| |
2223 | 2225 |
| |
2224 | 2226 |
| |
| |||
2228 | 2230 |
| |
2229 | 2231 |
| |
2230 | 2232 |
| |
| 2233 | + | |
| 2234 | + | |
| 2235 | + | |
| 2236 | + | |
2231 | 2237 |
| |
2232 | 2238 |
| |
2233 | 2239 |
| |
| |||
2293 | 2299 |
| |
2294 | 2300 |
| |
2295 | 2301 |
| |
| 2302 | + | |
2296 | 2303 |
| |
2297 | 2304 |
| |
2298 | 2305 |
| |
|
0 commit comments
Comments
(0)