You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@impala.apache.org by mj...@apache.org on 2017/07/31 16:12:41 UTC

[6/6] incubator-impala git commit: IMPALA-5529: Add additional function signatures for TRUNC()

IMPALA-5529: Add additional function signatures for TRUNC()

The following signatures to be added:
+--------------+----------------------------------+-------------+---------------+
| return type  | signature                        | binary type | is persistent |
+--------------+----------------------------------+-------------+---------------+
| DECIMAL(*,*) | trunc(DECIMAL(*,*))              | BUILTIN     | true          |
| DECIMAL(*,*) | trunc(DECIMAL(*,*), BIGINT)      | BUILTIN     | true          |
| DECIMAL(*,*) | trunc(DECIMAL(*,*), INT)         | BUILTIN     | true          |
| DECIMAL(*,*) | trunc(DECIMAL(*,*), SMALLINT)    | BUILTIN     | true          |
| DECIMAL(*,*) | trunc(DECIMAL(*,*), TINYINT)     | BUILTIN     | true          |
| BIGINT       | trunc(DOUBLE)                    | BUILTIN     | true          |
+--------------+----------------------------------+-------------+---------------+

Tests:
* Adds tests for the new builtin trunc()/dtrunc()

Change-Id: I856da9f817b948de3c72af60a0742b128398b4cf
Reviewed-on: http://gerrit.cloudera.org:8080/7450
Tested-by: Impala Public Jenkins
Reviewed-by: Matthew Jacobs <mj...@cloudera.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-impala/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-impala/commit/15e6cf8f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-impala/tree/15e6cf8f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-impala/diff/15e6cf8f

Branch: refs/heads/master
Commit: 15e6cf8fd03e3ae012435592f0bf4d6a64298099
Parents: c881426
Author: Jinchul <ji...@gmail.com>
Authored: Tue Jul 18 23:39:08 2017 +0900
Committer: Matthew Jacobs <mj...@cloudera.com>
Committed: Sat Jul 29 20:53:45 2017 +0000

----------------------------------------------------------------------
 common/function-registry/impala_functions.py    | 12 +++---
 .../impala/analysis/FunctionCallExpr.java       |  1 +
 .../impala/analysis/AnalyzeExprsTest.java       | 42 ++++++++++++++------
 3 files changed, 36 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-impala/blob/15e6cf8f/common/function-registry/impala_functions.py
