drill-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jacq...@apache.org
Subject [1/4] git commit: DRILL-729: Hash functions for Date, Time and Decimal data types
Date Fri, 16 May 2014 21:17:40 GMT
Repository: incubator-drill
Updated Branches:
  refs/heads/master 4ea36c3f1 -> 70fab8c96


DRILL-729: Hash functions for Date, Time and Decimal data types


Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/8a37b949
Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/8a37b949
Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/8a37b949

Branch: refs/heads/master
Commit: 8a37b949dc087fe5f5e43fad917ce1713cee8e44
Parents: 4ea36c3
Author: Mehant Baid <mehantr@gmail.com>
Authored: Tue May 13 17:27:15 2014 -0700
Committer: Jacques Nadeau <jacques@apache.org>
Committed: Fri May 16 13:34:33 2014 -0700

----------------------------------------------------------------------
 .../drill/exec/expr/fn/impl/HashFunctions.java  | 250 +++++++++++++++++++
 .../drill/jdbc/test/TestFunctionsQuery.java     |   9 +
 2 files changed, 259 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/8a37b949/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
index 766a32a..1ac4f21 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/HashFunctions.java
@@ -245,5 +245,255 @@ public class HashFunctions {
       out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(in.value).asInt();
     }
   }
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class DateHash implements DrillSimpleFunc {
+    @Param  DateHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableDateHash implements DrillSimpleFunc {
+    @Param  NullableDateHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class TimeStampHash implements DrillSimpleFunc {
+    @Param  TimeStampHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableTimeStampHash implements DrillSimpleFunc {
+    @Param  NullableTimeStampHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class TimeHash implements DrillSimpleFunc {
+    @Param  TimeHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableTimeHash implements DrillSimpleFunc {
+    @Param  NullableTimeHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class TimeStampTZHash implements DrillSimpleFunc {
+    @Param  TimeStampTZHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value ^ in.index).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableTimeStampTZHash implements DrillSimpleFunc {
+    @Param  NullableTimeStampTZHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value ^ in.index).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class Decimal9Hash implements DrillSimpleFunc {
+    @Param  Decimal9Holder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableDecimal9Hash implements DrillSimpleFunc {
+    @Param  NullableDecimal9Holder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class Decimal18Hash implements DrillSimpleFunc {
+    @Param  Decimal18Holder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableDecimal18Hash implements DrillSimpleFunc {
+    @Param  NullableDecimal18Holder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashLong(in.value).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class Decimal28Hash implements DrillSimpleFunc {
+    @Param  Decimal28SparseHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+
+      int xor = 0;
+      for (int i = 0; i < in.nDecimalDigits; i++) {
+        xor = xor ^ in.getInteger(i);
+      }
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(xor).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableDecimal28Hash implements DrillSimpleFunc {
+    @Param  NullableDecimal28SparseHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else {
+        int xor = 0;
+        for (int i = 0; i < in.nDecimalDigits; i++) {
+          xor = xor ^ in.getInteger(i);
+        }
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(xor).asInt();
+      }
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class Decimal38Hash implements DrillSimpleFunc {
+    @Param  Decimal38SparseHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
+
+    public void eval() {
+
+      int xor = 0;
+      for (int i = 0; i < in.nDecimalDigits; i++) {
+        xor = xor ^ in.getInteger(i);
+      }
+      out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(xor).asInt();
+    }
+  }
+
+  @FunctionTemplate(name = "hash", scope = FunctionScope.SIMPLE, nulls = FunctionTemplate.NullHandling.INTERNAL)
+  public static class NullableDecimal38Hash implements DrillSimpleFunc {
+    @Param  NullableDecimal38SparseHolder in;
+    @Output IntHolder out;
+
+    public void setup(RecordBatch incoming) {
+    }
 
+    public void eval() {
+      if (in.isSet == 0)
+        out.value = 0;
+      else {
+        int xor = 0;
+        for (int i = 0; i < in.nDecimalDigits; i++) {
+          xor = xor ^ in.getInteger(i);
+        }
+        out.value = com.google.common.hash.Hashing.murmur3_128().hashInt(xor).asInt();
+      }
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/8a37b949/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java
----------------------------------------------------------------------
diff --git a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java
index 79f29c8..78bcd95 100644
--- a/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java
+++ b/exec/jdbc/src/test/java/org/apache/drill/jdbc/test/TestFunctionsQuery.java
@@ -430,4 +430,13 @@ public class TestFunctionsQuery {
             "DEC_18=-2147483648.00\n"
         );
   }
+
+  @Test
+  public void testHashFunctions() throws Exception {
+    String query = "select hash(cast(hire_date as date)), hash(cast(employee_id as decimal(9,
2))), hash(cast(employee_id as decimal(38, 11)))" +
+                   "from cp.`employee.json` limit 1";
+
+    JdbcAssert.withNoDefaultSchema()
+        .sql(query);
+  }
 }


Mime
View raw message