You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by an...@apache.org on 2013/09/20 20:50:51 UTC

svn commit: r1525096 - in /pig/trunk: src/org/apache/pig/builtin/Assert.java test/org/apache/pig/test/TestAssert.java

Author: aniket486
Date: Fri Sep 20 18:50:50 2013
New Revision: 1525096

URL: http://svn.apache.org/r1525096
Log:
PIG-3410: LimitOptimizer is applied before PartitionFilterOptimizer - missing files (aniket486)

Added:
    pig/trunk/src/org/apache/pig/builtin/Assert.java
    pig/trunk/test/org/apache/pig/test/TestAssert.java

Added: pig/trunk/src/org/apache/pig/builtin/Assert.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/builtin/Assert.java?rev=1525096&view=auto
==============================================================================
--- pig/trunk/src/org/apache/pig/builtin/Assert.java (added)
+++ pig/trunk/src/org/apache/pig/builtin/Assert.java Fri Sep 20 18:50:50 2013
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.pig.builtin;
+
+import java.io.IOException;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FilterFunc;
+import org.apache.pig.data.Tuple;
+
+/**
+ * 
+ *
+ */
+public class Assert extends EvalFunc<Boolean>
+{
+  @Override
+  public Boolean exec(Tuple tuple)
+      throws IOException
+  {
+    if (!(Boolean) tuple.get(0)) {
+      if (tuple.size() > 1) {
+        throw new IOException("Assertion violated: " + tuple.get(1).toString());
+      }
+      else {
+        throw new IOException("Assertion violated. ");
+      }
+    }
+    else {
+      return true;
+    }
+  }
+}

Added: pig/trunk/test/org/apache/pig/test/TestAssert.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestAssert.java?rev=1525096&view=auto
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestAssert.java (added)
+++ pig/trunk/test/org/apache/pig/test/TestAssert.java Fri Sep 20 18:50:50 2013
@@ -0,0 +1,75 @@
+package org.apache.pig.test;
+
+import static junit.framework.Assert.assertEquals;
+import static org.apache.pig.builtin.mock.Storage.resetData;
+import static org.apache.pig.builtin.mock.Storage.tuple;
+
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.apache.pig.ExecType;
+import org.apache.pig.PigServer;
+import org.apache.pig.builtin.mock.Storage.Data;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.junit.Test;
+
+public class TestAssert {
+
+  /**
+   * Verify that ASSERT operator works
+   * @throws Exception
+   */
+  @Test
+  public void testPositive() throws Exception {
+      PigServer pigServer = new PigServer(ExecType.LOCAL);
+      Data data = resetData(pigServer);
+
+      data.set("foo",
+              tuple(1),
+              tuple(2),
+              tuple(3)
+              );
+
+      pigServer.setBatchOn();
+      pigServer.registerQuery("A = LOAD 'foo' USING mock.Storage() AS (i:int);");
+      pigServer.registerQuery("ASSERT A BY i > 0;");
+      pigServer.registerQuery("STORE A INTO 'bar' USING mock.Storage();");
+      
+      pigServer.executeBatch();
+
+      List<Tuple> out = data.get("bar");
+      assertEquals(3, out.size());
+      assertEquals(tuple(1), out.get(0));
+      assertEquals(tuple(2), out.get(1));
+      assertEquals(tuple(3), out.get(2));
+  }
+  
+  /**
+   * Verify that ASSERT operator works
+   * @throws Exception
+   */
+  @Test
+  public void testNegative() throws Exception {
+      PigServer pigServer = new PigServer(ExecType.LOCAL);
+      Data data = resetData(pigServer);
+
+      data.set("foo",
+              tuple(1),
+              tuple(2),
+              tuple(3)
+              );
+
+      pigServer.registerQuery("A = LOAD 'foo' USING mock.Storage() AS (i:int);");
+      pigServer.registerQuery("ASSERT A BY i > 1 , 'i should be greater than 1';");
+      
+      try {
+          pigServer.openIterator("A");
+      } catch (FrontendException fe) {
+          Assert.assertTrue(fe.getCause().getMessage().contains(
+                  "Job terminated with anomalous status FAILED"));
+      }
+       
+  }
+}