Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

SetIntersect shouldn't throw NPEs when either of the input bags are empty #22

Merged
merged 1 commit into from

2 participants

njwhite Matt Hayes
njwhite

No description provided.

Matt Hayes matthayes merged commit 4957df6 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 10, 2013
This page is out of date. Refresh to see the latest.
28 src/java/datafu/pig/bags/sets/SetIntersect.java
View
@@ -1,19 +1,19 @@
/*
* Copyright 2010 LinkedIn, Inc
- *
+ *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
-
+
package datafu.pig.bags.sets;
import java.io.IOException;
@@ -32,7 +32,7 @@
* <pre>
* {@code
* define SetIntersect datafu.pig.bags.sets.SetIntersect();
- *
+ *
* -- input:
* -- ({(1,10),(2,20),(3,30),(4,40)},{(2,20),(4,40),(8,80)})
* input = LOAD 'input' AS (B1:bag{T:tuple(val1:int,val2:int)},B2:bag{T:tuple(val1:int,val2:int)});
@@ -50,9 +50,8 @@
public class SetIntersect extends SetOperationsBase
{
private static final BagFactory bagFactory = BagFactory.getInstance();
- private static final TupleFactory tupleFactory = TupleFactory.getInstance();
- class pair implements Comparable<pair>
+ static class pair implements Comparable<pair>
{
final Iterator<Tuple> it;
Tuple data;
@@ -62,7 +61,7 @@
this.it = it;
this.data = it.next();
}
-
+
@Override
public int compareTo(pair o)
{
@@ -78,8 +77,9 @@ public int compareTo(pair o)
Object o = input.get(i);
if (!(o instanceof DataBag))
throw new RuntimeException("parameters must be databags");
- DataBag inputBag = (DataBag) o;
- pq.add(new pair(inputBag.iterator()));
+ Iterator<Tuple> inputIterator= ((DataBag) o).iterator();
+ if(inputIterator.hasNext())
+ pq.add(new pair(inputIterator));
}
return pq;
}
@@ -93,20 +93,22 @@ public boolean all_equal(PriorityQueue<pair> pq)
}
return true;
}
-
+
@Override
public DataBag exec(Tuple input) throws IOException
{
DataBag outputBag = bagFactory.newDefaultBag();
PriorityQueue<pair> pq = load_bags(input);
+ if(pq.size() != input.size())
+ return outputBag; // one or more input bags were empty
Tuple last_data = null;
-
+
while (true) {
if (pq.peek().data.compareTo(last_data) != 0 && all_equal(pq)) {
last_data = pq.peek().data;
outputBag.add(last_data);
}
-
+
pair p = pq.poll();
if (!p.it.hasNext())
break;
18 test/pig/datafu/test/pig/bags/sets/SetTests.java
View
@@ -1,8 +1,16 @@
package datafu.test.pig.bags.sets;
+import java.util.Arrays;
+
+import org.apache.pig.data.BagFactory;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.TupleFactory;
import org.apache.pig.pigunit.PigTest;
+import org.testng.Assert;
import org.testng.annotations.Test;
+import datafu.pig.bags.sets.SetIntersect;
import datafu.test.pig.PigTests;
public class SetTests extends PigTests
@@ -25,6 +33,16 @@ public void setIntersectTest() throws Exception
test.assertOutput("data",input,"data2",output);
}
+ @Test
+ public void testIntersectWithNullTuples() throws Exception {
+ DataBag one = BagFactory.getInstance().newDefaultBag();
+ DataBag two = BagFactory.getInstance().newDefaultBag();
+
+ Tuple input = TupleFactory.getInstance().newTuple(Arrays.asList(one, two));
+ DataBag output = new SetIntersect().exec(input);
+ Assert.assertEquals(0, output.size());
+ }
+
@Test(expectedExceptions=org.apache.pig.impl.logicalLayer.FrontendException.class)
public void setIntersectOutOfOrderTest() throws Exception
{
Something went wrong with that request. Please try again.