Skip to content

Commit f9f32a1

Browse files
authored
perf: Optimize CometSparkToColumnar for columnar input (apache#892)
* Modify `CometSparkToColumnarExec` to use `ColumnarArray` rather than row iteration when the child is producing columnar data. * Modify `CometSparkToColumnarExec` to use `ColumnarArray` rather than row iteration when the child is producing columnar data. * Run `mvn spotless:apply` to clean up formatting, changed to camelCase for variables, do not call `ColumnarArray.numElements()` in a tight loop, added `writeColNoNull` as an optimization, reserve the initial vector capacity in `writeCol` as an optimization. * Fix one more variable name. * Clean up duplicated code. * Add test for basic types. * Add more types to test. * Revert testing changes to CometExecBenchmark. * Add benchmark. * Link to issue in ArrowWriters. * Address review feedback. * make format * Address review feedback, mostly naming suggestions. Ran `make format` again.
1 parent 80e4a2e commit f9f32a1

5 files changed

Lines changed: 228 additions & 53 deletions

File tree

‎common/src/main/scala/org/apache/spark/sql/comet/execution/arrow/ArrowWriters.scala‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ import org.apache.spark.sql.catalyst.expressions.SpecializedGetters
2828
import org.apache.spark.sql.comet.util.Utils
2929
import org.apache.spark.sql.errors.QueryExecutionErrors
3030
import org.apache.spark.sql.types._
31+
import org.apache.spark.sql.vectorized.ColumnarArray
3132

3233
/**
3334
* This file is mostly copied from Spark SQL's
3435
* org.apache.spark.sql.execution.arrow.ArrowWriter.scala. Comet shadows Arrow classes to avoid
3536
* potential conflicts with Spark's Arrow dependencies, hence we cannot reuse Spark's ArrowWriter
3637
* directly.
38+
*
39+
* Performance enhancement: https://github.com/apache/datafusion-comet/issues/888
3740
*/
3841
private[arrow] object ArrowWriter {
3942
def create(root: VectorSchemaRoot): ArrowWriter = {
@@ -103,6 +106,16 @@ class ArrowWriter(val root: VectorSchemaRoot, fields: Array[ArrowFieldWriter]) {
103106
count += 1
104107
}
105108

109+
def writeCol(input: ColumnarArray, columnIndex: Int): Unit = {
110+
fields(columnIndex).writeCol(input)
111+
count = input.numElements()
112+
}
113+
114+
def writeColNoNull(input: ColumnarArray, columnIndex: Int): Unit = {
115+
fields(columnIndex).writeColNoNull(input)
116+
count = input.numElements()
117+
}
118+
106119
def finish(): Unit = {
107120
root.setRowCount(count)
108121
fields.foreach(_.finish())
@@ -137,6 +150,28 @@ private[arrow] abstract class ArrowFieldWriter {
137150
count += 1
138151
}
139152

153+
def writeCol(input: ColumnarArray): Unit = {
154+
val inputNumElements = input.numElements()
155+
valueVector.setInitialCapacity(inputNumElements)
156+
while (count < inputNumElements) {
157+
if (input.isNullAt(count)) {
158+
setNull()
159+
} else {
160+
setValue(input, count)
161+
}
162+
count += 1
163+
}
164+
}
165+
166+
def writeColNoNull(input: ColumnarArray): Unit = {
167+
val inputNumElements = input.numElements()
168+
valueVector.setInitialCapacity(inputNumElements)
169+
while (count < inputNumElements) {
170+
setValue(input, count)
171+
count += 1
172+
}
173+
}
174+
140175
def finish(): Unit = {
141176
valueVector.setValueCount(count)
142177
}

‎common/src/main/scala/org/apache/spark/sql/comet/execution/arrow/CometArrowConverters.scala‎

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,19 @@ package org.apache.spark.sql.comet.execution.arrow
2121

2222
import org.apache.arrow.memory.{BufferAllocator, RootAllocator}
2323
import org.apache.arrow.vector.VectorSchemaRoot
24+
import org.apache.arrow.vector.types.pojo.Schema
2425
import org.apache.spark.TaskContext
2526
import org.apache.spark.internal.Logging
2627
import org.apache.spark.sql.catalyst.InternalRow
2728
import org.apache.spark.sql.comet.util.Utils
2829
import org.apache.spark.sql.types.StructType
29-
import org.apache.spark.sql.vectorized.ColumnarBatch
30+
import org.apache.spark.sql.vectorized.{ColumnarArray, ColumnarBatch}
3031

3132
import org.apache.comet.vector.NativeUtil
3233

3334
object CometArrowConverters extends Logging {
3435
// TODO: we should reuse the same root allocator in the comet code base?
35-
val rootAllocator: BufferAllocator = new RootAllocator(Long.MaxValue)
36+
private val rootAllocator: BufferAllocator = new RootAllocator(Long.MaxValue)
3637

3738
// This is similar how Spark converts internal row to Arrow format except that it is transforming
3839
// the result batch to Comet's ColumnarBatch instead of serialized bytes.
@@ -44,46 +45,77 @@ object CometArrowConverters extends Logging {
4445
// exported process increases the reference count of the Arrow vectors. The reference count is
4546
// only decreased when the native plan is done with the vectors, which is usually longer than
4647
// all the ColumnarBatches are consumed.
47-
private[sql] class ArrowBatchIterator(
48-
rowIter: Iterator[InternalRow],
48+
49+
abstract private[sql] class ArrowBatchIterBase(
4950
schema: StructType,
50-
maxRecordsPerBatch: Long,
5151
timeZoneId: String,
5252
context: TaskContext)
5353
extends Iterator[ColumnarBatch]
5454
with AutoCloseable {
5555

56-
private val arrowSchema = Utils.toArrowSchema(schema, timeZoneId)
56+
protected val arrowSchema: Schema = Utils.toArrowSchema(schema, timeZoneId)
5757
// Reuse the same root allocator here.
58-
private val allocator =
58+
protected val allocator: BufferAllocator =
5959
rootAllocator.newChildAllocator(s"to${this.getClass.getSimpleName}", 0, Long.MaxValue)
60-
private val root = VectorSchemaRoot.create(arrowSchema, allocator)
61-
private val arrowWriter = ArrowWriter.create(root)
60+
protected val root: VectorSchemaRoot = VectorSchemaRoot.create(arrowSchema, allocator)
61+
protected val arrowWriter: ArrowWriter = ArrowWriter.create(root)
6262

63-
private var currentBatch: ColumnarBatch = null
64-
private var closed: Boolean = false
63+
protected var currentBatch: ColumnarBatch = null
64+
protected var closed: Boolean = false
6565

6666
Option(context).foreach {
6767
_.addTaskCompletionListener[Unit] { _ =>
6868
close(true)
6969
}
7070
}
7171

72-
override def hasNext: Boolean = rowIter.hasNext || {
72+
override def close(): Unit = {
7373
close(false)
74-
false
74+
}
75+
76+
protected def close(closeAllocator: Boolean): Unit = {
77+
try {
78+
if (!closed) {
79+
if (currentBatch != null) {
80+
arrowWriter.reset()
81+
currentBatch.close()
82+
currentBatch = null
83+
}
84+
root.close()
85+
closed = true
86+
}
87+
} finally {
88+
// the allocator shall be closed when the task is finished
89+
if (closeAllocator) {
90+
allocator.close()
91+
}
92+
}
7593
}
7694

7795
override def next(): ColumnarBatch = {
7896
currentBatch = nextBatch()
7997
currentBatch
8098
}
8199

82-
override def close(): Unit = {
100+
protected def nextBatch(): ColumnarBatch
101+
102+
}
103+
104+
private[sql] class RowToArrowBatchIter(
105+
rowIter: Iterator[InternalRow],
106+
schema: StructType,
107+
maxRecordsPerBatch: Long,
108+
timeZoneId: String,
109+
context: TaskContext)
110+
extends ArrowBatchIterBase(schema, timeZoneId, context)
111+
with AutoCloseable {
112+
113+
override def hasNext: Boolean = rowIter.hasNext || {
83114
close(false)
115+
false
84116
}
85117

86-
private def nextBatch(): ColumnarBatch = {
118+
override protected def nextBatch(): ColumnarBatch = {
87119
if (rowIter.hasNext) {
88120
// the arrow writer shall be reset before writing the next batch
89121
arrowWriter.reset()
@@ -99,33 +131,68 @@ object CometArrowConverters extends Logging {
99131
null
100132
}
101133
}
134+
}
102135

103-
private def close(closeAllocator: Boolean): Unit = {
104-
try {
105-
if (!closed) {
106-
if (currentBatch != null) {
107-
arrowWriter.reset()
108-
currentBatch.close()
109-
currentBatch = null
136+
def rowToArrowBatchIter(
137+
rowIter: Iterator[InternalRow],
138+
schema: StructType,
139+
maxRecordsPerBatch: Long,
140+
timeZoneId: String,
141+
context: TaskContext): Iterator[ColumnarBatch] = {
142+
new RowToArrowBatchIter(rowIter, schema, maxRecordsPerBatch, timeZoneId, context)
143+
}
144+
145+
private[sql] class ColumnBatchToArrowBatchIter(
146+
colBatch: ColumnarBatch,
147+
schema: StructType,
148+
maxRecordsPerBatch: Int,
149+
timeZoneId: String,
150+
context: TaskContext)
151+
extends ArrowBatchIterBase(schema, timeZoneId, context)
152+
with AutoCloseable {
153+
154+
private var rowsProduced: Int = 0
155+
156+
override def hasNext: Boolean = rowsProduced < colBatch.numRows() || {
157+
close(false)
158+
false
159+
}
160+
161+
override protected def nextBatch(): ColumnarBatch = {
162+
val rowsInBatch = colBatch.numRows()
163+
if (rowsProduced < rowsInBatch) {
164+
// the arrow writer shall be reset before writing the next batch
165+
arrowWriter.reset()
166+
val rowsToProduce =
167+
if (maxRecordsPerBatch <= 0) rowsInBatch - rowsProduced
168+
else Math.min(maxRecordsPerBatch, rowsInBatch - rowsProduced)
169+
170+
for (columnIndex <- 0 until colBatch.numCols()) {
171+
val column = colBatch.column(columnIndex)
172+
val columnArray = new ColumnarArray(column, rowsProduced, rowsToProduce)
173+
if (column.hasNull) {
174+
arrowWriter.writeCol(columnArray, columnIndex)
175+
} else {
176+
arrowWriter.writeColNoNull(columnArray, columnIndex)
110177
}
111-
root.close()
112-
closed = true
113-
}
114-
} finally {
115-
// the allocator shall be closed when the task is finished
116-
if (closeAllocator) {
117-
allocator.close()
118178
}
179+
180+
rowsProduced += rowsToProduce
181+
182+
arrowWriter.finish()
183+
NativeUtil.rootAsBatch(root)
184+
} else {
185+
null
119186
}
120187
}
121188
}
122189

123-
def toArrowBatchIterator(
124-
rowIter: Iterator[InternalRow],
190+
def columnarBatchToArrowBatchIter(
191+
colBatch: ColumnarBatch,
125192
schema: StructType,
126-
maxRecordsPerBatch: Long,
193+
maxRecordsPerBatch: Int,
127194
timeZoneId: String,
128195
context: TaskContext): Iterator[ColumnarBatch] = {
129-
new ArrowBatchIterator(rowIter, schema, maxRecordsPerBatch, timeZoneId, context)
196+
new ColumnBatchToArrowBatchIter(colBatch, schema, maxRecordsPerBatch, timeZoneId, context)
130197
}
131198
}

‎spark/src/main/scala/org/apache/spark/sql/comet/CometSparkToColumnarExec.scala‎

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919

2020
package org.apache.spark.sql.comet
2121

22-
import scala.collection.JavaConverters._
23-
2422
import org.apache.spark.TaskContext
2523
import org.apache.spark.broadcast.Broadcast
2624
import org.apache.spark.rdd.RDD
@@ -65,31 +63,44 @@ case class CometSparkToColumnarExec(child: SparkPlan)
6563
val timeZoneId = conf.sessionLocalTimeZone
6664
val schema = child.schema
6765

68-
val rdd: RDD[InternalRow] = if (child.supportsColumnar) {
66+
if (child.supportsColumnar) {
6967
child
7068
.executeColumnar()
7169
.mapPartitionsInternal { iter =>
72-
iter.flatMap(_.rowIterator().asScala)
70+
iter.flatMap { columnBatch =>
71+
val context = TaskContext.get()
72+
CometArrowConverters.columnarBatchToArrowBatchIter(
73+
columnBatch,
74+
schema,
75+
maxRecordsPerBatch,
76+
timeZoneId,
77+
context)
78+
}
79+
}
80+
.map { batch =>
81+
numInputRows += batch.numRows()
82+
numOutputBatches += 1
83+
batch
7384
}
7485
} else {
75-
child.execute()
86+
child
87+
.execute()
88+
.mapPartitionsInternal { iter =>
89+
val context = TaskContext.get()
90+
CometArrowConverters.rowToArrowBatchIter(
91+
iter,
92+
schema,
93+
maxRecordsPerBatch,
94+
timeZoneId,
95+
context)
96+
}
97+
.map { batch =>
98+
numInputRows += batch.numRows()
99+
numOutputBatches += 1
100+
batch
101+
}
76102
}
77103

78-
rdd
79-
.mapPartitionsInternal { iter =>
80-
val context = TaskContext.get()
81-
CometArrowConverters.toArrowBatchIterator(
82-
iter,
83-
schema,
84-
maxRecordsPerBatch,
85-
timeZoneId,
86-
context)
87-
}
88-
.map { batch =>
89-
numInputRows += batch.numRows()
90-
numOutputBatches += 1
91-
batch
92-
}
93104
}
94105

95106
override protected def withNewChildInternal(newChild: SparkPlan): CometSparkToColumnarExec =

‎spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,58 @@ class CometExecSuite extends CometTestBase {
15471547
})
15481548
}
15491549

1550+
test("SparkToColumnar over BatchScan (Spark Parquet reader)") {
1551+
Seq("", "parquet").foreach { v1List =>
1552+
Seq(true, false).foreach { parquetVectorized =>
1553+
Seq(
1554+
"cast(id as tinyint)",
1555+
"cast(id as smallint)",
1556+
"cast(id as integer)",
1557+
"cast(id as bigint)",
1558+
"cast(id as float)",
1559+
"cast(id as double)",
1560+
"cast(id as decimal)",
1561+
"cast(id as timestamp)",
1562+
"cast(id as string)",
1563+
"cast(id as binary)",
1564+
"struct(id)").foreach { valueType =>
1565+
{
1566+
withSQLConf(
1567+
SQLConf.USE_V1_SOURCE_LIST.key -> v1List,
1568+
CometConf.COMET_NATIVE_SCAN_ENABLED.key -> "false",
1569+
CometConf.COMET_CONVERT_FROM_PARQUET_ENABLED.key -> "true",
1570+
SQLConf.PARQUET_VECTORIZED_READER_ENABLED.key -> parquetVectorized.toString) {
1571+
withTempPath { dir =>
1572+
var df = spark
1573+
.range(10000)
1574+
.selectExpr("id as key", s"$valueType as value")
1575+
.toDF("key", "value")
1576+
1577+
df.write.parquet(dir.toString)
1578+
1579+
df = spark.read.parquet(dir.toString)
1580+
checkSparkAnswerAndOperator(
1581+
df.select("*").groupBy("key", "value").count(),
1582+
includeClasses = Seq(classOf[CometSparkToColumnarExec]))
1583+
1584+
// Verify that the BatchScanExec nodes supported columnar output when requested for Spark 3.4+.
1585+
// Earlier versions support columnar output for fewer type.
1586+
if (isSpark34Plus) {
1587+
val leaves = df.queryExecution.executedPlan.collectLeaves()
1588+
if (parquetVectorized && isSpark34Plus) {
1589+
assert(leaves.forall(_.supportsColumnar))
1590+
} else {
1591+
assert(!leaves.forall(_.supportsColumnar))
1592+
}
1593+
}
1594+
}
1595+
}
1596+
}
1597+
}
1598+
}
1599+
}
1600+
}
1601+
15501602
test("SparkToColumnar over InMemoryTableScanExec") {
15511603
Seq("true", "false").foreach(aqe => {
15521604
Seq("true", "false").foreach(cacheVectorized => {

0 commit comments

Comments
 (0)