|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.expression.datetime; |
| 7 | + |
| 8 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 9 | +import static org.opensearch.sql.data.type.ExprCoreType.DATETIME; |
| 10 | +import static org.opensearch.sql.data.type.ExprCoreType.TIME; |
| 11 | + |
| 12 | +import java.time.Instant; |
| 13 | +import java.time.LocalDate; |
| 14 | +import java.time.LocalDateTime; |
| 15 | +import java.time.LocalTime; |
| 16 | +import java.time.temporal.Temporal; |
| 17 | +import java.util.stream.Stream; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | +import org.junit.jupiter.params.ParameterizedTest; |
| 20 | +import org.junit.jupiter.params.provider.Arguments; |
| 21 | +import org.junit.jupiter.params.provider.MethodSource; |
| 22 | + |
| 23 | +public class AddTimeAndSubTimeTest extends DateTimeTestBase { |
| 24 | + |
| 25 | + @Test |
| 26 | + // (TIME, TIME/DATE/DATETIME/TIMESTAMP) -> TIME |
| 27 | + public void return_time_when_first_arg_is_time() { |
| 28 | + var res = addtime(LocalTime.of(21, 0), LocalTime.of(0, 5)); |
| 29 | + assertEquals(TIME, res.type()); |
| 30 | + assertEquals(LocalTime.of(21, 5), res.timeValue()); |
| 31 | + |
| 32 | + res = subtime(LocalTime.of(21, 0), LocalTime.of(0, 5)); |
| 33 | + assertEquals(TIME, res.type()); |
| 34 | + assertEquals(LocalTime.of(20, 55), res.timeValue()); |
| 35 | + |
| 36 | + res = addtime(LocalTime.of(12, 20), Instant.ofEpochSecond(42)); |
| 37 | + assertEquals(TIME, res.type()); |
| 38 | + assertEquals(LocalTime.of(12, 20, 42), res.timeValue()); |
| 39 | + |
| 40 | + res = subtime(LocalTime.of(10, 0), Instant.ofEpochSecond(42)); |
| 41 | + assertEquals(TIME, res.type()); |
| 42 | + assertEquals(LocalTime.of(9, 59, 18), res.timeValue()); |
| 43 | + |
| 44 | + res = addtime(LocalTime.of(2, 3, 4), LocalDateTime.of(1961, 4, 12, 9, 7)); |
| 45 | + assertEquals(TIME, res.type()); |
| 46 | + assertEquals(LocalTime.of(11, 10, 4), res.timeValue()); |
| 47 | + |
| 48 | + res = subtime(LocalTime.of(12, 3, 4), LocalDateTime.of(1961, 4, 12, 9, 7)); |
| 49 | + assertEquals(TIME, res.type()); |
| 50 | + assertEquals(LocalTime.of(2, 56, 4), res.timeValue()); |
| 51 | + |
| 52 | + res = addtime(LocalTime.of(9, 7), LocalDate.now()); |
| 53 | + assertEquals(TIME, res.type()); |
| 54 | + assertEquals(LocalTime.of(9, 7), res.timeValue()); |
| 55 | + |
| 56 | + res = subtime(LocalTime.of(9, 7), LocalDate.of(1961, 4, 12)); |
| 57 | + assertEquals(TIME, res.type()); |
| 58 | + assertEquals(LocalTime.of(9, 7), res.timeValue()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void time_limited_by_24_hours() { |
| 63 | + var res = addtime(LocalTime.of(21, 0), LocalTime.of(14, 5)); |
| 64 | + assertEquals(TIME, res.type()); |
| 65 | + assertEquals(LocalTime.of(11, 5), res.timeValue()); |
| 66 | + |
| 67 | + res = subtime(LocalTime.of(14, 0), LocalTime.of(21, 5)); |
| 68 | + assertEquals(TIME, res.type()); |
| 69 | + assertEquals(LocalTime.of(16, 55), res.timeValue()); |
| 70 | + } |
| 71 | + |
| 72 | + // Function signature is: |
| 73 | + // (DATE/DATETIME/TIMESTAMP, TIME/DATE/DATETIME/TIMESTAMP) -> DATETIME |
| 74 | + private static Stream<Arguments> getTestData() { |
| 75 | + return Stream.of( |
| 76 | + // DATETIME and TIME/DATE/DATETIME/TIMESTAMP |
| 77 | + Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalTime.of(1, 48), |
| 78 | + LocalDateTime.of(1961, 4, 12, 10, 55), LocalDateTime.of(1961, 4, 12, 7, 19)), |
| 79 | + Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalDate.of(2000, 1, 1), |
| 80 | + LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1961, 4, 12, 9, 7)), |
| 81 | + Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1235, 5, 6, 1, 48), |
| 82 | + LocalDateTime.of(1961, 4, 12, 10, 55), LocalDateTime.of(1961, 4, 12, 7, 19)), |
| 83 | + Arguments.of(LocalDateTime.of(1961, 4, 12, 9, 7), Instant.ofEpochSecond(42), |
| 84 | + LocalDateTime.of(1961, 4, 12, 9, 7, 42), LocalDateTime.of(1961, 4, 12, 9, 6, 18)), |
| 85 | + // DATE and TIME/DATE/DATETIME/TIMESTAMP |
| 86 | + Arguments.of(LocalDate.of(1961, 4, 12), LocalTime.of(9, 7), |
| 87 | + LocalDateTime.of(1961, 4, 12, 9, 7), LocalDateTime.of(1961, 4, 11, 14, 53)), |
| 88 | + Arguments.of(LocalDate.of(1961, 4, 12), LocalDate.of(2000, 1, 1), |
| 89 | + LocalDateTime.of(1961, 4, 12, 0, 0), LocalDateTime.of(1961, 4, 12, 0, 0)), |
| 90 | + Arguments.of(LocalDate.of(1961, 4, 12), LocalDateTime.of(1235, 5, 6, 1, 48), |
| 91 | + LocalDateTime.of(1961, 4, 12, 1, 48), LocalDateTime.of(1961, 4, 11, 22, 12)), |
| 92 | + Arguments.of(LocalDate.of(1961, 4, 12), Instant.ofEpochSecond(42), |
| 93 | + LocalDateTime.of(1961, 4, 12, 0, 0, 42), LocalDateTime.of(1961, 4, 11, 23, 59, 18)), |
| 94 | + // TIMESTAMP and TIME/DATE/DATETIME/TIMESTAMP |
| 95 | + Arguments.of(Instant.ofEpochSecond(42), LocalTime.of(9, 7), |
| 96 | + LocalDateTime.of(1970, 1, 1, 9, 7, 42), LocalDateTime.of(1969, 12, 31, 14, 53, 42)), |
| 97 | + Arguments.of(Instant.ofEpochSecond(42), LocalDate.of(1961, 4, 12), |
| 98 | + LocalDateTime.of(1970, 1, 1, 0, 0, 42), LocalDateTime.of(1970, 1, 1, 0, 0, 42)), |
| 99 | + Arguments.of(Instant.ofEpochSecond(42), LocalDateTime.of(1961, 4, 12, 9, 7), |
| 100 | + LocalDateTime.of(1970, 1, 1, 9, 7, 42), LocalDateTime.of(1969, 12, 31, 14, 53, 42)), |
| 101 | + Arguments.of(Instant.ofEpochSecond(42), Instant.ofEpochMilli(42), |
| 102 | + LocalDateTime.of(1970, 1, 1, 0, 0, 42, 42000000), |
| 103 | + LocalDateTime.of(1970, 1, 1, 0, 0, 41, 958000000)) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Check that `ADDTIME` and `SUBTIME` functions result value and type. |
| 109 | + * @param arg1 First argument. |
| 110 | + * @param arg2 Second argument. |
| 111 | + * @param addTimeExpectedResult Expected result for `ADDTIME`. |
| 112 | + * @param subTimeExpectedResult Expected result for `SUBTIME`. |
| 113 | + */ |
| 114 | + @ParameterizedTest |
| 115 | + @MethodSource("getTestData") |
| 116 | + public void return_datetime_when_first_arg_is_not_time(Temporal arg1, Temporal arg2, |
| 117 | + LocalDateTime addTimeExpectedResult, |
| 118 | + LocalDateTime subTimeExpectedResult) { |
| 119 | + var res = addtime(arg1, arg2); |
| 120 | + assertEquals(DATETIME, res.type()); |
| 121 | + assertEquals(addTimeExpectedResult, res.datetimeValue()); |
| 122 | + |
| 123 | + res = subtime(arg1, arg2); |
| 124 | + assertEquals(DATETIME, res.type()); |
| 125 | + assertEquals(subTimeExpectedResult, res.datetimeValue()); |
| 126 | + } |
| 127 | +} |
0 commit comments