tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hyun...@apache.org
Subject git commit: TAJO-307: Implement chr(int) function. (DaeMyung Kang via hyunsik)
Date Mon, 09 Dec 2013 09:06:38 GMT
Updated Branches:
  refs/heads/master 22a6136d3 -> ad6ff6ede


TAJO-307: Implement chr(int) 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/ad6ff6ed
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/ad6ff6ed
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/ad6ff6ed

Branch: refs/heads/master
Commit: ad6ff6ede187bea29e64c1745fad4d90f7049433
Parents: 22a6136
Author: Hyunsik Choi <hyunsik@apache.org>
Authored: Mon Dec 9 17:58:41 2013 +0900
Committer: Hyunsik Choi <hyunsik@apache.org>
Committed: Mon Dec 9 17:59:21 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../apache/tajo/engine/function/string/Chr.java | 54 ++++++++++++++++++++
 .../java/org/apache/tajo/master/TajoMaster.java |  4 ++
 .../TestStringOperatorsAndFunctions.java        | 18 +++++--
 4 files changed, 75 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/ad6ff6ed/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 19173ed..86facbb 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-307: Implement chr(int) function. (DaeMyung Kang via hyunsik)
+
     TAJO-365: Implement degrees/radians function. (DaeMyung Kang via hyunsik)
 
     TAJO-368: Implement quote_ident function. (Seungun Choe via hyunsik)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/ad6ff6ed/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Chr.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Chr.java
b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Chr.java
new file mode 100644
index 0000000..05a7a86
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Chr.java
@@ -0,0 +1,54 @@
+/**
+ * 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.string;
+
+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
+ *
+ * char chr(value int4)
+ */
+
+public class Chr extends GeneralFunction {
+  public Chr() {
+    super(new Column[]{
+            new Column("text", TajoDataTypes.Type.INT4)
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum datum = params.get(0);
+    if (datum instanceof NullDatum) return NullDatum.get();
+
+    int value = datum.asInt4();
+    if (value <= 0 || value > 65525) {
+        return NullDatum.get();
+    } else {
+        return DatumFactory.createText(String.valueOf((char)value));
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/ad6ff6ed/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 4c79e50..40696b9 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
@@ -433,6 +433,10 @@ public class TajoMaster extends CompositeService {
         new FunctionDesc("ascii", Ascii.class, FunctionType.GENERAL,
             CatalogUtil.newSimpleDataType(Type.INT4),
             CatalogUtil.newSimpleDataTypeArray(Type.TEXT)));
+    sqlFuncs.add(
+        new FunctionDesc("chr", Chr.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.CHAR),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT4)));
 
     sqlFuncs.add(
         new FunctionDesc("length", Length.class, FunctionType.GENERAL,

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/ad6ff6ed/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestStringOperatorsAndFunctions.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestStringOperatorsAndFunctions.java
b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestStringOperatorsAndFunctions.java
index 5888b80..a3d2f86 100644
--- a/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestStringOperatorsAndFunctions.java
+++ b/tajo-core/tajo-core-backend/src/test/java/org/apache/tajo/engine/function/TestStringOperatorsAndFunctions.java
@@ -25,9 +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.INT4;
-import static org.apache.tajo.common.TajoDataTypes.Type.TEXT;
+import static org.apache.tajo.common.TajoDataTypes.Type.*;
 
 public class TestStringOperatorsAndFunctions extends ExprTestBase {
 
@@ -465,6 +463,20 @@ public class TestStringOperatorsAndFunctions extends ExprTestBase {
   }
 
   @Test
+  public void testChr() throws IOException {
+    testSimpleEval("select chr(48) as col1 ", new String[]{"0"});
+    testSimpleEval("select chr(49) as col1 ", new String[]{"1"});
+    testSimpleEval("select chr(50) as col1 ", new String[]{"2"});
+    testSimpleEval("select chr(64) as col1 ", new String[]{"@"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", INT4);
+    testEval(schema, "table1", "65", "select chr(col1) from table1", new String[]{"A"});
+    testEval(schema, "table1", "66", "select chr(col1) from table1", new String[]{"B"});
+    testEval(schema, "table1", "52512", "select chr(col1) from table1", new String[]{"촠"});
+  }
+
+  @Test
   public void testLpad() throws IOException {
     testSimpleEval("select lpad('hi', 5, 'xy') ", new String[]{"xyxhi"});
     testSimpleEval("select LPAD('hello', 7, 'xy') ", new String[]{"xyhello"});


Mime
View raw message