tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jihoon...@apache.org
Subject [02/18] git commit: TAJO-350: Implement round, floor, ceil. (hyoungjunkim via hyunsik)
Date Tue, 03 Dec 2013 11:50:57 GMT
TAJO-350: Implement round, floor, ceil. (hyoungjunkim 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/3233d382
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/3233d382
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/3233d382

Branch: refs/heads/DAG-execplan
Commit: 3233d3824b8b8c572afe096f41511794d2633e9c
Parents: dc56c8e
Author: Hyunsik Choi <hyunsik@apache.org>
Authored: Sun Dec 1 18:37:16 2013 +0900
Committer: Hyunsik Choi <hyunsik@apache.org>
Committed: Sun Dec 1 18:37:16 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../apache/tajo/engine/function/math/Ceil.java  | 50 ++++++++++++
 .../apache/tajo/engine/function/math/Floor.java | 50 ++++++++++++
 .../apache/tajo/engine/function/math/Round.java | 50 ++++++++++++
 .../java/org/apache/tajo/master/TajoMaster.java | 31 ++++++++
 .../tajo/engine/function/TestMethFunction.java  | 83 ++++++++++++++++++++
 6 files changed, 266 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/3233d382/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index b99cc69..06bbc5c 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-350: Implement round, floor, ceil. (hyoungjunkim via hyunsik)
+
     TAJO-207: Implement bit_length(string) function. (DaeMyung Kang via jihoon)
 
     TAJO-341: Implement substr function. (hyoungjunkim via hyunsik)

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

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

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

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/3233d382/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 1c823d3..9071da1 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
@@ -46,6 +46,9 @@ import org.apache.tajo.conf.TajoConf.ConfVars;
 import org.apache.tajo.engine.function.Country;
 import org.apache.tajo.engine.function.InCountry;
 import org.apache.tajo.engine.function.builtin.*;
+import org.apache.tajo.engine.function.math.Ceil;
+import org.apache.tajo.engine.function.math.Floor;
+import org.apache.tajo.engine.function.math.Round;
 import org.apache.tajo.engine.function.string.*;
 import org.apache.tajo.master.querymaster.QueryJobManager;
 import org.apache.tajo.master.rm.TajoWorkerResourceManager;
@@ -407,6 +410,34 @@ public class TajoMaster extends CompositeService {
             CatalogUtil.newSimpleDataType(Type.TEXT),
             CatalogUtil.newSimpleDataTypeArray(Type.TEXT, Type.INT4, Type.INT4)));
 
+    sqlFuncs.add(
+        new FunctionDesc("round", Round.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT8)));
+    sqlFuncs.add(
+        new FunctionDesc("round", Round.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT4)));
+
+    sqlFuncs.add(
+        new FunctionDesc("floor", Floor.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT8)));
+    sqlFuncs.add(
+        new FunctionDesc("floor", Floor.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT4)));
+
+    sqlFuncs.add(
+        new FunctionDesc("ceil", Ceil.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT8)));
+
+    sqlFuncs.add(
+        new FunctionDesc("ceil", Ceil.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.FLOAT4)));
+
     return sqlFuncs;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/3233d382/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMethFunction.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMethFunction.java
b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMethFunction.java
new file mode 100644
index 0000000..77b0443
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestMethFunction.java
@@ -0,0 +1,83 @@
+/**
+ * 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;
+
+import org.apache.tajo.catalog.Schema;
+import org.apache.tajo.engine.eval.ExprTestBase;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.apache.tajo.common.TajoDataTypes.Type.FLOAT8;
+
+public class TestMethFunction extends ExprTestBase {
+  @Test
+  public void testRound() throws IOException {
+    testSimpleEval("select round(5.1) as col1 ", new String[]{"5"});
+    testSimpleEval("select round(5.5) as col1 ", new String[]{"6"});
+    testSimpleEval("select round(5.6) as col1 ", new String[]{"6"});
+
+//    testSimpleEval("select round(-5.1) as col1 ", new String[]{"-5"});
+//    testSimpleEval("select round(-5.5) as col1 ", new String[]{"-6"});
+//    testSimpleEval("select round(-5.6) as col1 ", new String[]{"-6"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", FLOAT8);
+    schema.addColumn("col2", FLOAT8);
+    schema.addColumn("col3", FLOAT8);
+
+    testEval(schema, "table1", "1.0, 0.2, 0.4", "select round(col1 + col2 + col3) from table1",
+        new String[]{"2"});
+  }
+
+  @Test
+  public void testFloor() throws IOException {
+    testSimpleEval("select floor(5.1) as col1 ", new String[]{"5"});
+    testSimpleEval("select floor(5.5) as col1 ", new String[]{"5"});
+    testSimpleEval("select floor(5.6) as col1 ", new String[]{"5"});
+//    testSimpleEval("select floor(-5.1) as col1 ", new String[]{"-6"});
+//    testSimpleEval("select floor(-5.6) as col1 ", new String[]{"-6"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", FLOAT8);
+    schema.addColumn("col2", FLOAT8);
+    schema.addColumn("col3", FLOAT8);
+
+    testEval(schema, "table1", "1.0, 0.2, 0.4", "select floor(col1 + col2 + col3) from table1",
+        new String[]{"1"});
+  }
+
+  @Test
+  public void testCeil() throws IOException {
+    testSimpleEval("select ceil(5.0) as col1 ", new String[]{"5"});
+    testSimpleEval("select ceil(5.1) as col1 ", new String[]{"6"});
+    testSimpleEval("select ceil(5.5) as col1 ", new String[]{"6"});
+    testSimpleEval("select ceil(5.6) as col1 ", new String[]{"6"});
+//    testSimpleEval("select ceil(-5.1) as col1 ", new String[]{"-5"});
+//    testSimpleEval("select ceil(-5.6) as col1 ", new String[]{"-5"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", FLOAT8);
+    schema.addColumn("col2", FLOAT8);
+    schema.addColumn("col3", FLOAT8);
+
+    testEval(schema, "table1", "1.0, 0.2, 0.1", "select ceil(col1 + col2 + col3) from table1",
+        new String[]{"2"});
+  }
+}


Mime
View raw message