diff --git ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java index e184b9d..a409252 100644 --- ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java +++ ql/src/java/org/apache/hadoop/hive/ql/stats/StatsUtils.java @@ -112,6 +112,7 @@ import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils; import org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo; import org.apache.hadoop.io.BytesWritable; +import org.apache.hadoop.io.Text; import org.apache.hive.common.util.AnnotationUtils; import org.apache.tez.mapreduce.hadoop.MRJobConfig; import org.slf4j.Logger; @@ -1368,13 +1369,13 @@ public static long getSizeOfStruct(StandardConstantStructObjectInspector soi) { */ public static long getWritableSize(ObjectInspector oi, Object value) { if (oi instanceof WritableStringObjectInspector) { - WritableStringObjectInspector woi = (WritableStringObjectInspector) oi; - return JavaDataModel.get().lengthForStringOfLength( - woi.getPrimitiveWritableObject(value).getLength()); + Text textValue = ((WritableStringObjectInspector) oi).getPrimitiveWritableObject(value); + int stringLength = textValue == null ? 0 : textValue.getLength(); + return JavaDataModel.get().lengthForStringOfLength(stringLength); } else if (oi instanceof WritableBinaryObjectInspector) { - WritableBinaryObjectInspector woi = (WritableBinaryObjectInspector) oi; - return JavaDataModel.get().lengthForByteArrayOfSize( - woi.getPrimitiveWritableObject(value).getLength()); + BytesWritable bytesWritableValue = ((WritableBinaryObjectInspector) oi).getPrimitiveWritableObject(value); + int bytesWritableLength = bytesWritableValue == null ? 0 : bytesWritableValue.getLength(); + return JavaDataModel.get().lengthForByteArrayOfSize(bytesWritableLength); } else if (oi instanceof WritableBooleanObjectInspector) { return JavaDataModel.get().primitive1(); } else if (oi instanceof WritableByteObjectInspector) { diff --git ql/src/test/org/apache/hadoop/hive/ql/stats/TestStatsUtils.java ql/src/test/org/apache/hadoop/hive/ql/stats/TestStatsUtils.java index 4add290..70fd650 100644 --- ql/src/test/org/apache/hadoop/hive/ql/stats/TestStatsUtils.java +++ ql/src/test/org/apache/hadoop/hive/ql/stats/TestStatsUtils.java @@ -30,6 +30,7 @@ import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.ql.plan.ColStatistics.Range; import org.apache.hadoop.hive.serde.serdeConstants; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; import org.junit.Test; import org.spark_project.guava.collect.Sets; @@ -101,4 +102,13 @@ public void testPrimitiveSizeEstimations() throws Exception { } } + @Test + public void testCanGetSizeForWritableStringObjectInspectorWhenValueIsNull() { + StatsUtils.getWritableSize(PrimitiveObjectInspectorFactory.writableStringObjectInspector, null); + } + + @Test + public void testCanGetSizeForWritableBinaryObjectInspectorWhenValueIsNull() { + StatsUtils.getWritableSize(PrimitiveObjectInspectorFactory.writableBinaryObjectInspector, null); + } } \ No newline at end of file