You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by da...@apache.org on 2015/08/05 19:30:51 UTC

svn commit: r1694268 - in /pig/trunk: CHANGES.txt src/org/apache/pig/builtin/mock/Storage.java test/org/apache/pig/builtin/mock/TestMockStorage.java

Author: daijy
Date: Wed Aug  5 17:30:51 2015
New Revision: 1694268

URL: http://svn.apache.org/r1694268
Log:
PIG-4405: Adding 'map[]' support to mock/Storage

Modified:
    pig/trunk/CHANGES.txt
    pig/trunk/src/org/apache/pig/builtin/mock/Storage.java
    pig/trunk/test/org/apache/pig/builtin/mock/TestMockStorage.java

Modified: pig/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/pig/trunk/CHANGES.txt?rev=1694268&r1=1694267&r2=1694268&view=diff
==============================================================================
--- pig/trunk/CHANGES.txt (original)
+++ pig/trunk/CHANGES.txt Wed Aug  5 17:30:51 2015
@@ -32,6 +32,8 @@ PIG-4570: Allow AvroStorage to use a cla
 
 BUG FIXES
 
+PIG-4405: Adding 'map[]' support to mock/Storage (nielsbasjes via daijy)
+
 PIG-4636: Occurred spelled incorrectly in error message for Launcher and POMergeCogroup (stevenmz via daijy)
 
 PIG-4624: Error on ORC empty file without schema (daijy)

Modified: pig/trunk/src/org/apache/pig/builtin/mock/Storage.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/builtin/mock/Storage.java?rev=1694268&r1=1694267&r2=1694268&view=diff
==============================================================================
--- pig/trunk/src/org/apache/pig/builtin/mock/Storage.java (original)
+++ pig/trunk/src/org/apache/pig/builtin/mock/Storage.java Wed Aug  5 17:30:51 2015
@@ -75,7 +75,9 @@ import org.apache.pig.parser.ParserExcep
  *      data.set("foo",
  *      tuple("a"),
  *      tuple("b"),
- *      tuple("c")
+ *      tuple("c"),
+ *      tuple(map("d","e", "f","g")),
+ *      tuple(bag(tuple("h"),tuple("i")))
  *      );
  *
  *  pigServer.registerQuery("A = LOAD 'foo' USING mock.Storage();");
@@ -86,6 +88,8 @@ import org.apache.pig.parser.ParserExcep
  *  assertEquals(tuple("a"), out.get(0));
  *  assertEquals(tuple("b"), out.get(1));
  *  assertEquals(tuple("c"), out.get(2));
+ *  assertEquals(tuple(map("f", "g", "d", "e" )), out.get(3));
+ *  assertEquals(tuple(bag(tuple("h"),tuple("i"))), out.get(4));
  * </pre>
  * With Schema:
  *  <pre>
@@ -135,6 +139,36 @@ public class Storage extends LoadFunc im
   }
 
   /**
+   * @param input These params are alternating "key", "value". So the number of params MUST be even !!
+   * Implementation is very similar to the TOMAP UDF.
+   * So map("A", B, "C", D) generates a map "A"->B, "C"->D
+   * @return a map containing the provided objects
+   */
+  public static Map<String, Object> map(Object... input) {
+    if (input == null || input.length < 2) {
+      return null;
+    }
+
+    try {
+      Map<String, Object> output = new HashMap<String, Object>();
+
+      for (int i = 0; i < input.length; i=i+2) {
+        String key = (String)input[i];
+        Object val = input[i+1];
+        output.put(key, val);
+      }
+
+      return output;
+    } catch (ClassCastException e){
+      throw new IllegalArgumentException("Map key must be a String");
+    } catch (ArrayIndexOutOfBoundsException e){
+      throw new IllegalArgumentException("Function input must have even number of parameters");
+    } catch (Exception e) {
+      throw new RuntimeException("Error while creating a map", e);
+    }
+  }
+
+  /**
    * @param schema
    * @return the schema represented by the string
    * @throws ParserException if the schema is invalid

Modified: pig/trunk/test/org/apache/pig/builtin/mock/TestMockStorage.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/builtin/mock/TestMockStorage.java?rev=1694268&r1=1694267&r2=1694268&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/builtin/mock/TestMockStorage.java (original)
+++ pig/trunk/test/org/apache/pig/builtin/mock/TestMockStorage.java Wed Aug  5 17:30:51 2015
@@ -17,13 +17,15 @@
  */
 package org.apache.pig.builtin.mock;
 
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.assertFalse;
-import static junit.framework.Assert.fail;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
 import static org.apache.pig.builtin.mock.Storage.resetData;
 import static org.apache.pig.builtin.mock.Storage.schema;
 import static org.apache.pig.builtin.mock.Storage.tuple;
+import static org.apache.pig.builtin.mock.Storage.bag;
+import static org.apache.pig.builtin.mock.Storage.map;
 
 import java.util.HashSet;
 import java.util.List;
@@ -47,7 +49,9 @@ public class TestMockStorage {
     data.set("foo",
         tuple("a"),
         tuple("b"),
-        tuple("c")
+        tuple("c"),
+        tuple(map("d","e", "f","g")),
+        tuple(bag(tuple("h"),tuple("i")))
         );
 
     pigServer.registerQuery("A = LOAD 'foo' USING mock.Storage();");
@@ -57,8 +61,10 @@ public class TestMockStorage {
     assertEquals(tuple("a"), out.get(0));
     assertEquals(tuple("b"), out.get(1));
     assertEquals(tuple("c"), out.get(2));
+    assertEquals(tuple(map("f", "g", "d", "e" )), out.get(3));
+    assertEquals(tuple(bag(tuple("h"),tuple("i"))), out.get(4));
   }
-  
+
   @Test
   public void testMockSchema() throws Exception {
     PigServer pigServer = new PigServer(Util.getLocalTestMode());