Index: serde/src/test/org/apache/hadoop/hive/serde2/lazybinary/TestLazyBinarySerDe.java =================================================================== --- serde/src/test/org/apache/hadoop/hive/serde2/lazybinary/TestLazyBinarySerDe.java (revision 1352373) +++ serde/src/test/org/apache/hadoop/hive/serde2/lazybinary/TestLazyBinarySerDe.java (working copy) @@ -18,6 +18,7 @@ package org.apache.hadoop.hive.serde2.lazybinary; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; @@ -35,12 +36,17 @@ import org.apache.hadoop.hive.serde2.binarysortable.MyTestInnerStruct; import org.apache.hadoop.hive.serde2.binarysortable.TestBinarySortableSerDe; import org.apache.hadoop.hive.serde2.lazy.ByteArrayRef; +import org.apache.hadoop.hive.serde2.lazy.LazyBinary; +import org.apache.hadoop.hive.serde2.lazy.LazyFactory; +import org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.AbstractPrimitiveLazyObjectInspector; +import org.apache.hadoop.hive.serde2.lazy.objectinspector.primitive.LazyPrimitiveObjectInspectorFactory; import org.apache.hadoop.hive.serde2.lazybinary.objectinspector.LazyBinaryMapObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory.ObjectInspectorOptions; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory; import org.apache.hadoop.hive.serde2.objectinspector.StructField; import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector; import org.apache.hadoop.io.BytesWritable; @@ -53,7 +59,7 @@ /** * Generate a random struct array. - * + * * @param r * random number generator * @return an struct array @@ -71,7 +77,7 @@ /** * Initialize the LazyBinarySerDe. - * + * * @param fieldNames * table field names * @param fieldTypes @@ -91,7 +97,7 @@ /** * Test the LazyBinarySerDe. - * + * * @param rows * array of structs to be serialized * @param rowOI @@ -134,7 +140,7 @@ * Compare two structs that have different number of fields. We just compare * the first few common fields, ignoring the fields existing in one struct but * not the other. - * + * * @see ObjectInspectorUtils#compare(Object, ObjectInspector, Object, * ObjectInspector) */ @@ -464,7 +470,7 @@ /** * The test entrance function. - * + * * @throws Throwable */ public void testLazyBinarySerDe() throws Throwable { @@ -522,4 +528,36 @@ throw e; } } + + + /** + * Test to see if ByteArrayRef with correct contents is generated by + * LazyBinaryObjectInspector from input BytesWritable + * @throws Throwable + */ + public void testLazyBinaryObjectInspector() throws Throwable { + + //create input ByteArrayRef + byte[] inpBArray = {'1','\u0001','3','4'}; + ByteArrayRef inpBARef = new ByteArrayRef(); + inpBARef.setData(inpBArray); + + AbstractPrimitiveLazyObjectInspector binInspector = LazyPrimitiveObjectInspectorFactory + .getLazyObjectInspector(PrimitiveCategory.BINARY, false, (byte)0); + + //create LazyBinary initialed with inputBA + LazyBinary lazyBin = (LazyBinary) LazyFactory.createLazyObject(binInspector); + lazyBin.init(inpBARef, 0, inpBArray.length); + + //use inspector to get a ByteArrayRef out of LazyBinary + ByteArrayRef outBARef = (ByteArrayRef) binInspector.getPrimitiveJavaObject(lazyBin); + + System.out.println("input ba " + Arrays.toString(inpBArray)); + System.out.println("outpu ba " + Arrays.toString(outBARef.getData())); + + assertTrue("compare input and output BAs", + Arrays.equals(inpBArray, outBARef.getData())); + + } + } Index: serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/primitive/LazyBinaryObjectInspector.java =================================================================== --- serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/primitive/LazyBinaryObjectInspector.java (revision 1352373) +++ serde/src/java/org/apache/hadoop/hive/serde2/lazy/objectinspector/primitive/LazyBinaryObjectInspector.java (working copy) @@ -43,7 +43,10 @@ return null; } ByteArrayRef ba = new ByteArrayRef(); - ba.setData(((LazyBinary) o).getWritableObject().getBytes()); + BytesWritable bWritable = ((LazyBinary) o).getWritableObject(); + byte[] data = new byte[bWritable.getLength()]; + System.arraycopy(bWritable.getBytes(), 0, data, 0, bWritable.getLength()); + ba.setData(data); return ba; }