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 15:56:57 UTC

git commit: TAJO-394: Implement abs function. (DaeMyung Kang via hyunsik)

Updated Branches:
  refs/heads/master c4d331352 -> 2c7f552a5


TAJO-394: Implement abs 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/2c7f552a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/2c7f552a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/2c7f552a

Branch: refs/heads/master
Commit: 2c7f552a54080d98545bc4b3cb162b48eadcf287
Parents: c4d3313
Author: Hyunsik Choi <hy...@apache.org>
Authored: Sun Dec 8 23:47:26 2013 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Sun Dec 8 23:49:01 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../tajo/engine/function/math/AbsDouble.java    | 50 ++++++++++++++++++++
 .../tajo/engine/function/math/AbsFloat.java     | 50 ++++++++++++++++++++
 .../tajo/engine/function/math/AbsInt.java       | 50 ++++++++++++++++++++
 .../tajo/engine/function/math/AbsLong.java      | 50 ++++++++++++++++++++
 .../java/org/apache/tajo/master/TajoMaster.java | 15 ++++++
 .../tajo/engine/function/TestMathFunctions.java | 32 +++++++++++--
 7 files changed, 244 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2c7f552a/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 72e3c9f..81eba7d 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-394: Implement abs function. (DaeMyung Kang via hyunsik)
+
     TAJO-395: Implement exp function. (DaeMyung Kang via hyunsik)
 
     TAJO-396: Implement sqrt function. (DaeMyung Kang via hyunsik)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2c7f552a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsDouble.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsDouble.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsDouble.java
new file mode 100644
index 0000000..cde4be7
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsDouble.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 abs(value FLOAT8)
+ */
+public class AbsDouble extends GeneralFunction {
+  public AbsDouble() {
+    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.abs(valueDatum.asFloat8()));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2c7f552a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsFloat.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsFloat.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsFloat.java
new file mode 100644
index 0000000..08ea90a
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsFloat.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
+ *
+ * FLOAT4 abs(value FLOAT4)
+ */
+public class AbsFloat extends GeneralFunction {
+  public AbsFloat() {
+    super(new Column[] {
+      new Column("value", TajoDataTypes.Type.FLOAT4)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum valueDatum = params.get(0);
+    if(valueDatum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    return DatumFactory.createFloat4(Math.abs(valueDatum.asFloat4()));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2c7f552a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsInt.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsInt.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsInt.java
new file mode 100644
index 0000000..4f76162
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsInt.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
+ *
+ * INT4 abs(value INT4)
+ */
+public class AbsInt extends GeneralFunction {
+  public AbsInt() {
+    super(new Column[] {
+      new Column("value", TajoDataTypes.Type.INT4)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum valueDatum = params.get(0);
+    if(valueDatum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    return DatumFactory.createInt4(Math.abs(valueDatum.asInt4()));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2c7f552a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsLong.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsLong.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsLong.java
new file mode 100644
index 0000000..9b42417
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/AbsLong.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
+ *
+ * INT8 abs(value INT8)
+ */
+public class AbsLong extends GeneralFunction {
+  public AbsLong() {
+    super(new Column[] {
+      new Column("value", TajoDataTypes.Type.INT8)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum valueDatum = params.get(0);
+    if(valueDatum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    return DatumFactory.createInt8(Math.abs(valueDatum.asInt8()));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2c7f552a/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 190de12..864afcb 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
@@ -655,6 +655,21 @@ public class TajoMaster extends CompositeService {
             CatalogUtil.newSimpleDataType(Type.FLOAT8),
             CatalogUtil.newSimpleDataTypeArray(Type.FLOAT8)));
 
+    //abs
+    sqlFuncs.add(new FunctionDesc("abs", AbsInt.class, FunctionType.GENERAL,
+        CatalogUtil.newSimpleDataType(Type.INT4),
+        CatalogUtil.newSimpleDataTypeArray(Type.INT4)));
+    sqlFuncs.add(new FunctionDesc("abs", AbsLong.class, FunctionType.GENERAL,
+        CatalogUtil.newSimpleDataType(Type.INT8),
+        CatalogUtil.newSimpleDataTypeArray(Type.INT8)));
+    sqlFuncs.add(new FunctionDesc("abs", AbsFloat.class, FunctionType.GENERAL,
+        CatalogUtil.newSimpleDataType(Type.FLOAT4),
+        CatalogUtil.newSimpleDataTypeArray(Type.FLOAT4)));
+    sqlFuncs.add(new FunctionDesc("abs", AbsDouble.class, FunctionType.GENERAL,
+        CatalogUtil.newSimpleDataType(Type.FLOAT8),
+        CatalogUtil.newSimpleDataTypeArray(Type.FLOAT8)));
+
+
     return sqlFuncs;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/2c7f552a/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 e01b3fa..00c4510 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
@@ -281,21 +281,43 @@ public class TestMathFunctions extends ExprTestBase {
 
   @Test
   public void testExp() throws IOException {
-    testSimpleEval("select exp(1.0) as col1 ", new String[]{"2.718281828459045"});
-    testSimpleEval("select exp(1.1) as col1 ", new String[]{"3.0041660239464334"});
-    testSimpleEval("select exp(1.2) as col1 ", new String[]{"3.3201169227365472"});
+    testSimpleEval("select exp(1.0) as col1 ", new String[]{String.valueOf(Math.exp(1.0d))});
+    testSimpleEval("select exp(1.1) as col1 ", new String[]{String.valueOf(Math.exp(1.1d))});
+    testSimpleEval("select exp(1.2) as col1 ", new String[]{String.valueOf(Math.exp(1.2d))});
 
 
     Schema schema = new Schema();
     schema.addColumn("col1", FLOAT4);
 
     testEval(schema, "table1", "1.123", "select exp(col1) from table1",
-        new String[]{"3.074062650703663"});
+        new String[]{String.valueOf(Math.exp(1.123f))});
 
     Schema schema2 = new Schema();
     schema2.addColumn("col1", FLOAT8);
 
     testEval(schema2, "table1", "1.123", "select exp(col1) from table1",
-        new String[]{"3.07406257154899"});
+        new String[]{String.valueOf(Math.exp(1.123d))});
+  }
+
+
+  @Test
+  public void testAbs() throws IOException {
+    testSimpleEval("select abs(9) as col1 ", new String[]{"9"});
+    testSimpleEval("select abs(-9) as col1 ", new String[]{"9"});
+    testSimpleEval("select abs(200000000000) as col1 ", new String[]{"200000000000"});
+    testSimpleEval("select abs(-200000000000) as col1 ", new String[]{"200000000000"});
+    testSimpleEval("select abs(2.0) as col1 ", new String[]{"2.0"});
+    testSimpleEval("select abs(-2.0) as col1 ", new String[]{"2.0"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", FLOAT4);
+    schema.addColumn("col2", FLOAT4);
+    testEval(schema, "table1", "0.39,-0.39", "select abs(col1), abs(col2) from table1", new String[]{"0.39", "0.39"});
+
+    Schema schema2 = new Schema();
+    schema2.addColumn("col1", FLOAT8);
+    schema2.addColumn("col2", FLOAT8);
+    testEval(schema2, "table1", "0.033312347,-0.033312347", "select abs(col1), abs(col2) from table1",
+        new String[]{"0.033312347", "0.033312347"});
   }
 }