[GitHub] spark pull request #15064: [SPARK-17509][SQL]When wrapping catalyst datatype...

2016-10-02 Thread asfgit
Github user asfgit closed the pull request at:

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


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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



[GitHub] spark pull request #15064: [SPARK-17509][SQL]When wrapping catalyst datatype...

2016-09-16 Thread sitalkedia
Github user sitalkedia commented on a diff in the pull request:

https://github.com/apache/spark/pull/15064#discussion_r79239277
  
--- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala ---
@@ -238,102 +238,166 @@ private[hive] trait HiveInspectors {
 case c => throw new AnalysisException(s"Unsupported java type $c")
   }
 
+  private def withNullSafe(f: Any => Any): Any => Any = {
+input => if (input == null) null else f(input)
+  }
+
   /**
* Wraps with Hive types based on object inspector.
-   * TODO: Consolidate all hive OI/data interface code.
*/
   protected def wrapperFor(oi: ObjectInspector, dataType: DataType): Any 
=> Any = oi match {
-case _: JavaHiveVarcharObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  val s = o.asInstanceOf[UTF8String].toString
-  new HiveVarchar(s, s.length)
-} else {
-  null
-}
-
-case _: JavaHiveCharObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  val s = o.asInstanceOf[UTF8String].toString
-  new HiveChar(s, s.length)
-} else {
-  null
-}
-
-case _: JavaHiveDecimalObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  HiveDecimal.create(o.asInstanceOf[Decimal].toJavaBigDecimal)
-} else {
-  null
-}
-
-case _: JavaDateObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  DateTimeUtils.toJavaDate(o.asInstanceOf[Int])
-} else {
-  null
-}
-
-case _: JavaTimestampObjectInspector =>
+case x: ConstantObjectInspector =>
   (o: Any) =>
-if (o != null) {
-  DateTimeUtils.toJavaTimestamp(o.asInstanceOf[Long])
-} else {
-  null
-}
+x.getWritableConstantValue
+case x: PrimitiveObjectInspector => x match {
+  // TODO we don't support the HiveVarcharObjectInspector yet.
+  case _: StringObjectInspector if x.preferWritable() =>
+withNullSafe(o => getStringWritable(o))
+  case _: StringObjectInspector =>
+withNullSafe(o => o.asInstanceOf[UTF8String].toString())
+  case _: IntObjectInspector if x.preferWritable() =>
+withNullSafe(o => getIntWritable(o))
+  case _: IntObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Integer])
+  case _: BooleanObjectInspector if x.preferWritable() =>
+withNullSafe(o => getBooleanWritable(o))
+  case _: BooleanObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Boolean])
+  case _: FloatObjectInspector if x.preferWritable() =>
+withNullSafe(o => getFloatWritable(o))
+  case _: FloatObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Float])
+  case _: DoubleObjectInspector if x.preferWritable() =>
+withNullSafe(o => getDoubleWritable(o))
+  case _: DoubleObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Double])
+  case _: LongObjectInspector if x.preferWritable() =>
+withNullSafe(o => getLongWritable(o))
+  case _: LongObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Long])
+  case _: ShortObjectInspector if x.preferWritable() =>
+withNullSafe(o => getShortWritable(o))
+  case _: ShortObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Short])
+  case _: ByteObjectInspector if x.preferWritable() =>
+withNullSafe(o => getByteWritable(o))
+  case _: ByteObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Byte])
+  case _: JavaHiveVarcharObjectInspector =>
+withNullSafe(
--- End diff --

Fixed, thanks.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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



[GitHub] spark pull request #15064: [SPARK-17509][SQL]When wrapping catalyst datatype...

2016-09-16 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/15064#discussion_r79212737
  
--- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala ---
@@ -238,102 +238,166 @@ private[hive] trait HiveInspectors {
 case c => throw new AnalysisException(s"Unsupported java type $c")
   }
 
+  private def withNullSafe(f: Any => Any): Any => Any = {
+input => if (input == null) null else f(input)
+  }
+
   /**
* Wraps with Hive types based on object inspector.
-   * TODO: Consolidate all hive OI/data interface code.
*/
   protected def wrapperFor(oi: ObjectInspector, dataType: DataType): Any 
=> Any = oi match {
-case _: JavaHiveVarcharObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  val s = o.asInstanceOf[UTF8String].toString
-  new HiveVarchar(s, s.length)
-} else {
-  null
-}
-
-case _: JavaHiveCharObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  val s = o.asInstanceOf[UTF8String].toString
-  new HiveChar(s, s.length)
-} else {
-  null
-}
-
-case _: JavaHiveDecimalObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  HiveDecimal.create(o.asInstanceOf[Decimal].toJavaBigDecimal)
-} else {
-  null
-}
-
-case _: JavaDateObjectInspector =>
-  (o: Any) =>
-if (o != null) {
-  DateTimeUtils.toJavaDate(o.asInstanceOf[Int])
-} else {
-  null
-}
-
-case _: JavaTimestampObjectInspector =>
+case x: ConstantObjectInspector =>
   (o: Any) =>
