FAQ
Author: hashutosh
Date: Mon Mar 2 19:35:10 2015
New Revision: 1663407

URL: http://svn.apache.org/r1663407
Log:
HIVE-9690 : Refactoring for non-numeric arithmetic operations (Jason Dere via Ashutosh Chauhan)

Added:
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseArithmetic.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseBinary.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericMinus.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericPlus.java
Modified:
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/RexNodeConverter.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseCompare.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseNumeric.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPMinus.java
     hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPPlus.java
     hive/trunk/ql/src/test/results/clientnegative/invalid_arithmetic_type.q.out

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/RexNodeConverter.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/RexNodeConverter.java?rev=1663407&r1=1663406&r2=1663407&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/RexNodeConverter.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/RexNodeConverter.java Mon Mar 2 19:35:10 2015
@@ -54,10 +54,12 @@ import org.apache.hadoop.hive.ql.parse.S
  import org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc;
  import org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc;
  import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
+import org.apache.hadoop.hive.ql.plan.ExprNodeDescUtils;
  import org.apache.hadoop.hive.ql.plan.ExprNodeFieldDesc;
  import org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc;
  import org.apache.hadoop.hive.ql.plan.ExprNodeNullDesc;
  import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDFBaseBinary;
  import org.apache.hadoop.hive.ql.udf.generic.GenericUDFBaseCompare;
  import org.apache.hadoop.hive.ql.udf.generic.GenericUDFBaseNumeric;
  import org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge;
@@ -68,7 +70,10 @@ import org.apache.hadoop.hive.ql.udf.gen
  import org.apache.hadoop.hive.ql.udf.generic.GenericUDFToVarchar;
  import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector;
  import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
  import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping;
  import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
  import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
