pig-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cheol...@apache.org
Subject svn commit: r1599268 - in /pig/trunk: CHANGES.txt src/org/apache/pig/scripting/js/JsFunction.java test/org/apache/pig/test/TestScriptUDF.java
Date Mon, 02 Jun 2014 17:07:03 GMT
Author: cheolsoo
Date: Mon Jun  2 17:07:03 2014
New Revision: 1599268

URL: http://svn.apache.org/r1599268
Log:
PIG-3969: Javascript UDF fails if no output schema is defined (lbendig via cheolsoo)

Modified:
    pig/trunk/CHANGES.txt
    pig/trunk/src/org/apache/pig/scripting/js/JsFunction.java
    pig/trunk/test/org/apache/pig/test/TestScriptUDF.java

Modified: pig/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/pig/trunk/CHANGES.txt?rev=1599268&r1=1599267&r2=1599268&view=diff
==============================================================================
--- pig/trunk/CHANGES.txt (original)
+++ pig/trunk/CHANGES.txt Mon Jun  2 17:07:03 2014
@@ -30,6 +30,8 @@ OPTIMIZATIONS
  
 BUG FIXES
 
+PIG-3969: Javascript UDF fails if no output schema is defined (lbendig via cheolsoo)
+
 PIG-3971: Pig on tez fails to run in Oozie in secure cluster (rohini)
 
 PIG-3968: OperatorPlan.serialVersionUID is not defined (daijy)

Modified: pig/trunk/src/org/apache/pig/scripting/js/JsFunction.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/scripting/js/JsFunction.java?rev=1599268&r1=1599267&r2=1599268&view=diff
==============================================================================
--- pig/trunk/src/org/apache/pig/scripting/js/JsFunction.java (original)
+++ pig/trunk/src/org/apache/pig/scripting/js/JsFunction.java Mon Jun  2 17:07:03 2014
@@ -23,7 +23,6 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.Map.Entry;
 
 import org.apache.commons.logging.Log;
@@ -38,10 +37,8 @@ import org.apache.pig.data.TupleFactory;
 import org.apache.pig.impl.logicalLayer.FrontendException;
 import org.apache.pig.impl.logicalLayer.schema.Schema;
 import org.apache.pig.impl.logicalLayer.schema.Schema.FieldSchema;
-import org.apache.pig.impl.util.UDFContext;
 import org.apache.pig.impl.util.Utils;
 import org.apache.pig.parser.ParserException;
-
 import org.mozilla.javascript.Context;
 import org.mozilla.javascript.NativeJavaObject;
 import org.mozilla.javascript.NativeObject;
@@ -175,13 +172,20 @@ public class JsFunction extends EvalFunc
     public JsFunction(String functionName) {
         this.jsScriptEngine = JsScriptEngine.getInstance();
         this.functionName = functionName;
-
-        String outputSchemaDef;
-        outputSchemaDef = jsScriptEngine.jsEval(this.getClass().getName()+"(String)", functionName+".outputSchema").toString();
-        try {
-            this.outputSchema = Utils.getSchemaFromString(outputSchemaDef);
-        } catch (ParserException e) {
-            throw new IllegalArgumentException(functionName+".outputSchema is not a valid
schema: "+e.getMessage(), e); 
+        Object outputSchemaObj = jsScriptEngine.jsEval(this.getClass().getName() + "(String)",
+                functionName + ".outputSchema");
+        //if no schema defined, fall back to bytearray
+        if (outputSchemaObj == null || outputSchemaObj instanceof Undefined) {
+            this.outputSchema = new Schema(new Schema.FieldSchema(null, DataType.BYTEARRAY));
+        }
+        else {
+            try {
+                this.outputSchema = Utils.getSchemaFromString(outputSchemaObj.toString());
+            }
+            catch (ParserException e) {
+                throw new IllegalArgumentException(functionName
+                        + ".outputSchema is not a valid schema: " + e.getMessage(), e);
+            }
         }
 
     }

Modified: pig/trunk/test/org/apache/pig/test/TestScriptUDF.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestScriptUDF.java?rev=1599268&r1=1599267&r2=1599268&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestScriptUDF.java (original)
+++ pig/trunk/test/org/apache/pig/test/TestScriptUDF.java Mon Jun  2 17:07:03 2014
@@ -23,8 +23,10 @@ import java.util.Iterator;
 import org.apache.hadoop.util.Shell;
 import org.apache.pig.PigServer;
 import org.apache.pig.data.BagFactory;
+import org.apache.pig.data.DataType;
 import org.apache.pig.data.Tuple;
 import org.apache.pig.data.TupleFactory;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
 import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.Before;
@@ -51,6 +53,9 @@ public class TestScriptUDF{
     @Test
     public void testJavascriptExampleScript() throws Exception{
         String[] script = {
+                "function simple(word) {",
+                "return word;",
+                "}",
                 "helloworld.outputSchema = \"word:chararray\";",
                 "function helloworld() {",
                 "return 'Hello, World';",
@@ -72,24 +77,34 @@ public class TestScriptUDF{
         // Test the namespace
         pigServer.registerCode("testJavascriptExampleScript.js", "javascript", "myfuncs");
         pigServer.registerQuery("A = LOAD 'table_testJavascriptExampleScript' as (a0:chararray,
a1:long);");
-        pigServer.registerQuery("B = foreach A generate myfuncs.helloworld(), myfuncs.complex($0);");
+        pigServer.registerQuery("B = foreach A generate myfuncs.simple($0), myfuncs.helloworld(),
myfuncs.complex($0);");
 
         Iterator<Tuple> iter = pigServer.openIterator("B");
         Assert.assertTrue(iter.hasNext());
         Tuple t = iter.next();
 
-        Assert.assertEquals(((Tuple)t.get(1)).get(1), 3);
+        Assert.assertEquals(((Tuple)t.get(2)).get(1), 3);
 
         Assert.assertTrue(iter.hasNext());
         t = iter.next();
 
-        Assert.assertEquals(((Tuple)t.get(1)).get(1), 3);
+        Assert.assertEquals(((Tuple)t.get(2)).get(1), 3);
 
         Assert.assertTrue(iter.hasNext());
         t = iter.next();
 
-        Assert.assertEquals(((Tuple)t.get(1)).get(1), 5);
+        Assert.assertEquals(((Tuple)t.get(2)).get(1), 5);
+
+        //test output schema
+        Schema outputSchema = pigServer.dumpSchema("B");
 
+        Assert.assertEquals(new Schema.FieldSchema(null, DataType.BYTEARRAY), outputSchema.getField(0));
+        Assert.assertEquals(new Schema.FieldSchema("word", DataType.CHARARRAY), outputSchema.getField(1));
+        
+        Schema inner = new Schema();
+        inner.add(new Schema.FieldSchema("word", DataType.CHARARRAY));
+        inner.add(new Schema.FieldSchema("num", DataType.LONG));
+        Assert.assertEquals(new Schema.FieldSchema("tuple_0", inner, DataType.TUPLE), outputSchema.getField(2));
     }
 
     // See Pig-1653 -- left here because we can't force absolute paths in e2e harness



Mime
View raw message