hvanhovell commented on code in PR #40611:
URL: https://github.com/apache/spark/pull/40611#discussion_r1261116899


##########
connector/connect/client/jvm/src/test/scala/org/apache/spark/sql/connect/client/arrow/ArrowEncoderSuite.scala:
##########
@@ -0,0 +1,837 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.connect.client.arrow
+
+import java.util
+import java.util.{Collections, Objects}
+
+import scala.collection.JavaConverters._
+import scala.collection.mutable
+import scala.reflect.classTag
+import scala.util.control.NonFatal
+
+import com.google.protobuf.ByteString
+import org.apache.arrow.memory.{BufferAllocator, RootAllocator}
+import org.apache.arrow.vector.VarBinaryVector
+import org.scalatest.BeforeAndAfterAll
+
+import org.apache.spark.SparkUnsupportedOperationException
+import org.apache.spark.connect.proto
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.catalyst.{DefinedByConstructorParams, DummyBean, 
FooEnum, JavaTypeInference, PrimitiveData, ScalaReflection}
+import org.apache.spark.sql.catalyst.FooEnum.FooEnum
+import org.apache.spark.sql.catalyst.encoders.{AgnosticEncoder, BoxedData, 
UDTForCaseClass}
+import 
org.apache.spark.sql.catalyst.encoders.AgnosticEncoders.{BoxedIntEncoder, 
CalendarIntervalEncoder, DateEncoder, EncoderField, InstantEncoder, 
IterableEncoder, JavaDecimalEncoder, LocalDateEncoder, PrimitiveDoubleEncoder, 
PrimitiveFloatEncoder, RowEncoder, StringEncoder, TimestampEncoder, UDTEncoder}
+import org.apache.spark.sql.catalyst.encoders.RowEncoder.{encoderFor => 
toRowEncoder}
+import org.apache.spark.sql.connect.client.SparkResult
+import org.apache.spark.sql.connect.client.util.ConnectFunSuite
+import org.apache.spark.sql.types.{ArrayType, Decimal, DecimalType, Metadata, 
StructType}
+
+/**
+ * Tests for encoding external data to and from arrow.
+ */
+class ArrowEncoderSuite extends ConnectFunSuite with BeforeAndAfterAll {
+  private val allocator = new RootAllocator()
+
+  private def newAllocator(name: String): BufferAllocator = {
+    allocator.newChildAllocator(name, 0, allocator.getLimit)
+  }
+
+  protected override def afterAll(): Unit = {
+    super.afterAll()
+    allocator.close()
+  }
+
+  private def withAllocator[T](f: BufferAllocator => T): T = {
+    val allocator = newAllocator("allocator")
+    try f(allocator)
+    finally {
+      allocator.close()
+    }
+  }
+
+  private def roundTrip[T](
+      encoder: AgnosticEncoder[T],
+      iterator: Iterator[T],
+      maxRecordsPerBatch: Int = 4 * 1024,
+      maxBatchSize: Long = 16 * 1024,
+      batchSizeCheckInterval: Int = 128,
+      inspectBatch: Array[Byte] => Unit = null): CloseableIterator[T] = {
+    // Use different allocators so we can pinpoint memory leaks better.
+    val serializerAllocator = newAllocator("serialization")
+    val deserializerAllocator = newAllocator("deserialization")
+
+    val arrowIterator = ArrowSerializer.serialize(
+      input = iterator,
+      enc = encoder,
+      allocator = serializerAllocator,
+      maxRecordsPerBatch = maxRecordsPerBatch,
+      maxBatchSize = maxBatchSize,
+      batchSizeCheckInterval = batchSizeCheckInterval,
+      timeZoneId = "UTC")
+
+    val inspectedIterator = if (inspectBatch != null) {
+      arrowIterator.map { batch =>
+        inspectBatch(batch)
+        batch
+      }
+    } else {
+      arrowIterator
+    }
+
+    val resultIterator =
+      try {
+        deserializeFromArrow(inspectedIterator, encoder, deserializerAllocator)
+      } catch {
+        case NonFatal(e) =>
+          arrowIterator.close()
+          serializerAllocator.close()
+          deserializerAllocator.close()
+          throw e
+      }
+    new CloseableIterator[T] {
+      override def close(): Unit = {
+        arrowIterator.close()
+        resultIterator.close()
+        serializerAllocator.close()
+        deserializerAllocator.close()
+      }
+      override def hasNext: Boolean = resultIterator.hasNext
+      override def next(): T = resultIterator.next()
+    }
+  }
+
+  // Temporary hack until we merge the deserializer.
+  private def deserializeFromArrow[E](
+      batches: Iterator[Array[Byte]],
+      encoder: AgnosticEncoder[E],
+      allocator: BufferAllocator): CloseableIterator[E] = {
+    val responses = batches.map { batch =>
+      val builder = proto.ExecutePlanResponse.newBuilder()
+      builder.getArrowBatchBuilder.setData(ByteString.copyFrom(batch))
+      builder.build()
+    }
+    val result = new SparkResult[E](responses.asJava, allocator, encoder)
+    new CloseableIterator[E] {
+      private val itr = result.iterator
+      override def close(): Unit = itr.close()
+      override def hasNext: Boolean = itr.hasNext
+      override def next(): E = itr.next()
+    }
+  }
+
+  private def roundTripAndCheck[T](
+      encoder: AgnosticEncoder[T],
+      toInputIterator: () => Iterator[Any],
+      toOutputIterator: () => Iterator[T],
+      maxRecordsPerBatch: Int = 4 * 1024,
+      maxBatchSize: Long = 16 * 1024,
+      batchSizeCheckInterval: Int = 128,
+      inspectBatch: Array[Byte] => Unit = null): Unit = {
+    val iterator = roundTrip(
+      encoder,
+      toInputIterator().asInstanceOf[Iterator[T]], // Erasure hack :)
+      maxRecordsPerBatch,
+      maxBatchSize,
+      batchSizeCheckInterval,
+      inspectBatch)
+    try {
+      compareIterators(toOutputIterator(), iterator)
+    } finally {
+      iterator.close()
+    }
+  }
+
+  private def roundTripAndCheckIdentical[T](
+      encoder: AgnosticEncoder[T],
+      maxRecordsPerBatch: Int = 4 * 1024,
+      maxBatchSize: Long = 16 * 1024,
+      batchSizeCheckInterval: Int = 128,
+      inspectBatch: Array[Byte] => Unit = null)(toIterator: () => 
Iterator[T]): Unit = {

Review Comment:
   Yeah this is for the next PR :)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to