Skip to content

Commit

Permalink
Optimize date_trunc with hour unit on timestamp
Browse files Browse the repository at this point in the history
Replace `date_trunc('hour', a_timestamp)` in comparison with a range predicate.
See 80c079f for more context.
  • Loading branch information
findepi committed Sep 19, 2022
1 parent 2c92f9f commit a3d2c2f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ private Expression unwrapDateTrunc(ComparisonExpression expression)
return expression;
}
SupportedUnit unit = unitIfSupported.get();
if (unit == SupportedUnit.DAY && rightType == DATE) {
// case handled by CanonicalizeExpressionRewriter
if (rightType == DATE && (unit == SupportedUnit.DAY || unit == SupportedUnit.HOUR)) {
// DAY case handled by CanonicalizeExpressionRewriter, other is illegal, will fail
return expression;
}

Expand Down Expand Up @@ -260,7 +260,7 @@ private Object calculateRangeEndInclusive(Object rangeStart, Type type, Supporte
if (type == DATE) {
LocalDate date = LocalDate.ofEpochDay((long) rangeStart);
LocalDate endExclusive = switch (rangeUnit) {
case DAY -> throw new UnsupportedOperationException("Unsupported type and unit: %s, %s".formatted(type, rangeUnit));
case HOUR, DAY -> throw new UnsupportedOperationException("Unsupported type and unit: %s, %s".formatted(type, rangeUnit));
case MONTH -> date.plusMonths(1);
case YEAR -> date.plusYears(1);
};
Expand All @@ -274,6 +274,7 @@ private Object calculateRangeEndInclusive(Object rangeStart, Type type, Supporte
verify(microOfSecond == 0, "Unexpected micros, value should be rounded to %s: %s", rangeUnit, microOfSecond);
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.UTC);
LocalDateTime endExclusive = switch (rangeUnit) {
case HOUR -> dateTime.plusHours(1);
case DAY -> dateTime.plusDays(1);
case MONTH -> dateTime.plusMonths(1);
case YEAR -> dateTime.plusYears(1);
Expand Down Expand Up @@ -321,6 +322,7 @@ private int compare(Type type, Object first, Object second)

private enum SupportedUnit
{
HOUR,
DAY,
MONTH,
YEAR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ else if (format == AVRO) {

// date_trunc
assertThat(query("SELECT * FROM test_hour_transform_timestamp WHERE date_trunc('hour', d) = TIMESTAMP '2015-05-15 12:00:00'"))
.isNotFullyPushedDown(FilterNode.class); // TODO convert into range
.isFullyPushedDown();
assertThat(query("SELECT * FROM test_hour_transform_timestamp WHERE date_trunc('day', d) = DATE '2015-05-15'"))
.isFullyPushedDown();
assertThat(query("SELECT * FROM test_hour_transform_timestamp WHERE date_trunc('month', d) = DATE '2015-05-01'"))
Expand Down

0 comments on commit a3d2c2f

Please sign in to comment.