You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by hy...@apache.org on 2013/12/08 13:43:21 UTC

git commit: TAJO-396: Implement sqrt function. (DaeMyung Kang via hyunsik)

Updated Branches:
  refs/heads/master 3a05bbf3d -> 595675cc7


TAJO-396: Implement sqrt function. (DaeMyung Kang via hyunsik)


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

Branch: refs/heads/master
Commit: 595675cc7190de87f60bcf3193c42ef1190c2837
Parents: 3a05bbf
Author: Hyunsik Choi <hy...@apache.org>
Authored: Sun Dec 8 21:39:48 2013 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Sun Dec 8 21:39:48 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../apache/tajo/engine/function/math/Sqrt.java  | 50 ++++++++++++++++++++
 .../java/org/apache/tajo/master/TajoMaster.java | 10 ++++
 .../tajo/engine/function/TestMathFunctions.java | 25 ++++++++++
 4 files changed, 87 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/595675cc/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 534b17d..91a68e2 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-396: Implement sqrt function. (DaeMyung Kang via hyunsik)
+
     TAJO-397: Implement sign function. (DaeMyung Kang via hyunsik)
 
     TAJO-343: Implement locate function. (KyoungBok Lee via hyunsik)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/595675cc/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Sqrt.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Sqrt.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Sqrt.java
new file mode 100644
index 0000000..35fa04c
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Sqrt.java
@@ -0,0 +1,50 @@
+/**
+ * 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.tajo.engine.function.math;
+
+import org.apache.tajo.catalog.Column;
+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.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+
+/**
+ * Function definition
+ *
+ * FLOAT8 sqrt(value FLOAT8)
+ */
+public class Sqrt extends GeneralFunction {
+  public Sqrt() {
+    super(new Column[] {
+      new Column("value", TajoDataTypes.Type.FLOAT8)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum valueDatum = params.get(0);
+    if(valueDatum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    return DatumFactory.createFloat8(Math.sqrt(valueDatum.asFloat8()));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/595675cc/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMaster.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMaster.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMaster.java
index 37a5e75..86ba42a 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMaster.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/master/TajoMaster.java
@@ -635,6 +635,16 @@ public class TajoMaster extends CompositeService {
             CatalogUtil.newSimpleDataType(Type.FLOAT8),
             CatalogUtil.newSimpleDataTypeArray(Type.FLOAT8)));
 
+    sqlFuncs.add(
+        new FunctionDesc("sqrt", Sqrt.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.FLOAT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT4)));
+
+    sqlFuncs.add(
+        new FunctionDesc("sqrt", Sqrt.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.FLOAT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT8)));
+
     return sqlFuncs;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/595675cc/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
index 2cd4ea1..4132238 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMathFunctions.java
@@ -253,4 +253,29 @@ public class TestMathFunctions extends ExprTestBase {
     testEval(schema2, "table1", "1.0, 0.2, 0.1", "select sign(col1 + col2 + col3) from table1",
         new String[]{"1.0"});
   }
+
+  @Test
+  public void testSqrt() throws IOException {
+    testSimpleEval("select sqrt(27.0) as col1 ", new String[]{"5.196152422706632"});
+    testSimpleEval("select sqrt(64.0) as col1 ", new String[]{"8.0"});
+    testSimpleEval("select sqrt(8.0) as col1 ", new String[]{"2.8284271247461903"});
+
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", FLOAT4);
+    schema.addColumn("col2", FLOAT4);
+    schema.addColumn("col3", FLOAT4);
+
+    testEval(schema, "table1", "1.0, 0.2, 0.1", "select sqrt(col1 + col2 + col3) from table1",
+        new String[]{"1.1401754466632896"});
+
+
+    Schema schema2 = new Schema();
+    schema2.addColumn("col1", FLOAT8);
+    schema2.addColumn("col2", FLOAT8);
+    schema2.addColumn("col3", FLOAT8);
+
+    testEval(schema2, "table1", "1.0, 0.2, 0.1", "select sqrt(col1 + col2 + col3) from table1",
+        new String[]{"1.140175425099138"});
+  }
 }