tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jihoon...@apache.org
Subject [01/17] git commit: TAJO-978: RoundFloat8 should return Float8Datum type. (Hyoungjun Kim via hyunsik)
Date Mon, 11 Aug 2014 08:11:30 GMT
Repository: tajo
Updated Branches:
  refs/heads/index_support 5c0277fd8 -> fa8f037bb


TAJO-978: RoundFloat8 should return Float8Datum type. (Hyoungjun Kim via hyunsik)

Closes #96


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

Branch: refs/heads/index_support
Commit: 650157c5907c43a2f9228cd9a3ff59619e24efb6
Parents: 8024f6a
Author: Hyunsik Choi <hyunsik@apache.org>
Authored: Fri Aug 1 15:06:14 2014 +0900
Committer: Hyunsik Choi <hyunsik@apache.org>
Committed: Fri Aug 1 15:11:46 2014 +0900

----------------------------------------------------------------------
 CHANGES                                         |  3 ++
 .../tajo/engine/function/math/RoundFloat8.java  | 39 ++++++++------------
 .../tajo/engine/function/TestMathFunctions.java | 27 +++++++++-----
 .../tajo/engine/query/TestSelectQuery.java      | 21 +++++++++++
 .../TestSelectQuery/testCaseWhenRound.sql       |  8 ++++
 .../TestSelectQuery/testCaseWhenRound.result    | 22 +++++++++++
 6 files changed, 87 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/650157c5/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index f423548..76cfff7 100644
--- a/CHANGES
+++ b/CHANGES
@@ -97,6 +97,9 @@ Release 0.9.0 - unreleased
 
   BUG FIXES
     
+    TAJO-978: RoundFloat8 should return Float8Datum type. (Hyoungjun Kim via 
+    hyunsik)
+
     TAJO-957: ROUND should be support INT parameter. (Mai Hai Thanh via hyunsik)
 
     TAJO-980: execution page in Web UI broken. (hyunsik)

http://git-wip-us.apache.org/repos/asf/tajo/blob/650157c5/tajo-core/src/main/java/org/apache/tajo/engine/function/math/RoundFloat8.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/main/java/org/apache/tajo/engine/function/math/RoundFloat8.java
b/tajo-core/src/main/java/org/apache/tajo/engine/function/math/RoundFloat8.java
index 56d40e4..d104431 100644
--- a/tajo-core/src/main/java/org/apache/tajo/engine/function/math/RoundFloat8.java
+++ b/tajo-core/src/main/java/org/apache/tajo/engine/function/math/RoundFloat8.java
@@ -23,13 +23,14 @@ import org.apache.tajo.common.TajoDataTypes;
 import org.apache.tajo.datum.Datum;
 import org.apache.tajo.datum.DatumFactory;
 import org.apache.tajo.datum.NullDatum;
-import org.apache.tajo.engine.eval.FunctionEval;
 import org.apache.tajo.engine.function.GeneralFunction;
 import org.apache.tajo.engine.function.annotation.Description;
 import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.exception.InvalidOperationException;
 import org.apache.tajo.storage.Tuple;
 
