Author: olga
Date: Thu Dec  4 11:24:59 2008
New Revision: 723402

URL: http://svn.apache.org/viewvc?rev=723402&view=rev
Log:
PIG-385: null const handling

Modified:
    hadoop/pig/branches/types/CHANGES.txt
    hadoop/pig/branches/types/src/org/apache/pig/data/DataType.java
    
hadoop/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt
    hadoop/pig/branches/types/test/org/apache/pig/test/TestNullConstant.java

Modified: hadoop/pig/branches/types/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/pig/branches/types/CHANGES.txt?rev=723402&r1=723401&r2=723402&view=diff
==============================================================================
--- hadoop/pig/branches/types/CHANGES.txt (original)
+++ hadoop/pig/branches/types/CHANGES.txt Thu Dec  4 11:24:59 2008
@@ -322,4 +322,6 @@
     PIG-537: Failure in Hadoop map collect stage due to type mismatch in the
     keys used in cogroup (pradeepk vi olgan)
 
-    PIG-538: support for null constants
+    PIG-538: support for null constants (pradeepk via olgan)
+
+    PIG-385: more null handling (pradeepl via olgan)

Modified: hadoop/pig/branches/types/src/org/apache/pig/data/DataType.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/branches/types/src/org/apache/pig/data/DataType.java?rev=723402&r1=723401&r2=723402&view=diff
==============================================================================
--- hadoop/pig/branches/types/src/org/apache/pig/data/DataType.java (original)
+++ hadoop/pig/branches/types/src/org/apache/pig/data/DataType.java Thu Dec  4 
11:24:59 2008
@@ -694,7 +694,9 @@
             }
             sb.append(o.toString());
             sb.append("#");
-            sb.append(m.get(o).toString());
+            if(m.get(o) != null) {
+                sb.append(m.get(o).toString());
+            }
         }
         sb.append("]");
         return sb.toString();

Modified: 
hadoop/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt
URL: 
http://svn.apache.org/viewvc/hadoop/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt?rev=723402&r1=723401&r2=723402&view=diff
==============================================================================
--- 
hadoop/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt
 (original)
+++ 
hadoop/pig/branches/types/src/org/apache/pig/impl/logicalLayer/parser/QueryParser.jjt
 Thu Dec  4 11:24:59 2008
@@ -3091,6 +3091,8 @@
 {
        (key = AtomDatum() "#" value = Datum())
        {
+               if(key == null)
+                   throw new ParseException("key in a map cannot be null 
(provided input has '"+ key + "#" + value + "')");
                keyValues.put(key, value);
                log.trace("Exiting KeyValuePair");
        }

Modified: 
hadoop/pig/branches/types/test/org/apache/pig/test/TestNullConstant.java
URL: 
http://svn.apache.org/viewvc/hadoop/pig/branches/types/test/org/apache/pig/test/TestNullConstant.java?rev=723402&r1=723401&r2=723402&view=diff
==============================================================================
--- hadoop/pig/branches/types/test/org/apache/pig/test/TestNullConstant.java 
(original)
+++ hadoop/pig/branches/types/test/org/apache/pig/test/TestNullConstant.java 
Thu Dec  4 11:24:59 2008
@@ -7,12 +7,14 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
+import java.util.Map;
 
 import junit.framework.TestCase;
 
 import org.apache.pig.ExecType;
 import org.apache.pig.PigServer;
 import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.data.DataBag;
 import org.apache.pig.data.Tuple;
 import org.junit.Before;
 import org.junit.Test;
@@ -122,4 +124,47 @@
         }
     }
 
+    @Test
+    public void testExplicitCast() throws IOException, ExecException {
+        File input = Util.createInputFile("tmp", "", 
+                new String[] {"10\t11.0\tstring"});
+        pigServer.registerQuery("a = load 'file:" + 
Util.encodeEscape(input.toString()) + "' as (x:int, y:double, str:chararray);");
+        pigServer.registerQuery("b = foreach a generate (int)null, 
(double)null, (chararray)null, (map[])null;");
+        Iterator<Tuple> it = pigServer.openIterator("b");
+        Tuple t = it.next();
+        for (int i = 0; i < 3; i++) {
+            assertEquals(null, t.get(i));
+        }
+    }
+    
+    @Test
+    public void testComplexNullConstants() throws IOException, ExecException {
+        File input = Util.createInputFile("tmp", "", 
+                new String[] {"10\t11.0\tstring"});
+        pigServer.registerQuery("a = load 'file:" + 
Util.encodeEscape(input.toString()) + "' as (x:int, y:double, str:chararray);");
+        pigServer.registerQuery("b = foreach a generate {(null)}, 
['2'#null];");
+        Iterator<Tuple> it = pigServer.openIterator("b");
+        Tuple t = it.next();
+        assertEquals(null, ((DataBag)t.get(0)).iterator().next().get(0));
+        assertEquals(null, ((Map<Object, Object>)t.get(1)).get("2"));
+        
+    }
+
+    @Test
+    public void testMapNullKeyFailure() throws IOException {
+        File input;
+        input = Util.createInputFile("tmp", "", 
+                new String[] {"10\t11.0\tstring"});
+        pigServer.registerQuery("a = load 'file:" + 
Util.encodeEscape(input.toString()) + "' as (x:int, y:double, str:chararray);");
+
+        boolean exceptionOccured = false;
+        try {
+            pigServer.registerQuery("b = foreach a generate [null#'2'];");
+        } catch(Exception e) {
+            exceptionOccured = true;
+            String msg = e.getMessage();
+            assertTrue(msg.contains("key in a map cannot be null"));
+        }
+        if(!exceptionOccured) fail();        
+    }
 }


Reply via email to