-
Notifications
You must be signed in to change notification settings - Fork 11
/
basicOperators.scala
393 lines (337 loc) · 13.1 KB
/
basicOperators.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* 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.execution
import scala.collection.mutable.ArrayBuffer
import org.apache.spark.{SparkEnv, HashPartitioner, SparkConf}
import org.apache.spark.annotation.DeveloperApi
import org.apache.spark.rdd.{RDD, ShuffledRDD}
import org.apache.spark.shuffle.sort.SortShuffleManager
import org.apache.spark.sql.catalyst.ScalaReflection
import org.apache.spark.sql.catalyst.errors._
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.physical.{ClusteredDistribution, OrderedDistribution, SinglePartition, UnspecifiedDistribution}
import org.apache.spark.util.MutablePair
import org.apache.spark.util.collection.ExternalSorter
/**
* :: DeveloperApi ::
*/
@DeveloperApi
case class Project(projectList: Seq[NamedExpression], child: SparkPlan) extends UnaryNode {
override def output = projectList.map(_.toAttribute)
@transient lazy val buildProjection = newMutableProjection(projectList, child.output)
def execute() = child.execute().mapPartitions { iter =>
val resuableProjection = buildProjection()
iter.map(resuableProjection)
}
}
/**
* A projection operator that is tailored to improve performance of UDF execution using
* in-memory memoization.
*
* NOTE: This assumes that we are only caching for a single UDF. If there are multiple
* UDFs, it will only cache for the last UDF. All other UDFs will be executed regularly.
*
* Once you have completed implementing the functions in [[CS143Utils]], this operator
* should work.
*/
@DeveloperApi
case class CacheProject(projectList: Seq[Expression], child: SparkPlan) extends UnaryNode {
override def output = child.output
def execute() = {
/* Generate the caching iterator. You should trace this code to understand it!
You have to implement parts of the stack to make this work. */
val generator: (Iterator[Row] => Iterator[Row]) = CS143Utils.generateCachingIterator(projectList, child.output)
/* This is Spark magic. In short, it applies the generator function to each of the slices of an RDD.
For the purposes of CS 143, we will only ever have one slice. */
child.execute().mapPartitions(generator)
}
}
/**
* A projection operator that is tailor to improve performance of UDF execution by using
* external hashing.
*
* @param projectList
* @param child
*/
@DeveloperApi
case class PartitionProject(projectList: Seq[Expression], child: SparkPlan) extends UnaryNode {
override def output = child.output
def execute() = {
child.execute().mapPartitions(generateIterator)
}
/**
* This method takes an iterator as an input. It should first partition the whole input to disk.
* It should then read each partition from disk and construct do in-memory memoization over each
* partition to avoid recomputation of UDFs.
*
* @param input the input iterator
* @return the result of applying the projection
*/
def generateIterator(input: Iterator[Row]): Iterator[Row] = {
// This is the key generator for the course-grained external hashing.
val keyGenerator = CS143Utils.getNewProjection(projectList, child.output)
// IMPLEMENT ME
new Iterator[Row] {
def hasNext() = {
// IMPLEMENT ME
false
}
def next() = {
// IMPLEMENT ME
null
}
/**
* This fetches the next partition over which we will iterate or returns false if there are no more partitions
* over which we can iterate.
*
* @return
*/
private def fetchNextPartition(): Boolean = {
// IMPLEMENT ME
false
}
}
}
}
/**
* :: DeveloperApi ::
*/
@DeveloperApi
case class Filter(condition: Expression, child: SparkPlan) extends UnaryNode {
override def output = child.output
@transient lazy val conditionEvaluator = newPredicate(condition, child.output)
def execute() = child.execute().mapPartitions { iter =>
iter.filter(conditionEvaluator)
}
}
/**
* :: DeveloperApi ::
*/
@DeveloperApi
case class Sample(fraction: Double, withReplacement: Boolean, seed: Long, child: SparkPlan)
extends UnaryNode
{
override def output = child.output
// TODO: How to pick seed?
override def execute() = child.execute().sample(withReplacement, fraction, seed)
}
/**
* :: DeveloperApi ::
*/
@DeveloperApi
case class Union(children: Seq[SparkPlan]) extends SparkPlan {
// TODO: attributes output by union should be distinct for nullability purposes
override def output = children.head.output
override def execute() = sparkContext.union(children.map(_.execute()))
}
/**
* :: DeveloperApi ::
* Take the first limit elements. Note that the implementation is different depending on whether
* this is a terminal operator or not. If it is terminal and is invoked using executeCollect,
* this operator uses something similar to Spark's take method on the Spark driver. If it is not
* terminal or is invoked using execute, we first take the limit on each partition, and then
* repartition all the data to a single partition to compute the global limit.
*/
@DeveloperApi
case class Limit(limit: Int, child: SparkPlan)
extends UnaryNode {
// TODO: Implement a partition local limit, and use a strategy to generate the proper limit plan:
// partition local limit -> exchange into one partition -> partition local limit again
/** We must copy rows when sort based shuffle is on */
private def sortBasedShuffleOn = SparkEnv.get.shuffleManager.isInstanceOf[SortShuffleManager]
override def output = child.output
override def outputPartitioning = SinglePartition
/**
* A custom implementation modeled after the take function on RDDs but which never runs any job
* locally. This is to avoid shipping an entire partition of data in order to retrieve only a few
* rows.
*/
override def executeCollect(): Array[Row] = {
if (limit == 0) {
return new Array[Row](0)
}
val childRDD = child.execute().map(_.copy())
val buf = new ArrayBuffer[Row]
val totalParts = childRDD.partitions.length
var partsScanned = 0
while (buf.size < limit && partsScanned < totalParts) {
// The number of partitions to try in this iteration. It is ok for this number to be
// greater than totalParts because we actually cap it at totalParts in runJob.
var numPartsToTry = 1
if (partsScanned > 0) {
// If we didn't find any rows after the first iteration, just try all partitions next.
// Otherwise, interpolate the number of partitions we need to try, but overestimate it
// by 50%.
if (buf.size == 0) {
numPartsToTry = totalParts - 1
} else {
numPartsToTry = (1.5 * limit * partsScanned / buf.size).toInt
}
}
numPartsToTry = math.max(0, numPartsToTry) // guard against negative num of partitions
val left = limit - buf.size
val p = partsScanned until math.min(partsScanned + numPartsToTry, totalParts)
val sc = sqlContext.sparkContext
val res =
sc.runJob(childRDD, (it: Iterator[Row]) => it.take(left).toArray, p, allowLocal = false)
res.foreach(buf ++= _.take(limit - buf.size))
partsScanned += numPartsToTry
}
buf.toArray.map(ScalaReflection.convertRowToScala(_, this.schema))
}
override def execute() = {
val rdd: RDD[_ <: Product2[Boolean, Row]] = if (sortBasedShuffleOn) {
child.execute().mapPartitions { iter =>
iter.take(limit).map(row => (false, row.copy()))
}
} else {
child.execute().mapPartitions { iter =>
val mutablePair = new MutablePair[Boolean, Row]()
iter.take(limit).map(row => mutablePair.update(false, row))
}
}
val part = new HashPartitioner(1)
val shuffled = new ShuffledRDD[Boolean, Row, Row](rdd, part)
shuffled.setSerializer(new SparkSqlSerializer(new SparkConf(false)))
shuffled.mapPartitions(_.take(limit).map(_._2))
}
}
/**
* :: DeveloperApi ::
* Take the first limit elements as defined by the sortOrder. This is logically equivalent to
* having a [[Limit]] operator after a [[Sort]] operator. This could have been named TopK, but
* Spark's top operator does the opposite in ordering so we name it TakeOrdered to avoid confusion.
*/
@DeveloperApi
case class TakeOrdered(limit: Int, sortOrder: Seq[SortOrder], child: SparkPlan) extends UnaryNode {
override def output = child.output
override def outputPartitioning = SinglePartition
val ord = new RowOrdering(sortOrder, child.output)
// TODO: Is this copying for no reason?
override def executeCollect() = child.execute().map(_.copy()).takeOrdered(limit)(ord)
.map(ScalaReflection.convertRowToScala(_, this.schema))
// TODO: Terminal split should be implemented differently from non-terminal split.
// TODO: Pick num splits based on |limit|.
override def execute() = sparkContext.makeRDD(executeCollect(), 1)
}
/**
* :: DeveloperApi ::
* Performs a sort on-heap.
* @param global when true performs a global sort of all partitions by shuffling the data first
* if necessary.
*/
@DeveloperApi
case class Sort(
sortOrder: Seq[SortOrder],
global: Boolean,
child: SparkPlan)
extends UnaryNode {
override def requiredChildDistribution =
if (global) OrderedDistribution(sortOrder) :: Nil else UnspecifiedDistribution :: Nil
override def execute() = attachTree(this, "sort") {
child.execute().mapPartitions( { iterator =>
val ordering = newOrdering(sortOrder, child.output)
iterator.map(_.copy()).toArray.sorted(ordering).iterator
}, preservesPartitioning = true)
}
override def output = child.output
}
/**
* :: DeveloperApi ::
* Performs a sort, spilling to disk as needed.
* @param global when true performs a global sort of all partitions by shuffling the data first
* if necessary.
*/
@DeveloperApi
case class ExternalSort(
sortOrder: Seq[SortOrder],
global: Boolean,
child: SparkPlan)
extends UnaryNode {
override def requiredChildDistribution =
if (global) OrderedDistribution(sortOrder) :: Nil else UnspecifiedDistribution :: Nil
override def execute() = attachTree(this, "sort") {
child.execute().mapPartitions( { iterator =>
val ordering = newOrdering(sortOrder, child.output)
val sorter = new ExternalSorter[Row, Null, Row](ordering = Some(ordering))
sorter.insertAll(iterator.map(r => (r, null)))
sorter.iterator.map(_._1)
}, preservesPartitioning = true)
}
override def output = child.output
}
/**
* :: DeveloperApi ::
* Computes the set of distinct input rows using a HashSet.
* @param partial when true the distinct operation is performed partially, per partition, without
* shuffling the data.
* @param child the input query plan.
*/
@DeveloperApi
case class Distinct(partial: Boolean, child: SparkPlan) extends UnaryNode {
override def output = child.output
override def requiredChildDistribution =
if (partial) UnspecifiedDistribution :: Nil else ClusteredDistribution(child.output) :: Nil
override def execute() = {
child.execute().mapPartitions { iter =>
val hashSet = new scala.collection.mutable.HashSet[Row]()
var currentRow: Row = null
while (iter.hasNext) {
currentRow = iter.next()
if (!hashSet.contains(currentRow)) {
hashSet.add(currentRow.copy())
}
}
hashSet.iterator
}
}
}
/**
* :: DeveloperApi ::
* Returns a table with the elements from left that are not in right using
* the built-in spark subtract function.
*/
@DeveloperApi
case class Except(left: SparkPlan, right: SparkPlan) extends BinaryNode {
override def output = left.output
override def execute() = {
left.execute().map(_.copy()).subtract(right.execute().map(_.copy()))
}
}
/**
* :: DeveloperApi ::
* Returns the rows in left that also appear in right using the built in spark
* intersection function.
*/
@DeveloperApi
case class Intersect(left: SparkPlan, right: SparkPlan) extends BinaryNode {
override def output = children.head.output
override def execute() = {
left.execute().map(_.copy()).intersection(right.execute().map(_.copy()))
}
}
/**
* :: DeveloperApi ::
* A plan node that does nothing but lie about the output of its child. Used to spice a
* (hopefully structurally equivalent) tree from a different optimization sequence into an already
* resolved tree.
*/
@DeveloperApi
case class OutputFaker(output: Seq[Attribute], child: SparkPlan) extends SparkPlan {
def children = child :: Nil
def execute() = child.execute()
}