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/12/03 12:51:01 UTC

[06/18] git commit: TAJO-346: Implement hex function. (DaeMyung Kang via hyunsik)

TAJO-346: Implement hex 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/778c01f8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tajo/tree/778c01f8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tajo/diff/778c01f8

Branch: refs/heads/DAG-execplan
Commit: 778c01f8f71ea6dd4573e83c5b62ccefcd39b337
Parents: c268316
Author: Hyunsik Choi <hy...@apache.org>
Authored: Mon Dec 2 16:56:50 2013 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Mon Dec 2 16:56:50 2013 +0900

----------------------------------------------------------------------
 CHANGES.txt                                     |  2 +
 .../tajo/engine/function/string/ToHex.java      | 68 ++++++++++++++++++++
 .../apache/tajo/engine/parser/SQLAnalyzer.java  |  7 +-
 .../java/org/apache/tajo/master/TajoMaster.java |  8 +++
 .../TestStringOperatorsAndFunctions.java        | 15 +++++
 5 files changed, 99 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/778c01f8/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 6b97b85..caadc0c 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -4,6 +4,8 @@ Release 0.8.0 - unreleased
 
   NEW FEATURES
 
+    TAJO-346: Implement hex function. (DaeMyung Kang via hyunsik)
+
     TAJO-349: Implement md5(text). (DaeMyung Kang via hyunsik)
 
     TAJO-351: Implement reverse(text). (DaeMyung Kang via hyunsik)

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/778c01f8/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/ToHex.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/ToHex.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/ToHex.java
new file mode 100644
index 0000000..c5bebda
--- /dev/null
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/function/string/ToHex.java
@@ -0,0 +1,68 @@
+/**
+ * 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.datum.Float4Datum;
+import org.apache.tajo.datum.Float8Datum;
+import org.apache.tajo.engine.function.GeneralFunction;
+import org.apache.tajo.storage.Tuple;
+import org.apache.commons.codec.binary.Hex;
+import java.nio.ByteBuffer;
+
+/**
+ * Function definition
+ *
+ * text to_hex(text)
+ * text to_hex(int)
+ */
+public class ToHex extends GeneralFunction {
+
+  public ToHex() {
+    super(new Column[] {
+        new Column("text", TajoDataTypes.Type.TEXT)
+    });
+  }
+
+  public String trimZero(String hexString) {
+    int len = hexString.length();
+    for (int i = 0; i < len; i++) {
+        if (hexString.charAt(i) != '0') {
+            return hexString.substring(i);
+        }
+    }
+
+    return hexString;
+  }
+
+  @Override
+  public Datum eval(Tuple params) {
+    Datum datum = params.get(0);
+    if(datum instanceof NullDatum) {
+      return NullDatum.get();
+    }
+
+    String ret = new String(Hex.encodeHex(datum.asByteArray()));
+    return DatumFactory.createText(trimZero(ret));
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/778c01f8/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
----------------------------------------------------------------------
diff --git a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
index 2b641cb..48d5858 100644
--- a/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
+++ b/tajo-core/tajo-core-backend/src/main/java/org/apache/tajo/engine/parser/SQLAnalyzer.java
@@ -740,7 +740,12 @@ public class SQLAnalyzer extends SQLParserBaseVisitor<Expr> {
   @Override
   public LiteralValue visitUnsigned_numeric_literal(@NotNull SQLParser.Unsigned_numeric_literalContext ctx) {
     if (ctx.NUMBER() != null) {
-      return new LiteralValue(ctx.getText(), LiteralType.Unsigned_Integer);
+      long lValue = Long.parseLong(ctx.getText());
+      if (lValue >= Integer.MIN_VALUE && lValue <= Integer.MAX_VALUE) {
+        return new LiteralValue(ctx.getText(), LiteralType.Unsigned_Integer);
+      } else {
+        return new LiteralValue(ctx.getText(), LiteralType.Unsigned_Large_Integer);
+      } 
     } else {
       return new LiteralValue(ctx.getText(), LiteralType.Unsigned_Float);
     }

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/778c01f8/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 6be7c83..a3a85c4 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
@@ -345,6 +345,14 @@ public class TajoMaster extends CompositeService {
             CatalogUtil.newSimpleDataTypeArray(Type.TEXT)));
 
     sqlFuncs.add(
+        new FunctionDesc("to_hex", ToHex.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.TEXT),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT4)));
+    sqlFuncs.add(
+        new FunctionDesc("to_hex", ToHex.class, FunctionType.GENERAL,
+            CatalogUtil.newSimpleDataType(Type.TEXT),
+            CatalogUtil.newSimpleDataTypeArray(Type.INT8)));
+    sqlFuncs.add(
         new FunctionDesc("upper", Upper.class, FunctionType.GENERAL,
             CatalogUtil.newSimpleDataType(Type.TEXT),
             CatalogUtil.newSimpleDataTypeArray(Type.TEXT)));

http://git-wip-us.apache.org/repos/asf/incubator-tajo/blob/778c01f8/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 2e46101..221249a 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
@@ -224,6 +224,21 @@ public class TestStringOperatorsAndFunctions extends ExprTestBase {
   }
 
   @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"});
+    testSimpleEval("select to_hex(1234) as col1 ", new String[]{"4d2"});
+    testSimpleEval("select to_hex(1023456788888888) as col1 ", new String[]{"3a2d41a583d38"});
+
+    Schema schema = new Schema();
+    schema.addColumn("col1", TEXT);
+    schema.addColumn("col2", TEXT);
+    schema.addColumn("col3", TEXT);
+    testEval(schema, "table1", ",abcdef,3.14", "select to_hex(10) from table1",
+        new String[]{"a"});
+  }
+
+  @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"});