paul-rogers commented on a change in pull request #2268:
URL: https://github.com/apache/drill/pull/2268#discussion_r671922160
##########
File path:
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/impl/DateTypeFunctions.java
##########
@@ -470,43 +500,73 @@ public void eval() {
@Param DateHolder left;
@Param DateHolder right;
@Output IntervalHolder out;
+ @Inject ContextInformation contextInfo;
@Override
public void setup() {
}
@Override
public void eval() {
- long diff = left.value - right.value;
- long days = diff /
org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis;
- out.months = (int) (days /
org.apache.drill.exec.vector.DateUtilities.monthToStandardDays);
- out.days = (int) (days %
org.apache.drill.exec.vector.DateUtilities.monthToStandardDays);
- out.milliseconds = (int) (diff %
org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis);
+ java.time.OffsetDateTime from =
java.time.Instant.ofEpochMilli(right.value).atOffset(java.time.ZoneOffset.UTC);
+ java.time.OffsetDateTime to =
java.time.Instant.ofEpochMilli(left.value).atOffset(java.time.ZoneOffset.UTC);
+ java.time.Duration duration =
java.time.Duration.between(from.toLocalTime(), to.toLocalTime());
+ java.time.Period period;
+
+ if (from.isAfter(to) &&
duration.compareTo(java.time.Duration.ZERO) > 0) {
+ // negative period and positive duration
+ period = java.time.Period.between(from.toLocalDate(),
to.toLocalDate().plusDays(1));
+ duration = duration.minusDays(1);
+ } else if (from.isBefore(to) &&
duration.compareTo(java.time.Duration.ZERO) < 0) {
+ // positive period and negative duration
+ period = java.time.Period.between(from.toLocalDate(),
to.toLocalDate().minusDays(1));
+ duration = duration.plusDays(1);
+ } else {
+ period = java.time.Period.between(from.toLocalDate(),
to.toLocalDate());
+ }
+
+ out.months = (int) period.toTotalMonths();
+ out.days = period.getDays();
+ out.milliseconds = (int) duration.toMillis();
}
}
@FunctionTemplate(name = "age", scope =
FunctionTemplate.FunctionScope.SIMPLE, nulls = NullHandling.NULL_IF_NULL)
public static class AgeDate2Function implements DrillSimpleFunc {
@Param DateHolder right;
- @Workspace long queryStartDate;
+ @Workspace java.time.OffsetDateTime queryStartDate;
@Output IntervalHolder out;
@Inject ContextInformation contextInfo;
@Override
public void setup() {
int timeZoneIndex = contextInfo.getRootFragmentTimeZone();
- org.joda.time.DateTimeZone timeZone =
org.joda.time.DateTimeZone.forID(org.apache.drill.exec.expr.fn.impl.DateUtility.getTimeZone(timeZoneIndex));
- org.joda.time.DateTime now = new
org.joda.time.DateTime(contextInfo.getQueryStartTime(), timeZone);
- queryStartDate = (new org.joda.time.DateMidnight(now.getYear(),
now.getMonthOfYear(), now.getDayOfMonth(), timeZone)).getMillis();
+ java.time.ZoneId zoneId =
java.time.ZoneId.of(org.apache.drill.exec.expr.fn.impl.DateUtility.getTimeZone(timeZoneIndex));
+ java.time.ZonedDateTime dt =
java.time.Instant.ofEpochMilli(contextInfo.getQueryStartTime()).atZone(zoneId);
+ queryStartDate = java.time.OffsetDateTime.of(dt.toLocalDate(),
java.time.LocalTime.MIDNIGHT, java.time.ZoneOffset.UTC);
}
@Override
public void eval() {
- long diff = queryStartDate - right.value;
- long days = diff /
org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis;
- out.months = (int) (days /
org.apache.drill.exec.vector.DateUtilities.monthToStandardDays);
- out.days = (int) (days %
org.apache.drill.exec.vector.DateUtilities.monthToStandardDays);
- out.milliseconds = (int) (diff %
org.apache.drill.exec.vector.DateUtilities.daysToStandardMillis);
+ java.time.OffsetDateTime dt =
java.time.Instant.ofEpochMilli(right.value).atOffset(java.time.ZoneOffset.UTC);
+ java.time.Duration duration =
java.time.Duration.between(dt.toLocalTime(), queryStartDate.toLocalTime());
+ java.time.Period period;
Review comment:
@oleg-zinovev, please don't go to heroic lengths to rewrite the date
functions. Date code is complex, we do want to use an existing library. The
question is more along the lines of, "so the Joda and Java techniques work, but
create many temporary objects in the inner per-row loop. Is there some other
*existing* library we can use?" If there isn't, then we just live with the
performance. After all `AGE()` is a very old-school function as it is not
stable in a distributed environment. It made more sense back when people used
just `DATE` types on a single-processor machine. So, the function probably
won't be used much. That's why we found the issue now, after many years.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]