DRILL-1354: Fix date truncate functions to use toString helper functions.
Minor cleanup: move the truncate logic in a separate template


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

Branch: refs/heads/master
Commit: 46de8816f4b197d54d937d0a190bae293c17d1ac
Parents: d8a770f
Author: Mehant Baid <meha...@gmail.com>
Authored: Wed Aug 27 16:47:54 2014 -0700
Committer: Jacques Nadeau <jacq...@apache.org>
Committed: Wed Aug 27 18:44:09 2014 -0700

----------------------------------------------------------------------
 .../DateDateArithmeticFunctions.java            | 40 ----------
 .../DateTruncFunctions.java                     | 84 ++++++++++++++++++++
 .../main/codegen/templates/ValueHolders.java    |  6 +-
 .../drill/jdbc/test/TestFunctionsQuery.java     |  1 -
 4 files changed, 89 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/46de8816/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateDateArithmeticFunctions.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateDateArithmeticFunctions.java
 
b/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateDateArithmeticFunctions.java
index e119958..6c03f3b 100644
--- 
a/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateDateArithmeticFunctions.java
+++ 
b/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateDateArithmeticFunctions.java
@@ -75,45 +75,5 @@ public static class G${type}Difference implements 
DrillSimpleFunc {
         </#if>
     }
 }
