You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by gengliangwang <gi...@git.apache.org> on 2018/07/13 16:50:03 UTC

[GitHub] spark pull request #21762: [SPARK-24800][SQL] Refactor Avro Serializer and D...

GitHub user gengliangwang opened a pull request:

    https://github.com/apache/spark/pull/21762

    [SPARK-24800][SQL] Refactor Avro Serializer and Deserializer

    ## What changes were proposed in this pull request?
    Currently the Avro Deserializer converts input Avro format data to `Row`, and then convert the `Row` to `InternalRow`.
    While the Avro Serializer converts `InternalRow` to `Row`, and then output Avro format data.
    This PR allows direct conversion between `InternalRow` and Avro format data.
    
    Credits to @cloud-fan . 
    ## How was this patch tested?
    
    Unit test


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/gengliangwang/spark avro_io

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/21762.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #21762
    
----
commit bb7a43c8f3e34c90ebe8f0e22019c096776b6da3
Author: Gengliang Wang <ge...@...>
Date:   2018-07-13T08:18:12Z

    refactor avro Serializer and Deserializer

----


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    **[Test build #93026 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/93026/testReport)** for PR 21762 at commit [`aa5e79e`](https://github.com/apache/spark/commit/aa5e79ec67fbe0be54678a75258cb1b02cf5c9c1).


---

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


[GitHub] spark pull request #21762: [SPARK-24800][SQL] Refactor Avro Serializer and D...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/21762


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Merged build finished. Test PASSed.


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Merged build finished. Test PASSed.


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/testing-k8s-prb-make-spark-distribution-unified/929/
    Test PASSed.


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Merged build finished. Test PASSed.


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    thanks, merging to master!


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    **[Test build #93026 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/93026/testReport)** for PR 21762 at commit [`aa5e79e`](https://github.com/apache/spark/commit/aa5e79ec67fbe0be54678a75258cb1b02cf5c9c1).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/92975/
    Test PASSed.


---

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


[GitHub] spark pull request #21762: [SPARK-24800][SQL] Refactor Avro Serializer and D...

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/21762#discussion_r202502724
  
    --- Diff: external/avro/src/main/scala/org/apache/spark/sql/avro/SchemaConverters.scala ---
    @@ -17,34 +17,30 @@
     
     package org.apache.spark.sql.avro
     
    -import java.nio.ByteBuffer
    -import java.sql.{Date, Timestamp}
    -
     import scala.collection.JavaConverters._
     
     import org.apache.avro.{Schema, SchemaBuilder}
     import org.apache.avro.Schema.Type._
    -import org.apache.avro.SchemaBuilder._
    -import org.apache.avro.generic.{GenericData, GenericRecord}
    -import org.apache.avro.generic.GenericFixed
     
    -import org.apache.spark.sql.catalyst.expressions.GenericRow
     import org.apache.spark.sql.types._
     
     /**
      * This object contains method that are used to convert sparkSQL schemas to avro schemas and vice
      * versa.
      */
     object SchemaConverters {
    -
    -  class IncompatibleSchemaException(msg: String, ex: Throwable = null) extends Exception(msg, ex)
    -
       case class SchemaType(dataType: DataType, nullable: Boolean)
     
    +  /**
    +   * This function takes an avro schema and returns a sql schema.
    +   * An alias name of toCatalystType.
    +   */
    +  def toSqlType(avroSchema: Schema): SchemaType = toCatalystType(avroSchema)
    +
       /**
        * This function takes an avro schema and returns a sql schema.
        */
    -  def toSqlType(avroSchema: Schema): SchemaType = {
    +  def toCatalystType(avroSchema: Schema): SchemaType = {
    --- End diff --
    
    maybe we don't need to rename it?


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    **[Test build #92975 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/92975/testReport)** for PR 21762 at commit [`bb7a43c`](https://github.com/apache/spark/commit/bb7a43c8f3e34c90ebe8f0e22019c096776b6da3).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds the following public classes _(experimental)_:
      * `class AvroDeserializer(rootAvroType: Schema, rootCatalystType: DataType) `
      * `  sealed trait CatalystDataUpdater `
      * `  final class RowUpdater(row: InternalRow) extends CatalystDataUpdater `
      * `  final class ArrayDataUpdater(array: ArrayData) extends CatalystDataUpdater `
      * `class AvroSerializer(rootCatalystType: DataType, rootAvroType: Schema, nullable: Boolean) `
      * `class IncompatibleSchemaException(msg: String, ex: Throwable = null) extends Exception(msg, ex)`
      * `class SerializableSchema(@transient var value: Schema)`


---

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


[GitHub] spark pull request #21762: [SPARK-24800][SQL] Refactor Avro Serializer and D...

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/21762#discussion_r202502621
  
    --- Diff: external/avro/src/main/scala/org/apache/spark/sql/avro/AvroDeserializer.scala ---
    @@ -0,0 +1,348 @@
    +/*
    + * 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 scala.collection.JavaConverters._
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.avro.{Schema, SchemaBuilder}
    +import org.apache.avro.Schema.Type._
    +import org.apache.avro.generic._
    +import org.apache.avro.util.Utf8
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.expressions.{SpecificInternalRow, UnsafeArrayData}
    +import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, ArrayData, DateTimeUtils, GenericArrayData}
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +
    +/**
    + * A deserializer to deserialize data in avro format to data in catalyst format.
    + */
    +class AvroDeserializer(rootAvroType: Schema, rootCatalystType: DataType) {
    +  private val converter: Any => Any = rootCatalystType match {
    +    // A shortcut for empty schema.
    +    case st: StructType if st.isEmpty =>
    +      (data: Any) => InternalRow.empty
    +
    +    case st: StructType =>
    +      val resultRow = new SpecificInternalRow(st.map(_.dataType))
    +      val fieldUpdater = new RowUpdater(resultRow)
    +      val writer = getRecordWriter(rootAvroType, st, Nil)
    +      (data: Any) => {
    +        val record = data.asInstanceOf[GenericRecord]
    +        writer(fieldUpdater, record)
    +        resultRow
    +      }
    +
    +    case _ =>
    +      val tmpRow = new SpecificInternalRow(Seq(rootCatalystType))
    +      val fieldUpdater = new RowUpdater(tmpRow)
    +      val writer = newWriter(rootAvroType, rootCatalystType, Nil)
    +      (data: Any) => {
    +        writer(fieldUpdater, 0, data)
    +        tmpRow.get(0, rootCatalystType)
    +      }
    +  }
    +
    +  def deserialize(data: Any): Any = converter(data)
    +
    +  /**
    +   * Creates a writer to writer avro values to Catalyst values at the given ordinal with the given
    +   * updater.
    +   */
    +  private def newWriter(
    +      avroType: Schema,
    +      catalystType: DataType,
    +      path: List[String]): (CatalystDataUpdater, Int, Any) => Unit =
    +    (avroType.getType, catalystType) match {
    +      case (NULL, NullType) => (updater, ordinal, _) =>
    +        updater.setNullAt(ordinal)
    +
    +      // TODO: we can avoid boxing if future version of avro provide primitive accessors.
    +      case (BOOLEAN, BooleanType) => (updater, ordinal, value) =>
    +        updater.setBoolean(ordinal, value.asInstanceOf[Boolean])
    +
    +      case (INT, IntegerType) => (updater, ordinal, value) =>
    +        updater.setInt(ordinal, value.asInstanceOf[Int])
    +
    +      case (LONG, LongType) => (updater, ordinal, value) =>
    +        updater.setLong(ordinal, value.asInstanceOf[Long])
    +
    +      case (LONG, TimestampType) => (updater, ordinal, value) =>
    +        updater.setLong(ordinal, value.asInstanceOf[Long] * 1000)
    +
    +      case (LONG, DateType) => (updater, ordinal, value) =>
    +        updater.setInt(ordinal, (value.asInstanceOf[Long] / DateTimeUtils.MILLIS_PER_DAY).toInt)
    +
    +      case (FLOAT, FloatType) => (updater, ordinal, value) =>
    +        updater.setFloat(ordinal, value.asInstanceOf[Float])
    +
    +      case (DOUBLE, DoubleType) => (updater, ordinal, value) =>
    +        updater.setDouble(ordinal, value.asInstanceOf[Double])
    +
    +      case (STRING, StringType) => (updater, ordinal, value) =>
    +        val str = value match {
    +          case s: String => UTF8String.fromString(s)
    +          case s: Utf8 =>
    +            val bytes = new Array[Byte](s.getByteLength)
    +            System.arraycopy(s.getBytes, 0, bytes, 0, s.getByteLength)
    +            UTF8String.fromBytes(bytes)
    +        }
    +        updater.set(ordinal, str)
    +
    +      case (ENUM, StringType) => (updater, ordinal, value) =>
    +        updater.set(ordinal, UTF8String.fromString(value.toString))
    +
    +      case (FIXED, BinaryType) => (updater, ordinal, value) =>
    +        updater.set(ordinal, value.asInstanceOf[GenericFixed].bytes().clone())
    +
    +      case (BYTES, BinaryType) => (updater, ordinal, value) =>
    +        val bytes = value match {
    +          case b: ByteBuffer =>
    +            val bytes = new Array[Byte](b.remaining)
    +            b.get(bytes)
    +            bytes
    +          case b: Array[Byte] => b
    +          case other => throw new RuntimeException(s"$other is not a valid avro binary.")
    +
    +        }
    +        updater.set(ordinal, bytes)
    +
    +      case (RECORD, st: StructType) =>
    +        val writeRecord = getRecordWriter(avroType, st, path)
    +        (updater, ordinal, value) =>
    +          val row = new SpecificInternalRow(st)
    +          writeRecord(new RowUpdater(row), value.asInstanceOf[GenericRecord])
    +          updater.set(ordinal, row)
    +
    +      case (ARRAY, ArrayType(elementType, containsNull)) =>
    +        val elementWriter = newWriter(avroType.getElementType, elementType, path)
    +        (updater, ordinal, value) =>
    +          val array = value.asInstanceOf[GenericData.Array[Any]]
    +          val len = array.size()
    +          val result = createArrayData(elementType, len)
    +          val elementUpdater = new ArrayDataUpdater(result)
    +
    +          var i = 0
    +          while (i < len) {
    +            val element = array.get(i)
    +            if (element == null) {
    +              if (!containsNull) {
    +                throw new RuntimeException(s"Array value at path ${path.mkString(".")} is not " +
    +                  "allowed to be null")
    +              } else {
    +                elementUpdater.setNullAt(i)
    +              }
    +            } else {
    +              elementWriter(elementUpdater, i, element)
    +            }
    +            i += 1
    +          }
    +
    +          updater.set(ordinal, result)
    +
    +      case (MAP, MapType(keyType, valueType, valueContainsNull)) if keyType == StringType =>
    +        val keyWriter = newWriter(SchemaBuilder.builder().stringType(), StringType, path)
    +        val valueWriter = newWriter(avroType.getValueType, valueType, path)
    +        (updater, ordinal, value) =>
    +          val map = value.asInstanceOf[java.util.Map[AnyRef, AnyRef]]
    +          val keyArray = createArrayData(keyType, map.size())
    +          val keyUpdater = new ArrayDataUpdater(keyArray)
    +          val valueArray = createArrayData(valueType, map.size())
    +          val valueUpdater = new ArrayDataUpdater(valueArray)
    +          val iter = map.entrySet().iterator()
    +          var i = 0
    +          while (iter.hasNext) {
    +            val entry = iter.next()
    +            assert(entry.getKey != null)
    +            keyWriter(keyUpdater, i, entry.getKey)
    +            if (entry.getValue == null) {
    +              if (!valueContainsNull) {
    +                throw new RuntimeException(s"Map value at path ${path.mkString(".")} is not " +
    +                  "allowed to be null")
    +              } else {
    +                valueUpdater.setNullAt(i)
    +              }
    +            } else {
    +              valueWriter(valueUpdater, i, entry.getValue)
    +            }
    +            i += 1
    +          }
    +
    +          updater.set(ordinal, new ArrayBasedMapData(keyArray, valueArray))
    +
    +      case (UNION, _) =>
    +        val allTypes = avroType.getTypes.asScala
    +        val nonNullTypes = allTypes.filter(_.getType != NULL)
    +        if (nonNullTypes.nonEmpty) {
    +          if (nonNullTypes.length == 1) {
    +            newWriter(nonNullTypes.head, catalystType, path)
    +          } else {
    +            nonNullTypes.map(_.getType) match {
    +              case Seq(a, b) if Set(a, b) == Set(INT, LONG) && catalystType == LongType =>
    +                (updater, ordinal, value) => value match {
    +                  case null => updater.setNullAt(ordinal)
    +                  case l: java.lang.Long => updater.setLong(ordinal, l)
    +                  case i: java.lang.Integer => updater.setLong(ordinal, i.longValue())
    +                }
    +
    +              case Seq(a, b) if Set(a, b) == Set(FLOAT, DOUBLE) && catalystType == DoubleType =>
    +                (updater, ordinal, value) => value match {
    +                  case null => updater.setNullAt(ordinal)
    +                  case d: java.lang.Double => updater.setDouble(ordinal, d)
    +                  case f: java.lang.Float => updater.setDouble(ordinal, f.doubleValue())
    +                }
    +
    +              case _ =>
    +                catalystType match {
    +                  case st: StructType if st.length == nonNullTypes.size =>
    +                    val fieldWriters = nonNullTypes.zip(st.fields).map {
    +                      case (schema, field) => newWriter(schema, field.dataType, path :+ field.name)
    +                    }.toArray
    +                    (updater, ordinal, value) => {
    +                      val row = new SpecificInternalRow(st)
    +                      val fieldUpdater = new RowUpdater(row)
    +                      val i = GenericData.get().resolveUnion(avroType, value)
    +                      fieldWriters(i)(fieldUpdater, i, value)
    +                      updater.set(ordinal, row)
    +                    }
    +
    +                  case _ =>
    +                    throw new IncompatibleSchemaException(
    +                      s"Cannot convert Avro to catalyst because schema at path " +
    +                        s"${path.mkString(".")} is not compatible " +
    +                        s"(avroType = $avroType, sqlType = $catalystType).\n" +
    +                        s"Source Avro schema: $rootAvroType.\n" +
    +                        s"Target Catalyst type: $rootCatalystType")
    +                }
    +            }
    +          }
    +        } else {
    +          (updater, ordinal, value) => updater.setNullAt(ordinal)
    +        }
    +
    +      case _ =>
    +        throw new IncompatibleSchemaException(
    +          s"Cannot convert Avro to catalyst because schema at path ${path.mkString(".")} " +
    +            s"is not compatible (avroType = $avroType, sqlType = $catalystType).\n" +
    +            s"Source Avro schema: $rootAvroType.\n" +
    +            s"Target Catalyst type: $rootCatalystType")
    +    }
    +
    +  private def getRecordWriter(
    +      avroType: Schema,
    +      sqlType: StructType,
    +      path: List[String]): (CatalystDataUpdater, GenericRecord) => Unit = {
    +    val validFieldIndexes = ArrayBuffer.empty[Int]
    +    val fieldWriters = ArrayBuffer.empty[(CatalystDataUpdater, Any) => Unit]
    +
    +    val length = sqlType.length
    +    var i = 0
    +    while (i < length) {
    +      val sqlField = sqlType.fields(i)
    +      val avroField = avroType.getField(sqlField.name)
    +      if (avroField != null) {
    +        validFieldIndexes += avroField.pos()
    +
    +        val baseWriter = newWriter(avroField.schema(), sqlField.dataType, path :+ sqlField.name)
    +        val ordinal = i
    +        val fieldWriter = (fieldUpdater: CatalystDataUpdater, value: Any) => {
    +          if (value == null) {
    +            fieldUpdater.setNullAt(ordinal)
    +          } else {
    +            baseWriter(fieldUpdater, ordinal, value)
    +          }
    +        }
    +        fieldWriters += fieldWriter
    +      } else if (!sqlField.nullable) {
    +        throw new IncompatibleSchemaException(
    +          s"""
    +             |Cannot find non-nullable field ${path.mkString(".")}.${sqlField.name} in Avro schema.
    +             |Source Avro schema: $rootAvroType.
    +             |Target Catalyst type: $rootCatalystType.
    +           """.stripMargin)
    +      }
    +      i += 1
    +    }
    +
    +    (fieldUpdater, record) => {
    +      var i = 0
    +      while (i < validFieldIndexes.length) {
    +        fieldWriters(i)(fieldUpdater, record.get(validFieldIndexes(i)))
    +        i += 1
    +      }
    +    }
    +  }
    +
    +  private def createArrayData(elementType: DataType, length: Int): ArrayData = elementType match {
    +    case BooleanType => UnsafeArrayData.fromPrimitiveArray(new Array[Boolean](length))
    +    case ByteType => UnsafeArrayData.fromPrimitiveArray(new Array[Byte](length))
    +    case ShortType => UnsafeArrayData.fromPrimitiveArray(new Array[Short](length))
    +    case IntegerType => UnsafeArrayData.fromPrimitiveArray(new Array[Int](length))
    +    case LongType => UnsafeArrayData.fromPrimitiveArray(new Array[Long](length))
    +    case FloatType => UnsafeArrayData.fromPrimitiveArray(new Array[Float](length))
    +    case DoubleType => UnsafeArrayData.fromPrimitiveArray(new Array[Double](length))
    +    case _ => new GenericArrayData(new Array[Any](length))
    +  }
    +
    +  /**
    +   * A base interface for updating values inside catalyst data structure like `InternalRow` and
    +   * `ArrayData`.
    +   */
    +  sealed trait CatalystDataUpdater {
    +    def set(ordinal: Int, value: Any): Unit
    +
    +    def setNullAt(ordinal: Int): Unit = set(ordinal, null)
    +    def setBoolean(ordinal: Int, value: Boolean): Unit = set(ordinal, value)
    +    def setByte(ordinal: Int, value: Byte): Unit = set(ordinal, value)
    +    def setShort(ordinal: Int, value: Short): Unit = set(ordinal, value)
    +    def setInt(ordinal: Int, value: Int): Unit = set(ordinal, value)
    +    def setLong(ordinal: Int, value: Long): Unit = set(ordinal, value)
    +    def setDouble(ordinal: Int, value: Double): Unit = set(ordinal, value)
    +    def setFloat(ordinal: Int, value: Float): Unit = set(ordinal, value)
    --- End diff --
    
    seems we don't need these default implementation


---

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


[GitHub] spark pull request #21762: [SPARK-24800][SQL] Refactor Avro Serializer and D...

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/21762#discussion_r202502456
  
    --- Diff: external/avro/src/main/scala/org/apache/spark/sql/avro/AvroDeserializer.scala ---
    @@ -0,0 +1,348 @@
    +/*
    + * 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 scala.collection.JavaConverters._
    +import scala.collection.mutable.ArrayBuffer
    +
    +import org.apache.avro.{Schema, SchemaBuilder}
    +import org.apache.avro.Schema.Type._
    +import org.apache.avro.generic._
    +import org.apache.avro.util.Utf8
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.expressions.{SpecificInternalRow, UnsafeArrayData}
    +import org.apache.spark.sql.catalyst.util.{ArrayBasedMapData, ArrayData, DateTimeUtils, GenericArrayData}
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +
    +/**
    + * A deserializer to deserialize data in avro format to data in catalyst format.
    + */
    +class AvroDeserializer(rootAvroType: Schema, rootCatalystType: DataType) {
    +  private val converter: Any => Any = rootCatalystType match {
    +    // A shortcut for empty schema.
    +    case st: StructType if st.isEmpty =>
    +      (data: Any) => InternalRow.empty
    +
    +    case st: StructType =>
    +      val resultRow = new SpecificInternalRow(st.map(_.dataType))
    +      val fieldUpdater = new RowUpdater(resultRow)
    +      val writer = getRecordWriter(rootAvroType, st, Nil)
    +      (data: Any) => {
    +        val record = data.asInstanceOf[GenericRecord]
    +        writer(fieldUpdater, record)
    +        resultRow
    +      }
    +
    +    case _ =>
    +      val tmpRow = new SpecificInternalRow(Seq(rootCatalystType))
    +      val fieldUpdater = new RowUpdater(tmpRow)
    +      val writer = newWriter(rootAvroType, rootCatalystType, Nil)
    +      (data: Any) => {
    +        writer(fieldUpdater, 0, data)
    +        tmpRow.get(0, rootCatalystType)
    +      }
    +  }
    +
    +  def deserialize(data: Any): Any = converter(data)
    +
    +  /**
    +   * Creates a writer to writer avro values to Catalyst values at the given ordinal with the given
    --- End diff --
    
    nit `a writer to write`


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/93026/
    Test PASSed.


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Merged build finished. Test PASSed.


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    **[Test build #92975 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/92975/testReport)** for PR 21762 at commit [`bb7a43c`](https://github.com/apache/spark/commit/bb7a43c8f3e34c90ebe8f0e22019c096776b6da3).


---

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


[GitHub] spark issue #21762: [SPARK-24800][SQL] Refactor Avro Serializer and Deserial...

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/21762
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/testing-k8s-prb-make-spark-distribution-unified/970/
    Test PASSed.


---

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