You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by bl...@apache.org on 2013/12/05 07:46:31 UTC

git commit: TAJO-364: Implement mod/div function. (DaeMyung Kang via jaehwa)

Updated Branches:
  refs/heads/master 96665ff0b -> 45de54bcd


TAJO-364: Implement mod/div function. (DaeMyung Kang via jaehwa)


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

Branch: refs/heads/master
Commit: 45de54bcd915f125c6bf39f3fb62d5b4f0d0780f
Parents: 96665ff
Author: blrunner <jh...@gruter.com>
Authored: Thu Dec 5 15:46:19 2013 +0900
Committer: blrunner <jh...@gruter.com>
Committed: Thu Dec 5 15:46:19 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../apache/tajo/engine/function/math/Div.java   | 63 ++++++++++++++++++++
 .../apache/tajo/engine/function/math/Mod.java   | 63 ++++++++++++++++++++
 .../java/org/apache/tajo/master/TajoMaster.java | 32 ++++++++++
 .../tajo/engine/function/TestMathFunctions.java | 48 +++++++++++++++
 5 files changed, 208 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/45de54bc/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 392c010..96e444b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-364: Implement mod/div function. (DaeMyung Kang via jaehwa)
+
     TAJO-361: Implement rpad function. (Seungun Choe via jaehwa)
 
     TAJO-359: Implement lpad function. (Seungun Choe via jaehwa)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/45de54bc/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Div.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Div.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Div.java
new file mode 100644
index 0000000..7bf2d20
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Div.java
@@ -0,0 +1,63 @@
+/**
+ * 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 div(value INT8, value INT8)
+ */
+public class Div extends GeneralFunction {
+  public Div() {
+    super(new Column[] {
+      new Column("value1", TajoDataTypes.Type.INT8),
+      new Column("value2", TajoDataTypes.Type.INT8)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum value1Datum = params.get(0);
+    if(value1Datum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    Datum value2Datum = params.get(1);
+    if(value2Datum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    long value1 = value1Datum.asInt8();
+    long value2 = value2Datum.asInt8();
+
+    if (value2 == 0) {
+      return NullDatum.get();
+    }
+
+    return DatumFactory.createInt8(value1/value2);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/45de54bc/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Mod.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Mod.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Mod.java
new file mode 100644
index 0000000..0878464
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/math/Mod.java
@@ -0,0 +1,63 @@
+/**
+ * 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 mod(value INT8, value INT8)
+ */
+public class Mod extends GeneralFunction {
+  public Mod() {
+    super(new Column[] {
+      new Column("value1", TajoDataTypes.Type.INT8),
+      new Column("value2", TajoDataTypes.Type.INT8)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum value1Datum = params.get(0);
+    if(value1Datum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    Datum value2Datum = params.get(1);
+    if(value2Datum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    long value1 = value1Datum.asInt8();
+    long value2 = value2Datum.asInt8();
+
+    if (value2 == 0) {
+      return NullDatum.get();
+    }
+
+    return DatumFactory.createInt8(value1%value2);
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/45de54bc/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 5a8fea0..fa3732d 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
@@ -557,7 +557,39 @@ public class TajoMaster extends CompositeService {
             CatalogUtil.newSimpleDataType(Type.FLOAT8),
             CatalogUtil.newSimpleDataTypeArray(Type.FLOAT4, Type.FLOAT4)));
 
+    sqlFuncs.add(
+        new FunctionDesc("mod", Mod.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT8, Type.INT8)));
+    sqlFuncs.add(
+        new FunctionDesc("mod", Mod.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT4, Type.INT4)));
+    sqlFuncs.add(
+        new FunctionDesc("mod", Mod.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT4, Type.INT8)));
+    sqlFuncs.add(
+        new FunctionDesc("mod", Mod.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT8, Type.INT4)));
 
+    sqlFuncs.add(
+        new FunctionDesc("div", Div.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT8, Type.INT8)));
+    sqlFuncs.add(
+        new FunctionDesc("div", Div.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT4, Type.INT4)));
+    sqlFuncs.add(
+        new FunctionDesc("div", Div.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT4, Type.INT8)));
+    sqlFuncs.add(
+        new FunctionDesc("div", Div.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.INT8),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT8, Type.INT4)));
 
     sqlFuncs.add(
         new FunctionDesc("initcap", InitCap.class, FunctionType.GENERAL,

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/45de54bc/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 703d49d..d905a1b 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
@@ -25,6 +25,7 @@ import org.junit.Test;
 import java.io.IOException;
 
 import static org.apache.tajo.common.TajoDataTypes.Type.FLOAT8;
+import static org.apache.tajo.common.TajoDataTypes.Type.INT8;
 
 public class TestMathFunctions extends ExprTestBase {
   @Test
@@ -195,5 +196,52 @@ public class TestMathFunctions extends ExprTestBase {
         new String[]{"1.4876550949064553"});
   }
 
+  @Test
+  public void testMod() throws IOException {
+    testSimpleEval("select mod(9,4) as col1 ", new String[]{"1"});
+    testSimpleEval("select mod(200000000001,200000000000) as col1 ", new String[]{"1"});
+    testSimpleEval("select mod(200000000000,2) as col1 ", new String[]{"0"});
+    testSimpleEval("select mod(2,200000000000) as col1 ", new String[]{"2"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", INT8);
+    schema.addColumn("col2", INT8);
+    schema.addColumn("col3", INT8);
 
+    testEval(schema, "table1", "9,2,3", "select mod(col1 + col2, col3) from table1", 
+        new String[]{"2"});
+  }
+
+  @Test
+  public void testDiv() throws IOException {
+    testSimpleEval("select div(9,4) as col1 ", new String[]{"2"});
+    testSimpleEval("select div(200000000001,200000000000) as col1 ", new String[]{"1"});
+    testSimpleEval("select div(200000000000,2) as col1 ", new String[]{"100000000000"});
+    testSimpleEval("select div(2,200000000000) as col1 ", new String[]{"0"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", INT8);
+    schema.addColumn("col2", INT8);
+    schema.addColumn("col3", INT8);
+
+    testEval(schema, "table1", "9,2,3", "select div(col1 + col2, col3) from table1", 
+        new String[]{"3"});
+  }
+/*
+  @Test
+  public void testDiv() throws IOException {
+    testSimpleEval("select mod(9,4) as col1 ", new String[]{"1"});
+    testSimpleEval("select mod(200000000001,200000000000) as col1 ", new String[]{"1"});
+    testSimpleEval("select mod(200000000000,2) as col1 ", new String[]{"0"});
+    testSimpleEval("select mod(2,200000000000) as col1 ", new String[]{"2"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", INT8);
+    schema.addColumn("col2", INT8);
+    schema.addColumn("col3", INT8);
+
+    testEval(schema, "table1", "9,2,3", "select mod(col1 + col2, col3) from table1", 
+        new String[]{"2"});
+  }
+*/
 }