-
Notifications
You must be signed in to change notification settings - Fork 28.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-13930][SQL] Apply fast serialization on collect limit operator #11759
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -218,48 +218,64 @@ abstract class SparkPlan extends QueryPlan[SparkPlan] with Logging with Serializ | |
} | ||
|
||
/** | ||
* Runs this query returning the result as an array. | ||
* Packing the UnsafeRows into byte array for faster serialization. | ||
* The byte arrays are in the following format: | ||
* [size] [bytes of UnsafeRow] [size] [bytes of UnsafeRow] ... [-1] | ||
* | ||
* UnsafeRow is highly compressible (at least 8 bytes for any column), the byte array is also | ||
* compressed. | ||
*/ | ||
def executeCollect(): Array[InternalRow] = { | ||
// Packing the UnsafeRows into byte array for faster serialization. | ||
// The byte arrays are in the following format: | ||
// [size] [bytes of UnsafeRow] [size] [bytes of UnsafeRow] ... [-1] | ||
// | ||
// UnsafeRow is highly compressible (at least 8 bytes for any column), the byte array is also | ||
// compressed. | ||
val byteArrayRdd = execute().mapPartitionsInternal { iter => | ||
private def getByteArrayRdd(n: Int = -1): RDD[Array[Byte]] = { | ||
execute().mapPartitionsInternal { iter => | ||
var count = 0 | ||
val buffer = new Array[Byte](4 << 10) // 4K | ||
val codec = CompressionCodec.createCodec(SparkEnv.get.conf) | ||
val bos = new ByteArrayOutputStream() | ||
val out = new DataOutputStream(codec.compressedOutputStream(bos)) | ||
while (iter.hasNext) { | ||
while (iter.hasNext && (n < 0 || count < n)) { | ||
val row = iter.next().asInstanceOf[UnsafeRow] | ||
out.writeInt(row.getSizeInBytes) | ||
row.writeToStream(out, buffer) | ||
count += 1 | ||
} | ||
out.writeInt(-1) | ||
out.flush() | ||
out.close() | ||
Iterator(bos.toByteArray) | ||
} | ||
} | ||
|
||
// Collect the byte arrays back to driver, then decode them as UnsafeRows. | ||
/** | ||
* Collect the byte arrays back to driver, then decode them as UnsafeRows. | ||
*/ | ||
private def collectRowFromBytes(bytes: Array[Byte]): Array[InternalRow] = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function has nothong about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done with decodeUnsafeRow. |
||
val nFields = schema.length | ||
val results = ArrayBuffer[InternalRow]() | ||
|
||
val codec = CompressionCodec.createCodec(SparkEnv.get.conf) | ||
val bis = new ByteArrayInputStream(bytes) | ||
val ins = new DataInputStream(codec.compressedInputStream(bis)) | ||
var sizeOfNextRow = ins.readInt() | ||
while (sizeOfNextRow >= 0) { | ||
val bs = new Array[Byte](sizeOfNextRow) | ||
ins.readFully(bs) | ||
val row = new UnsafeRow(nFields) | ||
row.pointTo(bs, sizeOfNextRow) | ||
results += row | ||
sizeOfNextRow = ins.readInt() | ||
} | ||
results.toArray | ||
} | ||
|
||
/** | ||
* Runs this query returning the result as an array. | ||
*/ | ||
def executeCollect(): Array[InternalRow] = { | ||
val byteArrayRdd = getByteArrayRdd() | ||
|
||
val results = ArrayBuffer[InternalRow]() | ||
byteArrayRdd.collect().foreach { bytes => | ||
val codec = CompressionCodec.createCodec(SparkEnv.get.conf) | ||
val bis = new ByteArrayInputStream(bytes) | ||
val ins = new DataInputStream(codec.compressedInputStream(bis)) | ||
var sizeOfNextRow = ins.readInt() | ||
while (sizeOfNextRow >= 0) { | ||
val bs = new Array[Byte](sizeOfNextRow) | ||
ins.readFully(bs) | ||
val row = new UnsafeRow(nFields) | ||
row.pointTo(bs, sizeOfNextRow) | ||
results += row | ||
sizeOfNextRow = ins.readInt() | ||
} | ||
results ++= collectRowFromBytes(bytes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we avoid this copy ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should. Updated. |
||
} | ||
results.toArray | ||
} | ||
|
@@ -282,7 +298,7 @@ abstract class SparkPlan extends QueryPlan[SparkPlan] with Logging with Serializ | |
return new Array[InternalRow](0) | ||
} | ||
|
||
val childRDD = execute().map(_.copy()) | ||
val childRDD = getByteArrayRdd(n) | ||
|
||
val buf = new ArrayBuffer[InternalRow] | ||
val totalParts = childRDD.partitions.length | ||
|
@@ -306,9 +322,15 @@ abstract class SparkPlan extends QueryPlan[SparkPlan] with Logging with Serializ | |
val left = n - buf.size | ||
val p = partsScanned.until(math.min(partsScanned + numPartsToTry, totalParts).toInt) | ||
val sc = sqlContext.sparkContext | ||
val res = sc.runJob(childRDD, (it: Iterator[InternalRow]) => it.take(left).toArray, p) | ||
val res = sc.runJob(childRDD, | ||
(it: Iterator[Array[Byte]]) => if (it.hasNext) it.next() else Array.empty, p) | ||
|
||
val results = ArrayBuffer[InternalRow]() | ||
res.foreach { r => | ||
results ++= collectRowFromBytes(r.asInstanceOf[Array[Byte]]) | ||
} | ||
|
||
res.foreach(buf ++= _.take(n - buf.size)) | ||
buf ++= results.take(n - buf.size) | ||
partsScanned += p.size | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the implementation details, it has nothing with the APIs, I'd like to keep these as comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm, you make it as an function.