cloud-fan commented on code in PR #43707: URL: https://github.com/apache/spark/pull/43707#discussion_r1389084120
########## sql/core/src/test/scala/org/apache/spark/sql/VariantSuite.scala: ########## @@ -0,0 +1,76 @@ +/* + * 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 + +import java.io.File + +import scala.util.Random + +import org.apache.spark.sql.test.SharedSparkSession +import org.apache.spark.sql.types.StructType +import org.apache.spark.unsafe.types.VariantVal + +class VariantSuite extends QueryTest with SharedSparkSession { + test("basic tests") { + def verifyResult(df: DataFrame): Unit = { + val result = df.collect() + .map(_.get(0).asInstanceOf[VariantVal].toString) + .sorted + .toSeq + assert(result == (1 until 10).map(id => "1" * id)) + } + + // At this point, JSON parsing logic is not really implemented. We just construct some number + // inputs that are also valid JSON. This exercises passing VariantVal throughout the system. + val query = spark.sql("select parse_json(repeat('1', id)) as v from range(1, 10)") + verifyResult(query) + + // Write into and read from Parquet. + withTempDir { dir => + val tempDir = new File(dir, "files").getCanonicalPath + query.write.parquet(tempDir) + verifyResult(spark.read.parquet(tempDir)) + } + } + + test("round trip tests") { + val rand = new Random(42) + val input = Seq.fill(50) { + if (rand.nextInt(10) == 0) { + null + } else { + val value = new Array[Byte](rand.nextInt(50)) + rand.nextBytes(value) + val metadata = new Array[Byte](rand.nextInt(50)) + rand.nextBytes(metadata) + new VariantVal(value, metadata) + } + } + + val df = spark.createDataFrame( + spark.sparkContext.parallelize(input.map(Row(_))), + StructType.fromDDL("v variant") + ) + val result = df.collect().map(_.get(0).asInstanceOf[VariantVal]) + + def prepareAnswer(values: Seq[VariantVal]): Seq[String] = { Review Comment: we don't need it if `VariantVal` implements `equals` and `hashCode` correctly. -- 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]