----------------------------------------------------------------------
diff --git a/common/function-registry/impala_functions.py b/common/function-registry/impala_functions.py
index 1502809..f43c7c1 100644
--- a/common/function-registry/impala_functions.py
+++ b/common/function-registry/impala_functions.py
@@ -275,7 +275,7 @@ visible_functions = [
   [['degrees'], 'DOUBLE', ['DOUBLE'], 'impala::MathFunctions::Degrees'],
   [['ceil', 'ceiling', 'dceil'], 'BIGINT', ['DOUBLE'], 'impala::MathFunctions::Ceil'],
   [['floor', 'dfloor'], 'BIGINT', ['DOUBLE'], 'impala::MathFunctions::Floor'],
-  [['truncate','dtrunc'], 'BIGINT', ['DOUBLE'], 'impala::MathFunctions::Truncate'],
+  [['truncate','dtrunc','trunc'], 'BIGINT', ['DOUBLE'], 'impala::MathFunctions::Truncate'],
   [['round','dround'], 'BIGINT', ['DOUBLE'], 'impala::MathFunctions::Round'],
   [['round','dround'], 'DOUBLE', ['DOUBLE', 'INT'], 'impala::MathFunctions::RoundUpTo'],
   [['exp', 'dexp'], 'DOUBLE', ['DOUBLE'], 'impala::MathFunctions::Exp'],
@@ -396,14 +396,14 @@ visible_functions = [
   [['round','dround'], 'DECIMAL', ['DECIMAL', 'SMALLINT'], 'impala::DecimalFunctions::RoundTo'],
   [['round','dround'], 'DECIMAL', ['DECIMAL', 'INT'], 'impala::DecimalFunctions::RoundTo'],
   [['round','dround'], 'DECIMAL', ['DECIMAL', 'BIGINT'], 'impala::DecimalFunctions::RoundTo'],
-  [['truncate','dtrunc'], 'DECIMAL', ['DECIMAL'], 'impala::DecimalFunctions::Truncate'],
-  [['truncate','dtrunc'], 'DECIMAL', ['DECIMAL', 'TINYINT'],
+  [['truncate','dtrunc','trunc'], 'DECIMAL', ['DECIMAL'], 'impala::DecimalFunctions::Truncate'],
+  [['truncate','dtrunc','trunc'], 'DECIMAL', ['DECIMAL', 'TINYINT'],
       'impala::DecimalFunctions::TruncateTo'],
-  [['truncate','dtrunc'], 'DECIMAL', ['DECIMAL', 'SMALLINT'],
+  [['truncate','dtrunc','trunc'], 'DECIMAL', ['DECIMAL', 'SMALLINT'],
       'impala::DecimalFunctions::TruncateTo'],
-  [['truncate','dtrunc'], 'DECIMAL', ['DECIMAL', 'INT'],
+  [['truncate','dtrunc','trunc'], 'DECIMAL', ['DECIMAL', 'INT'],
       'impala::DecimalFunctions::TruncateTo'],
-  [['truncate','dtrunc'], 'DECIMAL', ['DECIMAL', 'BIGINT'],
+  [['truncate','dtrunc','trunc'], 'DECIMAL', ['DECIMAL', 'BIGINT'],
       'impala::DecimalFunctions::TruncateTo'],
 
   # String builtin functions

http://git-wip-us.apache.org/repos/asf/incubator-impala/blob/15e6cf8f/fe/src/main/java/org/apache/impala/analysis/FunctionCallExpr.java
----------------------------------------------------------------------
diff --git a/fe/src/main/java/org/apache/impala/analysis/FunctionCallExpr.java b/fe/src/main/java/org/apache/impala/analysis/FunctionCallExpr.java
index 20add13..9364f12 100644
--- a/fe/src/main/java/org/apache/impala/analysis/FunctionCallExpr.java
+++ b/fe/src/main/java/org/apache/impala/analysis/FunctionCallExpr.java
@@ -394,6 +394,7 @@ public class FunctionCallExpr extends Expr {
       digitsAfter = 0;
     } else if (fnName_.getFunction().equalsIgnoreCase("truncate") ||
                fnName_.getFunction().equalsIgnoreCase("dtrunc") ||
+               fnName_.getFunction().equalsIgnoreCase("trunc") ||
                fnName_.getFunction().equalsIgnoreCase("round") ||
                fnName_.getFunction().equalsIgnoreCase("dround")) {
       if (children_.size() > 1) {

http://git-wip-us.apache.org/repos/asf/incubator-impala/blob/15e6cf8f/fe/src/test/java/org/apache/impala/analysis/AnalyzeExprsTest.java
----------------------------------------------------------------------
diff --git a/fe/src/test/java/org/apache/impala/analysis/AnalyzeExprsTest.java b/fe/src/test/java/org/apache/impala/analysis/AnalyzeExprsTest.java
index 6f883a3..5d843bf 100644
--- a/fe/src/test/java/org/apache/impala/analysis/AnalyzeExprsTest.java
+++ b/fe/src/test/java/org/apache/impala/analysis/AnalyzeExprsTest.java
@@ -2193,6 +2193,7 @@ public class AnalyzeExprsTest extends AnalyzerTest {
 
   @Test
   public void TestDecimalFunctions() throws AnalysisException {
+    final String [] aliasesOfTruncate = new String[]{"truncate", "dtrunc", "trunc"};
     AnalyzesOk("select abs(cast(1 as decimal))");
     AnalyzesOk("select abs(cast(-1.1 as decimal(10,3)))");
 
@@ -2205,18 +2206,25 @@ public class AnalyzeExprsTest extends AnalyzerTest {
     AnalyzesOk("select round(cast(1.123 as decimal(10,3)), 5)");
     AnalyzesOk("select round(cast(1.123 as decimal(10,3)), -2)");
 
-    AnalyzesOk("select truncate(cast(1.123 as decimal(10,3)))");
-    AnalyzesOk("select truncate(cast(1.123 as decimal(10,3)), 0)");
-    AnalyzesOk("select truncate(cast(1.123 as decimal(10,3)), 2)");
-    AnalyzesOk("select truncate(cast(1.123 as decimal(10,3)), 5)");
-    AnalyzesOk("select truncate(cast(1.123 as decimal(10,3)), -1)");
+    for (final String alias : aliasesOfTruncate) {
+        AnalyzesOk(String.format("select %s(cast(1.123 as decimal(10,3)))", alias));
+        AnalyzesOk(String.format("select %s(cast(1.123 as decimal(10,3)), 0)", alias));
+        AnalyzesOk(String.format("select %s(cast(1.123 as decimal(10,3)), 2)", alias));
+        AnalyzesOk(String.format("select %s(cast(1.123 as decimal(10,3)), 5)", alias));
+        AnalyzesOk(String.format("select %s(cast(1.123 as decimal(10,3)), -1)", alias));
+    }
 
     AnalysisError("select round(cast(1.123 as decimal(10,3)), 5.1)",
         "No matching function with signature: round(DECIMAL(10,3), DECIMAL(2,1))");
     AnalysisError("select round(cast(1.123 as decimal(30,20)), 40)",
         "Cannot round/truncate to scales greater than 38.");
-    AnalysisError("select truncate(cast(1.123 as decimal(10,3)), 40)",
-        "Cannot round/truncate to scales greater than 38.");
+    for (final String alias : aliasesOfTruncate) {
+        AnalysisError(String.format("select truncate(cast(1.123 as decimal(10,3)), 40)",
+            alias), "Cannot round/truncate to scales greater than 38.");
+        AnalyzesOk(String.format("select %s(NULL)", alias));
+        AnalysisError(String.format("select %s(NULL, 1)", alias),
+            "Cannot resolve DECIMAL precision and scale from NULL type.");
+    }
     AnalysisError("select round(cast(1.123 as decimal(10,3)), NULL)",
         "round() cannot be called with a NULL second argument.");
 
@@ -2244,12 +2252,20 @@ public class AnalyzeExprsTest extends AnalyzerTest {
     testDecimalExpr("ceil(123.45)", ScalarType.createDecimalType(4, 0));
     testDecimalExpr("floor(12.345)", ScalarType.createDecimalType(3, 0));
 
-    testDecimalExpr("truncate(1.23)", ScalarType.createDecimalType(1, 0));
-    testDecimalExpr("truncate(1.23, 1)", ScalarType.createDecimalType(2, 1));
-    testDecimalExpr("truncate(1.23, 0)", ScalarType.createDecimalType(1, 0));
-    testDecimalExpr("truncate(1.23, 3)", ScalarType.createDecimalType(4, 3));
-    testDecimalExpr("truncate(1.23, -1)", ScalarType.createDecimalType(1, 0));
-    testDecimalExpr("truncate(1.23, -2)", ScalarType.createDecimalType(1, 0));
+    for (final String alias : aliasesOfTruncate) {
+        testDecimalExpr(String.format("%s(1.23)", alias),
+            ScalarType.createDecimalType(1, 0));
+        testDecimalExpr(String.format("%s(1.23, 1)", alias),
+            ScalarType.createDecimalType(2, 1));
+        testDecimalExpr(String.format("%s(1.23, 0)", alias),
+            ScalarType.createDecimalType(1, 0));
+        testDecimalExpr(String.format("%s(1.23, 3)", alias),
+            ScalarType.createDecimalType(4, 3));
+        testDecimalExpr(String.format("%s(1.23, -1)", alias),
+            ScalarType.createDecimalType(1, 0));
+        testDecimalExpr(String.format("%s(1.23, -2)", alias),
+            ScalarType.createDecimalType(1, 0));
+    }
   }
 
   /**