You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by gd...@apache.org on 2012/09/21 12:02:15 UTC
svn commit: r1388400 - in /pig/trunk: CHANGES.txt
src/org/apache/pig/builtin/STARTSWITH.java
test/org/apache/pig/test/TestStringUDFs.java
Author: gdfm
Date: Fri Sep 21 10:02:15 2012
New Revision: 1388400
URL: http://svn.apache.org/viewvc?rev=1388400&view=rev
Log:
PIG-2879: Pig current releases lack a UDF startsWith.This UDF tests if a given string starts with the specified prefix. (initialcontext via azaroth)
Added:
pig/trunk/src/org/apache/pig/builtin/STARTSWITH.java
Modified:
pig/trunk/CHANGES.txt
pig/trunk/test/org/apache/pig/test/TestStringUDFs.java
Modified: pig/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/pig/trunk/CHANGES.txt?rev=1388400&r1=1388399&r2=1388400&view=diff
==============================================================================
--- pig/trunk/CHANGES.txt (original)
+++ pig/trunk/CHANGES.txt Fri Sep 21 10:02:15 2012
@@ -25,6 +25,8 @@ PIG-1891 Enable StoreFunc to make intell
IMPROVEMENTS
+PIG-2879: Pig current releases lack a UDF startsWith.This UDF tests if a given string starts with the specified prefix. (initialcontext via azaroth)
+
PIG-2712: Pig does not call OutputCommitter.abortJob() on the underlying OutputFormat (rohini via gates)
PIG-2918: Avoid Spillable bag overhead where possible (dvryaboy)
Added: pig/trunk/src/org/apache/pig/builtin/STARTSWITH.java
URL: http://svn.apache.org/viewvc/pig/trunk/src/org/apache/pig/builtin/STARTSWITH.java?rev=1388400&view=auto
==============================================================================
--- pig/trunk/src/org/apache/pig/builtin/STARTSWITH.java (added)
+++ pig/trunk/src/org/apache/pig/builtin/STARTSWITH.java Fri Sep 21 10:02:15 2012
@@ -0,0 +1,45 @@
+package org.apache.pig.builtin;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.DataType;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.backend.executionengine.ExecException;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+
+/**
+ * Pig UDF to test input <code>tuple.get(0)</code> against <code>tuple.get(1)</code>
+ * to determine if the first argument starts with the string in the second.
+ */
+public class STARTSWITH extends EvalFunc<Boolean> {
+ @Override
+ public Boolean exec(Tuple tuple) {
+ if (tuple == null || tuple.size() != 2) {
+ return null;
+ }
+ String argument = null;
+ String testAgainst = null;
+ try {
+ argument = (String) tuple.get(0);
+ testAgainst = (String) tuple.get(1);
+ return argument.startsWith(testAgainst);
+ } catch (ExecException exe) {
+ System.err.println("UDF STARTSWITH threw ExecException while processing '" +
+ argument + "' while attempting to locate prefix '" + testAgainst + "'");
+ return null;
+ }
+ }
+
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ Schema s = new Schema();
+ s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+ s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
+ funcList.add(new FuncSpec(this.getClass().getName(), s));
+ return funcList;
+ }
+}
Modified: pig/trunk/test/org/apache/pig/test/TestStringUDFs.java
URL: http://svn.apache.org/viewvc/pig/trunk/test/org/apache/pig/test/TestStringUDFs.java?rev=1388400&r1=1388399&r2=1388400&view=diff
==============================================================================
--- pig/trunk/test/org/apache/pig/test/TestStringUDFs.java (original)
+++ pig/trunk/test/org/apache/pig/test/TestStringUDFs.java Fri Sep 21 10:02:15 2012
@@ -19,6 +19,8 @@
package org.apache.pig.test;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import java.io.IOException;
@@ -27,6 +29,7 @@ import org.apache.pig.EvalFunc;
import org.apache.pig.builtin.INDEXOF;
import org.apache.pig.builtin.LAST_INDEX_OF;
import org.apache.pig.builtin.REPLACE;
+import org.apache.pig.builtin.STARTSWITH;
import org.apache.pig.builtin.STRSPLIT;
import org.apache.pig.builtin.SUBSTRING;
import org.apache.pig.builtin.TRIM;
@@ -174,4 +177,13 @@ public class TestStringUDFs {
assertEquals("foo", splits.get(0));
assertEquals("bar:baz", splits.get(1));
}
+
+ @Test
+ public void testStartsWith() throws IOException {
+ STARTSWITH startsWith = new STARTSWITH();
+ Tuple testTuple1 = Util.buildTuple("foo", "bar");
+ assertFalse("String prefix should not match", startsWith.exec(testTuple1));
+ Tuple testTuple2 = Util.buildTuple("foobaz", "foo");
+ assertTrue("String prefix should match", startsWith.exec(testTuple2));
+ }
}