diff --git a/amber/src/bench/scala/org/apache/texera/amber/bench/ArrowFlightActorBench.scala b/amber/src/bench/scala/org/apache/texera/amber/bench/ArrowFlightActorBench.scala index 602f4f08415..002a41936e1 100644 --- a/amber/src/bench/scala/org/apache/texera/amber/bench/ArrowFlightActorBench.scala +++ b/amber/src/bench/scala/org/apache/texera/amber/bench/ArrowFlightActorBench.scala @@ -331,7 +331,8 @@ object ArrowFlightActorBench { // batch_size = cfg.batchSize keeps the Python-side partitioning // buffer aligned with our send size — one Java DataFrame in maps // to exactly one Python DataFrame out. - OneToOnePartitioning(batchSize = cfg.batchSize, channels = Seq(dataChannelOut)) + OneToOnePartitioning(batchSize = cfg.batchSize, channels = Seq(dataChannelOut)), + columnar = false ), ctx, 4L diff --git a/amber/src/main/protobuf/org/apache/texera/amber/engine/architecture/rpc/controlcommands.proto b/amber/src/main/protobuf/org/apache/texera/amber/engine/architecture/rpc/controlcommands.proto index e60b7cd74f6..ecca229c54d 100644 --- a/amber/src/main/protobuf/org/apache/texera/amber/engine/architecture/rpc/controlcommands.proto +++ b/amber/src/main/protobuf/org/apache/texera/amber/engine/architecture/rpc/controlcommands.proto @@ -235,6 +235,10 @@ message AddInputChannelRequest { message AddPartitioningRequest { core.PhysicalLink tag = 1 [(scalapb.field).no_box = true]; sendsemantics.Partitioning partitioning = 2 [(scalapb.field).no_box = true]; + // Per-edge columnar-wire decision from the planner: ship this link's batches as + // Arrow (ColumnarFrame) rather than rows. True only when the downstream operator + // consumes columns natively and the columnar wire is enabled. + bool columnar = 3; } message AssignPortRequest { diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/LinkWorkersHandler.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/LinkWorkersHandler.scala index 4df3c495224..cd8f924c30c 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/LinkWorkersHandler.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/LinkWorkersHandler.scala @@ -49,7 +49,7 @@ trait LinkWorkersHandler { linkExecution.initChannelExecution(channelId) Seq( workerInterface.addPartitioning( - AddPartitioningRequest(msg.link, linkConfig.partitioning), + AddPartitioningRequest(msg.link, linkConfig.partitioning, linkConfig.columnar), mkContext(channelId.fromWorkerId) ), workerInterface.addInputChannel( diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/messaginglayer/OutputManager.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/messaginglayer/OutputManager.scala index b15e8992546..d395ab6f1f9 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/messaginglayer/OutputManager.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/messaginglayer/OutputManager.scala @@ -37,7 +37,10 @@ import org.apache.texera.amber.engine.architecture.worker.managers.{ PortStorageWriterTerminateSignal } import org.apache.texera.amber.engine.common.AmberLogging -import org.apache.texera.amber.util.VirtualIdentityUtils +import org.apache.texera.amber.engine.common.ambermessage.ColumnarFrame +import org.apache.texera.amber.util.{ArrowUtils, VirtualIdentityUtils} +import org.apache.arrow.memory.{BufferAllocator, RootAllocator} +import org.apache.arrow.vector.VectorSchemaRoot import java.net.URI import scala.collection.mutable @@ -134,12 +137,16 @@ class OutputManager( */ def addPartitionerWithPartitioning( link: PhysicalLink, - partitioning: Partitioning + partitioning: Partitioning, + // Per-edge columnar-wire decision from the planner: ship this edge's batches as + // Arrow (ColumnarFrame) rather than rows. Defaults off so the row wire is unchanged. + columnar: Boolean = false ): Unit = { val partitioner = toPartitioner(partitioning, actorId) partitioners.update(link, partitioner) partitioner.allReceivers.foreach(receiver => { - val buffer = new NetworkOutputBuffer(receiver, outputGateway, getBatchSize(partitioning)) + val buffer = + new NetworkOutputBuffer(receiver, outputGateway, getBatchSize(partitioning), columnar) networkOutputBuffers.update((link, receiver), buffer) outputGateway.addOutputChannel(ChannelIdentity(actorId, receiver, isControl = false)) }) @@ -168,6 +175,113 @@ class OutputManager( } } + /** + * Provisional (phase 1): turn the columnar output path on or off for every current + * edge. Phase 2 replaces this blanket switch with a per-edge decision made by the + * planner (local vs cross-node, and whether the receiver consumes columns). + */ + def setColumnarOutput(enabled: Boolean): Unit = + networkOutputBuffers.values.foreach(_.columnar = enabled) + + // Backs the per-bucket roots the columnar shuffle materializes; each is freed right + // after it is serialized, so this allocator's outstanding bytes return to zero. + private lazy val columnarAllocator: BufferAllocator = new RootAllocator() + + /** + * Columnar output path (no round-trip): ship an operator's Arrow result batch straight + * downstream without decoding it to tuples and re-encoding it. Eligible only when the + * output port needs no storage and every edge on it is in columnar mode. A + * non-splitting edge (one-to-one, broadcast) sends the whole batch, serialized once, to + * each receiver. A splitting edge (hash/range/round-robin) assigns each row a bucket + * through the same getBucketIndex the row path uses (reading only the key columns into a + * partial tuple), gathers each bucket's rows in Arrow, and sends one ColumnarFrame per + * non-empty bucket. Bucket assignment is therefore identical to the row path. Does NOT + * close `root` (caller owns it). + * + * @return Some((portId, totalSerializedByteCount)) if it shipped the batch (caller + * records output statistics), or None if the port is not eligible (fall back). + */ + def tryEmitColumnarBatch( + root: VectorSchemaRoot, + outputPortId: Option[PortIdentity] + ): Option[(PortIdentity, Long)] = { + if (outputPortId.isEmpty && ports.size != 1) return None + val portId = outputPortId.getOrElse(ports.head._1) + val links = partitioners.filter(_._1.fromPortId == portId).toSeq + val eligible = + links.nonEmpty && + !outputPortResultWriterThreads.contains(portId) && + links.forall { + case (link, partitioner) => + partitioner.allReceivers.forall(r => + networkOutputBuffers.get((link, r)).exists(_.columnar) + ) + } + if (!eligible) return None + val schema = getPort(portId).schema + // The executor's Arrow schema must match the declared output port schema; if not, + // fall back so the row path's enforceSchema can adjust it. + if (ArrowUtils.toTexeraSchema(root.getSchema) != schema) return None + Some((portId, links.map { case (link, p) => emitColumnarToLink(root, schema, link, p) }.sum)) + } + + // Ship `root` to one link's receivers in Arrow, splitting per bucket if the partitioner + // splits. Returns the total serialized bytes put on the wire for this link. + private def emitColumnarToLink( + root: VectorSchemaRoot, + schema: Schema, + link: PhysicalLink, + partitioner: Partitioner + ): Long = { + val receivers = partitioner.allReceivers + if (!partitioner.columnarSplits) { + // One-to-one / broadcast: the whole batch goes to each receiver; serialize once. + val bytes = ArrowUtils.serializeRoot(root) + val frame = ColumnarFrame(bytes, root.getRowCount, schema) + receivers.foreach(r => networkOutputBuffers((link, r)).sendColumnarFrame(frame)) + bytes.length.toLong + } else { + // Splitting shuffle: assign each row a bucket, then gather and send per bucket. + val n = root.getRowCount + val rowsPerBucket = Array.fill(receivers.length)(new mutable.ArrayBuffer[Int]()) + // Build only the columns getBucketIndex needs into a partial tuple per row. + val keyCols = + if (partitioner.requiresFullTuple) schema.getAttributes.map(_.getName) + else partitioner.bucketKeyColumns + val keySchema = Schema(keyCols.map(schema.getAttribute).toList) + val keyIdx = + if (keyCols.isEmpty) Array.empty[Int] else ArrowUtils.projectionIndices(root, keyCols) + val keyTypes = keyCols.map(schema.getAttribute(_).getType).toArray + val vectors = root.getFieldVectors + var i = 0 + while (i < n) { + val keyValues = new Array[Any](keyIdx.length) + var c = 0 + while (c < keyIdx.length) { + keyValues(c) = ArrowUtils.getCell(vectors.get(keyIdx(c)), i, keyTypes(c)); c += 1 + } + partitioner.getBucketIndex(Tuple(keySchema, keyValues)).foreach(b => rowsPerBucket(b) += i) + i += 1 + } + var bytes = 0L + var b = 0 + while (b < receivers.length) { + val rows = rowsPerBucket(b) + if (rows.nonEmpty) { + val bucketRoot = ArrowUtils.gather(root, rows.toArray, columnarAllocator) + try { + val frameBytes = ArrowUtils.serializeRoot(bucketRoot) + networkOutputBuffers((link, receivers(b))) + .sendColumnarFrame(ColumnarFrame(frameBytes, rows.length, schema)) + bytes += frameBytes.length.toLong + } finally bucketRoot.close() + } + b += 1 + } + bytes + } + } + /** * Flushes the network output buffers based on the specified set of physical links. * 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/scheduling/config/LinkConfig.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/scheduling/config/LinkConfig.scala index ef92afef593..7c72c3a580a 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/scheduling/config/LinkConfig.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/scheduling/config/LinkConfig.scala @@ -94,5 +94,8 @@ case object LinkConfig { case class LinkConfig( channelConfigs: List[ChannelConfig], - partitioning: Partitioning + partitioning: Partitioning, + // Per-edge columnar-wire decision: ship this link's batches as Arrow when the + // downstream operator consumes columns natively and the columnar wire is enabled. + columnar: Boolean = false ) diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/scheduling/resourcePolicies/ResourceAllocator.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/scheduling/resourcePolicies/ResourceAllocator.scala index 346fc195e6c..9b4083c1c74 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/scheduling/resourcePolicies/ResourceAllocator.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/scheduling/resourcePolicies/ResourceAllocator.scala @@ -19,6 +19,7 @@ package org.apache.texera.amber.engine.architecture.scheduling.resourcePolicies +import org.apache.texera.common.config.ApplicationConfig import org.apache.texera.amber.core.virtualidentity.PhysicalOpIdentity import org.apache.texera.amber.core.workflow._ import org.apache.texera.amber.engine.architecture.scheduling.Region @@ -89,7 +90,14 @@ class DefaultResourceAllocator( operatorConfigs(physicalLink.toOpId).workerConfigs.map(_.workerId), linkPartitionInfos(physicalLink), workflowSettings.dataTransferBatchSize - ) + ), + // Per-edge columnar-wire decision: enabled only when the wire is on and the + // downstream operator consumes columns natively (otherwise shipping Arrow + // would force a costly per-tuple decode on the receiver). Locality (same-JVM + // links gain nothing) is a later refinement. + columnar = + ApplicationConfig.enableColumnarWire && + physicalPlan.getOperator(physicalLink.toOpId).isColumnarCapable ) }.toMap diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/BroadcastPartitioner.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/BroadcastPartitioner.scala index 71de92ced60..35c7f757327 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/BroadcastPartitioner.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/BroadcastPartitioner.scala @@ -32,4 +32,7 @@ case class BroadcastPartitioner(partitioning: BroadcastPartitioning) extends Par } override def allReceivers: Seq[ActorVirtualIdentity] = receivers + + // The whole batch goes to every receiver; no per-row split. + override def columnarSplits: Boolean = false } diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/HashBasedShufflePartitioner.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/HashBasedShufflePartitioner.scala index 7025a4bd2cf..b745f9de137 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/HashBasedShufflePartitioner.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/HashBasedShufflePartitioner.scala @@ -38,4 +38,8 @@ case class HashBasedShufflePartitioner(partitioning: HashBasedShufflePartitionin } override def allReceivers: Seq[ActorVirtualIdentity] = receivers + + // Hash reads the key columns (or the whole tuple when no keys are named). + override def bucketKeyColumns: Seq[String] = partitioning.hashAttributeNames + override def requiresFullTuple: Boolean = partitioning.hashAttributeNames.isEmpty } diff --git a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/OneToOnePartitioner.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/OneToOnePartitioner.scala index f1360f8be4a..41d4b020d81 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/OneToOnePartitioner.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/OneToOnePartitioner.scala @@ -30,4 +30,7 @@ case class OneToOnePartitioner(partitioning: OneToOnePartitioning, actorId: Acto override def allReceivers: Seq[ActorVirtualIdentity] = Seq(partitioning.channels.filter(_.fromWorkerId == actorId).head.toWorkerId) + + // The whole batch goes to the single receiver; no per-row split. + override def columnarSplits: Boolean = false } 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..7dfce654257 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 @@ -32,12 +33,36 @@ trait Partitioner extends Serializable { def getBucketIndex(tuple: Tuple): Iterator[Int] def allReceivers: Seq[ActorVirtualIdentity] + + // ---- Columnar-shuffle support (phase 3) ------------------------------------- + // These let the columnar output path assign buckets by reading only the columns a + // partitioner needs, then scatter whole rows in Arrow, without decoding every row + // to a Tuple. Bucket assignment still goes through getBucketIndex, so it is + // identical to the row path by construction. + + // Whether this partitioner splits a batch across receivers (hash/range/round-robin) + // or sends the whole batch to each receiver (one-to-one, broadcast). + def columnarSplits: Boolean = true + + // The columns getBucketIndex reads from a tuple (hash/range keys). Empty when the + // bucket does not depend on the row's data (round-robin). The columnar path builds a + // partial tuple of just these columns per row to assign the bucket. + def bucketKeyColumns: Seq[String] = Seq.empty + + // When true, the bucket depends on the whole row (e.g. hash with no key columns, which + // hashes the entire tuple), so the columnar path must build the full tuple per row. + def requiresFullTuple: Boolean = false } class NetworkOutputBuffer( val to: ActorVirtualIdentity, val dataOutputPort: NetworkOutputGateway, - val batchSize: Int = ApplicationConfig.defaultDataTransferBatchSize + val batchSize: Int = ApplicationConfig.defaultDataTransferBatchSize, + // Columnar wire: flush as ColumnarFrame (Arrow IPC) instead of DataFrame. The + // planner decides this per edge (it folds in ApplicationConfig.enableColumnarWire + // and the downstream operator's columnar capability) and passes it in; off by + // default so the row wire is unchanged. A var so the output manager can set it. + var columnar: Boolean = false ) { var buffer = new ArrayBuffer[Tuple]() @@ -55,9 +80,27 @@ class NetworkOutputBuffer( flush() } + // Columnar fast path: ship an already-serialized Arrow batch straight to the + // receiver, skipping the per-tuple accumulate/re-encode. Any tuples already + // buffered are flushed first so the batch keeps its place in the FIFO stream. + def sendColumnarFrame(frame: ColumnarFrame): Unit = { + flush() + dataOutputPort.sendTo(to, frame) + } + 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/sendsemantics/partitioners/RangeBasedShufflePartitioner.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/RangeBasedShufflePartitioner.scala index 9b8aae263f1..95a305abe9e 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/RangeBasedShufflePartitioner.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/RangeBasedShufflePartitioner.scala @@ -56,4 +56,6 @@ case class RangeBasedShufflePartitioner(partitioning: RangeBasedShufflePartition override def allReceivers: Seq[ActorVirtualIdentity] = receivers + // Range partitions on the first range attribute only. + override def bucketKeyColumns: Seq[String] = Seq(partitioning.rangeAttributeNames.head) } 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..031caf20bc7 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,98 @@ 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) => + try { + val rowCount = root.getRowCount + // No round-trip: on an eligible (one-to-one, no-storage, columnar) + // edge, ship the Arrow batch straight downstream. Otherwise + // materialize it to output tuples (the row output path) and let the + // network buffer handle it. + outputManager.tryEmitColumnarBatch(root, None) match { + case Some((portId, byteCount)) => + statisticsManager.increaseOutputStatistics(portId, rowCount.toLong, byteCount) + case None => + val tuples = ArrowUtils.tuplesOfRoot(root) + outputManager.outputIterator.setTupleOutput( + tuples.iterator.map(t => (t: TupleLike, None: Option[PortIdentity])) + ) + } + } finally root.close() + 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 +340,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..ce7cb2c8ff7 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)) } } @@ -94,11 +104,21 @@ class StatisticsManager { * @param portId the port identity * @param size the size of the tuple */ - def increaseOutputStatistics(portId: PortIdentity, size: Long): Unit = { + def increaseOutputStatistics(portId: PortIdentity, size: Long): Unit = + increaseOutputStatistics(portId, 1L, size) + + /** + * Increases the output statistics for a given port by a whole batch at once: + * `count` tuples totalling `size` bytes. The columnar-wire output fast path ships + * 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 increaseOutputStatistics(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") outputStatistics.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/architecture/worker/promisehandlers/AddPartitioningHandler.scala b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/AddPartitioningHandler.scala index c2da82214bd..6d45d9f8bd1 100644 --- a/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/AddPartitioningHandler.scala +++ b/amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/AddPartitioningHandler.scala @@ -40,7 +40,7 @@ trait AddPartitioningHandler { ctx: AsyncRPCContext ): Future[EmptyReturn] = { dp.stateManager.assertState(READY, RUNNING, PAUSED) - dp.outputManager.addPartitionerWithPartitioning(msg.tag, msg.partitioning) + dp.outputManager.addPartitionerWithPartitioning(msg.tag, msg.partitioning, msg.columnar) EmptyReturn() } 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/messaginglayer/OutputManagerSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/messaginglayer/OutputManagerSpec.scala index 943dd98ab63..26fbda10d09 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/messaginglayer/OutputManagerSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/messaginglayer/OutputManagerSpec.scala @@ -49,9 +49,13 @@ import org.apache.texera.amber.engine.architecture.messaginglayer.OutputManager. toPartitioner } import org.apache.texera.amber.engine.common.ambermessage._ +import org.apache.texera.amber.util.ArrowUtils +import org.apache.arrow.memory.RootAllocator import org.scalamock.scalatest.MockFactory import org.scalatest.flatspec.AnyFlatSpec +import scala.collection.mutable.ArrayBuffer + class OutputManagerSpec extends AnyFlatSpec with MockFactory { private val mockHandler = mock[WorkflowFIFOMessage => Unit] @@ -377,6 +381,171 @@ class OutputManagerSpec extends AnyFlatSpec with MockFactory { outputManager.closeOutputStorageWriterIfNeeded(PortIdentity(998)) } + // -- tryEmitColumnarBatch (phase 1: no-round-trip columnar output) -------- + + private def capturingOutputManager(): (OutputManager, ArrayBuffer[WorkflowFIFOMessage]) = { + val sent = ArrayBuffer[WorkflowFIFOMessage]() + val om = + new OutputManager(identifier, new NetworkOutputGateway(identifier, m => { sent += m; () })) + (om, sent) + } + + private def emitThrough( + om: OutputManager, + rows: Array[Tuple] + ): Option[(PortIdentity, Long)] = { + val alloc = new RootAllocator() + try { + ArrowUtils.deserializeRootFold(ArrowUtils.serializeTuples(schema, rows), alloc)(root => + om.tryEmitColumnarBatch(root, None) + ) + } finally alloc.close() + } + + private def rowWith(v: Int): Tuple = TupleLike(v, 2, 3, 4, "5", 9.8).enforceSchema(schema) + + // Emit a batch columnar through a fresh OutputManager carrying `partitioning`, then + // return, per receiver, the rows that receiver actually got (decoded from the Arrow + // frames put on the wire). + private def columnarShuffle( + portId: PortIdentity, + partitioning: Partitioning, + rows: Array[Tuple] + ): Map[ActorVirtualIdentity, List[Tuple]] = { + val (om, sent) = capturingOutputManager() + om.addPort(portId, schema, None) + om.addPartitionerWithPartitioning( + PhysicalLink(physicalOpId(), portId, physicalOpId(), portId), + partitioning, + columnar = true + ) + emitThrough(om, rows) + sent + .groupBy(_.channelId.toWorkerId) + .map { + case (receiver, msgs) => + receiver -> msgs + .flatMap(m => + ArrowUtils.deserializeTuples(m.payload.asInstanceOf[ColumnarFrame].arrowIpcBytes) + ) + .toList + } + } + + // Per receiver, the rows the row-path partitioner would assign, in order. + private def rowPathBuckets( + partitioner: org.apache.texera.amber.engine.architecture.sendsemantics.partitioners.Partitioner, + receivers: Seq[ActorVirtualIdentity], + rows: Array[Tuple] + ): Map[ActorVirtualIdentity, List[Tuple]] = + rows.toList + .flatMap(t => partitioner.getBucketIndex(t).map(b => receivers(b) -> t)) + .groupBy(_._1) + .map { case (r, pairs) => r -> pairs.map(_._2) } + + "tryEmitColumnarBatch" should "ship a one-to-one columnar edge as a single ColumnarFrame" in { + val (om, sent) = capturingOutputManager() + val portId = PortIdentity(20) + om.addPort(portId, schema, None) + om.addPartitionerWithPartitioning( + PhysicalLink(physicalOpId(), portId, physicalOpId(), portId), + OneToOnePartitioning(1000, Seq(channelTo(ActorVirtualIdentity("o2o-col-rec")))) + ) + om.setColumnarOutput(true) + + val rows = Array(sampleTuple(), sampleTuple(), sampleTuple()) + val result = emitThrough(om, rows) + + assert(result.exists(_._1 == portId)) + assert(sent.size == 1) + val cf = sent.head.payload.asInstanceOf[ColumnarFrame] + assert(cf.rowCount == 3) + assert(ArrowUtils.deserializeTuples(cf.arrowIpcBytes).toList == rows.toList) + } + + it should "hash-shuffle rows to the same buckets the row partitioner would" in { + val receivers = (0 until 3).map(i => ActorVirtualIdentity(s"hash-r$i")) + val partitioning = + HashBasedShufflePartitioning(1000, receivers.map(channelTo), Seq("field1")) + val rows = (0 until 50).map(rowWith).toArray + val got = columnarShuffle(PortIdentity(30), partitioning, rows) + val expected = rowPathBuckets(HashBasedShufflePartitioner(partitioning), receivers, rows) + // No row lost or duplicated, and each receiver gets exactly the row-path bucket. + assert(got.values.map(_.size).sum == rows.length) + receivers.foreach(r => assert(got.getOrElse(r, Nil) == expected.getOrElse(r, Nil))) + } + + it should "range-shuffle rows to the same buckets the row partitioner would" in { + val receivers = (0 until 3).map(i => ActorVirtualIdentity(s"range-r$i")) + val partitioning = + RangeBasedShufflePartitioning(1000, receivers.map(channelTo), Seq("field1"), 0, 49) + val rows = (0 until 50).map(rowWith).toArray + val got = columnarShuffle(PortIdentity(31), partitioning, rows) + val expected = rowPathBuckets(RangeBasedShufflePartitioner(partitioning), receivers, rows) + assert(got.values.map(_.size).sum == rows.length) + receivers.foreach(r => assert(got.getOrElse(r, Nil) == expected.getOrElse(r, Nil))) + } + + it should "round-robin rows to the same buckets the row partitioner would (counter parity)" in { + val receivers = (0 until 3).map(i => ActorVirtualIdentity(s"rr-r$i")) + val partitioning = RoundRobinPartitioning(1000, receivers.map(channelTo)) + val rows = (0 until 10).map(rowWith).toArray + val got = columnarShuffle(PortIdentity(32), partitioning, rows) + // A fresh row-path partitioner advances the same counter over the same row order. + val expected = rowPathBuckets(RoundRobinPartitioner(partitioning), receivers, rows) + assert(got.values.map(_.size).sum == rows.length) + receivers.foreach(r => assert(got.getOrElse(r, Nil) == expected.getOrElse(r, Nil))) + } + + it should "broadcast the whole batch to every receiver" in { + val receivers = (0 until 3).map(i => ActorVirtualIdentity(s"bc-r$i")) + val partitioning = BroadcastPartitioning(1000, receivers.map(channelTo)) + val rows = (0 until 5).map(rowWith).toArray + val got = columnarShuffle(PortIdentity(33), partitioning, rows) + receivers.foreach(r => assert(got.getOrElse(r, Nil) == rows.toList)) + } + + it should "decline when columnar output is off" in { + val (om, sent) = capturingOutputManager() + val portId = PortIdentity(22) + om.addPort(portId, schema, None) + om.addPartitionerWithPartitioning( + PhysicalLink(physicalOpId(), portId, physicalOpId(), portId), + OneToOnePartitioning(1000, Seq(channelTo(ActorVirtualIdentity("off-rec")))) + ) + // Columnar output left off. + assert(emitThrough(om, Array(sampleTuple())).isEmpty) + assert(sent.isEmpty) + } + + "addPartitionerWithPartitioning" should "make the edge's buffers columnar when told to" in { + val (om, sent) = capturingOutputManager() + val portId = PortIdentity(23) + om.addPort(portId, schema, None) + om.addPartitionerWithPartitioning( + PhysicalLink(physicalOpId(), portId, physicalOpId(), portId), + // batchSize 1 so the single tuple auto-flushes. + OneToOnePartitioning(1, Seq(channelTo(ActorVirtualIdentity("col-edge-rec")))), + columnar = true + ) + om.passTupleToDownstream(sampleTuple(), Some(portId)) + assert(sent.size == 1) + assert(sent.head.payload.isInstanceOf[ColumnarFrame]) + } + + it should "keep the edge's buffers on rows by default" in { + val (om, sent) = capturingOutputManager() + val portId = PortIdentity(24) + om.addPort(portId, schema, None) + om.addPartitionerWithPartitioning( + PhysicalLink(physicalOpId(), portId, physicalOpId(), portId), + OneToOnePartitioning(1, Seq(channelTo(ActorVirtualIdentity("row-edge-rec")))) + ) + om.passTupleToDownstream(sampleTuple(), Some(portId)) + assert(sent.size == 1) + assert(sent.head.payload.isInstanceOf[DataFrame]) + } + "getSingleOutputPortIdentity" should "return the sole port when exactly one is present" in { val outputManager: OutputManager = wire[OutputManager] val portId = PortIdentity(13) diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/ColumnarPipelineBenchSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/ColumnarPipelineBenchSpec.scala new file mode 100644 index 00000000000..f4c2273ca2c --- /dev/null +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/ColumnarPipelineBenchSpec.scala @@ -0,0 +1,224 @@ +/* + * 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.executor.ColumnarResult +import org.apache.texera.amber.core.tuple.{ + Attribute, + AttributeType, + Schema, + SchemaEnforceable, + Tuple +} +import org.apache.texera.amber.engine.common.AmberRuntime +import org.apache.texera.amber.engine.common.ambermessage.{ColumnarFrame, DataFrame} +import org.apache.texera.amber.operator.aggregate.{ + AggregateOpDesc, + AggregateOpExec, + AggregationFunction, + AggregationOperation +} +import org.apache.texera.amber.operator.filter.{ + ComparisonType, + FilterPredicate, + SpecializedFilterOpDesc, + SpecializedFilterOpExec +} +import org.apache.texera.amber.operator.projection.{ + AttributeUnit, + ProjectionOpDesc, + ProjectionOpExec +} +import org.apache.texera.amber.util.ArrowUtils +import org.apache.texera.amber.util.JSONUtils.{objectMapper => om} +import org.scalatest.BeforeAndAfterAll +import org.scalatest.flatspec.AnyFlatSpecLike + +import scala.util.Random + +/** + * End-to-end cost of a columnar region vs the row path for a realistic three-operator + * pipeline, project -> filter -> aggregate, with each operator on its own worker so the + * batch crosses a serialized edge between them. The row path serializes a DataFrame and + * runs each operator tuple-at-a-time; the columnar path serializes an Arrow ColumnarFrame + * and runs each operator's processColumnarBatch (no per-tuple decode), staying columnar + * across all three hops. Serialization uses the real Pekko/Kryo stack, so these numbers + * reflect a cross-worker (cross-node) region, which is where the columnar wire is meant to + * pay off. Run with: + * sbt -Dcolumnar.bench=true "WorkflowExecutionService/testOnly *ColumnarPipelineBenchSpec" + */ +class ColumnarPipelineBenchSpec + extends TestKit(ActorSystem("ColumnarPipelineBench", AmberRuntime.pekkoConfig)) + with AnyFlatSpecLike + with BeforeAndAfterAll { + + private val serde = SerializationExtension(system) + private val rng = new Random(7) + + // Source schema: a shuffle/group key, a numeric filter column, and two string payload + // columns the projection drops (so the row path pays to carry them to the projection). + private val srcSchema: Schema = Schema( + List( + new Attribute("key", AttributeType.INTEGER), + new Attribute("v", AttributeType.DOUBLE), + new Attribute("p1", AttributeType.STRING), + new Attribute("p2", AttributeType.STRING) + ) + ) + // After the projection: only the key and the numeric column remain. + private val projSchema: Schema = + Schema( + List(new Attribute("key", AttributeType.INTEGER), new Attribute("v", AttributeType.DOUBLE)) + ) + + private def sourceRows(n: Int): Array[Tuple] = + Array.tabulate(n) { _ => + Tuple + .builder(srcSchema) + .addSequentially( + Array[Any]( + Int.box(rng.nextInt(100)), + Double.box(rng.nextDouble() * 2 - 1), // roughly half pass v > 0 + rng.alphanumeric.take(16).mkString, + rng.alphanumeric.take(16).mkString + ) + ) + .build() + } + + // ---- operators (one instance each; aggregate is reset per run via open()) ---------- + + private val projExec: ProjectionOpExec = { + val d = new ProjectionOpDesc() + d.isDrop = false + d.attributes = List(new AttributeUnit("key", "key"), new AttributeUnit("v", "v")) + val e = new ProjectionOpExec(om.writeValueAsString(d)); e.open(); e + } + + private val 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 + } + + private val aggJson: String = { + val d = new AggregateOpDesc() + val sum = new AggregationOperation() + sum.aggFunction = AggregationFunction.SUM + sum.attribute = "v" + sum.resultAttribute = "sum_v" + d.aggregations = List(sum) + d.groupByKeys = List("key") + om.writeValueAsString(d) + } + private val aggExec: AggregateOpExec = new AggregateOpExec(aggJson) + + // ---- timing helpers --------------------------------------------------------------- + + 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 def pct(a: Double, b: Double): String = f"${a / b}%.2fx" + + // Row region: each hop is a Kryo DataFrame round-trip, each operator runs per tuple. + private def rowRun(rows: Array[Tuple]): Unit = { + aggExec.open() + // source -> project edge (carries the full wide rows, payload columns included). + val srcRows = + serde.deserialize(serde.serialize(DataFrame(rows)).get, classOf[DataFrame]).get.frame + val projected = srcRows.map(t => + projExec.processTuple(t, 0).next().asInstanceOf[SchemaEnforceable].enforceSchema(projSchema) + ) + val afterEdge1 = + serde.deserialize(serde.serialize(DataFrame(projected)).get, classOf[DataFrame]).get.frame + val filtered = afterEdge1.filter(filterExec.filterFunc) + val afterEdge2 = + serde.deserialize(serde.serialize(DataFrame(filtered)).get, classOf[DataFrame]).get.frame + var i = 0 + while (i < afterEdge2.length) { aggExec.processTuple(afterEdge2(i), 0); i += 1 } + aggExec.onFinish(0).foreach(_ => ()) + } + + // Columnar region: each hop is a Kryo ColumnarFrame round-trip, each operator runs + // processColumnarBatch, and the batch stays Arrow the whole way. + private def colRun(rows: Array[Tuple]): Unit = { + aggExec.open() + def hop(bytes: Array[Byte], rowCount: Int, schema: Schema): Array[Byte] = + serde + .deserialize( + serde.serialize(ColumnarFrame(bytes, rowCount, schema)).get, + classOf[ColumnarFrame] + ) + .get + .arrowIpcBytes + // source -> project + val srcWire = hop(ArrowUtils.serializeTuples(srcSchema, rows), rows.length, srcSchema) + val projBytes = projExec.processColumnarBatch(srcWire, 0) match { + case ColumnarResult.Emit(root) => + try ArrowUtils.serializeRoot(root) + finally root.close() + case other => fail(s"projection: expected Emit, got $other") + } + // project -> filter + val filterIn = hop(projBytes, rows.length, projSchema) + val (filterBytes, keptCount) = filterExec.processColumnarBatch(filterIn, 0) match { + case ColumnarResult.Emit(root) => + try (ArrowUtils.serializeRoot(root), root.getRowCount) + finally root.close() + case other => fail(s"filter: expected Emit, got $other") + } + // filter -> aggregate + val aggIn = hop(filterBytes, keptCount, projSchema) + aggExec.processColumnarBatch(aggIn, 0) + aggExec.onFinish(0).foreach(_ => ()) + } + + private def benchEnabled: Boolean = sys.props.get("columnar.bench").contains("true") + + "a columnar region (project -> filter -> aggregate)" should "beat the row path across serialized edges" in { + assume(benchEnabled, "set -Dcolumnar.bench=true to run the pipeline benchmark") + println( + "\n===== Pipeline project->filter->aggregate, columnar region vs row (median of %d) =====" + .format(trials) + ) + println("%-10s | %12s %12s %8s".format("rows", "row ms", "columnar ms", "col/row")) + for (n <- Seq(2000, 20000, 100000)) { + val rows = sourceRows(n) + for (_ <- 0 until warmups) { rowRun(rows); colRun(rows) } + val rowMs = median((0 until trials).map(_ => timeMs(rowRun(rows)))) + val colMs = median((0 until trials).map(_ => timeMs(colRun(rows)))) + println("%-10d | %12.2f %12.2f %8s".format(n, rowMs, colMs, pct(colMs, rowMs))) + } + println("(col/row < 1.00x means the columnar region is faster end to end)\n") + succeed + } + + override def afterAll(): Unit = { + projExec.close(); filterExec.close(); aggExec.close() + TestKit.shutdownActorSystem(system) + super.afterAll() + } +} 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..112b76aca60 --- /dev/null +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/sendsemantics/partitioners/NetworkOutputBufferColumnarSeamSpec.scala @@ -0,0 +1,139 @@ +/* + * 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 "ship a prebuilt ColumnarFrame after flushing buffered rows, in order" in { + // sendColumnarFrame is the no-round-trip output path: a prebuilt Arrow batch is + // sent straight through, but anything already buffered must flush first so FIFO + // order holds. + val (buffer, sent) = fixture(columnar = true) + buffer.addTuple(tuple(1)) + val frame = + ColumnarFrame(ArrowUtils.serializeTuples(schema, Array(tuple(2), tuple(3))), 2, schema) + buffer.sendColumnarFrame(frame) + + assert(sent.size == 2) + val flushed = sent.head.payload.asInstanceOf[ColumnarFrame] + assert( + ArrowUtils.deserializeTuples(flushed.arrowIpcBytes).map(_.getField[Any]("v")).toList == List( + 1 + ) + ) + assert(sent(1).payload eq frame) + } + + 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..a40052bb095 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,180 @@ 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 "materialize an Emit result to tuples when the output edge is not columnar" in { + val exec = new DataProcessorSpec.EmitAllColumnarExec + val (dp, sent) = columnarSetup(exec) + // Columnar output is off by default, so the Emit falls back to the row output path. + 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 "ship an Emit result downstream as a ColumnarFrame with no round-trip on an eligible edge" in { + val exec = new DataProcessorSpec.EmitAllColumnarExec + val (dp, sent) = columnarSetup(exec) + // One-to-one edge, no storage: enable columnar output so the fast path applies. + dp.outputManager.setColumnarOutput(true) + dp.processDataPayload(senderChannel, columnarFrame) + drain(dp) + // The batch goes out as a single ColumnarFrame (not decoded to tuples and + // re-encoded), decoding back to the input rows, and processTuple never ran. + val frames = sent.map(_.payload).collect { case cf: ColumnarFrame => cf } + assert(frames.size == 1) + assert(frames.head.rowCount == columnarBatch.length) + assert(ArrowUtils.deserializeTuples(frames.head.arrowIpcBytes).toList == columnarBatch.toList) + assert(tuplesSent(sent).isEmpty) + assert(exec.processTupleCalls == 0) + assert(dp.statisticsManager.getOutputTupleCount == 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/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/WorkerSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/WorkerSpec.scala index 96929eab9a2..7a89a1b92ba 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/WorkerSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/WorkerSpec.scala @@ -167,7 +167,7 @@ class WorkerSpec ) val invocation = AsyncRPCClient.ControlInvocation( METHOD_ADD_PARTITIONING, - AddPartitioningRequest(mockLink, mockPolicy), + AddPartitioningRequest(mockLink, mockPolicy, columnar = false), AsyncRPCContext(COORDINATOR, identifier1), 0 ) diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/faulttolerance/LoggingSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/faulttolerance/LoggingSpec.scala index e090642f3c3..5ba1943062f 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/faulttolerance/LoggingSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/faulttolerance/LoggingSpec.scala @@ -85,7 +85,7 @@ class LoggingSpec ), ControlInvocation( METHOD_ADD_PARTITIONING, - AddPartitioningRequest(mockLink, mockPolicy), + AddPartitioningRequest(mockLink, mockPolicy, columnar = false), AsyncRPCContext(COORDINATOR, identifier1), 0 ), 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/core/workflow/PhysicalOp.scala b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/workflow/PhysicalOp.scala index 9c57359f114..337615da51f 100644 --- a/common/workflow-core/src/main/scala/org/apache/texera/amber/core/workflow/PhysicalOp.scala +++ b/common/workflow-core/src/main/scala/org/apache/texera/amber/core/workflow/PhysicalOp.scala @@ -21,7 +21,12 @@ package org.apache.texera.amber.core.workflow import com.fasterxml.jackson.annotation.{JsonIgnore, JsonIgnoreProperties} import com.typesafe.scalalogging.LazyLogging -import org.apache.texera.amber.core.executor.{OpExecInitInfo, OpExecWithCode} +import org.apache.texera.amber.core.executor.{ + ColumnarOperatorExecutor, + OpExecInitInfo, + OpExecWithClassName, + OpExecWithCode +} import org.apache.texera.amber.core.tuple.Schema import org.apache.texera.amber.core.virtualidentity.{ ExecutionIdentity, @@ -270,6 +275,25 @@ case class PhysicalOp( } } + /** + * Whether this operator's executor can consume an Arrow columnar batch directly, + * i.e. its class mixes in ColumnarOperatorExecutor. Resolved by name without loading + * (initializing) or instantiating the class; any lookup failure (e.g. a code-based or + * unknown executor) is treated as not capable. The planner uses this to decide, per + * edge, whether to ship columns to this operator. + */ + @JsonIgnore + def isColumnarCapable: Boolean = { + opExecInitInfo match { + case OpExecWithClassName(className, _) => + try { + val cls = Class.forName(className, false, getClass.getClassLoader) + classOf[ColumnarOperatorExecutor].isAssignableFrom(cls) + } catch { case _: Throwable => false } + case _ => false + } + } + /** * creates a copy with the location preference information */ 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/core/workflow/PhysicalOpSpec.scala b/common/workflow-core/src/test/scala/org/apache/texera/amber/core/workflow/PhysicalOpSpec.scala index 3cfbbdad4f6..3d1bbc98fc1 100644 --- a/common/workflow-core/src/test/scala/org/apache/texera/amber/core/workflow/PhysicalOpSpec.scala +++ b/common/workflow-core/src/test/scala/org/apache/texera/amber/core/workflow/PhysicalOpSpec.scala @@ -19,7 +19,7 @@ package org.apache.texera.amber.core.workflow -import org.apache.texera.amber.core.executor.{OpExecInitInfo, OpExecWithCode} +import org.apache.texera.amber.core.executor.{OpExecInitInfo, OpExecWithClassName, OpExecWithCode} import org.apache.texera.amber.core.tuple.{Attribute, AttributeType, Schema} import org.apache.texera.amber.core.virtualidentity.{ ExecutionIdentity, @@ -142,6 +142,21 @@ class PhysicalOpSpec extends AnyFlatSpec { assert(!newOp("p").isPythonBased) } + "PhysicalOp.isColumnarCapable" should "hold only for executors that mix in ColumnarOperatorExecutor" in { + def withClass(cls: Class[_]): PhysicalOp = + newOp("cc").copy(opExecInitInfo = OpExecWithClassName(cls.getName, "{}")) + // A class that implements the columnar contract is capable; a plain executor is not. + assert(withClass(classOf[PhysicalOpSpec.CapableExec]).isColumnarCapable) + assert(!withClass(classOf[PhysicalOpSpec.PlainExec]).isColumnarCapable) + // A code-based executor and an unresolvable class name are both not capable. + assert(!newOp("cc").copy(opExecInitInfo = OpExecWithCode("code", "python")).isColumnarCapable) + assert( + !newOp("cc") + .copy(opExecInitInfo = OpExecWithClassName("no.such.Class", "{}")) + .isColumnarCapable + ) + } + "PhysicalOp.getCode" should "return the code or reject executors without code" in { val op = newOp("c").copy(opExecInitInfo = OpExecWithCode("print(1)", "python")) assert(op.getCode == "print(1)") @@ -249,3 +264,16 @@ class PhysicalOpSpec extends AnyFlatSpec { assert(op.propagateSchema.func(schemas) == schemas) } } + +object PhysicalOpSpec { + // Resolved by name in the isColumnarCapable test; must be top-level (not inner) + // so Class.forName can find them. + class CapableExec extends org.apache.texera.amber.core.executor.ColumnarOperatorExecutor { + override def processColumnarBatch( + arrowIpcBytes: Array[Byte], + port: Int + ): org.apache.texera.amber.core.executor.ColumnarResult = + org.apache.texera.amber.core.executor.ColumnarResult.Unsupported + } + class PlainExec +} 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/projection/ProjectionOpExec.scala b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/projection/ProjectionOpExec.scala index b0512cc826a..6b4da6cc32e 100644 --- a/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/projection/ProjectionOpExec.scala +++ b/common/workflow-operator/src/main/scala/org/apache/texera/amber/operator/projection/ProjectionOpExec.scala @@ -20,15 +20,20 @@ package org.apache.texera.amber.operator.projection import com.google.common.base.Preconditions -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} +import org.apache.texera.amber.core.tuple.{Attribute, Schema, Tuple, TupleLike} import org.apache.texera.amber.operator.map.MapOpExec +import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.amber.util.JSONUtils.objectMapper import scala.collection.mutable class ProjectionOpExec( descString: String -) extends MapOpExec { +) extends MapOpExec + with ColumnarOperatorExecutor { val desc: ProjectionOpDesc = objectMapper.readValue(descString, classOf[ProjectionOpDesc]) setMapFunc(project) @@ -66,4 +71,60 @@ class ProjectionOpExec( TupleLike(fields.toSeq: _*) } + // ---- Native-Arrow path: project columns straight on the Arrow batch, no per-tuple + // decode. Each output column takes its source column's buffers by an Arrow transfer + // (a move, not a per-cell copy), renamed to the alias. Falls back to the row path + // (Unsupported) for the shapes a single column move cannot express: a source column + // used by more than one output, a duplicated output name, or an unresolved column. + @transient private var columnarAllocator: RootAllocator = _ + + override def processColumnarBatch(arrowIpcBytes: Array[Byte], port: Int): ColumnarResult = { + if (columnarAllocator == null) columnarAllocator = new RootAllocator() + ArrowUtils.deserializeRootFold(arrowIpcBytes, columnarAllocator)(projectRoot) + } + + // Resolve the (sourceColumn -> outputAlias) pairs exactly as project() does, then move + // each source column into a fresh output root under its alias. + private def projectRoot(root: VectorSchemaRoot): ColumnarResult = { + val inSchema = ArrowUtils.toTexeraSchema(root.getSchema) + val units: List[(String, String)] = + if (desc.isDrop) { + val dropped = desc.attributes.map(_.getOriginalAttribute.toLowerCase).toSet + inSchema.getAttributeNames.filterNot(n => dropped.contains(n.toLowerCase)).map(n => (n, n)) + } else { + desc.attributes.map(u => (u.getOriginalAttribute, u.getAlias)) + } + val originals = units.map(_._1) + val aliases = units.map(_._2) + // A source column used twice cannot be moved twice; a duplicated alias is an error + // the row path raises. Let the row path handle both. + if ( + units.isEmpty || originals.distinct.size != originals.size || + aliases.distinct.size != aliases.size + ) { + return ColumnarResult.Unsupported + } + val outSchema = + try Schema(units.map { + case (orig, alias) => new Attribute(alias, inSchema.getAttribute(orig).getType) + }) + catch { case _: Exception => return ColumnarResult.Unsupported } + val srcIdx = + try ArrowUtils.projectionIndices(root, originals) + catch { case _: Exception => return ColumnarResult.Unsupported } + val outRoot = VectorSchemaRoot.create(ArrowUtils.fromTexeraSchema(outSchema), columnarAllocator) + var j = 0 + while (j < units.size) { + // Move the source column's buffers into the output column (same Arrow type), renamed. + root.getVector(srcIdx(j)).makeTransferPair(outRoot.getVector(j)).transfer() + j += 1 + } + outRoot.setRowCount(root.getRowCount) + ColumnarResult.Emit(outRoot) + } + + override def close(): Unit = { + if (columnarAllocator != null) { columnarAllocator.close(); columnarAllocator = null } + } + } 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() + } +} diff --git a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/projection/ProjectionOpExecSpec.scala b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/projection/ProjectionOpExecSpec.scala index 1d8ee9717bb..cf793cf4cd2 100644 --- a/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/projection/ProjectionOpExecSpec.scala +++ b/common/workflow-operator/src/test/scala/org/apache/texera/amber/operator/projection/ProjectionOpExecSpec.scala @@ -19,8 +19,10 @@ package org.apache.texera.amber.operator.projection +import org.apache.texera.amber.core.executor.ColumnarResult import org.apache.texera.amber.core.tuple._ import org.apache.texera.amber.core.workflow.PortIdentity +import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.amber.util.JSONUtils.objectMapper import org.scalatest.BeforeAndAfter import org.scalatest.flatspec.AnyFlatSpec @@ -273,4 +275,78 @@ class ProjectionOpExecSpec extends AnyFlatSpec with BeforeAndAfter { val output = projectionOpExec.processTuple(tuple, 0).next().asInstanceOf[MapTupleLike] assert(output.fieldMappings == Map("field1" -> "hello", "field3" -> true)) } + + // ---- Native-Arrow path (columnar projection) ---- + + private val tuple2: Tuple = Tuple + .builder(tupleSchema) + .add(new Attribute("field1", AttributeType.STRING), "world") + .add(new Attribute("field2", AttributeType.INTEGER), 2) + .add(new Attribute("field3", AttributeType.BOOLEAN), false) + .build() + + // Run the batch through processColumnarBatch and decode the emitted Arrow result. + private def columnarProject(exec: ProjectionOpExec, rows: Array[Tuple]): Array[Tuple] = + exec.processColumnarBatch(ArrowUtils.serializeTuples(tupleSchema, rows), 0) match { + case ColumnarResult.Emit(root) => + try ArrowUtils.tuplesOfRoot(root) + finally root.close() + case other => fail(s"expected Emit, got: $other") + } + + it should "project and rename columns on an Arrow batch identically to the row path" in { + opDesc.isDrop = false + opDesc.attributes = List(new AttributeUnit("field2", "f2"), new AttributeUnit("field1", "f1")) + val outputSchema = Schema() + .add(new Attribute("f2", AttributeType.INTEGER)) + .add(new Attribute("f1", AttributeType.STRING)) + val exec = new ProjectionOpExec(objectMapper.writeValueAsString(opDesc)) + exec.open() + + val rows = Array(tuple, tuple2) + val rowOut = rows.map( + exec.processTuple(_, 0).next().asInstanceOf[SchemaEnforceable].enforceSchema(outputSchema) + ) + val colOut = columnarProject(exec, rows) + + assert(colOut.length == rows.length) + colOut.zip(rowOut).foreach { + case (c, r) => + assert(c.getSchema == outputSchema) + assert(c.getField[Any]("f1") == r.getField[Any]("f1")) + assert(c.getField[Any]("f2") == r.getField[Any]("f2")) + } + exec.close() + } + + it should "drop columns on an Arrow batch identically to the row path" in { + opDesc.isDrop = true + opDesc.attributes = List(new AttributeUnit("field2", "")) + val outputSchema = Schema() + .add(new Attribute("field1", AttributeType.STRING)) + .add(new Attribute("field3", AttributeType.BOOLEAN)) + val exec = new ProjectionOpExec(objectMapper.writeValueAsString(opDesc)) + exec.open() + + val colOut = columnarProject(exec, Array(tuple, tuple2)) + assert(colOut.length == 2) + assert(colOut.head.getSchema == outputSchema) + assert(colOut.head.getField[String]("field1") == "hello") + assert(colOut.head.getField[Boolean]("field3")) + assert(colOut(1).getField[String]("field1") == "world") + assert(!colOut(1).getField[Boolean]("field3")) + exec.close() + } + + it should "fall back to the row path (Unsupported) on a duplicate alias" in { + opDesc.isDrop = false + opDesc.attributes = List(new AttributeUnit("field1", "f"), new AttributeUnit("field2", "f")) + val exec = new ProjectionOpExec(objectMapper.writeValueAsString(opDesc)) + exec.open() + assert( + exec.processColumnarBatch(ArrowUtils.serializeTuples(tupleSchema, Array(tuple)), 0) == + ColumnarResult.Unsupported + ) + exec.close() + } }