Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5725#discussion_r29300995
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/UnsafeRowConverter.scala
 ---
    @@ -0,0 +1,226 @@
    +/*
    + * 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.catalyst.expressions
    +
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.PlatformDependent
    +import org.apache.spark.unsafe.array.ByteArrayMethods
    +
    +/**
    + * Converts Rows into UnsafeRow format. This class is NOT thread-safe.
    + *
    + * @param fieldTypes the data types of the row's columns.
    + */
    +class UnsafeRowConverter(fieldTypes: Array[DataType]) {
    +
    +  def this(schema: StructType) {
    +    this(schema.fields.map(_.dataType))
    +  }
    +
    +  /** Re-used pointer to the unsafe row being written */
    +  private[this] val unsafeRow = new UnsafeRow()
    +
    +  /** Functions for encoding each column */
    +  private[this] val writers: Array[UnsafeColumnWriter[Any]] = {
    +    fieldTypes.map(t => 
UnsafeColumnWriter.forType(t).asInstanceOf[UnsafeColumnWriter[Any]])
    +  }
    +
    +  /** The size, in bytes, of the fixed-length portion of the row, 
including the null bitmap */
    +  private[this] val fixedLengthSize: Int =
    +    (8 * fieldTypes.length) + 
UnsafeRow.calculateBitSetWidthInBytes(fieldTypes.length)
    +
    +  /**
    +   * Compute the amount of space, in bytes, required to encode the given 
row.
    +   */
    +  def getSizeRequirement(row: Row): Int = {
    +    var fieldNumber = 0
    +    var variableLengthFieldSize: Int = 0
    +    while (fieldNumber < writers.length) {
    +      if (!row.isNullAt(fieldNumber)) {
    +        variableLengthFieldSize += 
writers(fieldNumber).getSize(row(fieldNumber))
    +      }
    +      fieldNumber += 1
    +    }
    +    fixedLengthSize + variableLengthFieldSize
    +  }
    +
    +  /**
    +   * Convert the given row into UnsafeRow format.
    +   *
    +   * @param row the row to convert
    +   * @param baseObject the base object of the destination address
    +   * @param baseOffset the base offset of the destination address
    +   * @return the number of bytes written. This should be equal to 
`getSizeRequirement(row)`.
    +   */
    +  def writeRow(row: Row, baseObject: Object, baseOffset: Long): Long = {
    +    unsafeRow.pointTo(baseObject, baseOffset, writers.length, null)
    +    var fieldNumber = 0
    +    var appendCursor: Int = fixedLengthSize
    +    while (fieldNumber < writers.length) {
    +      if (row.isNullAt(fieldNumber)) {
    +        unsafeRow.setNullAt(fieldNumber)
    +      } else {
    +        appendCursor += writers(fieldNumber).write(
    +          row(fieldNumber),
    --- End diff --
    
    This is pretty minor, but is there a reason we are boxing here instead of 
passing the row itself in, allowing a specific accessor to be used for 
extraction?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to