Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix bug of constant null value for ObjectInspector
  • Loading branch information
chenghao-intel committed Nov 5, 2014
commit f54f3694ab8788d74f6af8cda87392e38e68e440
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private[hive] trait HiveInspectors {
* @return convert the data into catalyst type
*/
def unwrap(data: Any, oi: ObjectInspector): Any = oi match {
case _ if data == null => null
case hvoi: HiveVarcharObjectInspector =>
if (data == null) null else hvoi.getPrimitiveJavaObject(data).getValue
case hdoi: HiveDecimalObjectInspector =>
Expand Down Expand Up @@ -254,46 +255,59 @@ private[hive] trait HiveInspectors {
}

def toInspector(expr: Expression): ObjectInspector = expr match {
case Literal(value: String, StringType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Int, IntegerType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Double, DoubleType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Boolean, BooleanType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Long, LongType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Float, FloatType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Short, ShortType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Byte, ByteType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Array[Byte], BinaryType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: java.sql.Date, DateType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: java.sql.Timestamp, TimestampType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: BigDecimal, DecimalType()) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value)
case Literal(value: Decimal, DecimalType()) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.toBigDecimal)
case Literal(value, StringType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[String])
case Literal(value, IntegerType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Int])
case Literal(value, DoubleType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Double])
case Literal(value, BooleanType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Boolean])
case Literal(value, LongType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Long])
case Literal(value, FloatType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Float])
case Literal(value, ShortType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Short])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the cast here? This destroys null values:

scala> null.asInstanceOf[Short]
res0: Short = 0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thanks, I thought it will be considered as Boxed primitives.

