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 2018/12/13 05:10:51 UTC

[GitHub] gengliangwang commented on a change in pull request #22878: [SPARK-25789][SQL] Support for Dataset of Avro

gengliangwang commented on a change in pull request #22878: [SPARK-25789][SQL] Support for Dataset of Avro
URL: https://github.com/apache/spark/pull/22878#discussion_r241273201
 
 

 ##########
 File path: external/avro/src/test/scala/org/apache/spark/sql/avro/AvroEncoderSuite.scala
 ##########
 @@ -0,0 +1,352 @@
+/*
+ * 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.avro
+
+import java.nio.ByteBuffer
+
+import org.apache.avro.{Schema, SchemaBuilder}
+import org.apache.avro.generic.{GenericData, GenericRecordBuilder}
+
+import org.apache.spark.{SparkConf, SparkContext}
+import org.apache.spark.rdd.RDD
+import org.apache.spark.sql.catalyst.encoders.ExpressionEncoder
+import org.apache.spark.sql.test.SharedSQLContext
+import org.apache.spark.sql.types.{FloatType, IntegerType, StringType}
+
+class AvroEncoderSuite extends SharedSQLContext {
+  import testImplicits._
+
+  test("encoder from json schema") {
+    val avroSchema =
+      """
+        |{
+        |  "type" : "record",
+        |  "name" : "simple_record",
+        |  "fields" :
+        |   [
+        |    { "name" : "myUInt", "type" : [ "int", "null" ], "default" : 1 },
+        |    { "name" : "myULong", "type" : [ "long", "null" ], "default" : 2 },
+        |    { "name" : "myUBool", "type" : [ "boolean", "null" ], "default" : true },
+        |    { "name" : "myUDouble", "type" : [ "double", "null" ], "default" : 3 },
+        |    { "name" : "myUFloat", "type" : [ "float", "null" ], "default" : 4.5 },
+        |    { "name" : "myUString", "type" : [ "string", "null" ], "default" : "foo" },
+        |
+        |    { "name" : "myInt", "type" : "int", "default" : 10 },
+        |    { "name" : "myLong", "type" : "long", "default" : 11 },
+        |    { "name" : "myBool", "type" : "boolean", "default" : false },
+        |    { "name" : "myDouble", "type" : "double", "default" : 12 },
+        |    { "name" : "myFloat", "type" : "float", "default" : 13.14 },
+        |    { "name" : "myString", "type" : "string", "default" : "bar" },
+        |
+        |    { "name" : "myArray", "type" :
+        |     { "type" : "array", "items" : "bytes" }, "default" : [ "a12b", "cc50" ] },
+        |    { "name" : "myMap", "type" : { "type" : "map", "values" : "string" },
+        |     "default" : {"a":"A", "b":"B"} }
+        |   ]
+        |}
+      """.stripMargin
+    val encoder = AvroEncoder.of(avroSchema)
+    val expressionEncoder = encoder.asInstanceOf[ExpressionEncoder[GenericData.Record]]
+    val schema = new Schema.Parser().parse(avroSchema)
+    val record = new GenericRecordBuilder(schema).build
+    val row = expressionEncoder.toRow(record)
+    val recordFromRow = expressionEncoder.resolveAndBind().fromRow(row)
+    assert(record.toString == recordFromRow.toString)
+  }
+
+  test("generic record converts to row and back") {
+    // complex schema including type of basic type, array with int/string/record/enum,
+    // nested record and map.
+    val avroSchema =
+      """
+        |{
+        |  "type" : "record",
+        |  "name" : "record",
+        |  "fields" : [ {
+        |    "name" : "boolean",
+        |    "type" : "boolean",
+        |    "default" : false
+        |  }, {
+        |    "name" : "int",
+        |    "type" : "int",
+        |    "default" : 0
+        |  }, {
+        |    "name" : "long",
+        |    "type" : "long",
+        |    "default" : 0
+        |  }, {
+        |    "name" : "float",
+        |    "type" : "float",
+        |    "default" : 0.0
+        |  }, {
+        |    "name" : "double",
+        |    "type" : "double",
+        |    "default" : 0.0
+        |  }, {
+        |    "name" : "string",
+        |    "type" : "string",
+        |    "default" : "string"
+        |  }, {
+        |    "name" : "bytes",
+        |    "type" : "bytes",
+        |    "default" : "bytes"
+        |  }, {
+        |    "name" : "nested",
+        |    "type" : {
+        |      "type" : "record",
+        |      "name" : "simple_record",
+        |      "fields" : [ {
+        |        "name" : "nested1",
+        |        "type" : "int",
+        |        "default" : 0
+        |      }, {
+        |        "name" : "nested2",
+        |        "type" : "string",
+        |        "default" : "string"
+        |      } ]
+        |    },
+        |    "default" : {
+        |      "nested1" : 0,
+        |      "nested2" : "string"
+        |    }
+        |  }, {
+        |    "name" : "enum",
+        |    "type" : {
+        |      "type" : "enum",
+        |      "name" : "simple_enums",
+        |      "symbols" : [ "SPADES", "HEARTS", "CLUBS", "DIAMONDS" ]
+        |    },
+        |    "default" : "SPADES"
+        |  }, {
+        |    "name" : "int_array",
+        |    "type" : {
+        |      "type" : "array",
+        |      "items" : "int"
+        |    },
+        |    "default" : [ 1, 2, 3 ]
+        |  }, {
+        |    "name" : "string_array",
+        |    "type" : {
+        |      "type" : "array",
+        |      "items" : "string"
+        |    },
+        |    "default" : [ "a", "b", "c" ]
+        |  }, {
+        |    "name" : "record_array",
+        |    "type" : {
+        |      "type" : "array",
+        |      "items" : "simple_record"
+        |    },
+        |    "default" : [ {
+        |      "nested1" : 0,
+        |      "nested2" : "string"
+        |    }, {
+        |      "nested1" : 0,
+        |      "nested2" : "string"
+        |    } ]
+        |  }, {
+        |    "name" : "enum_array",
+        |    "type" : {
+        |      "type" : "array",
+        |      "items" : "simple_enums"
+        |    },
+        |    "default" : [ "SPADES", "HEARTS", "SPADES" ]
+        |  }, {
+        |    "name" : "fixed_array",
+        |    "type" : {
+        |      "type" : "array",
+        |      "items" : {
+        |        "type" : "fixed",
+        |        "name" : "simple_fixed",
+        |        "size" : 3
+        |      }
+        |    },
+        |    "default" : [ "foo", "bar", "baz" ]
+        |  }, {
+        |    "name" : "fixed",
+        |    "type" : {
+        |      "type" : "fixed",
+        |      "name" : "simple_fixed_item",
+        |      "size" : 16
+        |    },
+        |    "default" : "string_length_16"
+        |  }, {
+        |    "name" : "map",
+        |    "type" : {
+        |      "type" : "map",
+        |      "values" : "string"
+        |    },
+        |    "default" : {
+        |      "a" : "A"
+        |    }
+        |  } ]
+        |}
+      """.stripMargin
+
+    val schema = new Schema.Parser().parse(avroSchema)
+    val encoder = AvroEncoder.of[GenericData.Record](schema)
+    logInfo(schema.toString)
+    val expressionEncoder = encoder.asInstanceOf[ExpressionEncoder[GenericData.Record]]
+    val record = new GenericRecordBuilder(schema).build
+    val row = expressionEncoder.toRow(record)
+    val recordFromRow = expressionEncoder.resolveAndBind().fromRow(row)
+    assert(record.toString == recordFromRow.toString)
+  }
+
+  test("encoder resolves union types to rows") {
+    val avroSchema =
+      """
+        |{
+        |  "type" : "record",
+        |  "name" : "record",
+        |  "fields" : [ {
+        |    "name" : "int_null_union",
+        |    "type" : [ "null", "int" ],
+        |    "default" : null
+        |  }, {
+        |    "name" : "string_null_union",
+        |    "type" : [ "null", "string" ],
+        |    "default" : null
+        |  }, {
+        |    "name" : "int_long_union",
+        |    "type" : [ "int", "long" ],
+        |    "default" : 0
+        |  }, {
+        |    "name" : "float_double_union",
+        |    "type" : [ "float", "double" ],
+        |    "default" : 0.0
+        |  } ]
+        |}
+      """.stripMargin
+
+    val schema = new Schema.Parser().parse(avroSchema)
+    val encoder = AvroEncoder.of[GenericData.Record](schema)
+    logInfo(schema.toString(true))
 
 Review comment:
   nit: remove logInfo, or improve the output

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

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