DRILL-1088: implemented date_trunc use TestFunctionsQuery instead of TestDateFunctions
Project: http://git-wip-us.apache.org/repos/asf/incubator-drill/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-drill/commit/0b905fe1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-drill/tree/0b905fe1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-drill/diff/0b905fe1 Branch: refs/heads/master Commit: 0b905fe1b4d7a225ef4264ffd09f35fd7d876f73 Parents: 53a89d6 Author: Cliff Buchanan <[email protected]> Authored: Thu Jun 26 14:35:03 2014 -0700 Committer: Jacques Nadeau <[email protected]> Committed: Mon Jul 7 15:53:21 2014 -0700 ---------------------------------------------------------------------- .../DateDateArithmeticFunctions.java | 50 ++++++++++++++++++-- .../drill/jdbc/test/TestFunctionsQuery.java | 21 ++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-drill/blob/0b905fe1/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 676df67..0074c7c 100644 --- a/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateDateArithmeticFunctions.java +++ b/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/DateDateArithmeticFunctions.java @@ -22,7 +22,7 @@ import org.apache.drill.exec.expr.annotations.Workspace; <#list dateIntervalFunc.dates as type> -<@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/G${type}Difference.java" /> +<@pp.changeOutputFile name="/org/apache/drill/exec/expr/fn/impl/G${type}Arithmetic.java" /> <#include "/@includes/license.ftl" /> @@ -40,8 +40,11 @@ import org.apache.drill.exec.record.RecordBatch; import io.netty.buffer.ByteBuf; @SuppressWarnings("unused") +public class G${type}Arithmetic { +@SuppressWarnings("unused") + @FunctionTemplate(names = {"date_diff", "subtract", "date_sub"}, scope = FunctionTemplate.FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL) -public class G${type}Difference implements DrillSimpleFunc { +public static class G${type}Difference implements DrillSimpleFunc { @Param ${type}Holder left; @Param ${type}Holder right; @@ -72,4 +75,45 @@ public class G${type}Difference implements DrillSimpleFunc { </#if> } } -</#list> \ No newline at end of file + +@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/0b905fe1/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 3ec79c8..53eef9c 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 @@ -529,6 +529,27 @@ public class TestFunctionsQuery { } @Test + public void testDateTrunc() throws Exception { + String query = "select " + + "date_trunc('MINUTE', time '2:30:21.5') as TIME1, " + + "date_trunc('SECOND', time '2:30:21.5') as TIME2, " + + "date_trunc('HOUR', timestamp '1991-05-05 10:11:12.100') as TS1, " + + "date_trunc('SECOND', timestamp '1991-05-05 10:11:12.100') as TS2, " + + "date_trunc('MONTH', date '2011-2-2') as DATE1, " + + "date_trunc('YEAR', date '2011-2-2') as DATE2 " + + "from cp.`employee.json` where employee_id < 2"; + JdbcAssert.withNoDefaultSchema() + .sql(query) + .returns( + "TIME1=02:30:00; " + + "TIME2=02:30:21; " + + "TS1=1991-05-05 10:00:00.0; " + + "TS2=1991-05-05 10:11:12.0; " + + "DATE1=2011-02-01; " + + "DATE2=2011-01-01\n"); + } + + @Test @Ignore public void testToTimeStamp() throws Exception { String query = "select to_timestamp(cast('800120400.12312' as decimal(38, 5))) as DEC38_TS, to_timestamp(200120400) as INT_TS " +
