tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From blrun...@apache.org
Subject git commit: TAJO-359: Implement lpad function. (Seungun Choe via jaehwa)
Date Thu, 05 Dec 2013 02:05:54 GMT
Updated Branches:
  refs/heads/master ab2da738b -> d9e458fa0


TAJO-359: Implement lpad function. (Seungun Choe 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/d9e458fa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/d9e458fa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/d9e458fa

Branch: refs/heads/master
Commit: d9e458fa011120d914b50212b094f0c5f9401fb0
Parents: ab2da73
Author: blrunner <jhjung@gruter.com>
Authored: Thu Dec 5 11:05:35 2013 +0900
Committer: blrunner <jhjung@gruter.com>
Committed: Thu Dec 5 11:05:35 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../tajo/engine/function/string/Lpad.java       | 78 ++++++++++++++++++++
 .../java/org/apache/tajo/master/TajoMaster.java |  5 ++
 .../TestStringOperatorsAndFunctions.java        | 10 +++
 4 files changed, 95 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/d9e458fa/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index dc512e3..7992797 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-359: Implement lpad function. (Seungun Choe via jaehwa)
+
     TAJO-306: Implement ascii(string) function. (SeongHwa Ahn via jaehwa)
 
     TAJO-354: Fix invalid type to valid type for udfs(bit_length/char_length).

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/d9e458fa/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Lpad.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Lpad.java
b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Lpad.java
new file mode 100644
index 0000000..4d5a8f8
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Lpad.java
@@ -0,0 +1,78 @@
+/**
+ * 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 com.google.gson.annotations.Expose;
+import org.apache.commons.lang.StringUtils;
+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.eval.FunctionEval;
+import org.apache.tajo.engine.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+
+
+/**
+ * Function definition
+ *
+ * text lpad(string text, length int [, fill text])
+ */
+public class Lpad extends GeneralFunction {
+  @Expose private boolean hasFillCharacters;
+
+  public Lpad() {
+    super(new Column[] {
+        new Column("text", TajoDataTypes.Type.TEXT),
+        new Column("length", TajoDataTypes.Type.INT4)
+    });
+  }
+
+  public void init(FunctionEval.ParamType[] paramTypes) {
+    if (paramTypes.length == 3) {
+      hasFillCharacters = true;
+    }
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum datum = params.get(0);
+    Datum lengthDatum = params.get(1);
+
+    if(datum instanceof NullDatum) return NullDatum.get();
+    if(lengthDatum instanceof NullDatum) return NullDatum.get();
+
+    Datum fillText=NullDatum.get();
+
+    if(hasFillCharacters) {
+      fillText=params.get(2);
+    }
+    else {
+      fillText=DatumFactory.createText(" ");
+    }
+
+    int templen = lengthDatum.asInt4() - datum.asChars().length();
+
+    if(templen<=0)
+      return DatumFactory.createText(datum.asChars().substring(0,lengthDatum.asInt4()));
+
+    return DatumFactory.createText(StringUtils.leftPad(datum.asChars(), lengthDatum.asInt4(),
fillText.asChars()));
+  }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/d9e458fa/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 ebfd623..57293f2 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
@@ -564,6 +564,11 @@ public class TajoMaster extends CompositeService {
             CatalogUtil.newSimpleDataType(Type.TEXT),
             CatalogUtil.newSimpleDataTypeArray(Type.TEXT)));
 
+    sqlFuncs.add(
+        new FunctionDesc("lpad", Lpad.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.TEXT),
+            CatalogUtil.newSimpleDataTypeArray(Type.TEXT, Type.INT4, Type.TEXT)));
+
     return sqlFuncs;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/d9e458fa/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 a090821..fb4d296 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
@@ -416,4 +416,14 @@ public class TestStringOperatorsAndFunctions extends ExprTestBase {
             new String[]{"49"});
 
   }
+
+  @Test
+  public void testLpad() throws IOException {
+    testSimpleEval("select lpad('hi', 5, 'xy') ", new String[]{"xyxhi"});
+    testSimpleEval("select LPAD('hello', 7, 'xy') ", new String[]{"xyhello"});
+    testSimpleEval("select LPAD('hello', 3, 'xy') ", new String[]{"hel"});
+    testSimpleEval("select lPAD('hello', 7) ", new String[]{"  hello"});
+    testSimpleEval("select lPAD('가나다라', 3) ", new String[]{"가나다"});
+
+  }
 }


Mime
View raw message