diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/pythonworker/PythonProxyClient.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/pythonworker/PythonProxyClient.scala index d3ea86cdd4a..ddd8927c4f1 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/pythonworker/PythonProxyClient.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/pythonworker/PythonProxyClient.scala @@ -124,6 +124,14 @@ class PythonProxyClient(portNumberPromise: Promise[Int], val actorId: ActorVirtu dataPayload match { case DataFrame(frame) => writeArrowStream(mutable.Queue(ArraySeq.unsafeWrapArray(frame): _*), from, "Data") + case ColumnarFrame(arrowIpcBytes, _, _) => + // A columnar-wire batch headed to a Python worker: decode it back to tuples + // and hand it over as a Data batch, same as a DataFrame. + writeArrowStream( + mutable.Queue(ArraySeq.unsafeWrapArray(ArrowUtils.deserializeTuples(arrowIpcBytes)): _*), + from, + "Data" + ) case StateFrame(state, loopCounter, loopStartId) => // The Arrow wire format for states IS the State row (content + // loop_counter + loop_start_id), so the envelope rides its own diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/Partitioner.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/Partitioner.scala index 620eb936512..6f129ced8a1 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/Partitioner.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/Partitioner.scala @@ -24,7 +24,8 @@ import org.apache.texera.amber.core.state.State import org.apache.texera.amber.core.tuple.Tuple import org.apache.texera.amber.core.virtualidentity.ActorVirtualIdentity import org.apache.texera.amber.engine.architecture.messaginglayer.NetworkOutputGateway -import org.apache.texera.amber.engine.common.ambermessage.{DataFrame, StateFrame} +import org.apache.texera.amber.engine.common.ambermessage.{ColumnarFrame, DataFrame, StateFrame} +import org.apache.texera.amber.util.ArrowUtils import scala.collection.mutable.ArrayBuffer @@ -37,7 +38,10 @@ trait Partitioner extends Serializable { class NetworkOutputBuffer( val to: ActorVirtualIdentity, val dataOutputPort: NetworkOutputGateway, - val batchSize: Int = ApplicationConfig.defaultDataTransferBatchSize + val batchSize: Int = ApplicationConfig.defaultDataTransferBatchSize, + // Columnar wire (opt-in): flush as ColumnarFrame (Arrow IPC) instead of + // DataFrame. Off by default, so the default wire is unchanged. + val columnar: Boolean = ApplicationConfig.enableColumnarWire ) { var buffer = new ArrayBuffer[Tuple]() @@ -57,7 +61,17 @@ class NetworkOutputBuffer( def flush(): Unit = { if (buffer.nonEmpty) { - dataOutputPort.sendTo(to, DataFrame(buffer.toArray)) + val batch = buffer.toArray + if (columnar) { + // Serialize the batch to Arrow once; all tuples on a link share the schema. + val schema = batch(0).getSchema + dataOutputPort.sendTo( + to, + ColumnarFrame(ArrowUtils.serializeTuples(schema, batch), batch.length, schema) + ) + } else { + dataOutputPort.sendTo(to, DataFrame(batch)) + } buffer = new ArrayBuffer[Tuple]() } } diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessor.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessor.scala index 0bfce2d04b9..bbda40707f0 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessor.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessor.scala @@ -21,7 +21,11 @@ package org.apache.texera.amber.engine.architecture.worker import com.softwaremill.macwire.wire import io.grpc.MethodDescriptor -import org.apache.texera.amber.core.executor.OperatorExecutor +import org.apache.texera.amber.core.executor.{ + ColumnarOperatorExecutor, + ColumnarResult, + OperatorExecutor +} import org.apache.texera.amber.core.state.State import org.apache.texera.amber.core.tuple._ import org.apache.texera.amber.core.virtualidentity.{ @@ -59,6 +63,7 @@ import org.apache.texera.amber.engine.common.ambermessage._ import org.apache.texera.amber.engine.common.statetransition.WorkerStateManager import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR import org.apache.texera.amber.error.ErrorUtils.{mkConsoleMessage, mkPrintConsoleMessage, safely} +import org.apache.texera.amber.util.ArrowUtils import java.util.concurrent.LinkedBlockingQueue @@ -114,6 +119,90 @@ class DataProcessor( * process currentInputTuple through executor logic. * this function is only called by the DP thread. */ + // Start processing one data batch: announce the worker is running, load the + // batch, and process its first tuple (the DP loop drains the rest). Shared by the + // DataFrame and ColumnarFrame input paths. + private[this] def startInputBatch( + channelId: ChannelIdentity, + tuples: Array[Tuple] + ): Unit = { + transitToRunning() + inputManager.initBatch(channelId, tuples) + processInputTuple(inputManager.getNextTuple) + } + + // Announce the worker is running (READY -> RUNNING, once). Shared by the row and + // columnar input paths. + private[this] def transitToRunning(): Unit = { + stateManager.conditionalTransitTo( + READY, + RUNNING, + () => { + val (state, stateVersion) = stateManager.getStateWithVersion + asyncRPCClient.coordinatorInterface.workerStateUpdated( + WorkerStateUpdatedRequest(state, stateVersion), + asyncRPCClient.mkContext(COORDINATOR) + ) + } + ) + } + + // Columnar wire: an Arrow batch arrived. If the executor can consume Arrow + // columns directly (a ColumnarOperatorExecutor), hand it the batch and route the + // result; otherwise, or if the executor declines the batch (Unsupported), decode + // to tuples and process it exactly as a DataFrame. + private[this] def processColumnarFrame( + channelId: ChannelIdentity, + portId: PortIdentity, + arrowIpcBytes: Array[Byte], + rowCount: Int + ): Unit = { + val columnarExecutor = executor match { + case c: ColumnarOperatorExecutor => c + case _ => null + } + if (columnarExecutor == null) { + startInputBatch(channelId, ArrowUtils.deserializeTuples(arrowIpcBytes)) + return + } + transitToRunning() + inputManager.currentChannelId = channelId + // The executor sees data: bind its loop variables first (parity with the row path). + bindStateReferences() + try { + columnarExecutor.processColumnarBatch(arrowIpcBytes, portId.id) match { + case ColumnarResult.Unsupported => + // The executor declined this batch: fall back to the row path. + startInputBatch(channelId, ArrowUtils.deserializeTuples(arrowIpcBytes)) + case consumed => + // Consumed natively: no per-tuple input remains for the DP loop to drain. + // Report the whole batch's input statistics in one call. + inputManager.initBatch(channelId, Array.empty) + statisticsManager.increaseInputStatistics( + portId, + rowCount.toLong, + arrowIpcBytes.length.toLong + ) + consumed match { + case ColumnarResult.EmitRows(rows) => + outputManager.outputIterator.setTupleOutput(rows) + case ColumnarResult.Emit(root) => + // Materialize the emitted Arrow batch to output tuples (the existing + // output path is tuple-based), then free the root. + val tuples = + try ArrowUtils.tuplesOfRoot(root) + finally root.close() + outputManager.outputIterator.setTupleOutput( + tuples.iterator.map(t => (t: TupleLike, None: Option[PortIdentity])) + ) + case _ => // Consumed: a blocking operator accumulated the batch, nothing to emit now + } + } + } catch safely { + case e => handleExecutorException(e) + } + } + private[this] def processInputTuple(tuple: Tuple): Unit = { try { val portIdentity: PortIdentity = @@ -243,19 +332,13 @@ class DataProcessor( val portId = this.inputGateway.getChannel(channelId).getPortId dataPayload match { case DataFrame(tuples) => - stateManager.conditionalTransitTo( - READY, - RUNNING, - () => { - val (state, stateVersion) = stateManager.getStateWithVersion - asyncRPCClient.coordinatorInterface.workerStateUpdated( - WorkerStateUpdatedRequest(state, stateVersion), - asyncRPCClient.mkContext(COORDINATOR) - ) - } - ) - inputManager.initBatch(channelId, tuples) - processInputTuple(inputManager.getNextTuple) + startInputBatch(channelId, tuples) + case ColumnarFrame(arrowIpcBytes, rowCount, _) => + // Columnar wire: the batch arrived as Arrow IPC bytes. A + // ColumnarOperatorExecutor consumes the Arrow columns directly (no per-row + // decode); any other operator, or one that declines the batch, decodes it + // back to tuples and processes it exactly as a DataFrame. + processColumnarFrame(channelId, portId, arrowIpcBytes, rowCount) case StateFrame(state, loopCounter, loopStartId) => processInputState(state, portId.id, loopCounter, loopStartId) } diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/managers/StatisticsManager.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/managers/StatisticsManager.scala index ab46e17654c..c71e55c788b 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/managers/StatisticsManager.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/managers/StatisticsManager.scala @@ -81,11 +81,21 @@ class StatisticsManager { * @param portId the port identity * @param size the size of the tuple */ - def increaseInputStatistics(portId: PortIdentity, size: Long): Unit = { + def increaseInputStatistics(portId: PortIdentity, size: Long): Unit = + increaseInputStatistics(portId, 1L, size) + + /** + * Increases the input statistics for a given port by a whole batch at once: + * `count` tuples totalling `size` bytes. The columnar-wire path consumes an + * Arrow batch without a per-tuple loop, so it reports the batch's row count and + * serialized size in one call rather than once per tuple. + */ + def increaseInputStatistics(portId: PortIdentity, count: Long, size: Long): Unit = { + require(count >= 0, "Tuple count must be non-negative") require(size >= 0, "Tuple size must be non-negative") inputStatistics.updateWith(portId) { - case Some((count, totalSize)) => Some((count + 1, totalSize + size)) - case None => Some((1L, size)) + case Some((c, totalSize)) => Some((c + count, totalSize + size)) + case None => Some((count, size)) } } diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/common/ambermessage/DataPayload.scala b/amber/src/main/scala/org/apache/texera/amber/engine/common/ambermessage/DataPayload.scala index b8ac640ad53..bc767c38b1a 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/common/ambermessage/DataPayload.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/common/ambermessage/DataPayload.scala @@ -20,7 +20,7 @@ package org.apache.texera.amber.engine.common.ambermessage import org.apache.texera.amber.core.state.State -import org.apache.texera.amber.core.tuple.Tuple +import org.apache.texera.amber.core.tuple.{Schema, Tuple} sealed trait DataPayload extends WorkflowFIFOMessagePayload {} @@ -37,6 +37,19 @@ sealed trait DataPayload extends WorkflowFIFOMessagePayload {} final case class StateFrame(frame: State, loopCounter: Long = 0L, loopStartId: String = "") extends DataPayload +/** + * A batch of rows carried over the wire as Arrow IPC stream bytes (the columnar + * wire format), instead of as decoded `Tuple`s in a `DataFrame`. `rowCount` and + * the Texera `schema` describe the batch without decoding it. The receiver either + * consumes the bytes directly (a `ColumnarOperatorExecutor`) or decodes them back + * to tuples (`ArrowUtils.deserializeTuples`). Produced only when the columnar wire + * is enabled; off by default, so the default wire stays `DataFrame`. + */ +final case class ColumnarFrame(arrowIpcBytes: Array[Byte], rowCount: Int, schema: Schema) + extends DataPayload { + val inMemSize: Long = arrowIpcBytes.length.toLong +} + final case class DataFrame(frame: Array[Tuple]) extends DataPayload { val inMemSize: Long = { frame.map(_.inMemSize).sum diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/ColumnarWireBenchSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/ColumnarWireBenchSpec.scala new file mode 100644 index 00000000000..43c994c398d --- /dev/null +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/ColumnarWireBenchSpec.scala @@ -0,0 +1,308 @@ +/* + * 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.texera.amber.engine.architecture.sendsemantics.partitioners + +import org.apache.pekko.actor.ActorSystem +import org.apache.pekko.serialization.SerializationExtension +import org.apache.pekko.testkit.TestKit +import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema, Tuple} +import org.apache.texera.amber.engine.common.AmberRuntime +import org.apache.texera.amber.engine.common.ambermessage.{ColumnarFrame, DataFrame} +import org.apache.texera.amber.util.ArrowUtils +import org.scalatest.BeforeAndAfterAll +import org.scalatest.flatspec.AnyFlatSpecLike + +import java.sql.Timestamp +import scala.util.Random + +/** + * Measures the per-batch wire cost of the two data-batch formats a cross-worker + * link can use: the row path (a DataFrame of Tuples, serialized by the real + * Pekko/Kryo stack) versus the columnar path (an Arrow IPC ColumnarFrame). For + * each format it reports sender cost (encode), receiver cost (decode), and the + * serialized payload size, so the numbers reflect what actually crosses the wire. + * + * This is a benchmark, not an assertion of a speedup (same-JVM links never + * serialize, so the columnar wire's payoff is specifically on cross-worker links; + * these numbers isolate that serialize/deserialize/size tradeoff). Run with: + * WorkflowExecutionService/testOnly *ColumnarWireBenchSpec + */ +class ColumnarWireBenchSpec + extends TestKit(ActorSystem("ColumnarWireBench", AmberRuntime.pekkoConfig)) + with AnyFlatSpecLike + with BeforeAndAfterAll { + + private val serde = SerializationExtension(system) + + private val rng = new Random(42) + + // ---- workloads ----------------------------------------------------------- + + private def narrowNumeric(n: Int): (Schema, Array[Tuple]) = { + val schema = Schema( + List(new Attribute("a", AttributeType.INTEGER), new Attribute("b", AttributeType.DOUBLE)) + ) + val rows = Array.tabulate(n) { _ => + Tuple + .builder(schema) + .addSequentially(Array[Any](Int.box(rng.nextInt()), Double.box(rng.nextDouble()))) + .build() + } + (schema, rows) + } + + private def wideMixed(n: Int): (Schema, Array[Tuple]) = { + val schema = Schema( + List( + new Attribute("i", AttributeType.INTEGER), + new Attribute("l", AttributeType.LONG), + new Attribute("d", AttributeType.DOUBLE), + new Attribute("b", AttributeType.BOOLEAN), + new Attribute("t", AttributeType.TIMESTAMP), + new Attribute("s", AttributeType.STRING) + ) + ) + val rows = Array.tabulate(n) { i => + Tuple + .builder(schema) + .addSequentially( + Array[Any]( + Int.box(rng.nextInt()), + Long.box(rng.nextLong()), + Double.box(rng.nextDouble()), + Boolean.box(i % 2 == 0), + new Timestamp(1_600_000_000_000L + i), + s"row-$i-${rng.alphanumeric.take(8).mkString}" + ) + ) + .build() + } + (schema, rows) + } + + private def stringHeavy(n: Int): (Schema, Array[Tuple]) = { + val schema = Schema( + List(new Attribute("s1", AttributeType.STRING), new Attribute("s2", AttributeType.STRING)) + ) + val rows = Array.tabulate(n) { _ => + Tuple + .builder(schema) + .addSequentially( + Array[Any](rng.alphanumeric.take(32).mkString, rng.alphanumeric.take(32).mkString) + ) + .build() + } + (schema, rows) + } + + // ---- timing -------------------------------------------------------------- + + private def median(xs: Seq[Double]): Double = { + val s = xs.sorted + s(s.length / 2) + } + + private def timeMs(body: => Unit): Double = { + val t0 = System.nanoTime() + body + (System.nanoTime() - t0) / 1e6 + } + + private val warmups = 3 + private val trials = 7 + + private case class Result( + rowEncMs: Double, + colEncMs: Double, + rowDecMs: Double, + colDecMs: Double, + rowBytes: Int, + colBytes: Int + ) + + private def measure(schema: Schema, rows: Array[Tuple]): Result = { + // Row path: Pekko/Kryo over the DataFrame of tuples. + def rowEncode(): Array[Byte] = serde.serialize(DataFrame(rows)).get + def rowDecode(bytes: Array[Byte]): AnyRef = + serde.deserialize(bytes, classOf[DataFrame]).get + + // Columnar path: Arrow-serialize the batch (as NetworkOutputBuffer does), then + // Pekko/Kryo over the resulting ColumnarFrame (which just wraps the bytes). + def colEncode(): Array[Byte] = { + val arrow = ArrowUtils.serializeTuples(schema, rows) + serde.serialize(ColumnarFrame(arrow, rows.length, schema)).get + } + def colDecode(bytes: Array[Byte]): AnyRef = { + val cf = serde.deserialize(bytes, classOf[ColumnarFrame]).get + ArrowUtils.deserializeTuples(cf.arrowIpcBytes) + } + + for (_ <- 0 until warmups) { rowDecode(rowEncode()); colDecode(colEncode()) } + + val rowWire = rowEncode() + val colWire = colEncode() + val rowEnc = median((0 until trials).map(_ => timeMs(rowEncode()))) + val colEnc = median((0 until trials).map(_ => timeMs(colEncode()))) + val rowDec = median((0 until trials).map(_ => timeMs(rowDecode(rowWire)))) + val colDec = median((0 until trials).map(_ => timeMs(colDecode(colWire)))) + Result(rowEnc, colEnc, rowDec, colDec, rowWire.length, colWire.length) + } + + private def pct(col: Double, row: Double): String = f"${col / row}%.2fx" + + // A benchmark, not a correctness gate: skipped unless -Dcolumnar.bench=true, so it + // does not slow CI. Run it with: + // sbt -Dcolumnar.bench=true "WorkflowExecutionService/testOnly *ColumnarWireBenchSpec" + private def benchEnabled: Boolean = sys.props.get("columnar.bench").contains("true") + + "columnar wire" should "report encode/decode/size vs the row wire" in { + assume(benchEnabled, "set -Dcolumnar.bench=true to run the wire benchmark") + val workloads: Seq[(String, Int => (Schema, Array[Tuple]))] = Seq( + "narrowNumeric" -> narrowNumeric, + "wideMixed" -> wideMixed, + "stringHeavy" -> stringHeavy + ) + val sizes = Seq(2000, 20000, 100000) + + println( + "\n================ Columnar wire vs row wire (median of %d) ================".format(trials) + ) + println( + "%-14s %8s | %10s %10s %6s | %10s %10s %6s | %10s %10s %6s" + .format( + "workload", + "rows", + "rowEnc ms", + "colEnc ms", + "enc", + "rowDec ms", + "colDec ms", + "dec", + "rowKB", + "colKB", + "size" + ) + ) + for ((name, build) <- workloads; n <- sizes) { + val (schema, rows) = build(n) + val r = measure(schema, rows) + println( + "%-14s %8d | %10.2f %10.2f %6s | %10.2f %10.2f %6s | %10.1f %10.1f %6s" + .format( + name, + n, + r.rowEncMs, + r.colEncMs, + pct(r.colEncMs, r.rowEncMs), + r.rowDecMs, + r.colDecMs, + pct(r.colDecMs, r.rowDecMs), + r.rowBytes / 1024.0, + r.colBytes / 1024.0, + pct(r.colBytes.toDouble, r.rowBytes.toDouble) + ) + ) + } + println("(enc/dec/size columns are columnar / row; < 1.00x means columnar is cheaper)\n") + succeed + } + + // The decisive case: a receiver that can VECTORIZE (a filter) skips the + // decode-to-tuples entirely and reads the Arrow columns directly via + // processColumnarBatch. This compares the full receive+process cost of the two + // paths for a filter edge: + // row path: Kryo-deserialize the DataFrame, then run the per-tuple predicate. + // col path: Kryo-deserialize the ColumnarFrame, then processColumnarBatch + // (reads the Arrow column, builds the survivor mask, no tuple decode). + "a vectorized receiver (filter)" should "show whether columnar beats row end to end" in { + assume(benchEnabled, "set -Dcolumnar.bench=true to run the wire benchmark") + import org.apache.texera.amber.operator.filter.{ + ComparisonType, + FilterPredicate, + SpecializedFilterOpExec, + SpecializedFilterOpDesc + } + import org.apache.texera.amber.util.JSONUtils.{objectMapper => om} + + val schema = + Schema( + List(new Attribute("v", AttributeType.INTEGER), new Attribute("w", AttributeType.DOUBLE)) + ) + + def filterExec(): SpecializedFilterOpExec = { + val d = new SpecializedFilterOpDesc() + d.predicates = List(new FilterPredicate("v", ComparisonType.GREATER_THAN, "0")) + val e = new SpecializedFilterOpExec(om.writeValueAsString(d)) + e.open() + e + } + + println("\n========= Filter receive+process: columnar(vectorized) vs row =========") + println("%-10s | %12s %14s %8s".format("rows", "row recv ms", "col recv ms", "col/row")) + for (n <- Seq(2000, 20000, 100000)) { + val rows = Array.tabulate(n)(_ => + Tuple + .builder(schema) + .addSequentially( + Array[Any](Int.box(rng.nextInt(200) - 100), Double.box(rng.nextDouble())) + ) + .build() + ) + val rowWire = serde.serialize(DataFrame(rows)).get + val colWire = + serde.serialize(ColumnarFrame(ArrowUtils.serializeTuples(schema, rows), n, schema)).get + + val rowExec = filterExec() + val colExec = filterExec() + + // Row receive: deserialize the batch, then run the predicate per tuple. + def rowRecv(): Int = { + val df = serde.deserialize(rowWire, classOf[DataFrame]).get + var kept = 0 + val fr = df.frame + var i = 0 + while (i < fr.length) { if (rowExec.filterFunc(fr(i))) kept += 1; i += 1 } + kept + } + // Columnar receive: deserialize the frame, then vectorized filter (no decode). + def colRecv(): Int = { + val cf = serde.deserialize(colWire, classOf[ColumnarFrame]).get + colExec.processColumnarBatch(cf.arrowIpcBytes, 0) match { + case org.apache.texera.amber.core.executor.ColumnarResult.Emit(root) => + val c = root.getRowCount; root.close(); c + case _ => 0 + } + } + + for (_ <- 0 until warmups) { rowRecv(); colRecv() } + val rowMs = median((0 until trials).map(_ => timeMs(rowRecv()))) + val colMs = median((0 until trials).map(_ => timeMs(colRecv()))) + println("%-10d | %12.2f %14.2f %8s".format(n, rowMs, colMs, pct(colMs, rowMs))) + rowExec.close(); colExec.close() + } + println("(col/row < 1.00x means the vectorized columnar receiver is faster)\n") + succeed + } + + override def afterAll(): Unit = { + TestKit.shutdownActorSystem(system) + super.afterAll() + } +} diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/NetworkOutputBufferColumnarSeamSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/NetworkOutputBufferColumnarSeamSpec.scala new file mode 100644 index 00000000000..4dbe15d7de1 --- /dev/null +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/NetworkOutputBufferColumnarSeamSpec.scala @@ -0,0 +1,119 @@ +/* + * 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.texera.amber.engine.architecture.sendsemantics.partitioners + +import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema, Tuple} +import org.apache.texera.amber.core.virtualidentity.ActorVirtualIdentity +import org.apache.texera.amber.engine.architecture.messaginglayer.NetworkOutputGateway +import org.apache.texera.amber.engine.common.ambermessage.{ + ColumnarFrame, + DataFrame, + WorkflowFIFOMessage +} +import org.apache.texera.amber.util.ArrowUtils +import org.scalatest.flatspec.AnyFlatSpec + +import scala.collection.mutable.ArrayBuffer + +/** + * The columnar-wire switch on NetworkOutputBuffer. + * + * A NetworkOutputBuffer chooses, on flush, how to put a batch on the wire: as row + * objects (DataFrame) or as an Arrow columnar block (ColumnarFrame). The choice is + * the constructor flag `columnar`; flush() reads exactly that flag. These tests + * verify the buffer honors the flag on both the manual and the size-triggered + * flush, carries the row count and schema on the columnar frame, and that a + * columnar frame's bytes decode back to the very rows that were buffered. + */ +class NetworkOutputBufferColumnarSeamSpec extends AnyFlatSpec { + + private val sender: ActorVirtualIdentity = ActorVirtualIdentity("sender") + private val receiver: ActorVirtualIdentity = ActorVirtualIdentity("receiver") + private val vAttr: Attribute = new Attribute("v", AttributeType.INTEGER) + private val schema: Schema = Schema().add(vAttr) + private def tuple(v: Int): Tuple = Tuple.builder(schema).add(vAttr, v).build() + + private def fixture( + columnar: Boolean, + batchSize: Int = 1000 + ): (NetworkOutputBuffer, ArrayBuffer[WorkflowFIFOMessage]) = { + val sent = ArrayBuffer[WorkflowFIFOMessage]() + val gateway = new NetworkOutputGateway(sender, m => { sent += m; () }) + val buffer = new NetworkOutputBuffer(receiver, gateway, batchSize, columnar) + (buffer, sent) + } + + it should "emit a ColumnarFrame carrying the same rows when columnar is on" in { + val (buffer, sent) = fixture(columnar = true) + buffer.addTuple(tuple(1)) + buffer.addTuple(tuple(2)) + buffer.addTuple(tuple(3)) + buffer.flush() + + assert(sent.size == 1) + sent.head.payload match { + case cf: ColumnarFrame => + assert(cf.rowCount == 3) + assert(cf.schema == schema) + // The Arrow bytes decode back to exactly the buffered rows, in order. + val back = ArrowUtils.deserializeTuples(cf.arrowIpcBytes) + assert(back.map(_.getField[Any]("v")).toList == List(1, 2, 3)) + case other => fail(s"expected a ColumnarFrame, got: $other") + } + } + + it should "emit a DataFrame (rows) when columnar is off" in { + val (buffer, sent) = fixture(columnar = false) + buffer.addTuple(tuple(10)) + buffer.addTuple(tuple(20)) + buffer.flush() + + assert(sent.size == 1) + sent.head.payload match { + case df: DataFrame => + assert(df.frame.map(_.getField[Any]("v")).toList == List(10, 20)) + case other => fail(s"expected a DataFrame, got: $other") + } + } + + it should "send nothing when flushed empty, either way" in { + val (on, onSent) = fixture(columnar = true) + on.flush() + assert(onSent.isEmpty) + val (off, offSent) = fixture(columnar = false) + off.flush() + assert(offSent.isEmpty) + } + + it should "honor the flag on the size-triggered auto-flush" in { + val (on, onSent) = fixture(columnar = true, batchSize = 2) + on.addTuple(tuple(1)) + assert(onSent.isEmpty) + on.addTuple(tuple(2)) + assert(onSent.size == 1) + assert(onSent.head.payload.isInstanceOf[ColumnarFrame]) + + val (off, offSent) = fixture(columnar = false, batchSize = 2) + off.addTuple(tuple(1)) + off.addTuple(tuple(2)) + assert(offSent.size == 1) + assert(offSent.head.payload.isInstanceOf[DataFrame]) + } +} diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessorSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessorSpec.scala index c48e9594208..f0aeeb9d6cc 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessorSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DataProcessorSpec.scala @@ -20,7 +20,14 @@ package org.apache.texera.amber.engine.architecture.worker import com.fasterxml.jackson.annotation.JsonProperty -import org.apache.texera.amber.core.executor.{ExecFactory, OpExecWithClassName, OperatorExecutor} +import org.apache.arrow.memory.RootAllocator +import org.apache.texera.amber.core.executor.{ + ColumnarOperatorExecutor, + ColumnarResult, + ExecFactory, + OpExecWithClassName, + OperatorExecutor +} import org.apache.texera.amber.core.state.{State, StateReferencing} import org.apache.texera.amber.core.tuple.{AttributeType, Schema, Tuple, TupleLike} import org.apache.texera.amber.core.virtualidentity._ @@ -52,10 +59,12 @@ import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.{ } import org.apache.texera.amber.engine.architecture.worker.statistics.WorkerState.READY import org.apache.texera.amber.engine.common.ambermessage.{ + ColumnarFrame, DataFrame, StateFrame, WorkflowFIFOMessage } +import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.amber.engine.common.rpc.AsyncRPCClient.ControlInvocation import org.apache.texera.amber.engine.common.storage.SequentialRecordStorage import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR @@ -745,10 +754,160 @@ class DataProcessorSpec extends AnyFlatSpec with MockFactory with Matchers with dp.pauseManager.isPaused shouldBe false } + // --------------------------------------------------------------------------- + // Columnar wire: routing a ColumnarFrame to a ColumnarOperatorExecutor + // --------------------------------------------------------------------------- + + // A batchSize-1 OneToOne partitioner flushes every output tuple immediately as a + // DataFrame (columnar wire is off by default), so what the operator emits is + // observable through the output handler without an explicit flush. + private def columnarSetup( + exec: OperatorExecutor + ): (DataProcessor, ArrayBuffer[WorkflowFIFOMessage]) = { + val dp = mkDataProcessor + val sent = ArrayBuffer[WorkflowFIFOMessage]() + (outputHandler.apply _) + .expects(*) + .onCall { (m: Either[MainThreadDelegateMessage, WorkflowFIFOMessage]) => + m.foreach(sent += _); () + } + .anyNumberOfTimes() + (adaptiveBatchingMonitor.startAdaptiveBatching _).expects().anyNumberOfTimes() + dp.executor = exec + dp.stateManager.transitTo(READY) + dp.inputManager.addPort(inputPortId, schema, List.empty, List.empty) + dp.inputGateway.getChannel(senderChannel).setPortId(inputPortId) + dp.outputManager.addPort(outputPortId, schema, None) + dp.outputManager.addPartitionerWithPartitioning( + PhysicalLink(testOpId, outputPortId, upstreamOpId, inputPortId), + OneToOnePartitioning(1, Seq(ChannelIdentity(testWorkerId, senderWorkerId, isControl = false))) + ) + (dp, sent) + } + + private val columnarBatch: Array[Tuple] = tuples.take(5) + private def columnarFrame: ColumnarFrame = + ColumnarFrame( + ArrowUtils.serializeTuples(schema, columnarBatch), + columnarBatch.length, + schema + ) + + "data processor" should "route a ColumnarFrame to a ColumnarOperatorExecutor and emit its Arrow result" in { + val exec = new DataProcessorSpec.EmitAllColumnarExec + val (dp, sent) = columnarSetup(exec) + dp.processDataPayload(senderChannel, columnarFrame) + drain(dp) + // Emit(root): the emitted Arrow batch is materialized to output tuples. The + // executor consumed the batch natively, so processTuple was never called. + assert(tuplesSent(sent) == columnarBatch.toList) + assert(exec.processTupleCalls == 0) + assert(exec.columnarCalls == 1) + assert(dp.statisticsManager.getInputTupleCount == columnarBatch.length.toLong) + exec.close() + } + + "data processor" should "emit the rows a ColumnarOperatorExecutor returns via EmitRows" in { + val rows = columnarBatch.toList.map(t => (t: TupleLike, None: Option[PortIdentity])) + val exec = new DataProcessorSpec.EmitRowsColumnarExec(rows.iterator) + val (dp, sent) = columnarSetup(exec) + dp.processDataPayload(senderChannel, columnarFrame) + drain(dp) + assert(tuplesSent(sent) == columnarBatch.toList) + assert(exec.processTupleCalls == 0) + } + + "data processor" should "emit nothing for a blocking ColumnarOperatorExecutor that returns Consumed" in { + val exec = new DataProcessorSpec.ConsumedColumnarExec + val (dp, sent) = columnarSetup(exec) + dp.processDataPayload(senderChannel, columnarFrame) + drain(dp) + // Consumed: the batch was accumulated, nothing emitted now, and the row path + // (processTuple) was not taken. Input statistics still count the whole batch. + assert(tuplesSent(sent).isEmpty) + assert(exec.processTupleCalls == 0) + assert(exec.columnarCalls == 1) + assert(dp.statisticsManager.getInputTupleCount == columnarBatch.length.toLong) + } + + "data processor" should "fall back to the row path when a ColumnarOperatorExecutor returns Unsupported" in { + val exec = new DataProcessorSpec.UnsupportedColumnarExec + val (dp, sent) = columnarSetup(exec) + dp.processDataPayload(senderChannel, columnarFrame) + drain(dp) + // Unsupported: the frame is decoded to tuples and processed tuple by tuple, so + // the echoing processTuple sees every row and emits it. + assert(tuplesSent(sent) == columnarBatch.toList) + assert(exec.processTupleCalls == columnarBatch.length) + assert(exec.columnarCalls == 1) + } + + "data processor" should "decode a ColumnarFrame to tuples for a non-columnar operator" in { + val exec = new DataProcessorSpec.RowOnlyExec + val (dp, sent) = columnarSetup(exec) + dp.processDataPayload(senderChannel, columnarFrame) + drain(dp) + // A plain OperatorExecutor cannot consume Arrow, so the frame is decoded and + // processed through the row path. + assert(tuplesSent(sent) == columnarBatch.toList) + assert(exec.processTupleCalls == columnarBatch.length) + } + } object DataProcessorSpec { + /** Echoes each input tuple, counting the row-path calls. Base for the columnar fakes. */ + class EchoingExec extends OperatorExecutor { + var processTupleCalls = 0 + override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { + processTupleCalls += 1 + Iterator.single(tuple) + } + } + + /** Consumes the Arrow batch and emits every row back (a pass-through filter). */ + class EmitAllColumnarExec extends EchoingExec with ColumnarOperatorExecutor { + var columnarCalls = 0 + private val alloc = new RootAllocator() + override def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult = { + columnarCalls += 1 + ArrowUtils.deserializeRootFold(arrowIpcBytes, alloc) { root => + ColumnarResult.Emit(ArrowUtils.selectRows(root, Array.fill(root.getRowCount)(true), alloc)) + } + } + override def close(): Unit = alloc.close() + } + + /** Returns the given rows directly (a join-shaped output). */ + class EmitRowsColumnarExec(rows: Iterator[(TupleLike, Option[PortIdentity])]) + extends EchoingExec + with ColumnarOperatorExecutor { + override def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult = + ColumnarResult.EmitRows(rows) + } + + /** Accumulates with no per-batch output (a blocking operator). */ + class ConsumedColumnarExec extends EchoingExec with ColumnarOperatorExecutor { + var columnarCalls = 0 + override def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult = { + columnarCalls += 1 + ColumnarResult.Consumed + } + } + + /** Declines every batch, forcing the row fallback. */ + class UnsupportedColumnarExec extends EchoingExec with ColumnarOperatorExecutor { + var columnarCalls = 0 + override def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult = { + columnarCalls += 1 + ColumnarResult.Unsupported + } + } + + /** A plain operator with no columnar path. */ + class RowOnlyExec extends EchoingExec + /** A descString whose `limit` refers to the loop variable `n`. */ val limitReferringToN = """{"limit":0,"stateReferences":{"/limit":"n"}}""" diff --git a/common/config/src/main/resources/application.conf b/common/config/src/main/resources/application.conf index d5f7ffed2e7..03993092b26 100644 --- a/common/config/src/main/resources/application.conf +++ b/common/config/src/main/resources/application.conf @@ -53,6 +53,14 @@ network-buffering { adaptive-buffering-timeout-ms = 500 adaptive-buffering-timeout-ms = ${?NETWORK_BUFFERING_ADAPTIVE_BUFFERING_TIMEOUT_MS} + + # Columnar wire (MVP, opt-in): when true, a worker flushes each data batch to + # downstream as Arrow IPC bytes (ColumnarFrame) instead of decoded tuples + # (DataFrame). The receiver consumes the Arrow batch directly if its operator + # implements ColumnarOperatorExecutor, else decodes it back to tuples. Off by + # default; the default wire stays DataFrame. + enable-columnar-wire = false + enable-columnar-wire = ${?NETWORK_BUFFERING_ENABLE_COLUMNAR_WIRE} } reconfiguration { diff --git a/common/config/src/main/scala/org/apache/texera/common/config/ApplicationConfig.scala b/common/config/src/main/scala/org/apache/texera/common/config/ApplicationConfig.scala index dea1e169d6a..47ff363ad99 100644 --- a/common/config/src/main/scala/org/apache/texera/common/config/ApplicationConfig.scala +++ b/common/config/src/main/scala/org/apache/texera/common/config/ApplicationConfig.scala @@ -66,6 +66,10 @@ object ApplicationConfig { getConfSource.getBoolean("network-buffering.enable-adaptive-buffering") val adaptiveBufferingTimeoutMs: Int = getConfSource.getInt("network-buffering.adaptive-buffering-timeout-ms") + // Columnar wire (opt-in): flush data batches as Arrow IPC (ColumnarFrame) rather + // than decoded tuples (DataFrame). Off by default. + val enableColumnarWire: Boolean = + getConfSource.getBoolean("network-buffering.enable-columnar-wire") // Reconfiguration val enableTransactionalReconfiguration: Boolean = diff --git a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/executor/ColumnarOperatorExecutor.scala b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/executor/ColumnarOperatorExecutor.scala new file mode 100644 index 00000000000..853004b32e1 --- /dev/null +++ b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/executor/ColumnarOperatorExecutor.scala @@ -0,0 +1,52 @@ +/* + * 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.texera.amber.core.executor + +import org.apache.arrow.vector.VectorSchemaRoot +import org.apache.texera.amber.core.tuple.TupleLike +import org.apache.texera.amber.core.workflow.PortIdentity + +/** + * Outcome of consuming one Arrow batch: + * - Emit: consumed, emit this Arrow result batch downstream (caller serializes and closes the root). + * - EmitRows: consumed, emit these row outputs (e.g. a join whose output is row-shaped); + * the caller ships them, still columnar on the wire if enabled. + * - Consumed: consumed, nothing to emit now (a blocking operator accumulating state). + * - Unsupported: not handled, caller falls back to the row path (decode + processTuple). + */ +sealed trait ColumnarResult +object ColumnarResult { + final case class Emit(root: VectorSchemaRoot) extends ColumnarResult + final case class EmitRows(rows: Iterator[(TupleLike, Option[PortIdentity])]) + extends ColumnarResult + case object Consumed extends ColumnarResult + case object Unsupported extends ColumnarResult +} + +/** + * An operator that can consume an Arrow columnar batch directly (no per-row + * Tuple decode). Given the incoming batch as Arrow IPC bytes and the input + * port it arrived on, returns a ColumnarResult telling the caller whether it + * emitted a batch/rows, consumed the batch with no output, or could not handle + * it (fall back to the row path). + */ +trait ColumnarOperatorExecutor { + def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult +} diff --git a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/executor/SourceOperatorExecutor.scala b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/executor/SourceOperatorExecutor.scala index 633314679a4..99f8f6d9b8d 100644 --- a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/executor/SourceOperatorExecutor.scala +++ b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/executor/SourceOperatorExecutor.scala @@ -19,6 +19,7 @@ package org.apache.texera.amber.core.executor +import org.apache.arrow.vector.VectorSchemaRoot import org.apache.texera.amber.core.tuple.{Tuple, TupleLike} import org.apache.texera.amber.core.workflow.PortIdentity @@ -35,6 +36,15 @@ trait SourceOperatorExecutor extends OperatorExecutor { def produceTuple(): Iterator[TupleLike] + /** + * Source operators whose data is already columnar (e.g. an Arrow file) can + * override this to hand out Arrow batches directly, skipping the per-row Tuple + * decode. The default returns None, meaning this source has no native columnar + * path and the engine uses produceTuple. The caller owns and closes each + * returned root. Honored only when the columnar wire is enabled. + */ + def produceColumnarBatch(): Option[Iterator[VectorSchemaRoot]] = None + override def onFinishMultiPort(port: Int): Iterator[(TupleLike, Option[PortIdentity])] = { // We assume there is only one input port for source operators. The current assumption // makes produceTuple to be invoked on each input port finish. diff --git a/common/workflow-core/src/main/scala/org/apache/texera/amber/util/ArrowUtils.scala b/common/workflow-core/src/main/scala/org/apache/texera/amber/util/ArrowUtils.scala index d8bcca13d84..ccd04c7e4f7 100644 --- a/common/workflow-core/src/main/scala/org/apache/texera/amber/util/ArrowUtils.scala +++ b/common/workflow-core/src/main/scala/org/apache/texera/amber/util/ArrowUtils.scala @@ -38,7 +38,10 @@ import org.apache.arrow.vector.{ VarCharVector, VectorSchemaRoot } +import org.apache.arrow.vector.ipc.{ArrowStreamReader, ArrowStreamWriter} +import java.io.{ByteArrayInputStream, ByteArrayOutputStream} +import java.nio.channels.Channels import java.nio.charset.StandardCharsets import java.sql.Timestamp import java.time.temporal.ChronoUnit @@ -55,6 +58,189 @@ object ArrowUtils extends LazyLogging { implicit def bool2int(b: Boolean): Int = if (b) 1 else 0 + // Serialize a VectorSchemaRoot to Arrow IPC stream bytes. Does not close root. + def serializeRoot(root: VectorSchemaRoot): Array[Byte] = { + val out = new ByteArrayOutputStream() + val writer = new ArrowStreamWriter(root, null, Channels.newChannel(out)) + try { writer.start(); writer.writeBatch(); writer.end() } + finally writer.close() + out.toByteArray + } + + // Read one Arrow IPC batch, apply `f` to the live root, then close the reader. + // `f` must copy anything it needs out (the root is freed on return). + def deserializeRootFold[T](bytes: Array[Byte], allocator: BufferAllocator)( + f: VectorSchemaRoot => T + ): T = { + val reader = new ArrowStreamReader(new ByteArrayInputStream(bytes), allocator) + try { + reader.loadNextBatch() + f(reader.getVectorSchemaRoot) + } finally reader.close() + } + + // Serialize a batch of tuples to Arrow IPC stream bytes (columnar wire format). + def serializeTuples(schema: Schema, tuples: Array[Tuple]): Array[Byte] = { + val root = VectorSchemaRoot.create(fromTexeraSchema(schema), allocator) + try { + var i = 0 + while (i < tuples.length) { setTexeraTuple(tuples(i), i, root); i += 1 } + root.setRowCount(tuples.length) + serializeRoot(root) + } finally root.close() + } + + // Inverse of serializeTuples: decode Arrow IPC stream bytes back to tuples. + def deserializeTuples(bytes: Array[Byte]): Array[Tuple] = { + val reader = new ArrowStreamReader(new ByteArrayInputStream(bytes), allocator) + try { + val out = scala.collection.mutable.ArrayBuffer[Tuple]() + val root = reader.getVectorSchemaRoot + // Resolved once per stream and reused per row: the Texera schema, the field + // vectors as a plain array, and each column's attribute type. Rebuilding these + // per row (an Arrow-to-Texera schema conversion plus collection allocation) was + // the dominant decode cost. + var schema: Schema = null + var vecs: Array[FieldVector] = null + var types: Array[AttributeType] = null + var numCols = 0 + while (reader.loadNextBatch()) { + if (schema == null) { + schema = toTexeraSchema(root.getSchema) + val fv = root.getFieldVectors + numCols = fv.size + vecs = new Array[FieldVector](numCols) + types = new Array[AttributeType](numCols) + var c = 0 + while (c < numCols) { + vecs(c) = fv.get(c); types(c) = schema.getAttributes(c).getType; c += 1 + } + } + val n = root.getRowCount + var i = 0 + while (i < n) { + val values = new Array[Any](numCols) + var c = 0 + while (c < numCols) { values(c) = getCell(vecs(c), i, types(c)); c += 1 } + out += Tuple(schema, values) + i += 1 + } + } + out.toArray + } finally reader.close() + } + + // Decode a live Arrow root to tuples. Like deserializeTuples but over an + // already-open root (the Emit output path's root-to-rows step); does not close + // the root. Resolves the schema, field vectors, and column types once, then + // reads each row with a plain index loop. + def tuplesOfRoot(root: VectorSchemaRoot): Array[Tuple] = { + val schema = toTexeraSchema(root.getSchema) + val fv = root.getFieldVectors + val numCols = fv.size + val vecs = new Array[FieldVector](numCols) + val types = new Array[AttributeType](numCols) + var c0 = 0 + while (c0 < numCols) { + vecs(c0) = fv.get(c0); types(c0) = schema.getAttributes(c0).getType; c0 += 1 + } + val n = root.getRowCount + val out = new Array[Tuple](n) + var i = 0 + while (i < n) { + val values = new Array[Any](numCols) + var c = 0 + while (c < numCols) { values(c) = getCell(vecs(c), i, types(c)); c += 1 } + out(i) = Tuple(schema, values) + i += 1 + } + out + } + + // Copy the rows of `src` where keep(i) is true into a new independent root. + def selectRows( + src: VectorSchemaRoot, + keep: Array[Boolean], + allocator: BufferAllocator + ): VectorSchemaRoot = { + val dest = VectorSchemaRoot.create(src.getSchema, allocator) + val srcVecs = src.getFieldVectors + val destVecs = dest.getFieldVectors + var destIdx = 0 + var i = 0 + val n = src.getRowCount + while (i < n) { + if (keep(i)) { + var c = 0 + while (c < srcVecs.size) { + destVecs.get(c).copyFromSafe(i, destIdx, srcVecs.get(c)); c += 1 + } + destIdx += 1 + } + i += 1 + } + dest.setRowCount(destIdx) + dest + } + + // Selection vector: the indices of `src`'s rows where keep(i) is true, in order. + // Records which rows passed without copying any cell (unlike selectRows). A + // consumer that only needs a count or an aggregate reads the indices and never + // materializes; a consumer that needs contiguous rows calls `gather`. + def selectionOf(keep: Array[Boolean], rowCount: Int): Array[Int] = { + val sel = new Array[Int](rowCount) + var n = 0 + var i = 0 + while (i < rowCount) { if (keep(i)) { sel(n) = i; n += 1 }; i += 1 } + if (n == rowCount) sel else java.util.Arrays.copyOf(sel, n) + } + + // Materialize the rows named by a selection vector into a new compacted root + // (the deferred copy). Inverse endpoint of selectionOf for consumers that need + // contiguous Arrow columns (e.g. shipping to the wire). + def gather( + src: VectorSchemaRoot, + selection: Array[Int], + allocator: BufferAllocator + ): VectorSchemaRoot = { + val dest = VectorSchemaRoot.create(src.getSchema, allocator) + val srcVecs = src.getFieldVectors + val destVecs = dest.getFieldVectors + var di = 0 + while (di < selection.length) { + val i = selection(di) + var c = 0 + while (c < srcVecs.size) { destVecs.get(c).copyFromSafe(i, di, srcVecs.get(c)); c += 1 } + di += 1 + } + dest.setRowCount(selection.length) + dest + } + + // Resolve the column indices of `names` in an Arrow batch, by exact name. + def projectionIndices(root: VectorSchemaRoot, names: Seq[String]): Array[Int] = { + val fields = root.getSchema.getFields + names.map { name => + var idx = -1 + var k = 0 + while (k < fields.size && idx < 0) { if (fields.get(k).getName == name) idx = k; k += 1 } + if (idx < 0) throw new IllegalArgumentException(s"column '$name' not found in Arrow batch") + idx + }.toArray + } + + // Read one cell as the Texera value for `attrType`, returning null on any + // conversion failure (matches the row path's lenient decode). + def getCell(vector: FieldVector, rowIndex: Int, attrType: AttributeType): AnyRef = { + val raw = vector.getObject(rowIndex) + try { + attrType match { + case AttributeType.TIMESTAMP => wallClockOf(raw, vector.getField.getType) + case _ => AttributeTypeUtils.parseField(raw, attrType).asInstanceOf[AnyRef] + } + } catch { case _: Exception => null } + } + /** * Reads a row of the given Arrow Vectors into a Texera.Tuple * e.g., diff --git a/common/workflow-core/src/test/scala/org/apache/texera/amber/util/ArrowUtilsSpec.scala b/common/workflow-core/src/test/scala/org/apache/texera/amber/util/ArrowUtilsSpec.scala index 6ce294e2a0b..d86f2eb893c 100644 --- a/common/workflow-core/src/test/scala/org/apache/texera/amber/util/ArrowUtilsSpec.scala +++ b/common/workflow-core/src/test/scala/org/apache/texera/amber/util/ArrowUtilsSpec.scala @@ -369,6 +369,111 @@ class ArrowUtilsSpec extends AnyFlatSpec with Matchers { } } + // ----- serializeTuples / deserializeTuples (the columnar wire round-trip) ----- + + "serializeTuples and deserializeTuples" should "round-trip a multi-row batch of every type" in { + // This is the columnar-wire payload contract: a NetworkOutputBuffer serializes a + // batch of tuples to Arrow IPC bytes, and the receiver decodes them back to the + // identical tuples, so the wire is transparent to the row path. + val rows = Array( + Tuple + .builder(tupleSchema) + .addSequentially( + Array[Any]( + Int.box(42), + Long.box(7L), + Double.box(3.14), + Boolean.box(true), + new Timestamp(10000L), + "hello", + Array[Byte](1, 2, 3) + ) + ) + .build(), + Tuple + .builder(tupleSchema) + .addSequentially(Array[Any](null, null, null, null, null, null, null)) + .build(), + Tuple + .builder(tupleSchema) + .addSequentially( + Array[Any]( + Int.box(-1), + Long.box(0L), + Double.box(-2.5), + Boolean.box(false), + new Timestamp(0L), + "", + Array[Byte]() + ) + ) + .build() + ) + + val back = ArrowUtils.deserializeTuples(ArrowUtils.serializeTuples(tupleSchema, rows)) + back.length shouldBe rows.length + back.zip(rows).foreach { + case (got, expected) => + tupleSchema.getAttributes.foreach { attribute => + val name = attribute.getName + (got.getField[AnyRef](name), expected.getField[AnyRef](name)) match { + case (g: Array[Byte], e: Array[Byte]) => g shouldBe e + case (g, e) => g shouldBe e + } + } + } + } + + it should "round-trip an empty batch" in { + ArrowUtils.deserializeTuples( + ArrowUtils.serializeTuples(tupleSchema, Array.empty[Tuple]) + ) shouldBe empty + } + + // tuplesOfRoot decodes a live root (the Emit output path's root-to-rows step), + // and must produce the same tuples deserializeTuples produces from the IPC bytes. + "tuplesOfRoot" should "decode a live root to the same tuples as deserializeTuples" in { + val rows = Array( + Tuple + .builder(tupleSchema) + .addSequentially( + Array[Any]( + Int.box(1), + Long.box(2L), + Double.box(3.0), + Boolean.box(true), + new Timestamp(123L), + "a", + Array[Byte](9) + ) + ) + .build(), + Tuple + .builder(tupleSchema) + .addSequentially(Array[Any](null, null, null, null, null, null, null)) + .build() + ) + val bytes = ArrowUtils.serializeTuples(tupleSchema, rows) + val allocator = new RootAllocator() + try { + val fromRoot = ArrowUtils.deserializeRootFold(bytes, allocator)(ArrowUtils.tuplesOfRoot) + val fromBytes = ArrowUtils.deserializeTuples(bytes) + fromRoot.length shouldBe rows.length + fromRoot.zip(fromBytes).foreach { + case (got, expected) => + tupleSchema.getAttributes.foreach { attribute => + ( + got.getField[AnyRef](attribute.getName), + expected.getField[AnyRef](attribute.getName) + ) match { + case (g: Array[Byte], e: Array[Byte]) => g shouldBe e + case (g, e) => g shouldBe e + } + } + } + } finally allocator.close() + } + // A Timestamp reaches past 2262, where counting microseconds through // nanoseconds overflowed, and before 1970. Both come back to the microsecond. it should "round-trip a timestamp either side of the nanosecond range" in { diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregateOpExec.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregateOpExec.scala index 3c26709116f..16871ff8870 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregateOpExec.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregateOpExec.scala @@ -19,8 +19,15 @@ package org.apache.texera.amber.operator.aggregate -import org.apache.texera.amber.core.executor.OperatorExecutor -import org.apache.texera.amber.core.tuple.{Tuple, TupleLike} +import org.apache.arrow.memory.RootAllocator +import org.apache.arrow.vector.VectorSchemaRoot +import org.apache.texera.amber.core.executor.{ + ColumnarOperatorExecutor, + ColumnarResult, + OperatorExecutor +} +import org.apache.texera.amber.core.tuple.{AttributeType, Schema, Tuple, TupleLike} +import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.amber.util.JSONUtils.objectMapper import scala.collection.mutable @@ -28,7 +35,7 @@ import scala.collection.mutable /** * AggregateOpExec performs aggregation operations on input tuples, optionally grouping them by specified keys. */ -class AggregateOpExec(descString: String) extends OperatorExecutor { +class AggregateOpExec(descString: String) extends OperatorExecutor with ColumnarOperatorExecutor { private val desc: AggregateOpDesc = objectMapper.readValue(descString, classOf[AggregateOpDesc]) private var keyedPartialAggregates: mutable.HashMap[List[Object], List[Object]] = _ private var distributedAggregations: List[DistributedAggregation[Object]] = _ @@ -41,6 +48,7 @@ class AggregateOpExec(descString: String) extends OperatorExecutor { override def close(): Unit = { keyedPartialAggregates.clear() distributedAggregations = null + if (columnarAllocator != null) { columnarAllocator.close(); columnarAllocator = null } } override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { @@ -78,6 +86,75 @@ class AggregateOpExec(descString: String) extends OperatorExecutor { } + // ---- Native-Arrow path: accumulate an Arrow batch by reading only the group + // keys and aggregated cells per row (no projected Tuple), feeding the same + // per-value accumulation (iterateValue) the row path uses. Blocking operator, so + // it emits nothing per batch (Consumed); results are produced at onFinish. + @transient private var columnarAllocator: RootAllocator = _ + // Per-batch resolution: the source-column index and Texera type of each group-by + // key, and of each aggregation's input column (index -1 for COUNT(*), which reads + // no column). + @transient private var colGroupIndices: Array[Int] = _ + @transient private var colGroupTypes: Array[AttributeType] = _ + @transient private var colAggIndices: Array[Int] = _ + @transient private var colAggTypes: Array[AttributeType] = _ + + private def aggInputType(agg: AggregationOperation, full: Schema): AttributeType = + if ( + agg.aggFunction == AggregationFunction.COUNT && + (agg.attribute == null || agg.attribute.trim.isEmpty) + ) null + else full.getAttribute(agg.attribute).getType + + // Accumulate one Arrow batch into the group state, reading each group key and + // aggregated value straight from its Arrow vector. Mirrors processTuple's + // accumulation exactly, feeding each aggregation the already-extracted value via + // iterateValue. Blocking operator, so it emits nothing now (Consumed). Does not + // close `root`. + private def accumulateRoot(root: VectorSchemaRoot): ColumnarResult = { + if (distributedAggregations == null || colGroupIndices == null) { + val full = ArrowUtils.toTexeraSchema(root.getSchema) + if (distributedAggregations == null) { + distributedAggregations = + desc.aggregations.map(agg => agg.getAggFunc(aggInputType(agg, full))) + } + colGroupIndices = ArrowUtils.projectionIndices(root, desc.groupByKeys) + colGroupTypes = desc.groupByKeys.map(full.getAttribute(_).getType).toArray + colAggIndices = desc.aggregations.map { agg => + if (agg.attribute == null || agg.attribute.trim.isEmpty) -1 + else ArrowUtils.projectionIndices(root, Seq(agg.attribute))(0) + }.toArray + colAggTypes = desc.aggregations.map(agg => aggInputType(agg, full)).toArray + } + val vectors = root.getFieldVectors + val n = root.getRowCount + val numGroups = colGroupIndices.length + var i = 0 + while (i < n) { + val key = + (0 until numGroups) + .map(g => ArrowUtils.getCell(vectors.get(colGroupIndices(g)), i, colGroupTypes(g))) + .toList + val partials = + keyedPartialAggregates.getOrElseUpdate(key, distributedAggregations.map(_.init())) + val updated = distributedAggregations.zip(partials).zipWithIndex.map { + case ((aggregation, partial), j) => + val idx = colAggIndices(j) + val value: Object = + if (idx < 0) null else ArrowUtils.getCell(vectors.get(idx), i, colAggTypes(j)) + aggregation.iterateValue(partial, value) + } + keyedPartialAggregates(key) = updated + i += 1 + } + ColumnarResult.Consumed + } + + override def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult = { + if (columnarAllocator == null) columnarAllocator = new RootAllocator() + ArrowUtils.deserializeRootFold(arrowIpcBytes, columnarAllocator)(accumulateRoot) + } + override def onFinish(port: Int): Iterator[TupleLike] = { // Finalize aggregation for all keys and produce the result keyedPartialAggregates.iterator.map { diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregationOperation.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregationOperation.scala index 67146e81af2..e4899345ea3 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregationOperation.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/AggregationOperation.scala @@ -21,7 +21,7 @@ package org.apache.texera.amber.operator.aggregate import com.fasterxml.jackson.annotation.{JsonIgnore, JsonProperty, JsonPropertyDescription} import com.kjetland.jackson.jsonSchema.annotations.{JsonSchemaInject, JsonSchemaTitle} -import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, AttributeTypeUtils, Tuple} +import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, AttributeTypeUtils} import org.apache.texera.amber.operator.metadata.annotations.AutofillAttributeName import javax.validation.constraints.NotNull @@ -146,12 +146,10 @@ class AggregationOperation { } new DistributedAggregation[Object]( () => AttributeTypeUtils.zeroValue(attributeType), - (partial, tuple) => { - val value = tuple.getField[Object](attribute) - AttributeTypeUtils.add(partial, value, attributeType) - }, + (partial, value) => AttributeTypeUtils.add(partial, value, attributeType), (partial1, partial2) => AttributeTypeUtils.add(partial1, partial2, attributeType), - partial => partial + partial => partial, + tuple => tuple.getField[Object](attribute) ) } @@ -161,24 +159,19 @@ class AggregationOperation { val countAllRows = attribute == null || attribute.trim.isEmpty new DistributedAggregation[Integer]( () => 0, - (partial, tuple) => - partial + (if (countAllRows || tuple.getField(attribute) != null) 1 else 0), + (partial, value) => partial + (if (countAllRows || value != null) 1 else 0), (partial1, partial2) => partial1 + partial2, - partial => partial + partial => partial, + tuple => if (countAllRows) null else tuple.getField(attribute) ) } private def concatAgg(): DistributedAggregation[String] = { new DistributedAggregation[String]( () => "", - (partial, tuple) => { - if (partial == "") { - if (tuple.getField(attribute) != null) tuple.getField(attribute).toString else "" - } else { - partial + "," + (if (tuple.getField(attribute) != null) - tuple.getField(attribute).toString - else "") - } + (partial, value) => { + val s = if (value != null) value.toString else "" + if (partial == "") s else partial + "," + s }, (partial1, partial2) => { if (partial1 != "" && partial2 != "") { @@ -187,7 +180,8 @@ class AggregationOperation { partial1 + partial2 } }, - partial => partial + partial => partial, + tuple => tuple.getField[Object](attribute) ) } @@ -204,15 +198,15 @@ class AggregationOperation { } new DistributedAggregation[Object]( () => AttributeTypeUtils.maxValue(attributeType), - (partial, tuple) => { - val value = tuple.getField[Object](attribute) + (partial, value) => { val comp = AttributeTypeUtils.compare(value, partial, attributeType) if (value != null && comp < 0) value else partial }, (partial1, partial2) => if (AttributeTypeUtils.compare(partial1, partial2, attributeType) < 0) partial1 else partial2, - partial => if (partial == AttributeTypeUtils.maxValue(attributeType)) null else partial + partial => if (partial == AttributeTypeUtils.maxValue(attributeType)) null else partial, + tuple => tuple.getField[Object](attribute) ) } @@ -229,36 +223,36 @@ class AggregationOperation { } new DistributedAggregation[Object]( () => AttributeTypeUtils.minValue(attributeType), - (partial, tuple) => { - val value = tuple.getField[Object](attribute) + (partial, value) => { val comp = AttributeTypeUtils.compare(value, partial, attributeType) if (value != null && comp > 0) value else partial }, (partial1, partial2) => if (AttributeTypeUtils.compare(partial1, partial2, attributeType) > 0) partial1 else partial2, - partial => if (partial == AttributeTypeUtils.minValue(attributeType)) null else partial + partial => if (partial == AttributeTypeUtils.minValue(attributeType)) null else partial, + tuple => tuple.getField[Object](attribute) ) } - private def getNumericalValue(tuple: Tuple): Option[Double] = { - val value: Object = tuple.getField(attribute) - if (value == null) - return None - - if (tuple.getSchema.getAttribute(attribute).getType == AttributeType.TIMESTAMP) - Option(AttributeTypeUtils.parseTimestamp(value.toString).getTime.toDouble) - else Option(value.toString.toDouble) - } + // Numerical reading of one already-extracted value. A timestamp counts as its + // epoch milliseconds; any other value parses through its string form. null (an + // absent value) is None, so it is skipped by the average. + private def numericalValue(value: Object): Option[Double] = + value match { + case null => None + case ts: java.sql.Timestamp => Option(ts.getTime.toDouble) + case other => Option(other.toString.toDouble) + } private def averageAgg(): DistributedAggregation[AveragePartialObj] = { new DistributedAggregation[AveragePartialObj]( () => AveragePartialObj(0, 0), - (partial, tuple) => { - val value = getNumericalValue(tuple) + (partial, value) => { + val num = numericalValue(value) AveragePartialObj( - partial.sum + (if (value.isDefined) value.get else 0), - partial.count + (if (value.isDefined) 1 else 0) + partial.sum + num.getOrElse(0d), + partial.count + (if (num.isDefined) 1 else 0) ) }, (partial1, partial2) => @@ -266,7 +260,8 @@ class AggregationOperation { partial => { val ret: java.lang.Double = if (partial.count == 0d) null else partial.sum / partial.count ret - } + }, + tuple => tuple.getField[Object](attribute) ) } } diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregation.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregation.scala index eab33e430be..55f063c02ee 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregation.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregation.scala @@ -43,10 +43,17 @@ import org.apache.texera.amber.core.tuple.Tuple case class DistributedAggregation[P <: AnyRef]( // () => PartialObject init: () => P, - // PartialObject + Tuple => PartialObject - iterate: (P, Tuple) => P, + // PartialObject + inputValue => PartialObject. The value is the one cell this + // aggregation reads (already extracted), or null for COUNT(*). Both the row path + // (via `iterate`) and the columnar path feed this, so accumulation is defined once. + iterateValue: (P, Object) => P, // PartialObject + PartialObject => PartialObject merge: (P, P) => P, // PartialObject => Tuple with one column, later be combined into FinalObject - finalAgg: (P) => Object -) + finalAgg: (P) => Object, + // Pull the one value this aggregation reads out of a tuple (null for COUNT(*)). + extract: Tuple => Object +) { + // Row path: extract the value this aggregation reads, then accumulate it. + def iterate(partial: P, tuple: Tuple): P = iterateValue(partial, extract(tuple)) +} diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/filter/SpecializedFilterOpExec.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/filter/SpecializedFilterOpExec.scala index 30f1d955865..32643cd73bc 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/filter/SpecializedFilterOpExec.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/filter/SpecializedFilterOpExec.scala @@ -19,11 +19,99 @@ package org.apache.texera.amber.operator.filter +import org.apache.arrow.memory.RootAllocator +import org.apache.arrow.vector.{Float8Vector, IntVector, VectorSchemaRoot} +import org.apache.texera.amber.core.executor.{ColumnarOperatorExecutor, ColumnarResult} import org.apache.texera.amber.core.tuple.Tuple +import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.amber.util.JSONUtils.objectMapper -class SpecializedFilterOpExec(descString: String) extends FilterOpExec { +class SpecializedFilterOpExec(descString: String) + extends FilterOpExec + with ColumnarOperatorExecutor { private val desc: SpecializedFilterOpDesc = objectMapper.readValue(descString, classOf[SpecializedFilterOpDesc]) + // Row path: OR over predicates, per tuple. setFilterFunc((tuple: Tuple) => desc.predicates.exists(_.evaluate(tuple))) + + private def cmpOf(c: ComparisonType): Int => Boolean = + c match { + case ComparisonType.GREATER_THAN => (r: Int) => r > 0 + case ComparisonType.GREATER_THAN_OR_EQUAL_TO => (r: Int) => r >= 0 + case ComparisonType.LESS_THAN => (r: Int) => r < 0 + case ComparisonType.LESS_THAN_OR_EQUAL_TO => (r: Int) => r <= 0 + case ComparisonType.EQUAL_TO => (r: Int) => r == 0 + case ComparisonType.NOT_EQUAL_TO => (r: Int) => r != 0 + case _ => null + } + + // Native-Arrow path: consume the Arrow batch directly, no per-tuple decode. + @transient private var columnarAllocator: RootAllocator = _ + + // Keep mask for a single supported numeric predicate over the batch, or None if + // the predicate/column shape is not vectorizable (caller falls back to the row + // path). Shared by the emit path and the selection-vector path. + private def columnarMask(root: VectorSchemaRoot): Option[Array[Boolean]] = { + if (desc.predicates.size != 1) return None + val p = desc.predicates.head + val cmp = cmpOf(p.condition) + if (cmp == null) return None + val c = + try p.value.toDouble + catch { case _: NumberFormatException => return None } + val fields = root.getSchema.getFields + var fieldIdx = -1 + var k = 0 + while (k < fields.size && fieldIdx < 0) { + if (fields.get(k).getName == p.attribute) fieldIdx = k + k += 1 + } + if (fieldIdx < 0) return None + val n = root.getRowCount + val mask = new Array[Boolean](n) + root.getVector(fieldIdx) match { + case v: Float8Vector => + var i = 0 + while (i < n) { + mask(i) = !v.isNull(i) && cmp(java.lang.Double.compare(v.get(i), c)); i += 1 + } + Some(mask) + case v: IntVector => + var i = 0 + while (i < n) { + mask(i) = !v.isNull(i) && cmp(java.lang.Double.compare(v.get(i).toDouble, c)); i += 1 + } + Some(mask) + case _ => None // non-numeric column: fall back to the row path + } + } + + override def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult = { + if (columnarAllocator == null) columnarAllocator = new RootAllocator() + ArrowUtils.deserializeRootFold(arrowIpcBytes, columnarAllocator) { root => + columnarMask(root) match { + case Some(mask) => ColumnarResult.Emit(ArrowUtils.selectRows(root, mask, columnarAllocator)) + case None => ColumnarResult.Unsupported + } + } + } + + // Selection-vector path: the indices of surviving rows, computed without + // compacting or copying the batch. None when the predicate is not vectorizable. + // For a fused consumer (count/aggregate) that reads the indices instead of a + // materialized batch. + def columnarSelection(arrowIpcBytes: Array[Byte], port: Int): Option[Array[Int]] = { + if (columnarAllocator == null) columnarAllocator = new RootAllocator() + ArrowUtils.deserializeRootFold(arrowIpcBytes, columnarAllocator) { root => + columnarMask(root).map(mask => ArrowUtils.selectionOf(mask, root.getRowCount)) + } + } + + // Fused filter -> count: number of surviving rows, no batch materialized. + def columnarMatchCount(arrowIpcBytes: Array[Byte], port: Int): Option[Long] = + columnarSelection(arrowIpcBytes, port).map(_.length.toLong) + + override def close(): Unit = { + if (columnarAllocator != null) columnarAllocator.close() + } } diff --git a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/source/scan/arrow/ArrowSourceOpExec.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/source/scan/arrow/ArrowSourceOpExec.scala index 03e5a212385..a92e0b7d052 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/source/scan/arrow/ArrowSourceOpExec.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/source/scan/arrow/ArrowSourceOpExec.scala @@ -25,11 +25,12 @@ import org.apache.texera.amber.core.tuple.TupleLike import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.amber.util.JSONUtils.objectMapper import org.apache.arrow.memory.RootAllocator -import org.apache.arrow.vector.VectorSchemaRoot +import org.apache.arrow.vector.{FieldVector, VectorSchemaRoot} import org.apache.arrow.vector.ipc.ArrowFileReader import java.net.URI import java.nio.file.{Files, StandardOpenOption} +import scala.jdk.CollectionConverters._ class ArrowSourceOpExec( descString: String @@ -57,6 +58,35 @@ class ArrowSourceOpExec( } } + // Columnar produce: the file is already Arrow batches, so hand each one out + // directly (no per-row Tuple decode, no text parse). Each loaded batch's vectors + // are moved into an independent root via zero-copy TransferPair so the caller can + // serialize and close it while the reader loads the next batch. Falls back to the + // row path (None) when offset/limit are set. + override def produceColumnarBatch(): Option[Iterator[VectorSchemaRoot]] = { + if (desc.offset.exists(_ > 0) || desc.limit.isDefined) return None + (reader, root, allocator) match { + case (Some(arrowReader), Some(srcRoot), Some(alloc)) => + Some(new Iterator[VectorSchemaRoot] { + private var loaded = arrowReader.loadNextBatch() + override def hasNext: Boolean = loaded + override def next(): VectorSchemaRoot = { + val rowCount = srcRoot.getRowCount + val moved = srcRoot.getFieldVectors.asScala.map { v => + val pair = v.getTransferPair(alloc) + pair.transfer() + pair.getTo.asInstanceOf[FieldVector] + }.toList + val out = new VectorSchemaRoot(moved.asJava) + out.setRowCount(rowCount) + loaded = arrowReader.loadNextBatch() + out + } + }) + case _ => None + } + } + override def produceTuple(): Iterator[TupleLike] = { val rowIterator = new Iterator[TupleLike] { private var currentIndex = 0 diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/AggregateOpSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/AggregateOpSpec.scala index 9bd4e476e27..52fa9a757f5 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/AggregateOpSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/AggregateOpSpec.scala @@ -20,6 +20,7 @@ package org.apache.texera.amber.operator.aggregate import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema, Tuple} +import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.amber.util.JSONUtils.objectMapper import org.scalatest.funsuite.AnyFunSuite @@ -710,4 +711,59 @@ class AggregateOpSpec extends AnyFunSuite { // 3, not 13: the first run's partial sum for "NY" must have been dropped. assert(results.head.getFields(1).asInstanceOf[Number].intValue() == 3) } + + test("AggregateOpExec columnar accumulate matches the row path (grouped SUM/COUNT/AVG/MIN/MAX)") { + // The native-Arrow accumulate (processColumnarBatch) must produce the same + // grouped result as feeding the same rows through processTuple, so enabling the + // columnar wire never changes an aggregate's output. + val schema = makeSchema( + "city" -> AttributeType.STRING, + "sales" -> AttributeType.INTEGER + ) + val rows = Seq( + makeTuple(schema, "NY", 10), + makeTuple(schema, "SF", 20), + makeTuple(schema, "NY", 5), + makeTuple(schema, "SF", 7), + makeTuple(schema, "NY", null) // a null value: counted by COUNT(*) only, skipped by the rest + ) + + val desc = new AggregateOpDesc() + desc.aggregations = List( + makeAggregationOp(AggregationFunction.SUM, "sales", "sum_sales"), + makeAggregationOp(AggregationFunction.COUNT, "", "n_rows"), + makeAggregationOp(AggregationFunction.AVERAGE, "sales", "avg_sales"), + makeAggregationOp(AggregationFunction.MIN, "sales", "min_sales"), + makeAggregationOp(AggregationFunction.MAX, "sales", "max_sales") + ) + desc.groupByKeys = List("city") + val descJson = objectMapper.writeValueAsString(desc) + + def resultsOf(feed: AggregateOpExec => Unit): Map[String, List[Any]] = { + val exec = new AggregateOpExec(descJson) + exec.open() + feed(exec) + val out = exec + .onFinish(0) + .toList + .map { t => + val f = t.getFields + f(0).asInstanceOf[String] -> f.drop(1).toList + } + .toMap + exec.close() + out + } + + val rowResult = resultsOf(exec => rows.foreach(exec.processTuple(_, 0))) + val colResult = + resultsOf(exec => + exec.processColumnarBatch(ArrowUtils.serializeTuples(schema, rows.toArray), 0) + ) + + assert(colResult.keySet == rowResult.keySet) + colResult.keys.foreach { city => + assert(colResult(city) == rowResult(city), s"group $city differs") + } + } } diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregationSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregationSpec.scala index 4a6cd7db97f..fedef169da7 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregationSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/aggregate/DistributedAggregationSpec.scala @@ -36,33 +36,50 @@ class DistributedAggregationSpec extends AnyFlatSpec { // Partial = (sum, count) private val avgAggregation = DistributedAggregation[(Long, Long)]( init = () => (0L, 0L), - iterate = (partial, t) => (partial._1 + t.getField[Integer]("v").longValue(), partial._2 + 1L), + iterateValue = + (partial, v) => (partial._1 + v.asInstanceOf[Integer].longValue(), partial._2 + 1L), merge = (a, b) => (a._1 + b._1, a._2 + b._2), finalAgg = partial => if (partial._2 == 0L) java.lang.Double.valueOf(0.0d) - else java.lang.Double.valueOf(partial._1.toDouble / partial._2.toDouble) + else java.lang.Double.valueOf(partial._1.toDouble / partial._2.toDouble), + extract = t => t.getField[Integer]("v") ) // --------------------------------------------------------------------------- // Case-class shape // --------------------------------------------------------------------------- - "DistributedAggregation" should "expose all four function members" in { + "DistributedAggregation" should "expose all function members" in { assert(avgAggregation.init != null) - assert(avgAggregation.iterate != null) + assert(avgAggregation.iterateValue != null) assert(avgAggregation.merge != null) assert(avgAggregation.finalAgg != null) + assert(avgAggregation.extract != null) } it should "be a case class (equality on same function refs)" in { val sameInit = avgAggregation.init - val sameIter = avgAggregation.iterate + val sameIterValue = avgAggregation.iterateValue val sameMerge = avgAggregation.merge val sameFinal = avgAggregation.finalAgg - val copy = DistributedAggregation[(Long, Long)](sameInit, sameIter, sameMerge, sameFinal) + val sameExtract = avgAggregation.extract + val copy = + DistributedAggregation[(Long, Long)]( + sameInit, + sameIterValue, + sameMerge, + sameFinal, + sameExtract + ) assert(copy == avgAggregation) } + it should "derive iterate from extract + iterateValue (row path equals value path)" in { + val viaTuple = avgAggregation.iterate(avgAggregation.init(), tup(9)) + val viaValue = avgAggregation.iterateValue(avgAggregation.init(), Integer.valueOf(9)) + assert(viaTuple == viaValue) + } + // --------------------------------------------------------------------------- // init — produces a fresh partial each call // --------------------------------------------------------------------------- diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/filter/SelectionVectorFilterSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/filter/SelectionVectorFilterSpec.scala new file mode 100644 index 00000000000..516df12879c --- /dev/null +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/filter/SelectionVectorFilterSpec.scala @@ -0,0 +1,111 @@ +/* + * 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.texera.amber.operator.filter + +import org.apache.texera.amber.core.executor.ColumnarResult +import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema, Tuple} +import org.apache.texera.amber.util.ArrowUtils +import org.apache.texera.amber.util.JSONUtils.objectMapper +import org.scalatest.flatspec.AnyFlatSpec + +import scala.util.Random + +/** + * The specialized filter's native-Arrow path. + * + * The selection vector (surviving row indices, no compaction) must name exactly + * the rows the row path keeps, the fused filter->count must equal the row path's + * survivor count without materializing a batch, and the compacting Emit path must + * return the same rows. All three are checked against the row path (filterFunc) as + * the source of truth. + */ +class SelectionVectorFilterSpec extends AnyFlatSpec { + + private def execFor(preds: (String, ComparisonType, String)*): SpecializedFilterOpExec = { + val desc = new SpecializedFilterOpDesc() + desc.predicates = preds.map { case (a, c, v) => new FilterPredicate(a, c, v) }.toList + new SpecializedFilterOpExec(objectMapper.writeValueAsString(desc)) + } + + private val schema: Schema = Schema().add(new Attribute("v", AttributeType.INTEGER)) + + private def batch(seed: Int, n: Int): Array[Tuple] = { + val rng = new Random(seed) + (0 until n).map { i => + val v: Any = if (i % 11 == 0) null else Int.box(rng.nextInt(100)) + Tuple.builder(schema).add(schema.getAttribute("v"), v).build() + }.toArray + } + + "selection vector" should "name exactly the rows the row path keeps" in { + val rows = batch(1, 2000) + val bytes = ArrowUtils.serializeTuples(schema, rows) + val exec = execFor(("v", ComparisonType.GREATER_THAN, "50")) + exec.open() + + val expectedIdx = rows.indices.filter(i => exec.filterFunc(rows(i))).toArray + + val sel = exec.columnarSelection(bytes, 0) + assert(sel.isDefined, "expected a vectorized selection") + assert(sel.get.toSeq == expectedIdx.toSeq, "selection indices must match row-path survivors") + exec.close() + } + + "fused filter -> count" should "equal the row-path count without materializing" in { + val rows = batch(2, 3000) + val bytes = ArrowUtils.serializeTuples(schema, rows) + val exec = execFor(("v", ComparisonType.LESS_THAN_OR_EQUAL_TO, "40")) + exec.open() + + val expected = rows.count(exec.filterFunc).toLong + assert(exec.columnarMatchCount(bytes, 0).contains(expected)) + exec.close() + } + + "the compact Emit path" should "still return the same surviving rows" in { + val rows = batch(3, 2500) + val bytes = ArrowUtils.serializeTuples(schema, rows) + val exec = execFor(("v", ComparisonType.GREATER_THAN, "10")) + exec.open() + + val expected = rows.count(exec.filterFunc) + exec.processColumnarBatch(bytes, 0) match { + case ColumnarResult.Emit(root) => + assert(root.getRowCount == expected) + root.close() + case other => fail(s"expected Emit, got $other") + } + exec.close() + } + + "gather from a selection" should "yield the same rows as the row path" in { + val rows = batch(4, 1500) + val bytes = ArrowUtils.serializeTuples(schema, rows) + val exec = execFor(("v", ComparisonType.GREATER_THAN, "70")) + exec.open() + + val sel = exec.columnarSelection(bytes, 0).get + val decoded = ArrowUtils.deserializeTuples(bytes) + val gathered = sel.map(i => decoded(i).getField[Any]("v")).toSeq + val expected = rows.filter(exec.filterFunc).map(_.getField[Any]("v")).toSeq + assert(gathered == expected) + exec.close() + } +}