Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ private static Comparable ceil(RexCall call, List<Comparable> values) {
switch (unit) {
case YEAR:
case MONTH:
case WEEK:
switch (call.getKind()) {
case FLOOR:
return DateTimeUtils.unixTimestampFloor(unit, v);
Expand All @@ -403,7 +404,7 @@ private static TimeUnitRange subUnit(TimeUnitRange unit) {
case QUARTER:
return TimeUnitRange.MONTH;
default:
return TimeUnitRange.DAY;
return unit;
}
}

Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/org/apache/calcite/rex/RexSimplify.java
Original file line number Diff line number Diff line change
Expand Up @@ -2646,12 +2646,13 @@

/** Method that returns whether we can rollup from inner time unit
* to outer time unit. */
private static boolean canRollUp(TimeUnit outer, TimeUnit inner) {

Check failure on line 2649 in core/src/main/java/org/apache/calcite/rex/RexSimplify.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_calcite&issues=AZq69k4tkIFRl7cHLGg-&open=AZq69k4tkIFRl7cHLGg-&pullRequest=4651
// Special handling for QUARTER as it is not in the expected
// order in TimeUnit
switch (outer) {
case YEAR:
case MONTH:
case WEEK:
case DAY:
case HOUR:
case MINUTE:
Expand All @@ -2662,6 +2663,7 @@
case YEAR:
case QUARTER:
case MONTH:
case WEEK:
case DAY:
case HOUR:
case MINUTE:
Expand All @@ -2671,6 +2673,12 @@
if (inner == TimeUnit.QUARTER) {
return outer == TimeUnit.YEAR;
}
if (outer == TimeUnit.WEEK && inner != TimeUnit.YEAR && inner != TimeUnit.MONTH) {
return true;
}
if (inner == TimeUnit.WEEK && outer != TimeUnit.YEAR && outer != TimeUnit.MONTH) {
return false;
}
return outer.ordinal() <= inner.ordinal();
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,13 +483,18 @@ public class RexImplicationCheckerTest {
hasToString("TRIM('BOTH', 'a', '1111':VARCHAR)"));
}

/** Test case for simplifier of ceil/floor. */
/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-2178">[CALCITE-2178]
* Extend expression simplifier to work on datetime CEIL/FLOOR functions</a>,
* <a href="https://issues.apache.org/jira/browse/CALCITE-7304">[CALCITE-7304]
* Floor/Ceil can not simplify with WEEK TimeUnit</a>. */
@Test void testSimplifyCeilFloor() {
// We can add more time units here once they are supported in
// RexInterpreter, e.g., TimeUnitRange.HOUR, TimeUnitRange.MINUTE,
// TimeUnitRange.SECOND.
final ImmutableList<TimeUnitRange> timeUnitRanges =
ImmutableList.of(TimeUnitRange.YEAR, TimeUnitRange.MONTH);
ImmutableList.of(TimeUnitRange.YEAR, TimeUnitRange.MONTH, TimeUnitRange.WEEK,
TimeUnitRange.HOUR, TimeUnitRange.MINUTE, TimeUnitRange.SECOND);
final Fixture f = new Fixture();

final RexNode literalTs =
Expand Down
Loading