You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by ji...@apache.org on 2013/11/30 15:16:15 UTC

[3/7] git commit: TAJO-341: Implement substr function. (hyoungjunkim via hyunsik)

TAJO-341: Implement substr function. (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/f9a6e9ca
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/f9a6e9ca
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/f9a6e9ca

Branch: refs/heads/DAG-execplan
Commit: f9a6e9ca3dbfc7c518ad6f0d3b1c9fe3196b654e
Parents: 2e27a02
Author: Hyunsik Choi <hy...@apache.org>
Authored: Sat Nov 30 14:49:53 2013 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Sat Nov 30 14:49:53 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../tajo/engine/function/string/Substr.java     | 75 ++++++++++++++++++++
 .../java/org/apache/tajo/master/TajoMaster.java |  5 ++
 .../TestStringOperatorsAndFunctions.java        | 32 +++++++++
 4 files changed, 114 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/f9a6e9ca/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 3cee24e..0242e99 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-341: Implement substr function. (hyoungjunkim via hyunsik)
+
     TAJO-308: Implement length(string) function. (hyoungjunkim via hyunsik)
 
     TAJO-200: RCFile compatible to apache hive. (jinho)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/f9a6e9ca/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Substr.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Substr.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Substr.java
new file mode 100644
index 0000000..cb42e3c
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Substr.java
@@ -0,0 +1,75 @@
+/**
+ * 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
+ *
+ * text substr(string text, from int4 [, length int4])
+ */
+public class Substr extends GeneralFunction {
+  public Substr() {
+    super(new Column[] {
+        new Column("text", TajoDataTypes.Type.TEXT),
+        new Column("from", TajoDataTypes.Type.INT4),
+        new Column("length", TajoDataTypes.Type.INT4)    //optional
+    });
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum valueDatum = params.get(0);
+    if(valueDatum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+    Datum fromDatum = params.get(1);
+    Datum lengthDatum = params.size() > 2 ? params.get(2) : null;
+
+    String value = valueDatum.asChars();
+    int valueLength = value.length();
+
+    int from = fromDatum.asInt4() - 1;
+    if (from >= valueLength) {
+      return DatumFactory.createText("");
+    }
+
+    int length = (lengthDatum == null) ? valueLength : lengthDatum.asInt4();
+
+    if (from < 0) {
+      from = 0;
+      length = (lengthDatum == null) ? value.length() : length - 1;
+    }
+
+    int to = from + length;
+
+    if (to > valueLength) {
+      to = valueLength;
+    }
+
+    return DatumFactory.createText(value.substring(from, to));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/f9a6e9ca/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 b634f25..f91cc0b 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
@@ -399,6 +399,11 @@ public class TajoMaster extends CompositeService {
             CatalogUtil.newSimpleDataType(Type.INT4),
             CatalogUtil.newSimpleDataTypeArray(Type.TEXT)));
 
+    sqlFuncs.add(
+        new FunctionDesc("substr", Substr.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.TEXT),
+            CatalogUtil.newSimpleDataTypeArray(Type.TEXT, Type.INT4, Type.INT4)));
+
     return sqlFuncs;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/f9a6e9ca/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 5124173..5e73f88 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
@@ -190,4 +190,36 @@ public class TestStringOperatorsAndFunctions extends ExprTestBase {
     testEval(schema, "table1", "ABC,DEF,3.14", "select length(lower(col1) || lower(col2)) from table1",
         new String[]{"6"});
   }
+
+  @Test
+  public void testSubstr() throws IOException {
+    testSimpleEval("select substr('abcdef', 3, 2) as col1 ", new String[]{"cd"});
+    testSimpleEval("select substr('abcdef', 3) as col1 ", new String[]{"cdef"});
+    testSimpleEval("select substr('abcdef', 1, 1) as col1 ", new String[]{"a"});
+    testSimpleEval("select substr('abcdef', 0, 1) as col1 ", new String[]{""});
+    testSimpleEval("select substr('abcdef', 0, 2) as col1 ", new String[]{"a"});
+    testSimpleEval("select substr('abcdef', 0) as col1 ", new String[]{"abcdef"});
+    testSimpleEval("select substr('abcdef', 1, 100) as col1 ", new String[]{"abcdef"});
+    testSimpleEval("select substr('abcdef', 0, 100) as col1 ", new String[]{"abcdef"});
+    testSimpleEval("select substr('일이삼사오', 2, 2) as col1 ", new String[]{"이삼"});
+    testSimpleEval("select substr('일이삼사오', 3) as col1 ", new String[]{"삼사오"});
+
+    //TODO If there is a minus value in function argument, next error occurred.
+    //org.apache.tajo.engine.parser.SQLSyntaxError: ERROR: syntax error at or near 'substr'
+    //LINE 1:7 select substr('abcdef', -1, 100) as col1
+    //               ^^^^^^
+    //at org.apache.tajo.engine.parser.SQLAnalyzer.parse(SQLAnalyzer.java:64)
+
+//    testSimpleEval("select substr('abcdef', -1) as col1 ", new String[]{"abcdef"});
+//    testSimpleEval("select substr('abcdef', -1, 100) as col1 ", new String[]{"abcdef"});
+//    testSimpleEval("select substr('abcdef', -1, 3) as col1 ", new String[]{"a"});
+//    testSimpleEval("select substr('abcdef', -1, 1) as col1 ", new String[]{""});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", TEXT);
+    schema.addColumn("col2", TEXT);
+    schema.addColumn("col3", TEXT);
+    testEval(schema, "table1", ",abcdef,3.14", "select substr(lower(col2), 2, 3) from table1",
+        new String[]{"bcd"});
+  }
 }