Author: gates Date: Fri Jun 19 04:15:38 2009 New Revision: 786366 URL: http://svn.apache.org/viewvc?rev=786366&view=rev Log: PIG-753 Allow UDFs with no parameters.
Added: hadoop/pig/trunk/test/org/apache/pig/test/TestUDFWithoutParameter.java hadoop/pig/trunk/test/org/apache/pig/test/utils/MyUDFWithoutParameter.java Modified: hadoop/pig/trunk/CHANGES.txt hadoop/pig/trunk/src/org/apache/pig/impl/logicalLayer/validators/TypeCheckingVisitor.java Modified: hadoop/pig/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/CHANGES.txt?rev=786366&r1=786365&r2=786366&view=diff ============================================================================== --- hadoop/pig/trunk/CHANGES.txt (original) +++ hadoop/pig/trunk/CHANGES.txt Fri Jun 19 04:15:38 2009 @@ -24,6 +24,8 @@ IMPROVEMENTS +PIG-753: Allow UDFs with no parameters (zjffdu via gates). + OPTIMIZATIONS BUG FIXES Modified: hadoop/pig/trunk/src/org/apache/pig/impl/logicalLayer/validators/TypeCheckingVisitor.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/src/org/apache/pig/impl/logicalLayer/validators/TypeCheckingVisitor.java?rev=786366&r1=786365&r2=786366&view=diff ============================================================================== --- hadoop/pig/trunk/src/org/apache/pig/impl/logicalLayer/validators/TypeCheckingVisitor.java (original) +++ hadoop/pig/trunk/src/org/apache/pig/impl/logicalLayer/validators/TypeCheckingVisitor.java Fri Jun 19 04:15:38 2009 @@ -2583,7 +2583,7 @@ if (innerRoot instanceof LOProject) { resolveLOProjectType((LOProject) innerRoot) ; } - else if (innerRoot instanceof LOConst) { + else if (innerRoot instanceof LOConst || innerRoot instanceof LOUserFunc) { // it's ok because LOConst always has // the right type information } Added: hadoop/pig/trunk/test/org/apache/pig/test/TestUDFWithoutParameter.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/test/org/apache/pig/test/TestUDFWithoutParameter.java?rev=786366&view=auto ============================================================================== --- hadoop/pig/trunk/test/org/apache/pig/test/TestUDFWithoutParameter.java (added) +++ hadoop/pig/trunk/test/org/apache/pig/test/TestUDFWithoutParameter.java Fri Jun 19 04:15:38 2009 @@ -0,0 +1,71 @@ +/* + * 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.test; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.apache.pig.ExecType; +import org.apache.pig.PigServer; +import org.apache.pig.data.Tuple; + +public class TestUDFWithoutParameter extends TestCase { + + static String[] ScriptStatement = { "A = LOAD 'test/org/apache/pig/test/data/passwd' USING PigStorage();", + "B = FOREACH A GENERATE org.apache.pig.test.utils.MyUDFWithoutParameter();" }; + + static File TempScriptFile = null; + + @Override + protected void setUp() throws Exception { + TempScriptFile = File.createTempFile("temp_jira_753", ".pig"); + FileWriter writer=new FileWriter(TempScriptFile); + for (String line:ScriptStatement){ + writer.write(line+"\n"); + } + writer.close(); + } + + public void testUDFWithoutParameter() { + try { + PigServer pig = new PigServer(ExecType.LOCAL); + pig.registerScript(TempScriptFile.getAbsolutePath()); + + Iterator<Tuple> iterator=pig.openIterator("B"); + int index=0; + while(iterator.hasNext()){ + Tuple tuple=iterator.next(); + index++; + int result=(Integer)tuple.get(0); + assertEquals(result, index); + } + } catch (IOException e) { + e.printStackTrace(); + fail(); + } + } + + @Override + protected void tearDown() throws Exception { + TempScriptFile.delete(); + } +} Added: hadoop/pig/trunk/test/org/apache/pig/test/utils/MyUDFWithoutParameter.java URL: http://svn.apache.org/viewvc/hadoop/pig/trunk/test/org/apache/pig/test/utils/MyUDFWithoutParameter.java?rev=786366&view=auto ============================================================================== --- hadoop/pig/trunk/test/org/apache/pig/test/utils/MyUDFWithoutParameter.java (added) +++ hadoop/pig/trunk/test/org/apache/pig/test/utils/MyUDFWithoutParameter.java Fri Jun 19 04:15:38 2009 @@ -0,0 +1,34 @@ +/* + * 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.test.utils; + +import java.io.IOException; + +import org.apache.pig.EvalFunc; +import org.apache.pig.data.Tuple; + +public class MyUDFWithoutParameter extends EvalFunc<Integer> { + + private int count = 0; + + @Override + public Integer exec(Tuple input) throws IOException { + return ++count; + } + +}