-if (o != null) {
-  DateTimeUtils.toJavaTimestamp(o.asInstanceOf[Long])
-} else {
-  null
-}
+x.getWritableConstantValue
+case x: PrimitiveObjectInspector => x match {
+  // TODO we don't support the HiveVarcharObjectInspector yet.
+  case _: StringObjectInspector if x.preferWritable() =>
+withNullSafe(o => getStringWritable(o))
+  case _: StringObjectInspector =>
+withNullSafe(o => o.asInstanceOf[UTF8String].toString())
+  case _: IntObjectInspector if x.preferWritable() =>
+withNullSafe(o => getIntWritable(o))
+  case _: IntObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Integer])
+  case _: BooleanObjectInspector if x.preferWritable() =>
+withNullSafe(o => getBooleanWritable(o))
+  case _: BooleanObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Boolean])
+  case _: FloatObjectInspector if x.preferWritable() =>
+withNullSafe(o => getFloatWritable(o))
+  case _: FloatObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Float])
+  case _: DoubleObjectInspector if x.preferWritable() =>
+withNullSafe(o => getDoubleWritable(o))
+  case _: DoubleObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Double])
+  case _: LongObjectInspector if x.preferWritable() =>
+withNullSafe(o => getLongWritable(o))
+  case _: LongObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Long])
+  case _: ShortObjectInspector if x.preferWritable() =>
+withNullSafe(o => getShortWritable(o))
+  case _: ShortObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Short])
+  case _: ByteObjectInspector if x.preferWritable() =>
+withNullSafe(o => getByteWritable(o))
+  case _: ByteObjectInspector =>
+withNullSafe(o => o.asInstanceOf[java.lang.Byte])
+  case _: JavaHiveVarcharObjectInspector =>
+withNullSafe(
--- End diff --

code style:
```
withNullSafe { o =>
  val s = o.asInstanceOf[UTF8String].toString
  new HiveVarchar(s, s.length)
}
```


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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



[GitHub] spark pull request #15064: [SPARK-17509][SQL]When wrapping catalyst datatype...

2016-09-16 Thread sitalkedia
Github user sitalkedia commented on a diff in the pull request:

https://github.com/apache/spark/pull/15064#discussion_r79201464
  
--- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala ---
@@ -240,74 +240,173 @@ private[hive] trait HiveInspectors {
 
   /**
* Wraps with Hive types based on object inspector.
-   * TODO: Consolidate all hive OI/data interface code.
*/
   protected def wrapperFor(oi: ObjectInspector, dataType: DataType): Any 
=> Any = oi match {
-case _: JavaHiveVarcharObjectInspector =>
+case x: ConstantObjectInspector =>
   (o: Any) =>
-if (o != null) {
-  val s = o.asInstanceOf[UTF8String].toString
-  new HiveVarchar(s, s.length)
-} else {
-  null
+x.getWritableConstantValue
+case x: PrimitiveObjectInspector => x match {
+  // TODO we don't support the HiveVarcharObjectInspector yet.
+  case _: StringObjectInspector if x.preferWritable() =>
+(o: Any) => getStringWritable(o)
+  case _: StringObjectInspector =>
+(o: Any) => if (o != null) o.asInstanceOf[UTF8String].toString() 
else null
+  case _: IntObjectInspector if x.preferWritable() =>
+(o: Any) => getIntWritable(o)
+  case _: IntObjectInspector =>
+(o: Any) => if (o != null) o.asInstanceOf[java.lang.Integer] else 
null
--- End diff --

Good point, done.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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



[GitHub] spark pull request #15064: [SPARK-17509][SQL]When wrapping catalyst datatype...

2016-09-15 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/15064#discussion_r79103035
  
--- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala ---
@@ -240,74 +240,173 @@ private[hive] trait HiveInspectors {
 
   /**
* Wraps with Hive types based on object inspector.
-   * TODO: Consolidate all hive OI/data interface code.
*/
   protected def wrapperFor(oi: ObjectInspector, dataType: DataType): Any 
=> Any = oi match {
-case _: JavaHiveVarcharObjectInspector =>
+case x: ConstantObjectInspector =>
   (o: Any) =>
-if (o != null) {
-  val s = o.asInstanceOf[UTF8String].toString
-  new HiveVarchar(s, s.length)
-} else {
-  null
+x.getWritableConstantValue
+case x: PrimitiveObjectInspector => x match {
+  // TODO we don't support the HiveVarcharObjectInspector yet.
+  case _: StringObjectInspector if x.preferWritable() =>
+(o: Any) => getStringWritable(o)
+  case _: StringObjectInspector =>
+(o: Any) => if (o != null) o.asInstanceOf[UTF8String].toString() 
else null
+  case _: IntObjectInspector if x.preferWritable() =>
+(o: Any) => getIntWritable(o)
+  case _: IntObjectInspector =>
+(o: Any) => if (o != null) o.asInstanceOf[java.lang.Integer] else 
null
--- End diff --

Let's add a helper method to abstract this null checking logic, e.g.
```
def withNullSafe(f: Any => Any): Any => Any = {
  input => if (input == null) null else f(null)
}
```


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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



[GitHub] spark pull request #15064: [SPARK-17509][SQL]When wrapping catalyst datatype...

2016-09-15 Thread cloud-fan
Github user cloud-fan commented on a diff in the pull request:

https://github.com/apache/spark/pull/15064#discussion_r79102781
  
--- Diff: 
sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveInspectors.scala ---
@@ -240,74 +240,173 @@ private[hive] trait HiveInspectors {
 
   /**
* Wraps with Hive types based on object inspector.
-   * TODO: Consolidate all hive OI/data interface code.
*/
   protected def wrapperFor(oi: ObjectInspector, dataType: DataType): Any 
=> Any = oi match {
-case _: JavaHiveVarcharObjectInspector =>
+case x: ConstantObjectInspector =>
   (o: Any) =>
-if (o != null) {
-  val s = o.asInstanceOf[UTF8String].toString
-  new HiveVarchar(s, s.length)
-} else {
-  null
+x.getWritableConstantValue
+case x: PrimitiveObjectInspector => x match {
+  // TODO we don't support the HiveVarcharObjectInspector yet.
+  case _: StringObjectInspector if x.preferWritable() =>
+(o: Any) => getStringWritable(o)
--- End diff --

in `wrap` we will only hit this branch if the input is not null, is it safe 
to skip this null checking here?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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