You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2022/10/21 04:02:43 UTC

[GitHub] [spark] wangyum commented on a diff in pull request #37972: [SPARK-40654][SQL] Protobuf support for Spark - from_protobuf AND to_protobuf

wangyum commented on code in PR #37972:
URL: https://github.com/apache/spark/pull/37972#discussion_r1001341190


##########
connector/protobuf/src/test/scala/org/apache/spark/sql/protobuf/ProtobufCatalystDataConversionSuite.scala:
##########
@@ -0,0 +1,212 @@
+/*
+ * 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.protobuf
+
+import com.google.protobuf.{ByteString, DynamicMessage, Message}
+
+import org.apache.spark.SparkFunSuite
+import org.apache.spark.sql.{RandomDataGenerator, Row}
+import org.apache.spark.sql.catalyst.{CatalystTypeConverters, InternalRow, NoopFilters, OrderedFilters, StructFilters}
+import org.apache.spark.sql.catalyst.expressions.{ExpressionEvalHelper, GenericInternalRow, Literal}
+import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, GenericArrayData, MapData}
+import org.apache.spark.sql.protobuf.utils.{ProtobufUtils, SchemaConverters}
+import org.apache.spark.sql.sources.{EqualTo, Not}
+import org.apache.spark.sql.test.SharedSparkSession
+import org.apache.spark.sql.types._
+import org.apache.spark.unsafe.types.UTF8String
+
+class ProtobufCatalystDataConversionSuite
+    extends SparkFunSuite
+    with SharedSparkSession
+    with ExpressionEvalHelper {
+
+  private def checkResult(
+      data: Literal,
+      descFilePath: String,
+      messageName: String,
+      expected: Any): Unit = {
+    checkEvaluation(
+      ProtobufDataToCatalyst(
+        CatalystDataToProtobuf(data, descFilePath, messageName),
+        descFilePath,
+        messageName,
+        Map.empty),
+      prepareExpectedResult(expected))
+  }
+
+  protected def checkUnsupportedRead(
+      data: Literal,
+      descFilePath: String,
+      actualSchema: String,
+      badSchema: String): Unit = {
+
+    val binary = CatalystDataToProtobuf(data, descFilePath, actualSchema)
+
+    intercept[Exception] {
+      ProtobufDataToCatalyst(binary, descFilePath, badSchema, Map("mode" -> "FAILFAST")).eval()
+    }
+
+    val expected = {
+      val expectedSchema = ProtobufUtils.buildDescriptor(descFilePath, badSchema)
+      SchemaConverters.toSqlType(expectedSchema).dataType match {
+        case st: StructType =>
+          Row.fromSeq((0 until st.length).map { _ =>
+            null
+          })
+        case _ => null
+      }
+    }
+
+    checkEvaluation(
+      ProtobufDataToCatalyst(binary, descFilePath, badSchema, Map("mode" -> "PERMISSIVE")),
+      expected)
+  }
+
+  protected def prepareExpectedResult(expected: Any): Any = expected match {
+    // Spark byte and short both map to Protobuf int
+    case b: Byte => b.toInt
+    case s: Short => s.toInt
+    case row: GenericInternalRow => InternalRow.fromSeq(row.values.map(prepareExpectedResult))
+    case array: GenericArrayData => new GenericArrayData(array.array.map(prepareExpectedResult))
+    case map: MapData =>
+      val keys = new GenericArrayData(
+        map.keyArray().asInstanceOf[GenericArrayData].array.map(prepareExpectedResult))
+      val values = new GenericArrayData(
+        map.valueArray().asInstanceOf[GenericArrayData].array.map(prepareExpectedResult))
+      new ArrayBasedMapData(keys, values)
+    case other => other
+  }
+
+  private val testingTypes = Seq(
+    StructType(StructField("int32_type", IntegerType, nullable = true) :: Nil),
+    StructType(StructField("double_type", DoubleType, nullable = true) :: Nil),
+    StructType(StructField("float_type", FloatType, nullable = true) :: Nil),
+    StructType(StructField("bytes_type", BinaryType, nullable = true) :: Nil),
+    StructType(StructField("string_type", StringType, nullable = true) :: Nil))
+
+  private val catalystTypesToProtoMessages: Map[DataType, String] = Map(
+    IntegerType -> "IntegerMsg",
+    DoubleType -> "DoubleMsg",
+    FloatType -> "FloatMsg",
+    BinaryType -> "BytesMsg",
+    StringType -> "StringMsg")
+
+  testingTypes.foreach { dt =>
+    val seed = 1 + scala.util.Random.nextInt((1024 - 1) + 1)
+    val filePath = testFile("protobuf/catalyst_types.desc").replace("file:/", "/")
+    test(s"single $dt with seed $seed") {

Review Comment:
   @SandishKumarHN Is it a flaky test?
   <img width="1057" alt="image" src="https://user-images.githubusercontent.com/5399861/197108901-f3a9b36f-0fc7-47c7-a4a1-e04162d66c65.png">



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org