PIG-5383: OrcStorage fails when "bytearray" represents unknown type (knoguchi)


git-svn-id: https://svn.apache.org/repos/asf/pig/trunk@1855282 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/CHANGES.txt b/CHANGES.txt
index ceba83d..0dba9b8 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -87,6 +87,9 @@
 OPTIMIZATIONS
  
 BUG FIXES
+
+PIG-5383: OrcStorage fails when "bytearray" represents unknown type (knoguchi)
+
 PIG-5372: SAMPLE/RANDOM(udf) before skewed join failing with NPE (knoguchi)
 
 PIG-5374: Use CircularFifoBuffer in InterRecordReader (szita)
diff --git a/src/org/apache/pig/impl/util/hive/HiveUtils.java b/src/org/apache/pig/impl/util/hive/HiveUtils.java
index f80effd..3691cd0 100644
--- a/src/org/apache/pig/impl/util/hive/HiveUtils.java
+++ b/src/org/apache/pig/impl/util/hive/HiveUtils.java
@@ -658,9 +658,13 @@
 
         @Override
         public BytesWritable getPrimitiveWritableObject(Object o) {
-            return o == null ? null : (o instanceof DataByteArray
+            try {
+                return o == null ? null : (o instanceof DataByteArray
                             ? new BytesWritable(((DataByteArray) o).get())
-                            : new BytesWritable((byte[]) o));
+                            : new BytesWritable((byte[]) DataType.toBytes(o)));
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
         }
 
         @Override
diff --git a/test/org/apache/pig/builtin/TestOrcStorage.java b/test/org/apache/pig/builtin/TestOrcStorage.java
index e881481..ddff101 100644
--- a/test/org/apache/pig/builtin/TestOrcStorage.java
+++ b/test/org/apache/pig/builtin/TestOrcStorage.java
@@ -20,6 +20,8 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.apache.pig.builtin.mock.Storage.resetData;
+import static org.apache.pig.builtin.mock.Storage.tuple;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -57,6 +59,7 @@
 import org.apache.hadoop.io.Text;
 import org.apache.pig.PigServer;
 import org.apache.pig.backend.hadoop.datastorage.ConfigurationUtil;
+import org.apache.pig.builtin.mock.Storage.Data;
 import org.apache.pig.data.BinSedesTuple;
 import org.apache.pig.data.DataByteArray;
 import org.apache.pig.data.DataType;
@@ -283,6 +286,28 @@
         assertTrue(t.toString().endsWith(",12345678.6547456)"));
     }
 
+    @Test
+    // See PIG-5383
+    public void testByteArrayStore() throws Exception {
+        Data data = resetData(pigServer);
+        data.set("foo", "intButTypeByteArray:bytearray",
+            tuple(1),
+            tuple(2),
+            tuple(3)
+        );
+
+        // Emunlating the case when "bytearray" represents an unknown type
+
+        pigServer.registerQuery("A = LOAD 'foo' USING mock.Storage();");
+        pigServer.registerQuery("store A into '" + OUTPUT1 + "' using OrcStorage();");
+        pigServer.registerQuery("B = load '" + OUTPUT1 + "' using OrcStorage();");
+        Iterator <Tuple> iter = pigServer.openIterator("B");
+        assertEquals(Integer.valueOf(1), DataType.toInteger(iter.next().get(0), DataType.BYTEARRAY));
+        assertEquals(Integer.valueOf(2), DataType.toInteger(iter.next().get(0), DataType.BYTEARRAY));
+        assertEquals(Integer.valueOf(3), DataType.toInteger(iter.next().get(0), DataType.BYTEARRAY));
+        assertFalse(iter.hasNext());
+    }
+
     private void verifyData(Path orcFile, Iterator<Tuple> iter, FileSystem fs, int expectedTotalRows) throws Exception {
 
         int expectedRows = 0;