|
13 | 13 | */ |
14 | 14 | package io.trino.util; |
15 | 15 |
|
| 16 | +import io.trino.sql.tree.IntervalLiteral; |
| 17 | +import io.trino.sql.tree.IntervalLiteral.IntervalField; |
16 | 18 | import org.junit.jupiter.api.Test; |
17 | 19 |
|
18 | 20 | import java.time.DateTimeException; |
| 21 | +import java.time.Duration; |
| 22 | +import java.util.Optional; |
19 | 23 |
|
| 24 | +import static io.trino.sql.tree.IntervalLiteral.IntervalField.DAY; |
| 25 | +import static io.trino.sql.tree.IntervalLiteral.IntervalField.HOUR; |
| 26 | +import static io.trino.sql.tree.IntervalLiteral.IntervalField.MINUTE; |
| 27 | +import static io.trino.sql.tree.IntervalLiteral.IntervalField.SECOND; |
| 28 | +import static io.trino.sql.tree.IntervalLiteral.Sign.NEGATIVE; |
| 29 | +import static io.trino.sql.tree.IntervalLiteral.Sign.POSITIVE; |
| 30 | +import static io.trino.util.DateTimeUtils.formatDayTimeInterval; |
| 31 | +import static io.trino.util.DateTimeUtils.parseDayTimeInterval; |
20 | 32 | import static io.trino.util.DateTimeUtils.parseIfIso8601DateFormat; |
21 | 33 | import static org.assertj.core.api.Assertions.assertThat; |
22 | 34 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
@@ -85,4 +97,81 @@ public void testParseIfIso8601DateFormat() |
85 | 97 | .isInstanceOf(DateTimeException.class) |
86 | 98 | .hasMessage("Invalid value for DayOfMonth (valid values 1 - 28/31): 41"); |
87 | 99 | } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testDayTimeIntervalRoundTrip() |
| 103 | + { |
| 104 | + testDayTimeIntervalRoundTrip("0", SECOND, Optional.empty()); |
| 105 | + testDayTimeIntervalRoundTrip("0.000", SECOND, Optional.empty(), "0", SECOND, Optional.empty()); |
| 106 | + testDayTimeIntervalRoundTrip("45", SECOND, Optional.empty()); |
| 107 | + testDayTimeIntervalRoundTrip("0.555", SECOND, Optional.empty()); |
| 108 | + testDayTimeIntervalRoundTrip("59.999", SECOND, Optional.empty()); |
| 109 | + testDayTimeIntervalRoundTrip("60", SECOND, Optional.empty(), "1", MINUTE, Optional.empty()); |
| 110 | + testDayTimeIntervalRoundTrip("61", SECOND, Optional.empty(), "1:01", MINUTE, Optional.of(SECOND)); |
| 111 | + testDayTimeIntervalRoundTrip("3661", SECOND, Optional.empty(), "1:01:01", HOUR, Optional.of(SECOND)); |
| 112 | + testDayTimeIntervalRoundTrip("90061", SECOND, Optional.empty(), "1 1:01:01", DAY, Optional.of(SECOND)); |
| 113 | + |
| 114 | + testDayTimeIntervalRoundTrip("0", MINUTE, Optional.empty(), "0", SECOND, Optional.empty()); |
| 115 | + testDayTimeIntervalRoundTrip("25", MINUTE, Optional.empty()); |
| 116 | + testDayTimeIntervalRoundTrip("15:30", MINUTE, Optional.of(SECOND)); |
| 117 | + testDayTimeIntervalRoundTrip("59:00.999", MINUTE, Optional.of(SECOND)); |
| 118 | + testDayTimeIntervalRoundTrip("60", MINUTE, Optional.empty(), "1", HOUR, Optional.empty()); |
| 119 | + testDayTimeIntervalRoundTrip("61", MINUTE, Optional.empty(), "1:01", HOUR, Optional.of(MINUTE)); |
| 120 | + testDayTimeIntervalRoundTrip("1500", MINUTE, Optional.empty(), "1 1", DAY, Optional.of(HOUR)); |
| 121 | + testDayTimeIntervalRoundTrip("1501", MINUTE, Optional.empty(), "1 1:01", DAY, Optional.of(MINUTE)); |
| 122 | + |
| 123 | + testDayTimeIntervalRoundTrip("0", HOUR, Optional.empty(), "0", SECOND, Optional.empty()); |
| 124 | + testDayTimeIntervalRoundTrip("8", HOUR, Optional.empty()); |
| 125 | + testDayTimeIntervalRoundTrip("2:45", HOUR, Optional.of(MINUTE)); |
| 126 | + testDayTimeIntervalRoundTrip("2:00:45", HOUR, Optional.of(SECOND)); |
| 127 | + testDayTimeIntervalRoundTrip("1:30:45", HOUR, Optional.of(SECOND)); |
| 128 | + testDayTimeIntervalRoundTrip("1:00:00.999", HOUR, Optional.of(SECOND)); |
| 129 | + testDayTimeIntervalRoundTrip("24", HOUR, Optional.empty(), "1", DAY, Optional.empty()); |
| 130 | + testDayTimeIntervalRoundTrip("25", HOUR, Optional.empty(), "1 1", DAY, Optional.of(HOUR)); |
| 131 | + testDayTimeIntervalRoundTrip("17520", HOUR, Optional.empty(), "730", DAY, Optional.empty()); |
| 132 | + |
| 133 | + testDayTimeIntervalRoundTrip("0", DAY, Optional.empty(), "0", SECOND, Optional.empty()); |
| 134 | + testDayTimeIntervalRoundTrip("340", DAY, Optional.empty()); |
| 135 | + testDayTimeIntervalRoundTrip("2 6", DAY, Optional.of(HOUR)); |
| 136 | + testDayTimeIntervalRoundTrip("3 0:30", DAY, Optional.of(MINUTE)); |
| 137 | + testDayTimeIntervalRoundTrip("3 12:30", DAY, Optional.of(MINUTE)); |
| 138 | + testDayTimeIntervalRoundTrip("1 0:00:15", DAY, Optional.of(SECOND)); |
| 139 | + testDayTimeIntervalRoundTrip("1 4:20:15", DAY, Optional.of(SECOND)); |
| 140 | + testDayTimeIntervalRoundTrip("1 0:00:00.999", DAY, Optional.of(SECOND)); |
| 141 | + testDayTimeIntervalRoundTrip("1 23:59:59.999", DAY, Optional.of(SECOND)); |
| 142 | + } |
| 143 | + |
| 144 | + private static void testDayTimeIntervalRoundTrip(String value, IntervalField start, Optional<IntervalField> end) |
| 145 | + { |
| 146 | + testDayTimeIntervalRoundTrip(value, start, end, value, start, end); |
| 147 | + } |
| 148 | + |
| 149 | + private static void testDayTimeIntervalRoundTrip( |
| 150 | + String inputValue, |
| 151 | + IntervalField inputStart, |
| 152 | + Optional<IntervalField> inputEnd, |
| 153 | + String expectedValue, |
| 154 | + IntervalField expectedStart, |
| 155 | + Optional<IntervalField> expectedEnd) |
| 156 | + { |
| 157 | + long millis = parseDayTimeInterval(inputValue, inputStart, inputEnd); |
| 158 | + assertThat(millis).isGreaterThanOrEqualTo(0); |
| 159 | + |
| 160 | + IntervalLiteral positiveInterval = formatDayTimeInterval(Duration.ofMillis(millis)); |
| 161 | + assertThat(positiveInterval.getSign()).isEqualTo(POSITIVE); |
| 162 | + assertThat(positiveInterval.getStartField()).isEqualTo(expectedStart); |
| 163 | + assertThat(positiveInterval.getEndField()).isEqualTo(expectedEnd); |
| 164 | + assertThat(positiveInterval.getValue()).isEqualTo(expectedValue); |
| 165 | + |
| 166 | + if (millis != 0) { |
| 167 | + long millisNegative = parseDayTimeInterval("-" + inputValue, inputStart, inputEnd); |
| 168 | + assertThat(millisNegative).isLessThan(0); |
| 169 | + |
| 170 | + IntervalLiteral negativeInterval = formatDayTimeInterval(Duration.ofMillis(millisNegative)); |
| 171 | + assertThat(negativeInterval.getSign()).isEqualTo(NEGATIVE); |
| 172 | + assertThat(negativeInterval.getStartField()).isEqualTo(expectedStart); |
| 173 | + assertThat(negativeInterval.getEndField()).isEqualTo(expectedEnd); |
| 174 | + assertThat(negativeInterval.getValue()).isEqualTo(expectedValue); |
| 175 | + } |
| 176 | + } |
88 | 177 | } |
0 commit comments