You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@drill.apache.org by cg...@apache.org on 2021/12/18 23:03:27 UTC

[drill] branch master updated: DRILL-8078: Add WEEK to Date Extract (#2407)

This is an automated email from the ASF dual-hosted git repository.

cgivre pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/drill.git


The following commit(s) were added to refs/heads/master by this push:
     new ef8ea00  DRILL-8078: Add WEEK to Date Extract (#2407)
ef8ea00 is described below

commit ef8ea00b89e5d58781e8659e860cf4d63b321662
Author: Charles S. Givre <cg...@apache.org>
AuthorDate: Sat Dec 18 18:03:20 2021 -0500

    DRILL-8078: Add WEEK to Date Extract (#2407)
---
 exec/java-exec/src/main/codegen/data/ExtractTypes.tdd          |  2 +-
 .../templates/DateIntervalFunctionTemplates/Extract.java       | 10 ++++++++--
 .../java/org/apache/drill/exec/planner/logical/DrillOptiq.java |  3 ++-
 .../org/apache/drill/exec/planner/sql/TypeInferenceUtils.java  |  5 +++--
 .../org/apache/drill/exec/fn/impl/TestNewDateFunctions.java    | 10 ++++++++++
 .../main/java/org/apache/drill/exec/vector/DateUtilities.java  |  1 +
 6 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/exec/java-exec/src/main/codegen/data/ExtractTypes.tdd b/exec/java-exec/src/main/codegen/data/ExtractTypes.tdd
index 4d8473d..2d48699 100644
--- a/exec/java-exec/src/main/codegen/data/ExtractTypes.tdd
+++ b/exec/java-exec/src/main/codegen/data/ExtractTypes.tdd
@@ -17,6 +17,6 @@
 #
 
 {
-  toTypes: [Second, Minute, Hour, Day, Month, Year],
+  toTypes: [Second, Minute, Hour, Day, Week, Month, Year],
   fromTypes: [Date, Time, TimeStamp, Interval, IntervalDay, IntervalYear]
 }
diff --git a/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/Extract.java b/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/Extract.java
index ece7128..b09956a 100644
--- a/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/Extract.java
+++ b/exec/java-exec/src/main/codegen/templates/DateIntervalFunctionTemplates/Extract.java
@@ -38,7 +38,7 @@ public class ${className} {
 <#list extract.fromTypes as fromUnit>
 <#list extract.toTypes as toUnit>
 <#if fromUnit == "Date" || fromUnit == "Time" || fromUnit == "TimeStamp">
-<#if !(fromUnit == "Time" && (toUnit == "Year" || toUnit == "Month" || toUnit == "Day"))>
+<#if !(fromUnit == "Time" && (toUnit == "Year" || toUnit == "Month" || toUnit == "Week" || toUnit == "Day"))>
   @FunctionTemplate(name = "extract${toUnit}", scope = FunctionTemplate.FunctionScope.SIMPLE,
       nulls = FunctionTemplate.NullHandling.NULL_IF_NULL)
   public static class ${toUnit}From${fromUnit} implements DrillSimpleFunc {
@@ -67,6 +67,8 @@ public class ${className} {
       out.value = dateTime.getHourOfDay();
     <#elseif toUnit = "Day">
       out.value = dateTime.getDayOfMonth();
+    <#elseif toUnit = "Week">
+      out.value = dateTime.getWeekOfWeekyear();
     <#elseif toUnit = "Month">
       out.value = dateTime.getMonthOfYear();
     <#elseif toUnit = "Year">
@@ -95,6 +97,8 @@ public class ${className} {
       out.value = (in.months / org.apache.drill.exec.vector.DateUtilities.yearsToMonths);
     <#elseif toUnit == "Month">
       out.value = (in.months % org.apache.drill.exec.vector.DateUtilities.yearsToMonths);
+    <#elseif toUnit == "Week">
+      out.value = (in.days / org.apache.drill.exec.vector.DateUtilities.daysToWeeks);
     <#elseif toUnit == "Day">
       out.value = in.days;
     <#elseif toUnit == "Hour">
@@ -109,6 +113,8 @@ public class ${className} {
   <#elseif fromUnit == "IntervalDay">
     <#if toUnit == "Year" || toUnit == "Month">
       out.value = 0;
+    <#elseif toUnit == "Week">
+      out.value = 0;
     <#elseif toUnit == "Day">
       out.value = in.days;
     <#elseif toUnit == "Hour">
@@ -134,4 +140,4 @@ public class ${className} {
 </#if>
 </#list>
 </#list>
-}
\ No newline at end of file
+}
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java
index df93ea4..7cd69c8 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillOptiq.java
@@ -560,6 +560,7 @@ public class DrillOptiq {
           switch (timeUnit) {
             case YEAR:
             case MONTH:
+            case WEEK:
             case DAY:
             case HOUR:
             case MINUTE:
@@ -568,7 +569,7 @@ public class DrillOptiq {
               functionName += functionPostfix;
               return FunctionCallFactory.createExpression(functionName, args.subList(1, 2));
             default:
-              throw new UnsupportedOperationException("extract function supports the following time units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND");
+              throw new UnsupportedOperationException("extract function supports the following time units: YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND");
           }
         }
         case "timestampdiff": {
diff --git a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
index 7e68c35..c95eb1a 100644
--- a/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
+++ b/exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
@@ -926,6 +926,7 @@ public class TypeInferenceUtils {
     switch (timeUnit) {
       case YEAR:
       case MONTH:
+      case WEEK:
       case DAY:
       case HOUR:
       case MINUTE:
@@ -935,7 +936,7 @@ public class TypeInferenceUtils {
       default:
         throw UserException
             .functionError()
-            .message("extract function supports the following time units: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND")
+            .message("extract function supports the following time units: YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND")
             .build(logger);
     }
   }
@@ -1047,4 +1048,4 @@ public class TypeInferenceUtils {
   private TypeInferenceUtils() {
 
   }
-}
\ No newline at end of file
+}
diff --git a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewDateFunctions.java b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewDateFunctions.java
index 5cb28e5..5e5fc01 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewDateFunctions.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/exec/fn/impl/TestNewDateFunctions.java
@@ -132,6 +132,16 @@ public class TestNewDateFunctions extends BaseTestQuery {
   }
 
   @Test
+  public void testExtractWeek() throws Exception {
+    testBuilder()
+      .sqlQuery("SELECT EXTRACT(WEEK FROM hire_date) AS col FROM cp.`employee.json` WHERE last_name='Nowmer'")
+      .unOrdered()
+      .baselineColumns("col")
+      .baselineValues(48L)
+      .go();
+  }
+
+  @Test
   public void testLocalTimestamp() throws Exception {
     testBuilder()
         .sqlQuery("select extract(day from localtimestamp) = extract(day from current_date) as col from cp.`employee.json` limit 1")
diff --git a/exec/vector/src/main/java/org/apache/drill/exec/vector/DateUtilities.java b/exec/vector/src/main/java/org/apache/drill/exec/vector/DateUtilities.java
index 330a742..9090192 100644
--- a/exec/vector/src/main/java/org/apache/drill/exec/vector/DateUtilities.java
+++ b/exec/vector/src/main/java/org/apache/drill/exec/vector/DateUtilities.java
@@ -36,6 +36,7 @@ import org.joda.time.Period;
 public class DateUtilities {
 
   public static final int yearsToMonths = 12;
+  public static final int daysToWeeks = 7;
   public static final int hoursToMillis = 60 * 60 * 1000;
   public static final int minutesToMillis = 60 * 1000;
   public static final int secondsToMillis = 1000;