You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by hy...@apache.org on 2013/12/02 06:31:47 UTC
git commit: TAJO-349: Implement md5(text). (DaeMyung Kang via hyunsik)
Updated Branches:
refs/heads/master 14ec0ec13 -> c26831669
TAJO-349: Implement md5(text). (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/c2683166
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/c2683166
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/c2683166
Branch: refs/heads/master
Commit: c26831669afd25b4e34fe7fad457eab4e98bcb00
Parents: 14ec0ec
Author: Hyunsik Choi <hy...@apache.org>
Authored: Mon Dec 2 14:30:59 2013 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Mon Dec 2 14:30:59 2013 +0900
----------------------------------------------------------------------
CHANGES.txt | 2 +
.../apache/tajo/engine/function/string/Md5.java | 55 ++++++++++++++++++++
.../java/org/apache/tajo/master/TajoMaster.java | 6 +++
.../TestStringOperatorsAndFunctions.java | 12 +++++
4 files changed, 75 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/c2683166/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index f051e1d..6b97b85 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
NEW FEATURES
+ TAJO-349: Implement md5(text). (DaeMyung Kang via hyunsik)
+
TAJO-351: Implement reverse(text). (DaeMyung Kang via hyunsik)
TAJO-342: Implement strpos(string, substring) function.
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/c2683166/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Md5.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Md5.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Md5.java
new file mode 100644
index 0000000..fc1db8d
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Md5.java
@@ -0,0 +1,55 @@
+/**
+ * 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;
+import java.security.*;
+import org.apache.commons.codec.binary.Hex;
+
+/**
+ * Function definition
+ *
+ * text md5(string text)
+ */
+public class Md5 extends GeneralFunction {
+ public Md5() {
+ super(new Column[] {
+ new Column("text", TajoDataTypes.Type.TEXT)
+ });
+ }
+
+ @Override
+ public Datum eval(Tuple params) {
+ Datum datum = params.get(0);
+ if(datum instanceof NullDatum) return NullDatum.get();
+
+ try {
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ return DatumFactory.createText(new String(Hex.encodeHex(md.digest(datum.asByteArray()))));
+ } catch (NoSuchAlgorithmException e){
+ return NullDatum.get();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/c2683166/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 557fd5a..6be7c83 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
@@ -352,6 +352,12 @@ public class TajoMaster extends CompositeService {
new FunctionDesc("lower", Lower.class, FunctionType.GENERAL,
CatalogUtil.newSimpleDataType(Type.TEXT),
CatalogUtil.newSimpleDataTypeArray(Type.TEXT)));
+
+ sqlFuncs.add(
+ new FunctionDesc("md5", Md5.class, FunctionType.GENERAL,
+ CatalogUtil.newSimpleDataType(Type.TEXT),
+ CatalogUtil.newSimpleDataTypeArray(Type.TEXT)));
+
sqlFuncs.add(
new FunctionDesc("char_length", CharLength.class, FunctionType.GENERAL,
CatalogUtil.newSimpleDataType(Type.TEXT),
http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/c2683166/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 b62b1b8..2e46101 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
@@ -212,6 +212,18 @@ public class TestStringOperatorsAndFunctions extends ExprTestBase {
}
@Test
+ public void testMd5() throws IOException {
+ testSimpleEval("select md5('1') as col1 ", new String[]{"c4ca4238a0b923820dcc509a6f75849b"});
+ testSimpleEval("select md5('tajo') as col1 ", new String[]{"742721b3a79f71a9491681b8e8a7ce85"});
+
+ Schema schema = new Schema();
+ schema.addColumn("col1", TEXT);
+ schema.addColumn("col2", TEXT);
+ schema.addColumn("col3", TEXT);
+ testEval(schema, "table1", "abc,efg,3.14", "select md5(col1) from table1", new String[]{"900150983cd24fb0d6963f7d28e17f72"});
+ }
+
+ @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"});