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

    https://github.com/apache/spark/pull/13676#discussion_r67639405
  
    --- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala ---
    @@ -479,8 +340,299 @@ private[hive] trait HiveInspectors {
       }
     
       /**
    -   * Builds specific unwrappers ahead of time according to object inspector
    +   * Builds unwrappers ahead of time according to object inspector
        * types to avoid pattern matching and branching costs per row.
    +   *
    +   * Strictly follows the following order in unwrapping (constant OI has 
the higher priority):
    +   * Constant Null object inspector =>
    +   *   return null
    +   * Constant object inspector =>
    +   *   extract the value from constant object inspector
    +   * If object inspector prefers writable =>
    +   *   extract writable from `data` and then get the catalyst type from 
the writable
    +   * Extract the java object directly from the object inspector
    +   *
    +   * NOTICE: the complex data type requires recursive unwrapping.
    +   *
    +   * @param objectInspector the ObjectInspector used to create an 
unwrapper.
    +   * @return A function that unwraps data objects.
    +   *         Use the overloaded HiveStructField version for in-place 
updating of a MutableRow.
    +   */
    +  def unwrapperFor(objectInspector: ObjectInspector): Any => Any =
    +    objectInspector match {
    +      case coi: ConstantObjectInspector if coi.getWritableConstantValue == 
null =>
    +        data: Any => null
    +      case poi: WritableConstantStringObjectInspector =>
    +        data: Any =>
    +          UTF8String.fromString(poi.getWritableConstantValue.toString)
    +      case poi: WritableConstantHiveVarcharObjectInspector =>
    +        data: Any =>
    +          
UTF8String.fromString(poi.getWritableConstantValue.getHiveVarchar.getValue)
    +      case poi: WritableConstantHiveCharObjectInspector =>
    +        data: Any =>
    +          
UTF8String.fromString(poi.getWritableConstantValue.getHiveChar.getValue)
    +      case poi: WritableConstantHiveDecimalObjectInspector =>
    +        data: Any =>
    +          HiveShim.toCatalystDecimal(
    +            PrimitiveObjectInspectorFactory.javaHiveDecimalObjectInspector,
    +            poi.getWritableConstantValue.getHiveDecimal)
    +      case poi: WritableConstantTimestampObjectInspector =>
    +        data: Any => {
    +          val t = poi.getWritableConstantValue
    +          t.getSeconds * 1000000L + t.getNanos / 1000L
    +        }
    +      case poi: WritableConstantIntObjectInspector =>
    +        data: Any =>
    +          poi.getWritableConstantValue.get()
    +      case poi: WritableConstantDoubleObjectInspector =>
    +        data: Any =>
    +          poi.getWritableConstantValue.get()
    +      case poi: WritableConstantBooleanObjectInspector =>
    +        data: Any =>
    +          poi.getWritableConstantValue.get()
    +      case poi: WritableConstantLongObjectInspector =>
    +        data: Any =>
    +          poi.getWritableConstantValue.get()
    +      case poi: WritableConstantFloatObjectInspector =>
    +        data: Any =>
    +          poi.getWritableConstantValue.get()
    +      case poi: WritableConstantShortObjectInspector =>
    +        data: Any =>
    +          poi.getWritableConstantValue.get()
    +      case poi: WritableConstantByteObjectInspector =>
    +        data: Any =>
    +          poi.getWritableConstantValue.get()
    +      case poi: WritableConstantBinaryObjectInspector =>
    +        data: Any => {
    +          val writable = poi.getWritableConstantValue
    +          val temp = new Array[Byte](writable.getLength)
    +          System.arraycopy(writable.getBytes, 0, temp, 0, temp.length)
    +          temp
    +        }
    +      case poi: WritableConstantDateObjectInspector =>
    +        data: Any =>
    +          DateTimeUtils.fromJavaDate(poi.getWritableConstantValue.get())
    +      case mi: StandardConstantMapObjectInspector =>
    +        val keyUnwrapper = unwrapperFor(mi.getMapKeyObjectInspector)
    +        val valueUnwrapper = unwrapperFor(mi.getMapValueObjectInspector)
    +        data: Any => {
    +          // take the value from the map inspector object, rather than the 
input data
    +          val keyValues = mi.getWritableConstantValue.asScala.toSeq
    +          val keys = keyValues.map(kv => keyUnwrapper(kv._1)).toArray
    +          val values = keyValues.map(kv => valueUnwrapper(kv._2)).toArray
    +          ArrayBasedMapData(keys, values)
    +        }
    +      case li: StandardConstantListObjectInspector =>
    +        val unwrapper = unwrapperFor(li.getListElementObjectInspector)
    +        data: Any => {
    +          // take the value from the list inspector object, rather than 
the input data
    +          val values = li.getWritableConstantValue.asScala
    +            .map(unwrapper)
    +            .toArray
    +          new GenericArrayData(values)
    +        }
    +      case poi: VoidObjectInspector =>
    +        data: Any =>
    +          null // always be null for void object inspector
    +      case pi: PrimitiveObjectInspector => pi match {
    +        // We think HiveVarchar/HiveChar is also a String
    +        case hvoi: HiveVarcharObjectInspector if hvoi.preferWritable() =>
    +          data: Any => {
    +            if (data != null) {
    +              
UTF8String.fromString(hvoi.getPrimitiveWritableObject(data).getHiveVarchar.getValue)
    +            } else {
    +              null
    +            }
    +          }
    +        case hvoi: HiveVarcharObjectInspector =>
    +          data: Any => {
    +            if (data != null) {
    +              
UTF8String.fromString(hvoi.getPrimitiveJavaObject(data).getValue)
    +            } else {
    +              null
    +            }
    +          }
    +        case hvoi: HiveCharObjectInspector if hvoi.preferWritable() =>
    +          data: Any => {
    +            if (data != null) {
    +              
UTF8String.fromString(hvoi.getPrimitiveWritableObject(data).getHiveChar.getValue)
    +            } else {
    +              null
    +            }
    +          }
    +        case hvoi: HiveCharObjectInspector =>
    +          data: Any => {
    +            if (data != null) {
    +              
UTF8String.fromString(hvoi.getPrimitiveJavaObject(data).getValue)
    +            } else {
    +              null
    +            }
    +          }
    +        case x: StringObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) {
    +              // Text is in UTF-8 already. No need to convert again via 
fromString. Copy bytes
    +              val wObj = x.getPrimitiveWritableObject(data)
    +              val result = wObj.copyBytes()
    +              UTF8String.fromBytes(result, 0, result.length)
    +            } else {
    +              null
    +            }
    +          }
    +        case x: StringObjectInspector =>
    +          data: Any => {
    +            if (data != null) {
    +              UTF8String.fromString(x.getPrimitiveJavaObject(data))
    +            } else {
    +              null
    +            }
    +          }
    +        case x: IntObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) x.get(data) else null
    +          }
    +        case x: BooleanObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) x.get(data) else null
    +          }
    +        case x: FloatObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) x.get(data) else null
    +          }
    +        case x: DoubleObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) x.get(data) else null
    +          }
    +        case x: LongObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) x.get(data) else null
    +          }
    +        case x: ShortObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) x.get(data) else null
    +          }
    +        case x: ByteObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) x.get(data) else null
    +          }
    +        case x: HiveDecimalObjectInspector =>
    +          data: Any => {
    +            if (data != null) {
    +              HiveShim.toCatalystDecimal(x, data)
    +            } else {
    +              null
    +            }
    +          }
    +        case x: BinaryObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) {
    +              // BytesWritable.copyBytes() only available since Hadoop2
    +              // In order to keep backward-compatible, we have to copy the
    +              // bytes with old apis
    +              val bw = x.getPrimitiveWritableObject(data)
    +              val result = new Array[Byte](bw.getLength())
    +              System.arraycopy(bw.getBytes(), 0, result, 0, bw.getLength())
    +              result
    +            } else {
    +              null
    +            }
    +          }
    +        case x: DateObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) {
    +              
DateTimeUtils.fromJavaDate(x.getPrimitiveWritableObject(data).get())
    +            } else {
    +              null
    +            }
    +          }
    +        case x: DateObjectInspector =>
    +          data: Any => {
    +            if (data != null) {
    +              DateTimeUtils.fromJavaDate(x.getPrimitiveJavaObject(data))
    +            } else {
    +              null
    +            }
    +          }
    +        case x: TimestampObjectInspector if x.preferWritable() =>
    +          data: Any => {
    +            if (data != null) {
    +              val t = x.getPrimitiveWritableObject(data)
    +              t.getSeconds * 1000000L + t.getNanos / 1000L
    +            } else {
    +              null
    +            }
    +          }
    +        case ti: TimestampObjectInspector =>
    +          data: Any => {
    +            if (data != null) {
    +              
DateTimeUtils.fromJavaTimestamp(ti.getPrimitiveJavaObject(data))
    +            } else {
    +              null
    +            }
    +          }
    +        case _ =>
    +          data: Any => {
    +            if (data != null) {
    +              pi.getPrimitiveJavaObject(data)
    +            } else {
    +              null
    +            }
    +          }
    +      }
    +      case li: ListObjectInspector =>
    +        val unwrapper = unwrapperFor(li.getListElementObjectInspector)
    +        data: Any => {
    +          if (data != null) {
    +            Option(li.getList(data))
    +              .map { l =>
    +                val values = l.asScala.map(unwrapper).toArray
    +                new GenericArrayData(values)
    +              }
    +              .orNull
    +          } else {
    +            null
    +          }
    +        }
    +      case mi: MapObjectInspector =>
    +        val keyUnwrapper = unwrapperFor(mi.getMapKeyObjectInspector)
    +        val valueUnwrapper = unwrapperFor(mi.getMapValueObjectInspector)
    +        data: Any => {
    +          if (data != null) {
    +            val map = mi.getMap(data)
    +            if (map == null) {
    +              null
    +            } else {
    +              val keyValues = map.asScala.toSeq
    +              val keys = keyValues.map(kv => keyUnwrapper(kv._1)).toArray
    +              val values = keyValues.map(kv => 
valueUnwrapper(kv._2)).toArray
    +              ArrayBasedMapData(keys, values)
    +            }
    +          } else {
    +            null
    +          }
    +        }
    +      // currently, hive doesn't provide the ConstantStructObjectInspector
    +      case si: StructObjectInspector =>
    +        val fields = si.getAllStructFieldRefs.asScala
    +        val fieldsToUnwrap = fields.zip(
    +          fields.map(_.getFieldObjectInspector).map(unwrapperFor))
    +        data: Any => {
    +          if (data != null) {
    +            InternalRow.fromSeq(fieldsToUnwrap.map(
    --- End diff --
    
    You can just can use a pattern match here to get to the field and the 
unwrap function directly, i.e.:
    ```scala
    InternalRow.fromSeq(fieldsToUnwrap.map { case (field, unwrap) =>
      unwrap(si.getStructFieldData(data, field))
    }
    ```
    That makes it a bit more readable.


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