case Literal(value, ByteType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Byte])
case Literal(value, BinaryType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[Array[Byte]])
case Literal(value, DateType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[java.sql.Date])
case Literal(value, TimestampType) =>
HiveShim.getPrimitiveWritableConstantObjectInspector(value.asInstanceOf[java.sql.Timestamp])
case Literal(value, DecimalType()) =>
if (null == value) {
HiveShim.getPrimitiveWritableConstantObjectInspector(
null.asInstanceOf[BigDecimal])
} else {
HiveShim.getPrimitiveWritableConstantObjectInspector(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: why remove this to only have to add a more verbose .asInstanceOf below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't bind a type here, just to keep consistent with pattern match above, and it is also helpful in debugging, you know, sometimes we just confused what type for catalyst array (Array or Seq), the .asInstanceOf will be a strong check.

value.asInstanceOf[Decimal].toBigDecimal)
}
case Literal(_, NullType) =>
HiveShim.getPrimitiveNullWritableConstantObjectInspector
case Literal(value: Seq[_], ArrayType(dt, _)) =>
case Literal(value, ArrayType(dt, _)) =>
val listObjectInspector = toInspector(dt)
val list = new java.util.ArrayList[Object]()
value.foreach(v => list.add(wrap(v, listObjectInspector)))
ObjectInspectorFactory.getStandardConstantListObjectInspector(listObjectInspector, list)
case Literal(map: Map[_, _], MapType(keyType, valueType, _)) =>
val value = new java.util.HashMap[Object, Object]()
if (value == null) {
ObjectInspectorFactory.getStandardConstantListObjectInspector(listObjectInspector, null)
} else {
val list = new java.util.ArrayList[Object]()
value.asInstanceOf[Seq[_]].foreach(v => list.add(wrap(v, listObjectInspector)))
ObjectInspectorFactory.getStandardConstantListObjectInspector(listObjectInspector, list)
}
case Literal(value, MapType(keyType, valueType, _)) =>
val keyOI = toInspector(keyType)
val valueOI = toInspector(valueType)
map.foreach (entry => value.put(wrap(entry._1, keyOI), wrap(entry._2, valueOI)))
ObjectInspectorFactory.getStandardConstantMapObjectInspector(keyOI, valueOI, value)
case Literal(_, dt) => sys.error(s"Hive doesn't support the constant type [$dt].")
if (value == null) {
ObjectInspectorFactory.getStandardConstantMapObjectInspector(keyOI, valueOI, null)
} else {
val map = new java.util.HashMap[Object, Object]()
value.asInstanceOf[Map[_, _]].foreach (entry => {
map.put(wrap(entry._1, keyOI), wrap(entry._2, valueOI))
})
ObjectInspectorFactory.getStandardConstantMapObjectInspector(keyOI, valueOI, map)
}
case _ => toInspector(expr.dataType)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 NULL 1 NULL 1.0 NULL true NULL 1 NULL 1.0 NULL 1 NULL 1 NULL 1 NULL 1970-01-01 NULL 1970-01-01 08:00:00.001 NULL 1 NULL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
There is no documentation for function 'if'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
There is no documentation for function 'if'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 1 1 1 NULL 2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
128 1.1 ABC 12.3
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,34 @@ case class TestData(a: Int, b: String)
* A set of test cases expressed in Hive QL that are not covered by the tests included in the hive distribution.
*/
class HiveQuerySuite extends HiveComparisonTest {
createQueryTest("constant null testing",
"""SELECT
|IF(FALSE, CAST(NULL AS STRING), CAST(1 AS STRING)) AS COL1,
|IF(TRUE, CAST(NULL AS STRING), CAST(1 AS STRING)) AS COL2,
|IF(FALSE, CAST(NULL AS INT), CAST(1 AS INT)) AS COL3,
|IF(TRUE, CAST(NULL AS INT), CAST(1 AS INT)) AS COL4,
|IF(FALSE, CAST(NULL AS DOUBLE), CAST(1 AS DOUBLE)) AS COL5,
|IF(TRUE, CAST(NULL AS DOUBLE), CAST(1 AS DOUBLE)) AS COL6,
|IF(FALSE, CAST(NULL AS BOOLEAN), CAST(1 AS BOOLEAN)) AS COL7,
|IF(TRUE, CAST(NULL AS BOOLEAN), CAST(1 AS BOOLEAN)) AS COL8,
|IF(FALSE, CAST(NULL AS BIGINT), CAST(1 AS BIGINT)) AS COL9,
|IF(TRUE, CAST(NULL AS BIGINT), CAST(1 AS BIGINT)) AS COL10,
|IF(FALSE, CAST(NULL AS FLOAT), CAST(1 AS FLOAT)) AS COL11,
|IF(TRUE, CAST(NULL AS FLOAT), CAST(1 AS FLOAT)) AS COL12,
|IF(FALSE, CAST(NULL AS SMALLINT), CAST(1 AS SMALLINT)) AS COL13,
|IF(TRUE, CAST(NULL AS SMALLINT), CAST(1 AS SMALLINT)) AS COL14,
|IF(FALSE, CAST(NULL AS TINYINT), CAST(1 AS TINYINT)) AS COL15,
|IF(TRUE, CAST(NULL AS TINYINT), CAST(1 AS TINYINT)) AS COL16,
|IF(FALSE, CAST(NULL AS BINARY), CAST("1" AS BINARY)) AS COL17,
|IF(TRUE, CAST(NULL AS BINARY), CAST("1" AS BINARY)) AS COL18,
|IF(FALSE, CAST(NULL AS DATE), CAST("1970-01-01" AS DATE)) AS COL19,
|IF(TRUE, CAST(NULL AS DATE), CAST("1970-01-01" AS DATE)) AS COL20,
|IF(FALSE, CAST(NULL AS TIMESTAMP), CAST(1 AS TIMESTAMP)) AS COL21,
|IF(TRUE, CAST(NULL AS TIMESTAMP), CAST(1 AS TIMESTAMP)) AS COL22,
|IF(FALSE, CAST(NULL AS DECIMAL), CAST(1 AS DECIMAL)) AS COL23,
|IF(TRUE, CAST(NULL AS DECIMAL), CAST(1 AS DECIMAL)) AS COL24
|FROM src LIMIT 1""".stripMargin)

createQueryTest("constant array",
"""
|SELECT sort_array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,52 +59,57 @@ private[hive] object HiveShim {

def getPrimitiveWritableConstantObjectInspector(value: String): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.STRING, new hadoopIo.Text(value))
PrimitiveCategory.STRING, if (value == null) null else new hadoopIo.Text(value))

def getPrimitiveWritableConstantObjectInspector(value: Int): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.INT, new hadoopIo.IntWritable(value))
PrimitiveCategory.INT, if (value == null) null else new hadoopIo.IntWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Double): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.DOUBLE, new hiveIo.DoubleWritable(value))
PrimitiveCategory.DOUBLE, if (value == null) null else new hiveIo.DoubleWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Boolean): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.BOOLEAN, new hadoopIo.BooleanWritable(value))
PrimitiveCategory.BOOLEAN, if (value == null) null else new hadoopIo.BooleanWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Long): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.LONG, new hadoopIo.LongWritable(value))
PrimitiveCategory.LONG, if (value == null) null else new hadoopIo.LongWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Float): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.FLOAT, new hadoopIo.FloatWritable(value))
PrimitiveCategory.FLOAT, if (value == null) null else new hadoopIo.FloatWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Short): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.SHORT, new hiveIo.ShortWritable(value))
PrimitiveCategory.SHORT, if (value == null) null else new hiveIo.ShortWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Byte): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.BYTE, new hiveIo.ByteWritable(value))
PrimitiveCategory.BYTE, if (value == null) null else new hiveIo.ByteWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Array[Byte]): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.BINARY, new hadoopIo.BytesWritable(value))
PrimitiveCategory.BINARY, if (value == null) null else new hadoopIo.BytesWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: java.sql.Date): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.DATE, new hiveIo.DateWritable(value))
PrimitiveCategory.DATE, if (value == null) null else new hiveIo.DateWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: java.sql.Timestamp): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.TIMESTAMP, new hiveIo.TimestampWritable(value))
PrimitiveCategory.TIMESTAMP,
if (value == null) null else new hiveIo.TimestampWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: BigDecimal): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
PrimitiveCategory.DECIMAL,
new hiveIo.HiveDecimalWritable(HiveShim.createDecimal(value.underlying())))
if (value == null) {
null
} else {
new hiveIo.HiveDecimalWritable(HiveShim.createDecimal(value.underlying()))
})

def getPrimitiveNullWritableConstantObjectInspector: ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,52 +58,76 @@ private[hive] object HiveShim {

def getPrimitiveWritableConstantObjectInspector(value: String): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.stringTypeInfo, new hadoopIo.Text(value))
TypeInfoFactory.stringTypeInfo, if (value == null) null else new hadoopIo.Text(value))

def getPrimitiveWritableConstantObjectInspector(value: Int): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.intTypeInfo, new hadoopIo.IntWritable(value))
TypeInfoFactory.intTypeInfo, if (value == null) null else new hadoopIo.IntWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Double): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.doubleTypeInfo, new hiveIo.DoubleWritable(value))
TypeInfoFactory.doubleTypeInfo, if (value == null) {
null
} else {
new hiveIo.DoubleWritable(value)
})

def getPrimitiveWritableConstantObjectInspector(value: Boolean): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.booleanTypeInfo, new hadoopIo.BooleanWritable(value))
TypeInfoFactory.booleanTypeInfo, if (value == null) {
null
} else {
new hadoopIo.BooleanWritable(value)
})

def getPrimitiveWritableConstantObjectInspector(value: Long): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.longTypeInfo, new hadoopIo.LongWritable(value))
TypeInfoFactory.longTypeInfo, if (value == null) null else new hadoopIo.LongWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Float): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.floatTypeInfo, new hadoopIo.FloatWritable(value))
TypeInfoFactory.floatTypeInfo, if (value == null) {
null
} else {
new hadoopIo.FloatWritable(value)
})

def getPrimitiveWritableConstantObjectInspector(value: Short): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.shortTypeInfo, new hiveIo.ShortWritable(value))
TypeInfoFactory.shortTypeInfo, if (value == null) null else new hiveIo.ShortWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Byte): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.byteTypeInfo, new hiveIo.ByteWritable(value))
TypeInfoFactory.byteTypeInfo, if (value == null) null else new hiveIo.ByteWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: Array[Byte]): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.binaryTypeInfo, new hadoopIo.BytesWritable(value))
TypeInfoFactory.binaryTypeInfo, if (value == null) {
null
} else {
new hadoopIo.BytesWritable(value)
})

def getPrimitiveWritableConstantObjectInspector(value: java.sql.Date): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.dateTypeInfo, new hiveIo.DateWritable(value))
TypeInfoFactory.dateTypeInfo, if (value == null) null else new hiveIo.DateWritable(value))

def getPrimitiveWritableConstantObjectInspector(value: java.sql.Timestamp): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.timestampTypeInfo, new hiveIo.TimestampWritable(value))
TypeInfoFactory.timestampTypeInfo, if (value == null) {
null
} else {
new hiveIo.TimestampWritable(value)
})

def getPrimitiveWritableConstantObjectInspector(value: BigDecimal): ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
TypeInfoFactory.decimalTypeInfo,
new hiveIo.HiveDecimalWritable(HiveShim.createDecimal(value.underlying())))
if (value == null) {
null
} else {
new hiveIo.HiveDecimalWritable(HiveShim.createDecimal(value.underlying()))
})

def getPrimitiveNullWritableConstantObjectInspector: ObjectInspector =
PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(
Expand Down