-
-@SuppressWarnings("unused")
-@FunctionTemplate(names = "date_trunc", scope = 
FunctionTemplate.FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
-public static class G${type}DateTrunc implements DrillSimpleFunc {
-
-    @Param  VarCharHolder left;
-    @Param  ${type}Holder right;
-    @Output ${type}Holder out;
-    @Workspace org.joda.time.MutableDateTime dateTime;
-
-    public void setup(RecordBatch incoming) {
-      dateTime = new 
org.joda.time.MutableDateTime(org.joda.time.DateTimeZone.UTC);
-    }
-
-    public void eval() {
-        dateTime.setMillis(right.value);
-        <#if type != "Time">
-        if (left.toString().equalsIgnoreCase("YEAR")) 
dateTime.setRounding(dateTime.getChronology().year());
-        else if (left.toString().equalsIgnoreCase("MONTH")) 
dateTime.setRounding(dateTime.getChronology().monthOfYear());
-        else if (left.toString().equalsIgnoreCase("DAY")) 
dateTime.setRounding(dateTime.getChronology().dayOfMonth());
-        else
-        </#if>
-        <#if type != "Date">
-        if (left.toString().equalsIgnoreCase("HOUR")) 
dateTime.setRounding(dateTime.getChronology().hourOfDay());
-        else if (left.toString().equalsIgnoreCase("MINUTE")) 
dateTime.setRounding(dateTime.getChronology().minuteOfHour());
-        else if (left.toString().equalsIgnoreCase("SECOND")) 
dateTime.setRounding(dateTime.getChronology().secondOfMinute());
-        else
-        </#if>
-        <#if type == "TimeStamp" || type == "TimeStampTZ">
-            throw new UnsupportedOperationException("date_trunc function 
supports the following time units for TimeStamp(TZ): YEAR, MONTH, DAY, HOUR, 
MINUTE, SECOND");
-        out.value = dateTime.getMillis();
-        <#elseif type == "Date">
-            throw new UnsupportedOperationException("date_trunc function 
supports the following time units for Date: YEAR, MONTH, DAY");
-        out.value = dateTime.getMillis();
-        <#elseif type == "Time">
-            throw new UnsupportedOperationException("date_trunc function 
supports the following time units for Time: HOUR, MINUTE, SECOND");
-        out.value = (int) dateTime.getMillis();
-        </#if>
-    }
-}
 }
 </#list>

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/46de8816/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateTruncFunctions.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateTruncFunctions.java
 
b/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateTruncFunctions.java
new file mode 100644
index 0000000..b9cd0c8
--- /dev/null
+++ 
b/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateTruncFunctions.java
@@ -0,0 +1,84 @@
+/**
+ * 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.
+ */
+
+import org.apache.drill.exec.expr.annotations.Workspace;
+
+<@pp.dropOutputFile />
+
+<@pp.changeOutputFile 
name="/org/apache/drill/exec/expr/fn/impl/GDateTimeTruncateFunctions.java" />
+
+<#include "/@includes/license.ftl" />
+
+package org.apache.drill.exec.expr.fn.impl;
+
+import org.apache.drill.exec.expr.DrillSimpleFunc;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate;
+import org.apache.drill.exec.expr.annotations.FunctionTemplate.NullHandling;
+import org.apache.drill.exec.expr.annotations.Output;
+import org.apache.drill.exec.expr.annotations.Workspace;
+import org.apache.drill.exec.expr.annotations.Param;
+import org.apache.drill.exec.expr.holders.*;
+import org.apache.drill.exec.record.RecordBatch;
+
+import io.netty.buffer.ByteBuf;
+
+public class GDateTimeTruncateFunctions {
+
+<#list dateIntervalFunc.dates as type>
+
+@SuppressWarnings("unused")
+@FunctionTemplate(names = "date_trunc", scope = 
FunctionTemplate.FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
+public static class G${type}DateTrunc implements DrillSimpleFunc {
+
+    @Param  VarCharHolder left;
+    @Param  ${type}Holder right;
+    @Output ${type}Holder out;
+    @Workspace org.joda.time.MutableDateTime dateTime;
+
+    public void setup(RecordBatch incoming) {
+      dateTime = new 
org.joda.time.MutableDateTime(org.joda.time.DateTimeZone.UTC);
+    }
+
+    public void eval() {
+        dateTime.setMillis(right.value);
+        <#if type != "Time">
+        if 
(org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(left.start,
 left.end, left.buffer).equalsIgnoreCase("YEAR")) 
dateTime.setRounding(dateTime.getChronology().year());
+        else if 
(org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(left.start,
 left.end, left.buffer).equalsIgnoreCase("MONTH")) 
dateTime.setRounding(dateTime.getChronology().monthOfYear());
+        else if 
(org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(left.start,
 left.end, left.buffer).equalsIgnoreCase("DAY")) 
dateTime.setRounding(dateTime.getChronology().dayOfMonth());
+        else
+        </#if>
+        <#if type != "Date">
+        if 
(org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(left.start,
 left.end, left.buffer).equalsIgnoreCase("HOUR")) 
dateTime.setRounding(dateTime.getChronology().hourOfDay());
+        else if 
(org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(left.start,
 left.end, left.buffer).equalsIgnoreCase("MINUTE")) 
dateTime.setRounding(dateTime.getChronology().minuteOfHour());
+        else if 
(org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(left.start,
 left.end, left.buffer).equalsIgnoreCase("SECOND")) 
dateTime.setRounding(dateTime.getChronology().secondOfMinute());
+        else
+        </#if>
+        <#if type == "TimeStamp" || type == "TimeStampTZ">
+            throw new UnsupportedOperationException("date_trunc function 
supports the following time units for TimeStamp(TZ): YEAR, MONTH, DAY, HOUR, 
MINUTE, SECOND");
+        out.value = dateTime.getMillis();
+        <#elseif type == "Date">
+            throw new UnsupportedOperationException("date_trunc function 
supports the following time units for Date: YEAR, MONTH, DAY");
+        out.value = dateTime.getMillis();
+        <#elseif type == "Time">
+            throw new UnsupportedOperationException("date_trunc function 
supports the following time units for Time: HOUR, MINUTE, SECOND");
+        out.value = (int) dateTime.getMillis();
+        </#if>
+    }
+}
+</#list>
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/46de8816/exec/java-exec/src/main/codegen/templates/ValueHolders.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/main/codegen/templates/ValueHolders.java 
b/exec/java-exec/src/main/codegen/templates/ValueHolders.java
index 1902843..4151bbe 100644
--- a/exec/java-exec/src/main/codegen/templates/ValueHolders.java
+++ b/exec/java-exec/src/main/codegen/templates/ValueHolders.java
@@ -95,7 +95,11 @@ public final class ${className} implements ValueHolder{
     public int hashCode(){
       throw new UnsupportedOperationException();
     }
-    
+
+    /*
+     * Reason for deprecation is that ValueHolders are potential scalar 
replacements
+     * and hence we don't want any methods to be invoked on them.
+     */
     @Deprecated
     public String toString(){
       throw new UnsupportedOperationException();

http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/46de8816/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 0e40d11..5aee392 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
@@ -539,7 +539,6 @@ public class TestFunctionsQuery {
   }
 
   @Test
-  @Ignore ("toString on varchar is not supported. date trunc functions seem to 
be using that")
   public void testDateTrunc() throws Exception {
     String query = "select "
         + "date_trunc('MINUTE', time '2:30:21.5') as TIME1, "

Reply via email to