drill-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From meh...@apache.org
Subject [1/3] drill git commit: DRILL-2356: Fix round function for exact input types
Date Thu, 16 Apr 2015 21:15:27 GMT
Repository: drill
Updated Branches:
  refs/heads/master cb47df0c8 -> 64e3ec52b


DRILL-2356: Fix round function for exact input types


Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/a7e95904
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/a7e95904
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/a7e95904

Branch: refs/heads/master
Commit: a7e959040dac7562aed6f86a62e21a2d192d67b0
Parents: cb47df0
Author: Mehant Baid <mehantr@gmail.com>
Authored: Tue Apr 14 17:59:34 2015 -0700
Committer: Mehant Baid <mehantr@gmail.com>
Committed: Thu Apr 16 10:48:36 2015 -0700

----------------------------------------------------------------------
 .../src/main/codegen/data/MathFunc.tdd          |  13 --
 .../exec/expr/fn/impl/conv/RoundFunctions.java  | 193 +++++++++++++++++++
 .../org/apache/drill/TestFunctionsQuery.java    |  18 ++
 3 files changed, 211 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/a7e95904/exec/java-exec/src/main/codegen/data/MathFunc.tdd
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/codegen/data/MathFunc.tdd b/exec/java-exec/src/main/codegen/data/MathFunc.tdd
index 154836c..c8bd95c 100644
--- a/exec/java-exec/src/main/codegen/data/MathFunc.tdd
+++ b/exec/java-exec/src/main/codegen/data/MathFunc.tdd
@@ -56,19 +56,6 @@ unaryMathFunctions : [
       {input: "UInt8", outputType: "UInt8", castType: "long"}
      ]
    	},
-  	{className: "Round", funcName: "round", javaFunc : "java.lang.Math.round", types: [
-      {input: "Int",  outputType: "Int", castType: "int"},
-      {input: "BigInt",  outputType: "BigInt", castType: "long"},
-      {input: "Float4", outputType: "Float4", castType: "float"},
-      {input: "Float8", outputType: "Float8", castType: "double"},
-      {input: "SmallInt",  outputType: "SmallInt", castType: "short"},
-      {input: "TinyInt",  outputType: "TinyInt", castType: "byte"},
-      {input: "UInt1", outputType: "UInt1", castType: "byte"},
-      {input: "UInt2", outputType: "UInt2", castType: "char"},
-      {input: "UInt4", outputType: "UInt4", castType: "int"},
-      {input: "UInt8", outputType: "UInt8", castType: "long"}
-     ]
-   	},
   	{className: "Sqrt", funcName: "sqrt", javaFunc : "java.lang.Math.sqrt", types: [
       {input: "Int",  outputType: "Int", castType: "int"},
       {input: "BigInt",  outputType: "BigInt", castType: "long"},

http://git-wip-us.apache.org/repos/asf/drill/blob/a7e95904/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/RoundFunctions.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/RoundFunctions.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/RoundFunctions.java
new file mode 100644
index 0000000..db39d15
--- /dev/null
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/conv/RoundFunctions.java
@@ -0,0 +1,193 @@
+/*******************************************************************************
+
+ * 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.drill.exec.expr.fn.impl.conv;
+
+import org.apache.drill.exec.expr.DrillSimpleFunc;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate;
+import org.apache.drill.exec.expr.annotations.Output;
+import org.apache.drill.exec.expr.annotations.Param;
+import org.apache.drill.exec.expr.holders.SmallIntHolder;
+import org.apache.drill.exec.expr.holders.TinyIntHolder;
+import org.apache.drill.exec.expr.holders.IntHolder;
+import org.apache.drill.exec.expr.holders.BigIntHolder;
+import org.apache.drill.exec.expr.holders.Float4Holder;
+import org.apache.drill.exec.expr.holders.Float8Holder;
+import org.apache.drill.exec.expr.holders.UInt1Holder;
+import org.apache.drill.exec.expr.holders.UInt2Holder;
+import org.apache.drill.exec.expr.holders.UInt4Holder;
+import org.apache.drill.exec.expr.holders.UInt8Holder;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate.FunctionScope;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
+
+
+public class RoundFunctions {
+
+  /*
+   * Following are round functions with no parameter. Per the SQL standard we simply return
the same output
+   * type as the input type for exact inputs (int, bigint etc) and inexact types (float,
double).
+   *
+   * TODO: Need to incorporate round function which accepts two parameters here.
+   */
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
+  public static class RoundInt implements DrillSimpleFunc {
+
+    @Param  IntHolder in;
+    @Output IntHolder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundBigInt implements DrillSimpleFunc {
+
+    @Param BigIntHolder in;
+    @Output BigIntHolder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundSmallInt implements DrillSimpleFunc {
+
+    @Param SmallIntHolder in;
+    @Output SmallIntHolder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundTinyInt implements DrillSimpleFunc {
+
+    @Param TinyIntHolder in;
+    @Output TinyIntHolder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundUInt1 implements DrillSimpleFunc {
+
+    @Param UInt1Holder in;
+    @Output UInt1Holder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundUInt2 implements DrillSimpleFunc {
+
+    @Param UInt2Holder in;
+    @Output UInt2Holder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundUInt4 implements DrillSimpleFunc {
+
+    @Param UInt4Holder in;
+    @Output UInt4Holder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundUInt8 implements DrillSimpleFunc {
+
+    @Param UInt8Holder in;
+    @Output UInt8Holder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      out.value = in.value;
+    }
+  }
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundFloat4 implements DrillSimpleFunc {
+
+    @Param Float4Holder in;
+    @Output Float4Holder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      java.math.BigDecimal input = java.math.BigDecimal.valueOf(in.value);
+      out.value = input.setScale(0, java.math.RoundingMode.HALF_UP).floatValue();
+    }
+  }
+
+  @FunctionTemplate(name = "round", scope = FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+  public static class RoundFloat8 implements DrillSimpleFunc {
+
+    @Param Float8Holder in;
+    @Output Float8Holder out;
+
+    public void setup() {
+    }
+
+    public void eval() {
+      java.math.BigDecimal input = java.math.BigDecimal.valueOf(in.value);
+      out.value = input.setScale(0, java.math.RoundingMode.HALF_UP).doubleValue();
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/drill/blob/a7e95904/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java b/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java
index 9a483a1..4b9001f 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/TestFunctionsQuery.java
@@ -472,6 +472,24 @@ public class TestFunctionsQuery extends BaseTestQuery {
   }
 
   @Test
+  public void testRoundWithOneParam() throws Exception {
+    String query = "select " +
+        "round(8124674407369523212) round_bigint," +
+        "round(9999999) round_int, " +
+        "round(cast('23.45' as float)) round_float_1, " +
+        "round(cast('23.55' as float)) round_float_2, " +
+        "round(8124674407369.2345) round_double_1, " +
+        "round(8124674407369.589) round_double_2 " +
+        " from cp.`tpch/region.parquet` limit 1";
+    testBuilder()
+        .sqlQuery(query)
+        .unOrdered()
+        .baselineColumns("round_bigint", "round_int", "round_float_1", "round_float_2", "round_double_1",
"round_double_2")
+        .baselineValues(8124674407369523212l, 9999999, 23.0f, 24.0f, 8124674407369.0d, 8124674407370.0d)
+        .go();
+  }
+
+  @Test
   public void testToCharFunction() throws Exception {
     String query = "SELECT " +
         "to_char(1234.5567, '#,###.##') as FLOAT8_1, " +


Mime
View raw message