@@ -152,8 +157,13 @@ public class RexNodeConverter {
      // TODO: 1) Expand to other functions as needed 2) What about types other than primitive.
      TypeInfo tgtDT = null;
      GenericUDF tgtUdf = func.getGenericUDF();
- boolean isNumeric = tgtUdf instanceof GenericUDFBaseNumeric,
- isCompare = !isNumeric && tgtUdf instanceof GenericUDFBaseCompare;
+
+ boolean isNumeric = (tgtUdf instanceof GenericUDFBaseBinary
+ && func.getTypeInfo().getCategory() == Category.PRIMITIVE
+ && (PrimitiveGrouping.NUMERIC_GROUP == PrimitiveObjectInspectorUtils.getPrimitiveGrouping(
+ ((PrimitiveTypeInfo) func.getTypeInfo()).getPrimitiveCategory())));
+ boolean isCompare = !isNumeric && tgtUdf instanceof GenericUDFBaseCompare;
+
      if (isNumeric) {
        tgtDT = func.getTypeInfo();

@@ -175,8 +185,7 @@ public class RexNodeConverter {
          } else if (isNumeric) {
            // For numeric, we'll do minimum necessary cast - if we cast to the type
            // of expression, bad things will happen.
- GenericUDFBaseNumeric numericUdf = (GenericUDFBaseNumeric)tgtUdf;
- PrimitiveTypeInfo minArgType = numericUdf.deriveMinArgumentCast(childExpr, tgtDT);
+ PrimitiveTypeInfo minArgType = ExprNodeDescUtils.deriveMinArgumentCast(childExpr, tgtDT);
            tmpExprNode = ParseUtils.createConversionCast(childExpr, minArgType);
          } else {
            throw new AssertionError("Unexpected " + tgtDT + " - not a numeric op or compare");

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java?rev=1663407&r1=1663406&r2=1663407&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java Mon Mar 2 19:35:10 2015
@@ -32,6 +32,10 @@ import org.apache.hadoop.hive.ql.udf.gen
  import org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge;
  import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
  import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory;
+import org.apache.hadoop.hive.serde2.typeinfo.HiveDecimalUtils;
+import org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
  import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
  import org.apache.hadoop.util.ReflectionUtils;

@@ -427,4 +431,17 @@ public class ExprNodeDescUtils {
      }
      return true;
    }
+
+ public static PrimitiveTypeInfo deriveMinArgumentCast(
+ ExprNodeDesc childExpr, TypeInfo targetType) {
+ assert targetType instanceof PrimitiveTypeInfo : "Not a primitive type" + targetType;
+ PrimitiveTypeInfo pti = (PrimitiveTypeInfo)targetType;
+ // We only do the minimum cast for decimals. Other types are assumed safe; fix if needed.
+ // We also don't do anything for non-primitive children (maybe we should assert).
+ if ((pti.getPrimitiveCategory() != PrimitiveCategory.DECIMAL)
+ || (!(childExpr.getTypeInfo() instanceof PrimitiveTypeInfo))) return pti;
+ PrimitiveTypeInfo childTi = (PrimitiveTypeInfo)childExpr.getTypeInfo();
+ // If the child is also decimal, no cast is needed (we hope - can target type be narrower?).
+ return HiveDecimalUtils.getDecimalTypeForPrimitiveCategory(childTi);
+ }
  }

Added: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseArithmetic.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseArithmetic.java?rev=1663407&view=auto
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseArithmetic.java (added)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseArithmetic.java Mon Mar 2 19:35:10 2015
@@ -0,0 +1,108 @@
+/**
+ * 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.hadoop.hive.ql.udf.generic;
+
+import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
+import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping;
+import org.apache.hive.common.HiveCompat;
+import org.apache.hive.common.HiveCompat.CompatLevel;
+
+/**
+ * Wrapper UDF that will instantiate the proper arithmetic UDF (numeric, date, etc)
+ * depending on the argument types
+ */
+public abstract class GenericUDFBaseArithmetic extends GenericUDFBaseBinary {
+ GenericUDF arithmeticOperation;
+
+ // Values needed for numeric arithmetic UDFs
+ protected boolean confLookupNeeded = true;
+ protected boolean ansiSqlArithmetic = false;
+
+ @Override
+ public ObjectInspector initialize(ObjectInspector[] arguments)
+ throws UDFArgumentException {
+
+ if (arguments.length != 2) {
+ throw new UDFArgumentException(getClass().getSimpleName() + " requires two arguments.");
+ }
+
+ // Lookup values needed for numeric arithmetic UDFs
+ if (confLookupNeeded) {
+ CompatLevel compatLevel = HiveCompat.getCompatLevel(SessionState.get().getConf());
+ ansiSqlArithmetic = compatLevel.ordinal() > CompatLevel.HIVE_0_12.ordinal();
+ confLookupNeeded = false;
+ }
+
+ // Determine if we are dealing with a numeric or date arithmetic operation
+ boolean isDateTimeOp = false;
+ for (int idx = 0; idx < 2; ++idx) {
+ if (arguments[idx].getCategory() == Category.PRIMITIVE) {
+ if (PrimitiveGrouping.DATE_GROUP == PrimitiveObjectInspectorUtils.getPrimitiveGrouping(
+ ((PrimitiveObjectInspector) arguments[idx]).getPrimitiveCategory())) {
+ isDateTimeOp = true;
+ break;
+ }
+ }
+ }
+
+ if (isDateTimeOp) {
+ arithmeticOperation = instantiateDTIUDF();
+ } else {
+ GenericUDFBaseNumeric numericUDF = instantiateNumericUDF();
+
+ // Set values needed for numeric arithmetic UDFs
+ numericUDF.setAnsiSqlArithmetic(ansiSqlArithmetic);
+ numericUDF.setConfLookupNeeded(confLookupNeeded);
+ arithmeticOperation = numericUDF;
+ }
+
+ return arithmeticOperation.initialize(arguments);
+ }
+
+ @Override
+ public Object evaluate(DeferredObject[] arguments) throws HiveException {
+ return arithmeticOperation.evaluate(arguments);
+ }
+
+ @Override
+ public void copyToNewInstance(Object newInstance) throws UDFArgumentException {
+ super.copyToNewInstance(newInstance);
+ GenericUDFBaseArithmetic other = (GenericUDFBaseArithmetic) newInstance;
+ other.confLookupNeeded = this.confLookupNeeded;
+ other.ansiSqlArithmetic = this.ansiSqlArithmetic;
+ }
+
+ /**
+ * Instantiate numeric version of the arithmetic UDF
+ * @return arithmetic UDF for numeric types
+ */
+ protected abstract GenericUDFBaseNumeric instantiateNumericUDF();
+
+ /**
+ * Instantiate date-time/interval version of the arithmetic UDF
+ * @return arithmetic UDF for date-time/interval types
+ */
+ protected abstract GenericUDF instantiateDTIUDF();
+}

Added: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseBinary.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseBinary.java?rev=1663407&view=auto
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseBinary.java (added)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseBinary.java Mon Mar 2 19:35:10 2015
@@ -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.hadoop.hive.ql.udf.generic;
+
+/**
+ * Base class for binary operators, overrides getDisplayString()
+ *
+ */
+public abstract class GenericUDFBaseBinary extends GenericUDF {
+ protected String opName = getClass().getSimpleName();
+ protected String opDisplayName; // should be set by child class
+
+ @Override
+ public String getDisplayString(String[] children) {
+ assert (children.length == 2);
+ return "(" + children[0] + " " + opDisplayName + " " + children[1] + ")";
+ }
+}

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseCompare.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseCompare.java?rev=1663407&r1=1663406&r2=1663407&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseCompare.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseCompare.java Mon Mar 2 19:35:10 2015
@@ -44,7 +44,7 @@ import org.apache.hadoop.io.BooleanWrita
   * GenericUDF Base Class for operations.
   */
  @Description(name = "op", value = "a op b - Returns the result of operation")
-public abstract class GenericUDFBaseCompare extends GenericUDF {
+public abstract class GenericUDFBaseCompare extends GenericUDFBaseBinary {
    public enum CompareType {
      // Now only string, text, int, long, byte and boolean comparisons are
      // treated as special cases.
@@ -53,9 +53,6 @@ public abstract class GenericUDFBaseComp
      COMPARE_BOOL, SAME_TYPE, NEED_CONVERT
    }

- protected String opName;
- protected String opDisplayName;
-
    protected transient ObjectInspector[] argumentOIs;

    protected transient ReturnObjectInspectorResolver conversionHelper = null;
@@ -182,12 +179,4 @@ public abstract class GenericUDFBaseComp
            o0, argumentOIs[0], o1, argumentOIs[1]);
      }
    }
