You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by xu...@apache.org on 2014/01/30 22:16:48 UTC
svn commit: r1562967 - in /hive/trunk:
common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java
ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFSign.java
Author: xuefu
Date: Thu Jan 30 21:16:47 2014
New Revision: 1562967
URL: http://svn.apache.org/r1562967
Log:
HIVE-6246: Sign(a) UDF is not supported for decimal type
Added:
hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFSign.java
Modified:
hive/trunk/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java
hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java
Modified: hive/trunk/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java
URL: http://svn.apache.org/viewvc/hive/trunk/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java?rev=1562967&r1=1562966&r2=1562967&view=diff
==============================================================================
--- hive/trunk/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java (original)
+++ hive/trunk/common/src/java/org/apache/hadoop/hive/common/type/HiveDecimal.java Thu Jan 30 21:16:47 2014
@@ -205,6 +205,14 @@ public class HiveDecimal implements Comp
return create(bd.divide(dec.bd, MAX_SCALE, RoundingMode.HALF_UP), true);
}
+ /**
+ * Get the sign of the underlying decimal.
+ * @return 0 if the decimal is equal to 0, -1 if less than zero, and 1 if greater than 0
+ */
+ public int signum() {
+ return bd.signum();
+ }
+
private static BigDecimal trim(BigDecimal d) {
if (d.compareTo(BigDecimal.ZERO) == 0) {
// Special case for 0, because java doesn't strip zeros correctly on that number.
Modified: hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java?rev=1562967&r1=1562966&r2=1562967&view=diff
==============================================================================
--- hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java (original)
+++ hive/trunk/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFSign.java Thu Jan 30 21:16:47 2014
@@ -26,6 +26,8 @@ import org.apache.hadoop.hive.ql.exec.ve
import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncSignDoubleToDouble;
import org.apache.hadoop.hive.ql.exec.vector.expressions.gen.FuncSignLongToDouble;
import org.apache.hadoop.hive.serde2.io.DoubleWritable;
+import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
+import org.apache.hadoop.io.IntWritable;
@Description(
name = "sign",
@@ -40,6 +42,7 @@ public class UDFSign extends UDF {
@SuppressWarnings("unused")
private static Log LOG = LogFactory.getLog(UDFSign.class.getName());
DoubleWritable result = new DoubleWritable();
+ IntWritable intWritable = new IntWritable();
public UDFSign() {
}
@@ -63,4 +66,18 @@ public class UDFSign extends UDF {
return result;
}
+ /**
+ * Get the sign of the decimal input
+ * @param dec decimal input
+ * @return -1, 0, or 1 representing the sign of the input decimal
+ */
+ public IntWritable evaluate(HiveDecimalWritable dec) {
+ if (dec == null || dec.getHiveDecimal() == null) {
+ return null;
+ }
+
+ intWritable.set(dec.getHiveDecimal().signum());
+ return intWritable;
+ }
+
}
Added: hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFSign.java
URL: http://svn.apache.org/viewvc/hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFSign.java?rev=1562967&view=auto
==============================================================================
--- hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFSign.java (added)
+++ hive/trunk/ql/src/test/org/apache/hadoop/hive/ql/udf/TestUDFSign.java Thu Jan 30 21:16:47 2014
@@ -0,0 +1,47 @@
+/**
+ * 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;
+
+import org.apache.hadoop.hive.common.type.HiveDecimal;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
+import org.apache.hadoop.io.IntWritable;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestUDFSign {
+
+ @Test
+ public void testDecimalSign() throws HiveException {
+ UDFSign udf = new UDFSign();
+
+ HiveDecimalWritable input = new HiveDecimalWritable(HiveDecimal.create("32300.004747"));
+ IntWritable res = udf.evaluate(input);
+ Assert.assertEquals(1, res.get());
+
+ input = new HiveDecimalWritable(HiveDecimal.create("-30.047"));
+ res = udf.evaluate(input);
+ Assert.assertEquals(-1, res.get());
+
+ input = new HiveDecimalWritable(HiveDecimal.ZERO);
+ res = udf.evaluate(input);
+ Assert.assertEquals(0, res.get());
+ }
+
+}