-
Notifications
You must be signed in to change notification settings - Fork 28k
/
complexTypesSuite.scala
379 lines (341 loc) · 13.1 KB
/
complexTypesSuite.scala
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql.catalyst.optimizer
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.catalyst.dsl.plans._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenContext
import org.apache.spark.sql.catalyst.plans.PlanTest
import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan, Range}
import org.apache.spark.sql.catalyst.rules.RuleExecutor
import org.apache.spark.sql.catalyst.util.GenericArrayData
import org.apache.spark.sql.types._
/**
* SPARK-18601 discusses simplification direct access to complex types creators.
* i.e. {{{create_named_struct(square, `x` * `x`).square}}} can be simplified to {{{`x` * `x`}}}.
* sam applies to create_array and create_map
*/
class ComplexTypesSuite extends PlanTest with ExpressionEvalHelper {
object Optimizer extends RuleExecutor[LogicalPlan] {
val batches =
Batch("collapse projections", FixedPoint(10),
CollapseProject) ::
Batch("Constant Folding", FixedPoint(10),
NullPropagation,
ConstantFolding,
BooleanSimplification,
SimplifyConditionals,
SimplifyBinaryComparison,
SimplifyExtractValueOps) :: Nil
}
val idAtt = ('id).long.notNull
val nullableIdAtt = ('nullable_id).long
lazy val relation = LocalRelation(idAtt, nullableIdAtt)
test("explicit get from namedStruct") {
val query = relation
.select(
GetStructField(
CreateNamedStruct(Seq("att", 'id )),
0,
None) as "outerAtt").analyze
val expected = relation.select('id as "outerAtt").analyze
comparePlans(Optimizer execute query, expected)
}
test("explicit get from named_struct- expression maintains original deduced alias") {
val query = relation
.select(GetStructField(CreateNamedStruct(Seq("att", 'id)), 0, None))
.analyze
val expected = relation
.select('id as "named_struct(att, id).att")
.analyze
comparePlans(Optimizer execute query, expected)
}
test("collapsed getStructField ontop of namedStruct") {
val query = relation
.select(CreateNamedStruct(Seq("att", 'id)) as "struct1")
.select(GetStructField('struct1, 0, None) as "struct1Att")
.analyze
val expected = relation.select('id as "struct1Att").analyze
comparePlans(Optimizer execute query, expected)
}
test("collapse multiple CreateNamedStruct/GetStructField pairs") {
val query = relation
.select(
CreateNamedStruct(Seq(
"att1", 'id,
"att2", 'id * 'id)) as "struct1")
.select(
GetStructField('struct1, 0, None) as "struct1Att1",
GetStructField('struct1, 1, None) as "struct1Att2")
.analyze
val expected =
relation.
select(
'id as "struct1Att1",
('id * 'id) as "struct1Att2")
.analyze
comparePlans(Optimizer execute query, expected)
}
test("collapsed2 - deduced names") {
val query = relation
.select(
CreateNamedStruct(Seq(
"att1", 'id,
"att2", 'id * 'id)) as "struct1")
.select(
GetStructField('struct1, 0, None),
GetStructField('struct1, 1, None))
.analyze
val expected =
relation.
select(
'id as "struct1.att1",
('id * 'id) as "struct1.att2")
.analyze
comparePlans(Optimizer execute query, expected)
}
test("simplified array ops") {
val rel = relation.select(
CreateArray(Seq(
CreateNamedStruct(Seq(
"att1", 'id,
"att2", 'id * 'id)),
CreateNamedStruct(Seq(
"att1", 'id + 1,
"att2", ('id + 1) * ('id + 1))
))
) as "arr"
)
val query = rel
.select(
GetArrayStructFields('arr, StructField("att1", LongType, false), 0, 1, false) as "a1",
GetArrayItem('arr, 1) as "a2",
GetStructField(GetArrayItem('arr, 1), 0, None) as "a3",
GetArrayItem(
GetArrayStructFields('arr,
StructField("att1", LongType, false),
0,
1,
false),
1) as "a4")
.analyze
val expected = relation
.select(
CreateArray(Seq('id, 'id + 1L)) as "a1",
CreateNamedStruct(Seq(
"att1", ('id + 1L),
"att2", (('id + 1L) * ('id + 1L)))) as "a2",
('id + 1L) as "a3",
('id + 1L) as "a4")
.analyze
comparePlans(Optimizer execute query, expected)
}
test("SPARK-22570: CreateArray should not create a lot of global variables") {
val ctx = new CodegenContext
CreateArray(Seq(Literal(1))).genCode(ctx)
assert(ctx.inlinedMutableStates.length == 0)
}
test("SPARK-23208: Test code splitting for create array related methods") {
val inputs = (1 to 2500).map(x => Literal(s"l_$x"))
checkEvaluation(CreateArray(inputs), new GenericArrayData(inputs.map(_.eval())))
}
test("simplify map ops") {
val rel = relation
.select(
CreateMap(Seq(
"r1", CreateNamedStruct(Seq("att1", 'id)),
"r2", CreateNamedStruct(Seq("att1", ('id + 1L))))) as "m")
val query = rel
.select(
GetMapValue('m, "r1") as "a1",
GetStructField(GetMapValue('m, "r1"), 0, None) as "a2",
GetMapValue('m, "r32") as "a3",
GetStructField(GetMapValue('m, "r32"), 0, None) as "a4")
.analyze
val expected =
relation.select(
CreateNamedStruct(Seq("att1", 'id)) as "a1",
'id as "a2",
Literal.create(
null,
StructType(
StructField("att1", LongType, nullable = false) :: Nil
)
) as "a3",
Literal.create(null, LongType) as "a4")
.analyze
comparePlans(Optimizer execute query, expected)
}
test("simplify map ops, constant lookup, dynamic keys") {
val query = relation.select(
GetMapValue(
CreateMap(Seq(
'id, ('id + 1L),
('id + 1L), ('id + 2L),
('id + 2L), ('id + 3L),
Literal(13L), 'id,
('id + 3L), ('id + 4L),
('id + 4L), ('id + 5L))),
13L) as "a")
.analyze
val expected = relation
.select(
CaseWhen(Seq(
(EqualTo(13L, 'id), ('id + 1L)),
(EqualTo(13L, ('id + 1L)), ('id + 2L)),
(EqualTo(13L, ('id + 2L)), ('id + 3L)),
(Literal(true), 'id))) as "a")
.analyze
comparePlans(Optimizer execute query, expected)
}
test("simplify map ops, dynamic lookup, dynamic keys, lookup is equivalent to one of the keys") {
val query = relation
.select(
GetMapValue(
CreateMap(Seq(
'id, ('id + 1L),
('id + 1L), ('id + 2L),
('id + 2L), ('id + 3L),
('id + 3L), ('id + 4L),
('id + 4L), ('id + 5L))),
('id + 3L)) as "a")
.analyze
val expected = relation
.select(
CaseWhen(Seq(
(EqualTo('id + 3L, 'id), ('id + 1L)),
(EqualTo('id + 3L, ('id + 1L)), ('id + 2L)),
(EqualTo('id + 3L, ('id + 2L)), ('id + 3L)),
(Literal(true), ('id + 4L)))) as "a")
.analyze
comparePlans(Optimizer execute query, expected)
}
test("simplify map ops, no positive match") {
val rel = relation
.select(
GetMapValue(
CreateMap(Seq(
'id, ('id + 1L),
('id + 1L), ('id + 2L),
('id + 2L), ('id + 3L),
('id + 3L), ('id + 4L),
('id + 4L), ('id + 5L))),
'id + 30L) as "a")
.analyze
val expected = relation.select(
CaseWhen(Seq(
(EqualTo('id + 30L, 'id), ('id + 1L)),
(EqualTo('id + 30L, ('id + 1L)), ('id + 2L)),
(EqualTo('id + 30L, ('id + 2L)), ('id + 3L)),
(EqualTo('id + 30L, ('id + 3L)), ('id + 4L)),
(EqualTo('id + 30L, ('id + 4L)), ('id + 5L)))) as "a")
.analyze
comparePlans(Optimizer execute rel, expected)
}
test("simplify map ops, constant lookup, mixed keys, eliminated constants") {
val rel = relation
.select(
GetMapValue(
CreateMap(Seq(
'id, ('id + 1L),
('id + 1L), ('id + 2L),
('id + 2L), ('id + 3L),
Literal(14L), 'id,
('id + 3L), ('id + 4L),
('id + 4L), ('id + 5L))),
13L) as "a")
.analyze
val expected = relation
.select(
CaseKeyWhen(13L,
Seq('id, ('id + 1L),
('id + 1L), ('id + 2L),
('id + 2L), ('id + 3L),
('id + 3L), ('id + 4L),
('id + 4L), ('id + 5L))) as "a")
.analyze
comparePlans(Optimizer execute rel, expected)
}
test("simplify map ops, potential dynamic match with null value + an absolute constant match") {
val rel = relation
.select(
GetMapValue(
CreateMap(Seq(
'id, ('id + 1L),
('id + 1L), ('id + 2L),
('id + 2L), Literal.create(null, LongType),
Literal(2L), 'id,
('id + 3L), ('id + 4L),
('id + 4L), ('id + 5L))),
2L ) as "a")
.analyze
val expected = relation
.select(
CaseWhen(Seq(
(EqualTo(2L, 'id), ('id + 1L)),
// these two are possible matches, we can't tell until runtime
(EqualTo(2L, ('id + 1L)), ('id + 2L)),
(EqualTo(2L, 'id + 2L), Literal.create(null, LongType)),
// this is a definite match (two constants),
// but it cannot override a potential match with ('id + 2L),
// which is exactly what [[Coalesce]] would do in this case.
(Literal.TrueLiteral, 'id))) as "a")
.analyze
comparePlans(Optimizer execute rel, expected)
}
test("SPARK-23500: Simplify complex ops that aren't at the plan root") {
val structRel = relation
.select(GetStructField(CreateNamedStruct(Seq("att1", 'nullable_id)), 0, None) as "foo")
.groupBy($"foo")("1").analyze
val structExpected = relation
.select('nullable_id as "foo")
.groupBy($"foo")("1").analyze
comparePlans(Optimizer execute structRel, structExpected)
// These tests must use nullable attributes from the base relation for the following reason:
// in the 'original' plans below, the Aggregate node produced by groupBy() has a
// nullable AttributeReference to a1, because both array indexing and map lookup are
// nullable expressions. After optimization, the same attribute is now non-nullable,
// but the AttributeReference is not updated to reflect this. In the 'expected' plans,
// the grouping expressions have the same nullability as the original attribute in the
// relation. If that attribute is non-nullable, the tests will fail as the plans will
// compare differently, so for these tests we must use a nullable attribute. See
// SPARK-23634.
val arrayRel = relation
.select(GetArrayItem(CreateArray(Seq('nullable_id, 'nullable_id + 1L)), 0) as "a1")
.groupBy($"a1")("1").analyze
val arrayExpected = relation.select('nullable_id as "a1").groupBy($"a1")("1").analyze
comparePlans(Optimizer execute arrayRel, arrayExpected)
val mapRel = relation
.select(GetMapValue(CreateMap(Seq("id", 'nullable_id)), "id") as "m1")
.groupBy($"m1")("1").analyze
val mapExpected = relation
.select('nullable_id as "m1")
.groupBy($"m1")("1").analyze
comparePlans(Optimizer execute mapRel, mapExpected)
}
test("SPARK-23500: Ensure that aggregation expressions are not simplified") {
// Make sure that aggregation exprs are correctly ignored. Maps can't be used in
// grouping exprs so aren't tested here.
val structAggRel = relation.groupBy(
CreateNamedStruct(Seq("att1", 'nullable_id)))(
GetStructField(CreateNamedStruct(Seq("att1", 'nullable_id)), 0, None)).analyze
comparePlans(Optimizer execute structAggRel, structAggRel)
val arrayAggRel = relation.groupBy(
CreateArray(Seq('nullable_id)))(GetArrayItem(CreateArray(Seq('nullable_id)), 0)).analyze
comparePlans(Optimizer execute arrayAggRel, arrayAggRel)
}
}