Commit 1a35685
[SPARK-37670][SQL] Support predicate pushdown and column pruning for de-duped CTEs
This PR adds predicate push-down and column pruning to CTEs that are not inlined as well as fixes a few potential correctness issues:
1) Replace (previously not inlined) CTE refs with Repartition operations at the end of logical plan optimization so that WithCTE is not carried over to physical plan. As a result, we can simplify the logic of physical planning, as well as avoid a correctness issue where the logical link of a physical plan node can point to `WithCTE` and lead to unexpected behaviors in AQE, e.g., class cast exceptions in DPP.
2) Pull (not inlined) CTE defs from subqueries up to the main query level, in order to avoid creating copies of the same CTE def during predicate push-downs and other transformations.
3) Make CTE IDs more deterministic by starting from 0 for each query.
Improve de-duped CTEs' performance with predicate pushdown and column pruning; fixes de-duped CTEs' correctness issues.
No.
Added UTs.
Closes #34929 from maryannxue/cte-followup.
Lead-authored-by: Maryann Xue <maryann.xue@gmail.com>
Co-authored-by: Wenchen Fan <wenchen@databricks.com>
Signed-off-by: Wenchen Fan <wenchen@databricks.com>
(cherry picked from commit 175e429)
Signed-off-by: Wenchen Fan <wenchen@databricks.com>1 parent 35ec300 commit 1a35685
File tree
22 files changed
+1177
-522
lines changed- sql
- catalyst/src
- main/scala/org/apache/spark/sql/catalyst
- analysis
- optimizer
- plans
- logical
- test/scala/org/apache/spark/sql/catalyst/analysis
- core/src
- main/scala/org/apache/spark/sql/execution
- adaptive
- test
- resources
- sql-tests
- inputs/subquery/scalar-subquery
- results/subquery/scalar-subquery
- tpcds-plan-stability/approved-plans-v1_4
- q23a.sf100
- q23b.sf100
- q4
- q5
- scala/org/apache/spark/sql
22 files changed
+1177
-522
lines changedLines changed: 28 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
66 | 66 | | |
67 | 67 | | |
68 | 68 | | |
69 | | - | |
| 69 | + | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
203 | 204 | | |
204 | 205 | | |
205 | 206 | | |
| |||
208 | 209 | | |
209 | 210 | | |
210 | 211 | | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
211 | 221 | | |
212 | 222 | | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
213 | 239 | | |
214 | 240 | | |
215 | 241 | | |
| |||
Lines changed: 5 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
90 | 90 | | |
91 | 91 | | |
92 | 92 | | |
93 | | - | |
94 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
95 | 97 | | |
96 | 98 | | |
97 | 99 | | |
| |||
Lines changed: 29 additions & 27 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | | - | |
32 | | - | |
33 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | | - | |
37 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
38 | 40 | | |
39 | | - | |
| 41 | + | |
| 42 | + | |
40 | 43 | | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
44 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
45 | 56 | | |
46 | 57 | | |
47 | 58 | | |
48 | 59 | | |
49 | 60 | | |
50 | | - | |
| 61 | + | |
51 | 62 | | |
52 | 63 | | |
53 | 64 | | |
| |||
93 | 104 | | |
94 | 105 | | |
95 | 106 | | |
96 | | - | |
97 | | - | |
| 107 | + | |
| 108 | + | |
98 | 109 | | |
99 | | - | |
100 | 110 | | |
101 | 111 | | |
102 | 112 | | |
103 | | - | |
| 113 | + | |
104 | 114 | | |
105 | | - | |
| 115 | + | |
106 | 116 | | |
107 | 117 | | |
108 | 118 | | |
109 | 119 | | |
110 | | - | |
| 120 | + | |
111 | 121 | | |
112 | 122 | | |
113 | 123 | | |
114 | | - | |
| 124 | + | |
115 | 125 | | |
116 | 126 | | |
117 | 127 | | |
| |||
125 | 135 | | |
126 | 136 | | |
127 | 137 | | |
128 | | - | |
129 | 138 | | |
130 | 139 | | |
131 | | - | |
132 | | - | |
| 140 | + | |
| 141 | + | |
133 | 142 | | |
134 | 143 | | |
135 | | - | |
| 144 | + | |
136 | 145 | | |
137 | | - | |
138 | 146 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
| 147 | + | |
146 | 148 | | |
147 | 149 | | |
148 | 150 | | |
Lines changed: 36 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
126 | 126 | | |
127 | 127 | | |
128 | 128 | | |
129 | | - | |
| 129 | + | |
| 130 | + | |
130 | 131 | | |
131 | 132 | | |
132 | 133 | | |
| |||
145 | 146 | | |
146 | 147 | | |
147 | 148 | | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
| 149 | + | |
163 | 150 | | |
164 | 151 | | |
165 | 152 | | |
| |||
168 | 155 | | |
169 | 156 | | |
170 | 157 | | |
| 158 | + | |
| 159 | + | |
171 | 160 | | |
172 | 161 | | |
173 | 162 | | |
| |||
204 | 193 | | |
205 | 194 | | |
206 | 195 | | |
| 196 | + | |
207 | 197 | | |
208 | 198 | | |
209 | 199 | | |
| |||
260 | 250 | | |
261 | 251 | | |
262 | 252 | | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
| 253 | + | |
271 | 254 | | |
272 | 255 | | |
273 | 256 | | |
| |||
281 | 264 | | |
282 | 265 | | |
283 | 266 | | |
284 | | - | |
285 | 267 | | |
286 | 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 | + | |
287 | 298 | | |
288 | 299 | | |
289 | 300 | | |
| |||
0 commit comments