tajo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hyun...@apache.org
Subject git commit: TAJO-498: Implement digest(text, text) function. (DaeMyung Kang via hyunsik)
Date Tue, 21 Jan 2014 01:43:29 GMT
Updated Branches:
  refs/heads/master d04f9a567 -> 9af2fca12


TAJO-498: Implement digest(text, text) 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/9af2fca1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/9af2fca1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/9af2fca1

Branch: refs/heads/master
Commit: 9af2fca12204648a16759aedf75a0234b438ae9b
Parents: d04f9a5
Author: Hyunsik Choi <hyunsik@apache.org>
Authored: Tue Jan 21 10:42:24 2014 +0900
Committer: Hyunsik Choi <hyunsik@apache.org>
Committed: Tue Jan 21 10:42:24 2014 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  5 +-
 .../tajo/engine/function/string/Digest.java     | 87 ++++++++++++++++++++
 .../TestStringOperatorsAndFunctions.java        | 14 ++++
 3 files changed, 105 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/9af2fca1/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 19a445a..104073f 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,7 +4,10 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
-    TAJO-122: Add EXPLAIN clause to show a logical plan. 
+    TAJO-498: Implement digest(text, text) function.
+    (DaeMyung Kang via hyunsik)
+
+    TAJO-122: Add EXPLAIN clause to show a logical plan.
     (DaeMyung Kang via hyunsik)
 
     TAJO-438: Date literal support. (Jae Young Lee via jihoon)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/9af2fca1/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Digest.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Digest.java
b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Digest.java
new file mode 100644
index 0000000..163c6fe
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/Digest.java
@@ -0,0 +1,87 @@
+/**
+ * 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.engine.function.annotation.Description;
+import org.apache.tajo.engine.function.annotation.ParamTypes;
+import org.apache.tajo.storage.Tuple;
+import java.security.*;
+import org.apache.commons.codec.binary.Hex;
+import org.apache.commons.codec.digest.DigestUtils;
+
+/**
+ * Function definition
+ *
+ * text digest(string text, type text)
+ */
+@Description(
+  functionName = "digest",
+  description = "Calculates the Digest hash of string",
+  example = "> SELECT digest('tajo', 'sha1');\n"
+          + "02b0e20540b89f0b735092bbac8093eb2e3804cf",
+  returnType = TajoDataTypes.Type.TEXT,
+  paramTypes = {@ParamTypes(paramTypes = {TajoDataTypes.Type.TEXT, TajoDataTypes.Type.TEXT})}
+)
+
+public class Digest extends GeneralFunction {
+  public Digest() {
+    super(new Column[] {
+        new Column("text", TajoDataTypes.Type.TEXT)
+    });
+  }
+
+  String digest(byte [] data, String type) throws NoSuchAlgorithmException{
+    if ("SHA1".equalsIgnoreCase(type) == true) {
+      MessageDigest md = MessageDigest.getInstance("SHA-1");
+      return new String(Hex.encodeHex(md.digest(data)));
+    } else if ("SHA256".equalsIgnoreCase(type) == true) {
+      return DigestUtils.sha256Hex(data);
+    } else if ("SHA384".equalsIgnoreCase(type) == true) {
+      return DigestUtils.sha384Hex(data);
+    } else if ("SHA512".equalsIgnoreCase(type) == true) {
+      return DigestUtils.sha512Hex(data);
+    } else if ("MD5".equalsIgnoreCase(type) == true) {
+      return DigestUtils.md5Hex(data);
+    } else if ("MD2".equalsIgnoreCase(type) == true) {
+      MessageDigest md = MessageDigest.getInstance("MD2");
+      return new String(Hex.encodeHex(md.digest(data)));
+    }
+
+    throw new NoSuchAlgorithmException("Not supported DigestType");
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum datum = params.get(0);
+    Datum typeDatum = params.get(1);
+    if(datum instanceof NullDatum || typeDatum instanceof NullDatum) return NullDatum.get();
+
+    try {
+        return DatumFactory.createText(digest(datum.asByteArray(), typeDatum.asChars()));
+    } catch (NoSuchAlgorithmException e){
+        return NullDatum.get();
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/9af2fca1/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 0df05b7..0237cf0 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
@@ -297,6 +297,20 @@ public class TestStringOperatorsAndFunctions extends ExprTestBase {
   }
 
   @Test
+  public void testDigest() throws IOException {
+    testSimpleEval("select digest('tajo', 'md2') as col1 ", new String[]{"bf523bce8241982f6bea9af0f7fd37ff"});
+    testSimpleEval("select digest('tajo', 'md5') as col1 ", new String[]{"742721b3a79f71a9491681b8e8a7ce85"});
+    testSimpleEval("select digest('tajo', 'sha1') as col1 ", new String[]{"02b0e20540b89f0b735092bbac8093eb2e3804cf"});
+    testSimpleEval("select digest('tajo', 'sha256') as col1 ", 
+      new String[]{"6440083be076869a9f9d0271a4bf298d98c8aa3ecb49df841895fbcddbb04a70"});
+    testSimpleEval("select digest('tajo', 'sha384') as col1 ", 
+      new String[]{"59ff99b0e274eb3d8e10f221b6b949bfc1244d2a1226c5c720062fb03d82272be633e4a0f2babccffbfdff7cc1cb06fb"});
+    testSimpleEval("select digest('tajo', 'sha512') as col1 ", 
+      new String[]{"ee8ba254d331ddfb1bca9aaf0c4b8c58aea5331928cbd20168c87828afb853b0c096af71ec69a23b669217a1dddd2934edaac33b1296fe526b22abd28a15c4b3"});
+    testSimpleEval("select digest('tajo', 'not') as col1 ", new String[]{""});
+  }
+
+  @Test
   public void testHex() throws IOException {
     testSimpleEval("select to_hex(1) as col1 ", new String[]{"1"});
     testSimpleEval("select to_hex(10) as col1 ", new String[]{"a"});


Mime
View raw message