You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pig.apache.org by ol...@apache.org on 2008/10/30 22:27:17 UTC
svn commit: r709249 [3/4] - in /hadoop/pig/branches/types/contrib: ./
piggybank/ piggybank/java/ piggybank/java/lib/ piggybank/java/src/
piggybank/java/src/main/ piggybank/java/src/main/java/
piggybank/java/src/main/java/org/ piggybank/java/src/main/ja...
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/RINT.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/RINT.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/RINT.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/RINT.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,55 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+/**
+ * math.RINT implements a binding to the Java function
+* {@link java.lang.Math#rint(double) Math.rint(double)}.
+* Given a single data atom it Returns the double value
+* that is closest in value to the argument and is equal
+* to a mathematical integer.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>RINT_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.RINT(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#rint(double)
+* @see
+* @author ajay garg
+*
+*/
+public class RINT extends DoubleBase{
+ Double compute(Double input){
+ return Math.rint(input);
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ROUND.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ROUND.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ROUND.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ROUND.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,100 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+ * math.ROUND implements a binding to the Java function
+* {@link java.lang.Math#round(double) Math.round(double)}.
+* Given a single data atom it Returns the closest long to the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Long</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>ROUND_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.ROUND(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#round(double)
+* @see
+* @author ajay garg
+*
+*/
+public class ROUND extends EvalFunc<Long>{
+ /**
+ * java level API
+ * @param input expects a single numeric value
+ * @param output returns a single numeric value,
+ * the closest long to the argument
+ */
+ @Override
+ public Long exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ try{
+ Double d = DataType.toDouble(input.get(0));
+ return Math.round(d);
+ } catch (NumberFormatException nfe){
+ System.err.println("Failed to process input; error - " + nfe.getMessage());
+ return null;
+ } catch (Exception e){
+ throw WrappedIOException.wrap("Caught exception processing input row ", e);
+ }
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.LONG));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
+ */
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.BYTEARRAY))));
+ funcList.add(new FuncSpec(DoubleRound.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.DOUBLE))));
+ funcList.add(new FuncSpec(FloatRound.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.FLOAT))));
+ return funcList;
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SCALB.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SCALB.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SCALB.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SCALB.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,107 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+ * math.SCALB implements a binding to the Java function
+* {@link java.lang.Math#scalb(double,int) Math.scalb(double,int)}.
+* Given a tuple with two data atom x and y ,it Return x pow(2,y)
+* rounded as if performed by a single correctly rounded
+* floating-point multiply to a member of the double value set.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Tuple containing two numeric values [Double, Integer]</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>SCALB_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.SCALB(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#scalb(double,int)
+* @see
+* @author ajay garg
+*
+*/
+public class SCALB extends EvalFunc<Double>{
+
+ /**
+ * java level API
+ * @param input expects a tuple containing two numeric DataAtom value
+ * @param output returns a single numeric DataAtom value, which is
+ * fistArgument pow(2,secondArgument) rounded as if performed by a
+ * single correctly rounded floating-point multiply to a member of
+ * the double value set.
+ */
+ public Double exec(Tuple input) throws IOException {
+ if (input == null || input.size() < 2)
+ return null;
+
+ try{
+ Double first = DataType.toDouble(input.get(0));
+ Integer second = DataType.toInteger(input.get(1));
+
+ return Math.scalb(first, second);
+ } catch (NumberFormatException nfe){
+ System.err.println("Failed to process input; error - " + nfe.getMessage());
+ return null;
+ } catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in MAX.Initial", e);
+ }
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.DOUBLE));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
+ */
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ Util.addToFunctionList(funcList, this.getClass().getName(), DataType.BYTEARRAY);
+ Util.addToFunctionList(funcList, DoubleMax.class.getName(), DataType.DOUBLE);
+ Util.addToFunctionList(funcList, FloatMax.class.getName(), DataType.FLOAT);
+
+ return funcList;
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIGNUM.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIGNUM.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIGNUM.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIGNUM.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,102 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+ * math.SIGNUM implements a binding to the Java function
+* {@link java.lang.Math#signum(double) Math.signum(double)}.
+* Given a single data atom it Returns the signum function of the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>SIGNUM_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.SIGNUM(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#signum(double)
+* @see
+* @author ajay garg
+*
+*/
+public class SIGNUM extends EvalFunc<Double>{
+ /**
+ * java level API
+ * @param input expects a single numeric DataAtom value
+ * @param output returns a single numeric DataAtom value,
+ * signum function of the argument
+ */
+ @Override
+ public Double exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ try{
+ Double d = DataType.toDouble(input.get(0));
+ return Math.signum(d);
+ }catch (NumberFormatException nfe){
+ System.err.println("Failed to process input; error - " + nfe.getMessage());
+ return null;
+ }catch (Exception e){
+ throw WrappedIOException.wrap("Caught exception processing input row ", e);
+ }
+
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.DOUBLE));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
+ */
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.BYTEARRAY))));
+ funcList.add(new FuncSpec(DoubleSignum.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.DOUBLE))));
+ funcList.add(new FuncSpec(FloatSignum.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.FLOAT))));
+ return funcList;
+ }
+
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIN.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIN.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIN.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SIN.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,54 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+/**
+ * math.SIN implements a binding to the Java function
+* {@link java.lang.Math#sin(double) Math.sin(double)}.
+* Given a single data atom it Returns the sine of the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>SIN_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.SIN(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#sin(double)
+* @see
+* @author ajay garg
+*
+*/
+public class SIN extends DoubleBase{
+ Double compute(Double input){
+ return Math.sin(input);
+
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SINH.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SINH.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SINH.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SINH.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,53 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+/**
+ * math.SINH implements a binding to the Java function
+* {@link java.lang.Math#sinh(double) Math.sinh(double)}.
+* Given a single data atom it Returns the hyperbolic sine of the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>SINH_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.SINH(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#sinh(double)
+* @see
+* @author ajay garg
+*
+*/
+public class SINH extends DoubleBase{
+ Double compute(Double input){
+ return Math.sinh(input);
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SQRT.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SQRT.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SQRT.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/SQRT.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,52 @@
+/*
+ * 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.piggybank.evaluation.math;
+/**
+ * math.SQRT implements a binding to the Java function
+* {@link java.lang.Math#sqrt(double) Math.sqrt(double)}.
+* Given a single data atom it Returns the square root of the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>SQRT_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.SQRT(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#sqrt(double)
+* @see
+* @author ajay garg
+*
+*/
+public class SQRT extends DoubleBase{
+ Double compute(Double input){
+ return Math.sqrt(input);
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TAN.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TAN.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TAN.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TAN.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,53 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+/**
+ * math.TAN implements a binding to the Java function
+* {@link java.lang.Math#tan(double) Math.tan(double)}.
+* Given a single data atom it Returns the tangent of the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>DataAtom Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>TAN_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.TAN(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#tan(double)
+* @see
+* @author ajay garg
+*
+*/
+public class TAN extends DoubleBase{
+ Double compute(Double input){
+ return Math.tan(input);
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TANH.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TANH.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TANH.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/TANH.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,54 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+/**
+ * math.TANH implements a binding to the Java function
+* {@link java.lang.Math#tanh(double) Math.tanh(double)}.
+* Given a single data atom it Returns the hyperbolic tangent
+* of the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>TANH_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.TANH(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#tanh(double)
+* @see
+* @author ajay garg
+*
+*/
+public class TANH extends DoubleBase{
+ Double compute(Double input){
+ return Math.tanh(input);
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ULP.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ULP.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ULP.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/ULP.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,102 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+ * math.ULP implements a binding to the Java function
+* {@link java.lang.Math#ulp(double) Math.ulp(double)}.
+* Given a single data atom it Returns the size of an ulp
+* of the argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>ULP_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.ULP(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#ulp(double)
+* @see
+* @author ajay garg
+*
+*/
+public class ULP extends EvalFunc<Double>{
+ /**
+ * java level API
+ * @param input expects a single numeric value
+ * @param output returns a single numeric value,
+ * the size of an ulp of the argument.
+ */
+ public Double exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ try{
+ Double d = DataType.toDouble(input.get(0));
+ return Math.ulp(d);
+ }catch(NumberFormatException nfe){
+ System.err.println("Failed to process input; error - " + nfe.getMessage());
+ return null;
+ } catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in ULP.", e);
+ }
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.DOUBLE));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
+ */
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.BYTEARRAY))));
+ funcList.add(new FuncSpec(DoubleUlp.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.DOUBLE))));
+ funcList.add(new FuncSpec(FloatUlp.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.FLOAT))));
+
+ return funcList;
+ }
+
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/Util.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/Util.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/Util.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/Util.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,38 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.FuncSpec;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+/**
+* base class for math udfs
+*/
+public class Util{
+ static void addToFunctionList (List<FuncSpec> funcList, String className, byte type){
+ List<Schema.FieldSchema> fields = new ArrayList<Schema.FieldSchema>();
+ fields.add (new Schema.FieldSchema(null, type));
+ fields.add (new Schema.FieldSchema(null, type));
+ funcList.add(new FuncSpec(className, new Schema(fields)));
+ }
+
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/copySign.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/copySign.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/copySign.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/copySign.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,103 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.FuncSpec;
+import org.apache.pig.EvalFunc;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+ * math.copySign implements a binding to the Java function
+* {@link java.lang.Math#copySign(double,double) Math.copySign(double,double)}.
+* Given a tuple with two data atom Returns the first floating-point argument
+* with the sign of the second floating-point argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Tuple containing two double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>copySign_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.copySign(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#copySign(double,double)
+* @see
+* @author ajay garg
+*
+*/
+public class copySign extends EvalFunc<Double>{
+ /**
+ * java level API
+ * @param input expects a tuple containing two numeric DataAtom value
+ * @param output returns a single numeric DataAtom value, which is
+ * first floating-point argument with the sign of the second
+ * floating-point argument.
+ */
+ @Override
+ public Double exec(Tuple input) throws IOException {
+ if (input == null || input.size() < 2)
+ return null;
+ try{
+ double first = DataType.toDouble(input.get(0));
+ double second = DataType.toDouble(input.get(1));
+ return Math.copySign(first, second);
+ } catch (NumberFormatException nfe){
+ System.err.println("Failed to process input; error - " + nfe.getMessage());
+ return null;
+ } catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception processing input row ", e);
+ }
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.DOUBLE));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
+ */
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ Util.addToFunctionList(funcList, this.getClass().getName(), DataType.BYTEARRAY);
+ Util.addToFunctionList(funcList, DoubleCopySign.class.getName(), DataType.DOUBLE);
+ Util.addToFunctionList(funcList, FloatCopySign.class.getName(), DataType.FLOAT);
+
+ return funcList;
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/getExponent.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/getExponent.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/getExponent.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/getExponent.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,101 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+ * math.getExponent implements a binding to the Java function
+* {@link java.lang.Math#getExponent(double) Math.getExponent(double)}.
+* Given a single data atom it returns the unbiased exponent used in
+* the representation of a double
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>int</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>getExponent_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.getExponent(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#getExponent(double)
+* @see
+* @author ajay garg
+*
+*/
+public class getExponent extends EvalFunc<Integer>{
+ /**
+ * java level API
+ * @param input expects a single numeric value
+ * @param output returns a single numeric value, unbiased
+ * exponent used in the representation of a double
+ */
+ public Integer exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ try {
+ Double d = DataType.toDouble(input.get(0));
+ return Math.getExponent(d);
+ } catch (NumberFormatException nfe){
+ System.err.println("Failed to process input; error - " + nfe.getMessage());
+ return null;
+ } catch (Exception e){
+ throw WrappedIOException.wrap("Caught exception processing input row ", e);
+ }
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.INTEGER));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
+ */
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ funcList.add(new FuncSpec(this.getClass().getName(), new Schema(new Schema.FieldSchema(null, DataType.BYTEARRAY))));
+ funcList.add(new FuncSpec(DoubleGetExponent.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.DOUBLE))));
+ funcList.add(new FuncSpec(FloatGetExponent.class.getName(), new Schema(new Schema.FieldSchema(null, DataType.FLOAT))));
+ return funcList;
+ }
+
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/nextAfter.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/nextAfter.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/nextAfter.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/nextAfter.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,107 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.FuncSpec;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.FrontendException;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+ * math.nextAfter implements a binding to the Java function
+* {@link java.lang.Math#nextAfter(double,double) Math.nextAfter(double,double)}.
+* Given a tuple with two data atom it Returns the
+* floating-point number adjacent to the first argument in the
+* direction of the second argument.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Tuple containing two double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>nextAfter_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.nextAfter(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#nextAfter(double,double)
+* @see
+* @author ajay garg
+*
+*/
+public class nextAfter extends EvalFunc<Double>{
+
+ /**
+ * java level API
+ * @param input expects a tuple containing two numeric DataAtom value
+ * @param output returns a single numeric DataAtom value, which is
+ * the floating-point number adjacent to the first argument in the
+ * direction of the second argument.
+ */
+ @Override
+ public Double exec(Tuple input) throws IOException {
+ try{
+ double first = DataType.toDouble(input.get(0));
+ double second = DataType.toDouble(input.get(1));
+ return Math.nextAfter(first, second);
+ } catch (NumberFormatException nfe){
+ System.err.println("Failed to process input; error - " + nfe.getMessage());
+ return null;
+ } catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in nextAfter", e);
+ }
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.DOUBLE));
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
+ */
+ @Override
+ public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
+ List<FuncSpec> funcList = new ArrayList<FuncSpec>();
+ Util.addToFunctionList(funcList, this.getClass().getName(), DataType.BYTEARRAY);
+ Util.addToFunctionList(funcList, DoubleNextAfter.class.getName(), DataType.DOUBLE);
+
+ List<Schema.FieldSchema> fields = new ArrayList<Schema.FieldSchema>();
+ fields.add (new Schema.FieldSchema(null, DataType.FLOAT));
+ fields.add (new Schema.FieldSchema(null, DataType.DOUBLE));
+ funcList.add(new FuncSpec(FloatNextAfter.class.getName(), new Schema(fields)));
+
+ return funcList;
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toDegrees.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toDegrees.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toDegrees.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toDegrees.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,55 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+/**
+ * math.toDegrees implements a binding to the Java function
+* {@link java.lang.Math#toDegrees(double) Math.toDegrees(double)}.
+* Given a single data atom it Converts an angle measured in radians
+* to an approximately equivalent angle measured in degrees.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>toDegrees_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.toDegrees(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#toDegrees(double)
+* @see
+* @author ajay garg
+*
+*/
+public class toDegrees extends DoubleBase{
+ Double compute(Double input){
+ return Math.toDegrees(input);
+
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toRadians.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toRadians.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toRadians.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/math/toRadians.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,54 @@
+/*
+ * 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.piggybank.evaluation.math;
+
+/**
+ * math.toRadians implements a binding to the Java function
+* {@link java.lang.Math#toRadians(double) Math.toRadians(double)}.
+* Given a single data atom it Converts an angle measured in degrees
+* to an approximately equivalent angle measured in radians.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>value</code> - <code>Double</code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>Double</code> </dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>toRadians_inputSchema</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register math.jar;<br/>
+* A = load 'mydata' using PigStorage() as ( float1 );<br/>
+* B = foreach A generate float1, math.toRadians(float1);
+* </code></dd>
+* </dl>
+*
+* @see Math#toRadians(double)
+* @see
+* @author ajay garg
+*
+*/
+public class toRadians extends DoubleBase{
+ Double compute(Double input){
+ return Math.toRadians(input);
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COR.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COR.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COR.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COR.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,367 @@
+/*
+ * 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.piggybank.evaluation.stats;
+
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Iterator;
+import java.util.Vector;
+import org.apache.pig.Algebraic;
+import org.apache.pig.EvalFunc;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.DefaultBagFactory;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.DefaultTupleFactory;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+* Computes the correlation between sets of data. The returned value
+* will be a bag which will contain a tuple for each combination of input
+* schema and inside tuple we will have two schema name and correlation between
+* those two schemas.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>data sets</code> - <code>tuple which contain DataBag corresponding to each data set, and inside
+* DataBag we have tuple corresponding to each data atom. E.g. ({(1),(2)},{(3),(4)}) </code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>DataBag which contain every possible combination of input schemas</code> correlation between data sets</dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>correlation</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register statistics.jar;<br/>
+* A = load 'input.xml' using PigStorage(':');<br/>
+* B = group A all;<br/>
+* define c COR('a','b','c');<br/>
+* D = foreach B generate group,c(A.$0,A.$1,A.$2);<br/>
+* </code></dd>
+* </dl>
+*
+* @author ajay garg
+* @see <a href = "http://en.wikipedia.org/wiki/Covariance"> http://en.wikipedia.org/wiki/Covariance</a><br>
+*/
+
+public class COR extends EvalFunc<DataBag> implements Algebraic,Serializable {
+ //name of the schemas. Initialize when user use define
+ protected Vector<String>schemaName = new Vector<String>();
+ //flag to indicate if define is called or not.
+ private boolean flag = false;
+
+ public COR(){}
+
+ public COR(String... schemaName){
+ for(int i=0;i<schemaName.length;i++){
+ this.schemaName.add(schemaName[i]);
+ flag = true;
+ }
+ }
+
+ /**
+ * Function to compute correlation between data sets.
+ * @param input input tuple which contains data sets.
+ * @param output output dataBag which contain correlation between each pair of data sets.
+ */
+ @Override
+ public DataBag exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ DataBag output = DefaultBagFactory.getInstance().newDefaultBag();
+ for(int i=0;i<input.size();i++){
+ for(int j=i+1;j<input.size();j++){
+ Tuple temp = DefaultTupleFactory.getInstance().newTuple(3);
+ try{
+ if(flag){
+ temp.set(0, schemaName.elementAt(i));
+ temp.set(1, schemaName.elementAt(j));
+ }
+ else{
+ temp.set(0, "var"+i);
+ temp.set(1, "var"+j);
+ }
+
+ Tuple tempResult = computeAll((DataBag)input.get(i),(DataBag)input.get(j));
+ double size = ((DataBag)input.get(i)).size();
+ double sum_x_y = (Double)tempResult.get(0);
+ double sum_x = (Double)tempResult.get(1);
+ double sum_y = (Double)tempResult.get(2);
+ double sum_x_square = (Double)tempResult.get(3);
+ double sum_y_square = (Double)tempResult.get(4);
+ double result = (size*sum_x_y - sum_x*sum_y)/Math.sqrt((size*sum_x_square-sum_x*sum_x)*(size*sum_y_square-sum_y*sum_y));
+ temp.set(2, result);
+ }catch (Exception e){
+ System.err.println("Failed to process input record; error - " + e.getMessage());
+ return null;
+ }
+ output.add(temp);
+ }
+ }
+
+ return output;
+ }
+
+ //used to pass schema name to Final class constructor
+ /**
+ * Function to return argument of constructor as string. It append ( and ) at starting and end or argument respectively.
+ * If default constructor is called is returns empty string.
+ * @return argument of constructor
+ */
+ @Override
+ public String toString() {
+ if(flag){
+ StringBuilder sb = new StringBuilder();
+ sb.append('(');
+ if(schemaName!=null){
+ for (String sch : schemaName) {
+ sb.append('\'' + sch + '\'' + ",");
+ }
+ sb.deleteCharAt(sb.length()-1);
+ }
+ sb.append(')');
+ return sb.toString();
+ }
+ else return "";
+ }
+
+ public String getInitial() {
+ return Initial.class.getName();
+ }
+
+ public String getIntermed() {
+ return Intermed.class.getName();
+ }
+
+ public String getFinal() {
+ return Final.class.getName() + toString();
+ }
+
+ static public class Initial extends EvalFunc<Tuple> implements Serializable{
+ @Override
+ public Tuple exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+ Tuple output = DefaultTupleFactory.getInstance().newTuple(input.size() * 2);
+ try {
+ int k = -1;
+ for(int i=0;i<input.size();i++){
+ for(int j=i+1;j<input.size();j++){
+ DataBag first = (DataBag)input.get(i);
+ DataBag second = (DataBag)input.get(j);
+ output.set(k++, computeAll(first, second));
+ output.set(k++, (Long)first.size());
+ }
+ }
+ } catch(Exception t) {
+ System.err.println("Failed to process input record; error - " + t.getMessage());
+ return null;
+ }
+ return output;
+ }
+ }
+
+ static public class Intermed extends EvalFunc<Tuple> implements Serializable{
+
+ @Override
+ public Tuple exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+ try{
+ return combine((DataBag)input.get(0));
+ }catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in COR.Intermed", e);
+ }
+ }
+ }
+
+ public static class Final extends EvalFunc<DataBag> implements Serializable{
+ protected Vector<String>schemaName = new Vector<String>();
+ boolean flag = false;
+ public Final(){}
+ public Final(String... schemaName){
+ for(int i=0;i<schemaName.length;i++){
+ this.schemaName.add(schemaName[i]);
+ flag = true;
+ }
+ }
+
+ @Override
+ public DataBag exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ Tuple combined;
+ try{
+ combined = combine((DataBag)input.get(0));
+ }catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in COR.Final", e);
+ }
+ int count = 0;
+ //for each pair of input schema combined contain 2 member. first member
+ //is tuple containing sum_x,sum_y,sum_x_y, sum_x_square, sum_y_square
+ //and second is number of tuples from which it is calculated .
+ //So if arity of combined is n then total number of schemas would be
+ //root of x*x - x - n =0
+ int totalSchemas=2;
+ while(totalSchemas*(totalSchemas-1)<combined.size()){
+ totalSchemas++;
+ }
+
+ DataBag output = DefaultBagFactory.getInstance().newDefaultBag();
+ for(int i=0;i<totalSchemas;i++){
+ for(int j=i+1;j<totalSchemas;j++){
+ Tuple result = DefaultTupleFactory.getInstance().newTuple(3);
+ try{
+ if(flag){
+ result.set(0, schemaName.elementAt(i));
+ result.set(1, schemaName.elementAt(j));
+ }
+ else{
+ result.set(0, "var"+i);
+ result.set(1, "var"+j);
+ }
+ Tuple tup = (Tuple)combined.get(count);
+ double tempCount = (Double)combined.get(count+1);
+ double sum_x_y = (Double)tup.get(0);
+ double sum_x = (Double)tup.get(1);
+ double sum_y = (Double)tup.get(2);
+ double sum_x_square = (Double)tup.get(3);
+ double sum_y_square = (Double)tup.get(4);
+ double correl = (tempCount*sum_x_y - sum_x*sum_y)/Math.sqrt((tempCount*sum_x_square-sum_x*sum_x)*(tempCount*sum_y_square-sum_y*sum_y));
+ result.set(2, correl);
+ }catch(Exception e){
+ System.err.println("Failed to process input record; error - " + e.getMessage());
+ return null;
+ }
+ output.add(result);
+ count+=2;
+ }
+ }
+
+ return output;
+ }
+ }
+
+ /**
+ * combine results of different data chunk
+ * @param values DataBag containing partial results computed on different data chunks
+ * @param output Tuple containing combined data
+ * @throws IOException
+ */
+ static protected Tuple combine(DataBag values) throws IOException {
+ Tuple output = DefaultTupleFactory.getInstance().newTuple();
+ Tuple tuple; // copy of DataBag values
+ tuple = DefaultTupleFactory.getInstance().newTuple(values.size());
+ int ct=0;
+
+ try{
+ for (Iterator<Tuple> it = values.iterator(); it.hasNext();ct++) {
+ Tuple t = it.next();
+ tuple.set(ct, t);
+ }
+ }catch(Exception e){}
+
+ try{
+ int size = ((Tuple)tuple.get(0)).size();
+ for(int i=0;i<size;i=i+2){
+ double count = 0;
+ double sum_x_y = 0.0;
+ double sum_x = 0.0;
+ double sum_y = 0.0;
+ double sum_x_square = 0.0;
+ double sum_y_square = 0.0;
+ for(int j=0;j<tuple.size();j++){
+ Tuple temp = (Tuple)tuple.get(j);
+ Tuple tem = (Tuple)temp.get(i);
+ count += (Double)temp.get(i+1);
+ sum_x_y += (Double)tem.get(0);
+ sum_x += (Double)tem.get(1);
+ sum_y += (Double)tem.get(2);
+ sum_x_square += (Double)tem.get(3);
+ sum_y_square += (Double)tem.get(4);
+ }
+ Tuple result = DefaultTupleFactory.getInstance().newTuple(5);
+ result.set(0, sum_x_y);
+ result.set(1, sum_x);
+ result.set(2, sum_y);
+ result.set(3, sum_x_square);
+ result.set(4, sum_y_square);
+ output.append(result);
+ output.append(count);
+ }
+ }catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in COR.combine", e);
+ }
+
+ return output;
+ }
+
+ /**
+ * compute sum(XY), sum(X), sum(Y), sum(XX), sum(YY) from given data sets
+ * @param first DataBag containing first data set
+ * @param second DataBag containing second data set
+ * @return tuple containing sum(XY), sum(X), sum(Y), sum(XX), sum(YY)
+ */
+ protected static Tuple computeAll(DataBag first, DataBag second) {
+ double sum_x_y = 0.0;
+ double sum_x = 0.0;
+ double sum_y = 0.0;
+ double sum_x_square = 0.0;
+ double sum_y_square = 0.0;
+ Iterator<Tuple> iterator_x = first.iterator();
+ Iterator<Tuple> iterator_y = second.iterator();
+ try{
+ while(iterator_x.hasNext()){
+ double x = (Double)iterator_x.next().get(0);
+ double y = (Double)iterator_y.next().get(0);
+ sum_x_y+=x*y;
+ sum_x+=x;
+ sum_y+=y;
+ sum_x_square+=x*x;
+ sum_y_square+=y*y;
+ }
+ }catch(Exception e){
+ System.err.println("Failed to process input record; error - " + e.getMessage());
+ return null;
+ }
+
+ Tuple result = DefaultTupleFactory.getInstance().newTuple(5);
+ try{
+ result.set(0, sum_x_y);
+ result.set(1, sum_x);
+ result.set(2, sum_y);
+ result.set(3, sum_x_square);
+ result.set(4, sum_y_square);
+ }catch(Exception e){}
+ return result;
+
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.BAG));
+ }
+
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COV.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COV.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COV.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/stats/COV.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,345 @@
+/*
+ * 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.piggybank.evaluation.stats;
+
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Iterator;
+import java.util.Vector;
+import org.apache.pig.Algebraic;
+import org.apache.pig.EvalFunc;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.DefaultBagFactory;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.DefaultTupleFactory;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.util.WrappedIOException;
+
+/**
+* Computes the covariance between sets of data. The returned value
+* will be a bag which will contain a tuple for each combination of input
+* schema and inside tuple we will have two schema name and covariance between
+* those two schemas.
+*
+* <dl>
+* <dt><b>Parameters:</b></dt>
+* <dd><code>data sets</code> - <code>tuple which contain DataBag corresponding to each data set, and inside
+* DataBag we have tuple corresponding to each data atom. E.g. ({(1),(2)},{(3),(4)}) </code>.</dd>
+*
+* <dt><b>Return Value:</b></dt>
+* <dd><code>DataBag which contain every possible combination of input schemas</code> covariance between data sets</dd>
+*
+* <dt><b>Return Schema:</b></dt>
+* <dd>covariance</dd>
+*
+* <dt><b>Example:</b></dt>
+* <dd><code>
+* register statistics.jar;<br/>
+* A = load 'input.xml' using PigStorage(':');<br/>
+* B = group A all;<br/>
+* define c COV('a','b','c');<br/>
+* D = foreach B generate group,c(A.$0,A.$1,A.$2);<br/>
+* </code></dd>
+* </dl>
+*
+* @author ajay garg
+* @see <a href = "http://en.wikipedia.org/wiki/Covariance">http://en.wikipedia.org/wiki/Covariance</a><br>
+*/
+public class COV extends EvalFunc<DataBag> implements Algebraic,Serializable {
+ //name of the schemas. Initialize when user use define
+ protected Vector<String>schemaName = new Vector<String>();
+ //flag to indicate if define is called or not.
+ private boolean flag = false;
+
+ public COV(){}
+
+
+ public COV(String... schemaName){
+ for(int i=0;i<schemaName.length;i++){
+ this.schemaName.add(schemaName[i]);
+ flag = true;
+ }
+ }
+
+
+ /**
+ * Function to compute covariance between data sets.
+ * @param input input tuple which contains data sets.
+ * @param output output dataBag which contain covariance between each pair of data sets.
+ */
+ @Override
+ public DataBag exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+ DataBag output = DefaultBagFactory.getInstance().newDefaultBag();
+
+ try{
+ for(int i=0;i<input.size();i++){
+ for(int j=i+1;j<input.size();j++){
+ Tuple temp = DefaultTupleFactory.getInstance().newTuple(3);
+ if(flag){
+ temp.set(0, schemaName.elementAt(i));
+ temp.set(1, schemaName.elementAt(j));
+ }
+ else{
+ temp.set(0, "var"+i);
+ temp.set(1, "var"+j);
+ }
+
+ Tuple tempResult = computeAll((DataBag)input.get(i), (DataBag)input.get(j));
+ double size = ((DataBag)input.get(i)).size();
+ double sum_x_y = (Double)tempResult.get(0);
+ double sum_x = (Double)tempResult.get(1);
+ double sum_y = (Double)tempResult.get(2);
+ double result = (size*sum_x_y - sum_x*sum_y)/(size*size);
+ temp.set(2, result);
+ output.add(temp);
+ }
+ }
+ }catch(Exception e){
+ System.err.println("Failed to process input; error - " + e.getMessage());
+ return null;
+ }
+
+ return output;
+ }
+
+
+ //used to pass schema name to Final class constructor
+ /**
+ * Function to return argument of constructor as string. It append ( and ) at starting and end or argument respectively.
+ * If default constructor is called is returns empty string.
+ * @return argument of constructor
+ */
+ @Override
+ public String toString() {
+ if(flag){
+ StringBuilder sb = new StringBuilder();
+ sb.append('(');
+ if(schemaName!=null){
+ for (String sch : schemaName) {
+ sb.append('\'' + sch + '\'' + ",");
+ }
+ sb.deleteCharAt(sb.length()-1);
+ }
+ sb.append(')');
+ return sb.toString();
+ }
+ else return "";
+ }
+ public String getInitial() {
+ return Initial.class.getName();
+ }
+
+ public String getIntermed() {
+ return Intermed.class.getName();
+ }
+
+ public String getFinal() {
+ //return Final.class.getName();
+ return Final.class.getName() + toString();
+ }
+
+ static public class Initial extends EvalFunc<Tuple> implements Serializable{
+ @Override
+ public Tuple exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ Tuple output = DefaultTupleFactory.getInstance().newTuple();
+ try {
+ for(int i=0;i<input.size();i++){
+ for(int j=i+1;j<input.size();j++){
+ DataBag first = (DataBag)input.get(i);
+ DataBag second = (DataBag)input.get(j);
+ output.append(computeAll(first, second));
+ output.append(first.size());
+ }
+ }
+ } catch(Exception t) {
+ System.err.println("Failed to process input; error - " + t.getMessage());
+ return null;
+ }
+
+ return output;
+ }
+ }
+
+ static public class Intermed extends EvalFunc<Tuple> implements Serializable{
+
+ @Override
+ public Tuple exec(Tuple input) throws IOException {
+ try{
+ return combine((DataBag)input.get(0));
+ }catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in COV.Intermed", e);
+ }
+ }
+ }
+
+ public static class Final extends EvalFunc<DataBag> implements Serializable{
+ protected Vector<String>schemaName = new Vector<String>();
+ boolean flag = false;
+
+ public Final(){}
+
+ public Final(String... schemaName){
+ for(int i=0;i<schemaName.length;i++){
+ this.schemaName.add(schemaName[i]);
+ flag = true;
+ }
+ }
+
+ @Override
+ public DataBag exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ DataBag output = DefaultBagFactory.getInstance().newDefaultBag();
+ int count = 0;
+
+ //for each pair of input schema combined contain 2 member. first member
+ //is tuple containing sum_x,sum_y,sum_x_y and second is number of tuples
+ //from which it is calculated . So if arity of combined is n then total
+ //number of schemas would be root of x*x - x - n =0
+
+ try{
+ Tuple combined = combine((DataBag)input.get(0));
+ int totalSchemas=2;
+ while(totalSchemas*(totalSchemas-1)<combined.size()){
+ totalSchemas++;
+ }
+ for(int i=0;i<totalSchemas;i++){
+ for(int j=i+1;j<totalSchemas;j++){
+ Tuple result = DefaultTupleFactory.getInstance().newTuple(3);
+ if(flag){
+ result.set(0, schemaName.elementAt(i));
+ result.set(1, schemaName.elementAt(j));
+ }
+ else{
+ result.set(0, "var"+i);
+ result.set(1, "var"+j);
+ }
+ Tuple tup = (Tuple)combined.get(count);
+ double tempCount = (Double)combined.get(count+1);
+ double sum_x_y = (Double)tup.get(0);
+ double sum_x = (Double)tup.get(1);
+ double sum_y = (Double)tup.get(2);
+ double covar = (tempCount*sum_x_y - sum_x*sum_y)/(tempCount*tempCount);
+ result.set(2, covar);
+ output.add(result);
+ count+=2;
+ }
+ }
+ }catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception in COV.Intermed", e);
+ }
+
+ return output;
+ }
+ }
+
+ /**
+ * combine results of different data chunk
+ * @param values DataBag containing partial results computed on different data chunks
+ * @param output Tuple containing combined data
+ * @throws IOException
+ */
+ static protected Tuple combine(DataBag values) throws IOException {
+ Tuple tuple = DefaultTupleFactory.getInstance().newTuple(Double.valueOf(values.size()).intValue());
+ Tuple output = DefaultTupleFactory.getInstance().newTuple();
+
+ int ct=0;
+ for (Iterator<Tuple> it = values.iterator(); it.hasNext();ct++) {
+ Tuple t = it.next();
+ try{ tuple.set(ct, t);}catch(Exception e){}
+ }
+
+ try{
+ Tuple temp = (Tuple)tuple.get(0);
+ int size = temp.size();
+ for(int i=0;i<size;i=i+2){
+ double count = 0;
+ double sum_x_y = 0.0;
+ double sum_x = 0.0;
+ double sum_y = 0.0;
+ for(int j=0;j<tuple.size();j++){
+ Tuple t = (Tuple)tuple.get(j);
+ Tuple tem = (Tuple)t.get(i);
+ count += (Double)t.get(i+1);
+ sum_x_y+=(Double)tem.get(0);
+ sum_x+=(Double)tem.get(1);
+ sum_y+=(Double)tem.get(2);
+ }
+ Tuple result = DefaultTupleFactory.getInstance().newTuple(3);
+ result.set(0, sum_x_y);
+ result.set(1, sum_x);
+ result.set(2, sum_y);
+ output.append(result);
+ output.append(count);
+ }
+ }catch(Exception e){
+ throw WrappedIOException.wrap("Caught exception processing input", e);
+ }
+
+ return output;
+ }
+
+ /**
+ * compute sum(XY), sum(X), sum(Y) from given data sets
+ * @param first DataBag containing first data set
+ * @param second DataBag containing second data set
+ * @return tuple containing sum(XY), sum(X), sum(Y)
+ */
+ protected static Tuple computeAll(DataBag first, DataBag second) throws IOException {
+ double sum_x_y = 0.0;
+ double sum_x = 0.0;
+ double sum_y = 0.0;
+ Iterator<Tuple> iterator_x = first.iterator();
+ Iterator<Tuple> iterator_y = second.iterator();
+ try{
+ while(iterator_x.hasNext()){
+ double x = (Double)iterator_x.next().get(0);
+ double y = (Double)iterator_y.next().get(0);
+ sum_x_y+=x*y;
+ sum_x+=x;
+ sum_y+=y;
+ }
+ }catch (Exception e){
+ throw WrappedIOException.wrap("Caught exception processing input", e);
+ }
+
+ Tuple result = DefaultTupleFactory.getInstance().newTuple(3);
+ try{
+ result.set(0, sum_x_y);
+ result.set(1, sum_x);
+ result.set(2, sum_y);
+ }catch(Exception e) {}
+ return result;
+
+ }
+
+ @Override
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.BAG));
+ }
+
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/string/UPPER.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/string/UPPER.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/string/UPPER.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/main/java/org/apache/pig/piggybank/evaluation/string/UPPER.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,66 @@
+/*
+ * 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.piggybank.evaluation.string;
+
+import java.io.IOException;
+
+import org.apache.pig.EvalFunc;
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+
+/**
+ * string.UPPER implements eval function to convert a string to upper case
+ * Example:
+ * register pigudfs.jar;
+ * A = load 'mydata' as (name);
+ * B = foreach A generate string.UPPER(name);
+ * dump B;
+ */
+public class UPPER extends EvalFunc<String>
+{
+ /**
+ * Method invoked on every tuple during foreach evaluation
+ * @param input tuple; first column is assumed to have the column to convert
+ * @param output - resulting value
+ * @exception IOException
+ */
+ public String exec(Tuple input) throws IOException {
+ if (input == null || input.size() == 0)
+ return null;
+
+ try{
+ String str = (String)input.get(0);
+ return str.toUpperCase();
+ }catch(Exception e){
+ System.err.println("Failed to process input; error - " + e.getMessage());
+ return null;
+ }
+ }
+
+ //@Override
+ /**
+ * This method gives a name to the column.
+ * @param input - schema of the input data
+ * @return schema of the input data
+ */
+ public Schema outputSchema(Schema input) {
+ return new Schema(new Schema.FieldSchema(getSchemaName(this.getClass().getName().toLowerCase(), input), DataType.CHARARRAY));
+ }
+}
Added: hadoop/pig/branches/types/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/TestEvalString.java
URL: http://svn.apache.org/viewvc/hadoop/pig/branches/types/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/TestEvalString.java?rev=709249&view=auto
==============================================================================
--- hadoop/pig/branches/types/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/TestEvalString.java (added)
+++ hadoop/pig/branches/types/contrib/piggybank/java/src/test/java/org/apache/pig/piggybank/test/evaluation/TestEvalString.java Thu Oct 30 14:27:14 2008
@@ -0,0 +1,59 @@
+/*
+ * 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.piggybank.test.evaluation;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.util.Iterator;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+import org.junit.Test;
+
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.DefaultTupleFactory;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+
+import org.apache.pig.piggybank.evaluation.string.UPPER;
+
+// This class tests all string eval functions.
+
+public class TestEvalString extends TestCase {
+
+ @Test
+ public void testUPPER() throws Exception {
+ UPPER func = new UPPER();
+
+ // test excution
+ String in = "Hello World!";
+ String expected = "HELLO WORLD!";
+
+ Tuple input = DefaultTupleFactory.getInstance().newTuple(in);
+
+ String output = func.exec(input);
+ assertTrue(output.equals(expected));
+
+ // test schema creation
+
+ // FIXME
+ //Schema outSchema = func.outputSchema(tupleSchema);
+ //assertTrue(outSchema.toString().equals("upper_" + fieldName));
+
+ }
+}