cashmand commented on code in PR #45703:
URL: https://github.com/apache/spark/pull/45703#discussion_r1538074039


##########
sql/core/src/test/scala/org/apache/spark/sql/VariantSuite.scala:
##########
@@ -192,4 +193,48 @@ class VariantSuite extends QueryTest with 
SharedSparkSession {
       }
     }
   }
+
+  test("invalid variant binary") {
+    // Write a struct-of-binary that looks like a Variant, but with minor 
variations that may make
+    // it invalid to read.
+    // Test cases:
+    // 1) A binary that has the correct parquet structure for Variant.
+    // 2) A binary that has the correct parquet structure in unexpected order. 
This is valid.
+    // 3) A binary that is almost correct, but contains an extra field "paths"
+    // 4,5) A binary with incorrect field names
+    // 6) Incorrect data typea
+    // 7,8) Nullable value or metdata
+
+    // Binary value of empty metadata
+    val m = "X'010000'"
+    // Binary value of a literal "false"
+    val v = "X'8'"
+    val cases = Seq(
+        (true, s"named_struct('value', $v, 'metadata', $m)"),
+        (true, s"named_struct('metadata', $m, 'value', $v)"),
+        (false, s"named_struct('value', $v, 'metadata', $m, 'paths', $v)"),
+        (false, s"named_struct('value', $v, 'dictionary', $m)"),
+        (false, s"named_struct('val', $v, 'metadata', $m)"),
+        (false, s"named_struct('value', 8, 'metadata', $m)"),
+        (false, s"named_struct('value', cast(null as binary), 'metadata', 
$m)"),
+        (false, s"named_struct('value', $v, 'metadata', cast(null as binary))")
+    )
+    cases.foreach { case (expectValid, structDef) =>
+      withTempDir { dir =>
+        val file = new File(dir, "dir").getCanonicalPath
+        val df = spark.sql(s"select $structDef as v from range(10)")
+        df.write.parquet(file)
+        val schema = StructType(Seq(StructField("v", VariantType)))
+        val result = spark.read.schema(schema).parquet(file)
+            .selectExpr("to_json(v)")
+        if (expectValid) {
+          checkAnswer(result, Seq.fill(10)(Row("false")))
+        } else {
+          val e = intercept[org.apache.spark.SparkException](result.collect())
+          assert(e.getCause.isInstanceOf[AnalysisException] ||

Review Comment:
   I just refactored a bit so that we only throw AnalysisException, not 
SchemaColumnConvertNotSupportedException. We'll either throw 
QueryCompilationErrors.illegalParquetTypeError or 
QueryCompilationErrors.parquetTypeUnsupportedYetError.



-- 
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