-import java.text.NumberFormat;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
 
 /**
  * Function definition
@@ -46,9 +47,6 @@ import java.text.NumberFormat;
         @ParamTypes(paramTypes = {TajoDataTypes.Type.INT8, TajoDataTypes.Type.INT4})}
 )
 public class RoundFloat8 extends GeneralFunction {
-  private NumberFormat numberFormat;
-  private boolean formatConstant;
-
   public RoundFloat8() {
     super(new Column[] {
         new Column("value", TajoDataTypes.Type.FLOAT8),
@@ -57,11 +55,6 @@ public class RoundFloat8 extends GeneralFunction {
   }
 
   @Override
-  public void init(FunctionEval.ParamType [] paramTypes) {
-    formatConstant = paramTypes[1] == FunctionEval.ParamType.CONSTANT;
-  }
-
-  @Override
   public Datum eval(Tuple params) {
     Datum valueDatum = params.get(0);
     Datum roundDatum = params.get(1);
@@ -70,23 +63,23 @@ public class RoundFloat8 extends GeneralFunction {
       return NullDatum.get();
     }
 
-    if (numberFormat == null || !formatConstant) {
-      numberFormat = NumberFormat.getInstance();
-      numberFormat.setGroupingUsed(false);
-      numberFormat.setMaximumFractionDigits(roundDatum.asInt4());
-    }
-
     double value = valueDatum.asFloat8();
-    int roundPnt = roundDatum.asInt4();
-    double roundNum;
+    int rountPoint = roundDatum.asInt4();
 
-    if (value > 0) {
-      roundNum = (long)(value * Math.pow(10, roundPnt) + 0.5d) / Math.pow(10, roundPnt);
+    if (Double.isNaN(value)) {
+      throw new InvalidOperationException("value is not a number.");
     }
-    else {
-      roundNum = (long)(value * Math.pow(10, roundPnt) - 0.5d) / Math.pow(10, roundPnt);
+
+    if (Double.isInfinite(value)) {
+      throw new InvalidOperationException("value is infinite.");
     }
 
-    return DatumFactory.createText(numberFormat.format(roundNum));
+    try {
+      return DatumFactory.createFloat8(BigDecimal.valueOf(value).setScale(rountPoint,
+          RoundingMode.HALF_UP).doubleValue());
+    } catch (Exception e) {
+      throw new InvalidOperationException("RoundFloat8 eval error cause " + e.getMessage()
+ ", value=" + value +
+          ", round point=" + rountPoint);
+    }
   }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/650157c5/tajo-core/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
index 41fcbfe..8068180 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
@@ -20,13 +20,13 @@ package org.apache.tajo.engine.function;
 
 import org.apache.tajo.catalog.Schema;
 import org.apache.tajo.engine.eval.ExprTestBase;
+import org.apache.tajo.exception.InvalidOperationException;
 import org.junit.Test;
 
 import java.io.IOException;
-import java.sql.ResultSet;
 
 import static org.apache.tajo.common.TajoDataTypes.Type.*;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
 
 public class TestMathFunctions extends ExprTestBase {
   @Test
@@ -440,19 +440,26 @@ public class TestMathFunctions extends ExprTestBase {
 
   @Test
   public void testRoundWithSpecifiedPrecision() throws IOException {
+    // divide zero
+    try {
+      testSimpleEval("select round(10.0/0.0,2) ", new String[]{""});
+      fail("10.0/0 should throw InvalidOperationException");
+    } catch (InvalidOperationException e) {
+      //success
+    }
+
     testSimpleEval("select round(42.4382,2) ", new String[]{"42.44"});
     testSimpleEval("select round(-42.4382,2) ", new String[]{"-42.44"});
-    testSimpleEval("select round(-425,2) ", new String[]{"-425"});
-    testSimpleEval("select round(425,2) ", new String[]{"425"});
+    testSimpleEval("select round(-425,2) ", new String[]{"-425.0"});
+    testSimpleEval("select round(425,2) ", new String[]{"425.0"});
 
-    testSimpleEval("select round(1234567890,0) ", new String[]{"1234567890"});
-    testSimpleEval("select round(1234567890,1) ", new String[]{"1234567890"});
-    testSimpleEval("select round(1234567890,2) ", new String[]{"1234567890"});
+    testSimpleEval("select round(1234567890,0) ", new String[]{"1.23456789E9"});
+    testSimpleEval("select round(1234567890,1) ", new String[]{"1.23456789E9"});
+    testSimpleEval("select round(1234567890,2) ", new String[]{"1.23456789E9"});
 
     testSimpleEval("select round(1.2345678901234567,13) ", new String[]{"1.2345678901235"});
-    testSimpleEval("select round(1234567890.1234567,3) ", new String[]{"1234567890.123"});
-    testSimpleEval("select round(1234567890.1234567,5) ", new String[]{"1234567890.12346"});
-    //testSimpleEval("select round(1234567890.1234567890,7) ", new String[]{"1234567890.1234568"});
+    testSimpleEval("select round(1234567890.1234567,3) ", new String[]{"1.234567890123E9"});
+    testSimpleEval("select round(1234567890.1234567,5) ", new String[]{"1.23456789012346E9"});
 
     Schema schema = new Schema();
     schema.addColumn("col1", FLOAT8);

http://git-wip-us.apache.org/repos/asf/tajo/blob/650157c5/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
index 8898067..2fd7b14 100644
--- a/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
+++ b/tajo-core/src/test/java/org/apache/tajo/engine/query/TestSelectQuery.java
@@ -470,4 +470,25 @@ public class TestSelectQuery extends QueryTestCaseBase {
       executeString("DROP TABLE table11 PURGE");
     }
   }
+
+  @Test
+  public void testCaseWhenRound() throws Exception {
+    /*
+    select *
+        from (select n_nationkey as key,
+    case
+      when n_nationkey > 6 then round((n_nationkey * 100 / 2.123) / (n_regionkey * 50
/ 2.123), 2) else 100.0 end as val
+    from
+      nation
+    where
+      n_regionkey > 0 and n_nationkey > 0
+    ) a
+    order by
+      a.key
+    */
+
+    ResultSet res = executeQuery();
+    assertResultSet(res);
+    cleanupQuery(res);
+  }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/650157c5/tajo-core/src/test/resources/queries/TestSelectQuery/testCaseWhenRound.sql
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/queries/TestSelectQuery/testCaseWhenRound.sql b/tajo-core/src/test/resources/queries/TestSelectQuery/testCaseWhenRound.sql
new file mode 100644
index 0000000..cee905a
--- /dev/null
+++ b/tajo-core/src/test/resources/queries/TestSelectQuery/testCaseWhenRound.sql
@@ -0,0 +1,8 @@
+select *
+  from (select n_nationkey as key,
+               case when n_nationkey > 6 then round((n_nationkey * 100 / 2.123) / (n_regionkey
* 50 / 2.123), 2) else 100.0 end as val
+          from nation
+         where n_regionkey > 0
+           and n_nationkey > 0
+  ) a
+order by a.key
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tajo/blob/650157c5/tajo-core/src/test/resources/results/TestSelectQuery/testCaseWhenRound.result
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/resources/results/TestSelectQuery/testCaseWhenRound.result
b/tajo-core/src/test/resources/results/TestSelectQuery/testCaseWhenRound.result
new file mode 100644
index 0000000..2a50cd0
--- /dev/null
+++ b/tajo-core/src/test/resources/results/TestSelectQuery/testCaseWhenRound.result
@@ -0,0 +1,22 @@
+key,val
+-------------------------------
+1,100.0
+2,100.0
+3,100.0
+4,100.0
+6,100.0
+7,4.67
+8,8.0
+9,9.0
+10,5.0
+11,5.5
+12,12.0
+13,6.5
+17,34.0
+18,18.0
+19,12.67
+20,10.0
+21,21.0
+22,14.67
+23,15.33
+24,48.0


Mime
View raw message