-
- @Override
- public String getDisplayString(String[] children) {
- assert (children.length == 2);
- return "(" + children[0] + " " + opDisplayName + " " + children[1] + ")";
-
- }
-
  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseNumeric.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseNumeric.java?rev=1663407&r1=1663406&r2=1663407&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseNumeric.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFBaseNumeric.java Mon Mar 2 19:35:10 2015
@@ -59,9 +59,7 @@ import org.apache.hive.common.HiveCompat
   * GenericUDF Base Class for operations.
   */
  @Description(name = "op", value = "a op b - Returns the result of operation")
-public abstract class GenericUDFBaseNumeric extends GenericUDF {
- protected String opName;
- protected String opDisplayName;
+public abstract class GenericUDFBaseNumeric extends GenericUDFBaseBinary {

    protected transient PrimitiveObjectInspector leftOI;
    protected transient PrimitiveObjectInspector rightOI;
@@ -82,7 +80,6 @@ public abstract class GenericUDFBaseNume
    protected boolean ansiSqlArithmetic = false;

    public GenericUDFBaseNumeric() {
- opName = getClass().getSimpleName();
    }

    @Override
@@ -291,12 +288,6 @@ public abstract class GenericUDFBaseNume

    protected abstract DecimalTypeInfo deriveResultDecimalTypeInfo(int prec1, int scale1, int prec2, int scale2);

- @Override
- public String getDisplayString(String[] children) {
- assert (children.length == 2) : opDisplayName + " with " + children.length + " children";
- return "(" + children[0] + " " + opDisplayName + " " + children[1] + ")";
- }
-
    public void copyToNewInstance(Object newInstance) throws UDFArgumentException {
      super.copyToNewInstance(newInstance);
      GenericUDFBaseNumeric other = (GenericUDFBaseNumeric) newInstance;
@@ -319,17 +310,4 @@ public abstract class GenericUDFBaseNume
    public void setAnsiSqlArithmetic(boolean ansiSqlArithmetic) {
      this.ansiSqlArithmetic = ansiSqlArithmetic;
    }
-
- public PrimitiveTypeInfo deriveMinArgumentCast(
- ExprNodeDesc childExpr, TypeInfo targetType) {
- assert targetType instanceof PrimitiveTypeInfo : "Not a primitive type" + targetType;
- PrimitiveTypeInfo pti = (PrimitiveTypeInfo)targetType;
- // We only do the minimum cast for decimals. Other types are assumed safe; fix if needed.
- // We also don't do anything for non-primitive children (maybe we should assert).
- if ((pti.getPrimitiveCategory() != PrimitiveCategory.DECIMAL)
- || (!(childExpr.getTypeInfo() instanceof PrimitiveTypeInfo))) return pti;
- PrimitiveTypeInfo childTi = (PrimitiveTypeInfo)childExpr.getTypeInfo();
- // If the child is also decimal, no cast is needed (we hope - can target type be narrower?).
- return HiveDecimalUtils.getDecimalTypeForPrimitiveCategory(childTi);
- }
  }

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPMinus.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPMinus.java?rev=1663407&r1=1663406&r2=1663407&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPMinus.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPMinus.java Mon Mar 2 19:35:10 2015
@@ -18,19 +18,10 @@

  package org.apache.hadoop.hive.ql.udf.generic;

-import org.apache.hadoop.hive.common.type.HiveDecimal;
  import org.apache.hadoop.hive.ql.exec.Description;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.*;
-import org.apache.hadoop.hive.serde2.io.ByteWritable;
-import org.apache.hadoop.hive.serde2.io.DoubleWritable;
-import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
-import org.apache.hadoop.hive.serde2.io.ShortWritable;
-import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
-import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
-import org.apache.hadoop.io.FloatWritable;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.io.LongWritable;
+

  @Description(name = "-", value = "a _FUNC_ b - Returns the difference a-b")
  @VectorizedExpressions({LongColSubtractLongColumn.class, LongColSubtractDoubleColumn.class,
@@ -41,7 +32,7 @@ import org.apache.hadoop.io.LongWritable
    DoubleScalarSubtractLongColumn.class, DoubleScalarSubtractDoubleColumn.class,
    DecimalColSubtractDecimalColumn.class, DecimalColSubtractDecimalScalar.class,
    DecimalScalarSubtractDecimalColumn.class})
-public class GenericUDFOPMinus extends GenericUDFBaseNumeric {
+public class GenericUDFOPMinus extends GenericUDFBaseArithmetic {

    public GenericUDFOPMinus() {
      super();
@@ -49,57 +40,13 @@ public class GenericUDFOPMinus extends G
    }

    @Override
- protected ByteWritable evaluate(ByteWritable left, ByteWritable right) {
- byteWritable.set((byte)(left.get() - right.get()));
- return byteWritable;
- }
-
- @Override
- protected ShortWritable evaluate(ShortWritable left, ShortWritable right) {
- shortWritable.set((short)(left.get() - right.get()));
- return shortWritable;
- }
-
- @Override
- protected IntWritable evaluate(IntWritable left, IntWritable right) {
- intWritable.set(left.get() - right.get());
- return intWritable;
- }
-
- @Override
- protected LongWritable evaluate(LongWritable left, LongWritable right) {
- longWritable.set(left.get() - right.get());
- return longWritable;
+ protected GenericUDFBaseNumeric instantiateNumericUDF() {
+ return new GenericUDFOPNumericMinus();
    }

    @Override
- protected FloatWritable evaluate(FloatWritable left, FloatWritable right) {
- floatWritable.set(left.get() - right.get());
- return floatWritable;
+ protected GenericUDF instantiateDTIUDF() {
+ // TODO: implement date-time/interval version of UDF
+ return new GenericUDFOPNumericMinus();
    }
-
- @Override
- protected DoubleWritable evaluate(DoubleWritable left, DoubleWritable right) {
- doubleWritable.set(left.get() - right.get());
- return doubleWritable;
- }
-
- @Override
- protected HiveDecimalWritable evaluate(HiveDecimal left, HiveDecimal right) {
- HiveDecimal dec = left.subtract(right);
- if (dec == null) {
- return null;
- }
- decimalWritable.set(dec);
- return decimalWritable;
- }
-
- @Override
- protected DecimalTypeInfo deriveResultDecimalTypeInfo(int prec1, int scale1, int prec2, int scale2) {
- int intPart = Math.max(prec1 - scale1, prec2 - scale2);
- int scale = Math.max(scale1, scale2);
- int prec = Math.min(intPart + scale + 1, HiveDecimal.MAX_PRECISION);
- return TypeInfoFactory.getDecimalTypeInfo(prec, scale);
- }
-
  }

Added: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericMinus.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericMinus.java?rev=1663407&view=auto
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericMinus.java (added)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericMinus.java Mon Mar 2 19:35:10 2015
@@ -0,0 +1,97 @@
+/**
+ * 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.hadoop.hive.ql.udf.generic;
+
+import org.apache.hadoop.hive.common.type.HiveDecimal;
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.*;
+import org.apache.hadoop.hive.serde2.io.ByteWritable;
+import org.apache.hadoop.hive.serde2.io.DoubleWritable;
+import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
+import org.apache.hadoop.hive.serde2.io.ShortWritable;
+import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
+import org.apache.hadoop.io.FloatWritable;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.LongWritable;
+
+@Description(name = "-", value = "a _FUNC_ b - Returns the difference a-b")
+public class GenericUDFOPNumericMinus extends GenericUDFBaseNumeric {
+
+ public GenericUDFOPNumericMinus() {
+ super();
+ this.opDisplayName = "-";
+ }
+
+ @Override
+ protected ByteWritable evaluate(ByteWritable left, ByteWritable right) {
+ byteWritable.set((byte)(left.get() - right.get()));
+ return byteWritable;
+ }
+
+ @Override
+ protected ShortWritable evaluate(ShortWritable left, ShortWritable right) {
+ shortWritable.set((short)(left.get() - right.get()));
+ return shortWritable;
+ }
+
+ @Override
+ protected IntWritable evaluate(IntWritable left, IntWritable right) {
+ intWritable.set(left.get() - right.get());
+ return intWritable;
+ }
+
+ @Override
+ protected LongWritable evaluate(LongWritable left, LongWritable right) {
+ longWritable.set(left.get() - right.get());
+ return longWritable;
+ }
+
+ @Override
+ protected FloatWritable evaluate(FloatWritable left, FloatWritable right) {
+ floatWritable.set(left.get() - right.get());
+ return floatWritable;
+ }
+
+ @Override
+ protected DoubleWritable evaluate(DoubleWritable left, DoubleWritable right) {
+ doubleWritable.set(left.get() - right.get());
+ return doubleWritable;
+ }
+
+ @Override
+ protected HiveDecimalWritable evaluate(HiveDecimal left, HiveDecimal right) {
+ HiveDecimal dec = left.subtract(right);
+ if (dec == null) {
+ return null;
+ }
+ decimalWritable.set(dec);
+ return decimalWritable;
+ }
+
+ @Override
+ protected DecimalTypeInfo deriveResultDecimalTypeInfo(int prec1, int scale1, int prec2, int scale2) {
+ int intPart = Math.max(prec1 - scale1, prec2 - scale2);
+ int scale = Math.max(scale1, scale2);
+ int prec = Math.min(intPart + scale + 1, HiveDecimal.MAX_PRECISION);
+ return TypeInfoFactory.getDecimalTypeInfo(prec, scale);
+ }
+
+}

Added: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericPlus.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericPlus.java?rev=1663407&view=auto
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericPlus.java (added)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPNumericPlus.java Mon Mar 2 19:35:10 2015
@@ -0,0 +1,108 @@
+/**
+ * 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.hadoop.hive.ql.udf.generic;
+
+import org.apache.hadoop.hive.common.type.HiveDecimal;
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
+import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.*;
+import org.apache.hadoop.hive.serde2.io.ByteWritable;
+import org.apache.hadoop.hive.serde2.io.DoubleWritable;
+import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
+import org.apache.hadoop.hive.serde2.io.ShortWritable;
+import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
+import org.apache.hadoop.io.FloatWritable;
+import org.apache.hadoop.io.IntWritable;
+import org.apache.hadoop.io.LongWritable;
+
+/**
+ * The reason that we list evaluate methods with all numeric types is for both
+ * better performance and type checking (so we know int + int is still an int
+ * instead of a double); otherwise a single method that takes (Number a, Number
+ * b) and use a.doubleValue() == b.doubleValue() is enough.
+ *
+ * The case of int + double will be handled by implicit type casting using
+ * UDFRegistry.implicitConvertable method.
+ */
+@Description(name = "+", value = "a _FUNC_ b - Returns a+b")
+public class GenericUDFOPNumericPlus extends GenericUDFBaseNumeric {
+
+ public GenericUDFOPNumericPlus() {
+ super();
+ this.opDisplayName = "+";
+ }
+
+ @Override
+ protected ByteWritable evaluate(ByteWritable left, ByteWritable right) {
+ byteWritable.set((byte)(left.get() + right.get()));
+ return byteWritable;
+ }
+
+ @Override
+ protected ShortWritable evaluate(ShortWritable left, ShortWritable right) {
+ shortWritable.set((short)(left.get() + right.get()));
+ return shortWritable;
+ }
+
+ @Override
+ protected IntWritable evaluate(IntWritable left, IntWritable right) {
+ intWritable.set(left.get() + right.get());
+ return intWritable;
+ }
+
+ @Override
+ protected LongWritable evaluate(LongWritable left, LongWritable right) {
+ longWritable.set(left.get() + right.get());
+ return longWritable;
+ }
+
+ @Override
+ protected FloatWritable evaluate(FloatWritable left, FloatWritable right) {
+ floatWritable.set(left.get() + right.get());
+ return floatWritable;
+ }
+
+ @Override
+ protected DoubleWritable evaluate(DoubleWritable left, DoubleWritable right) {
+ doubleWritable.set(left.get() + right.get());
+ return doubleWritable;
+ }
+
+ @Override
+ protected HiveDecimalWritable evaluate(HiveDecimal left, HiveDecimal right) {
+ HiveDecimal dec = left.add(right);
+
+ if (dec == null) {
+ return null;
+ }
+
+ decimalWritable.set(dec);
+ return decimalWritable;
+ }
+
+ @Override
+ protected DecimalTypeInfo deriveResultDecimalTypeInfo(int prec1, int scale1, int prec2, int scale2) {
+ int intPart = Math.max(prec1 - scale1, prec2 - scale2);
+ int scale = Math.max(scale1, scale2);
+ int prec = Math.min(intPart + scale + 1, HiveDecimal.MAX_PRECISION);
+ return TypeInfoFactory.getDecimalTypeInfo(prec, scale);
+ }
+
+}

Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPPlus.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPPlus.java?rev=1663407&r1=1663406&r2=1663407&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPPlus.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFOPPlus.java Mon Mar 2 19:35:10 2015
@@ -18,19 +18,9 @@

  package org.apache.hadoop.hive.ql.udf.generic;

-import org.apache.hadoop.hive.common.type.HiveDecimal;
  import org.apache.hadoop.hive.ql.exec.Description;
  import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
  import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.*;
-import org.apache.hadoop.hive.serde2.io.ByteWritable;
-import org.apache.hadoop.hive.serde2.io.DoubleWritable;
-import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
-import org.apache.hadoop.hive.serde2.io.ShortWritable;
-import org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo;
-import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory;
-import org.apache.hadoop.io.FloatWritable;
-import org.apache.hadoop.io.IntWritable;
-import org.apache.hadoop.io.LongWritable;

  /**
   * The reason that we list evaluate methods with all numeric types is for both
@@ -48,7 +38,7 @@ import org.apache.hadoop.io.LongWritable
    LongScalarAddLongColumn.class, LongScalarAddDoubleColumn.class, DoubleScalarAddLongColumn.class,
    DoubleScalarAddDoubleColumn.class, DecimalScalarAddDecimalColumn.class, DecimalColAddDecimalColumn.class,
    DecimalColAddDecimalScalar.class})
-public class GenericUDFOPPlus extends GenericUDFBaseNumeric {
+public class GenericUDFOPPlus extends GenericUDFBaseArithmetic {

    public GenericUDFOPPlus() {
      super();
@@ -56,59 +46,13 @@ public class GenericUDFOPPlus extends Ge
    }

    @Override
- protected ByteWritable evaluate(ByteWritable left, ByteWritable right) {
- byteWritable.set((byte)(left.get() + right.get()));
- return byteWritable;
+ protected GenericUDFBaseNumeric instantiateNumericUDF() {
+ return new GenericUDFOPNumericPlus();
    }

    @Override
- protected ShortWritable evaluate(ShortWritable left, ShortWritable right) {
- shortWritable.set((short)(left.get() + right.get()));
- return shortWritable;
+ protected GenericUDF instantiateDTIUDF() {
+ // TODO: implement date-time/interval version of UDF
+ return new GenericUDFOPNumericPlus();
    }
-
- @Override
- protected IntWritable evaluate(IntWritable left, IntWritable right) {
- intWritable.set(left.get() + right.get());
- return intWritable;
- }
-
- @Override
- protected LongWritable evaluate(LongWritable left, LongWritable right) {
- longWritable.set(left.get() + right.get());
- return longWritable;
- }
-
- @Override
- protected FloatWritable evaluate(FloatWritable left, FloatWritable right) {
- floatWritable.set(left.get() + right.get());
- return floatWritable;
- }
-
- @Override
- protected DoubleWritable evaluate(DoubleWritable left, DoubleWritable right) {
- doubleWritable.set(left.get() + right.get());
- return doubleWritable;
- }
-
- @Override
- protected HiveDecimalWritable evaluate(HiveDecimal left, HiveDecimal right) {
- HiveDecimal dec = left.add(right);
-
- if (dec == null) {
- return null;
- }
-
- decimalWritable.set(dec);
- return decimalWritable;
- }
-
- @Override
- protected DecimalTypeInfo deriveResultDecimalTypeInfo(int prec1, int scale1, int prec2, int scale2) {
- int intPart = Math.max(prec1 - scale1, prec2 - scale2);
- int scale = Math.max(scale1, scale2);
- int prec = Math.min(intPart + scale + 1, HiveDecimal.MAX_PRECISION);
- return TypeInfoFactory.getDecimalTypeInfo(prec, scale);
- }
-
  }

Modified: hive/trunk/ql/src/test/results/clientnegative/invalid_arithmetic_type.q.out
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/results/clientnegative/invalid_arithmetic_type.q.out?rev=1663407&r1=1663406&r2=1663407&view=diff
==============================================================================
--- hive/trunk/ql/src/test/results/clientnegative/invalid_arithmetic_type.q.out (original)
+++ hive/trunk/ql/src/test/results/clientnegative/invalid_arithmetic_type.q.out Mon Mar 2 19:35:10 2015
@@ -1 +1 @@
-FAILED: SemanticException Line 0:-1 Wrong arguments ''2000-01-01 00:00:01'': No matching method for class org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPMinus with (timestamp, timestamp)
+FAILED: SemanticException Line 0:-1 Wrong arguments ''2000-01-01 00:00:01'': No matching method for class org.apache.hadoop.hive.ql.udf.generic.GenericUDFOPNumericMinus with (timestamp, timestamp)

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcommits @
categorieshive, hadoop
postedMar 2, '15 at 7:35p
activeMar 2, '15 at 7:35p
posts1
users1
websitehive.apache.org

1 user in discussion

Hashutosh: 1 post

People

Translate

site design / logo © 2021 Grokbase