diff --git a/core/trino-main/src/test/java/io/trino/type/TestLongTimestampWithTimeZoneType.java b/core/trino-main/src/test/java/io/trino/type/TestLongTimestampWithTimeZoneType.java index 12927eb15cba..5c192b20d4ec 100644 --- a/core/trino-main/src/test/java/io/trino/type/TestLongTimestampWithTimeZoneType.java +++ b/core/trino-main/src/test/java/io/trino/type/TestLongTimestampWithTimeZoneType.java @@ -26,10 +26,12 @@ import java.util.Optional; import java.util.stream.Stream; +import static io.trino.spi.type.LongTimestampWithTimeZone.fromEpochMillisAndFraction; import static io.trino.spi.type.TimeZoneKey.UTC_KEY; import static io.trino.spi.type.TimeZoneKey.getTimeZoneKeyForOffset; -import static io.trino.spi.type.TimestampType.createTimestampType; import static io.trino.spi.type.TimestampWithTimeZoneType.TIMESTAMP_TZ_MICROS; +import static io.trino.spi.type.TimestampWithTimeZoneType.createTimestampWithTimeZoneType; +import static io.trino.spi.type.Timestamps.PICOSECONDS_PER_MILLISECOND; import static org.assertj.core.api.Assertions.assertThat; public class TestLongTimestampWithTimeZoneType @@ -43,17 +45,17 @@ public TestLongTimestampWithTimeZoneType() public static ValueBlock createTestBlock() { BlockBuilder blockBuilder = TIMESTAMP_TZ_MICROS.createFixedSizeBlockBuilder(15); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(1111, 0, getTimeZoneKeyForOffset(0))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(1111, 0, getTimeZoneKeyForOffset(1))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(1111, 0, getTimeZoneKeyForOffset(2))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(3))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(4))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(5))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(6))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(7))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(3333, 0, getTimeZoneKeyForOffset(8))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(3333, 0, getTimeZoneKeyForOffset(9))); - TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, LongTimestampWithTimeZone.fromEpochMillisAndFraction(4444, 0, getTimeZoneKeyForOffset(10))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(1111, 0, getTimeZoneKeyForOffset(0))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(1111, 0, getTimeZoneKeyForOffset(1))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(1111, 0, getTimeZoneKeyForOffset(2))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(3))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(4))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(5))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(6))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(2222, 0, getTimeZoneKeyForOffset(7))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(3333, 0, getTimeZoneKeyForOffset(8))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(3333, 0, getTimeZoneKeyForOffset(9))); + TIMESTAMP_TZ_MICROS.writeObject(blockBuilder, fromEpochMillisAndFraction(4444, 0, getTimeZoneKeyForOffset(10))); return blockBuilder.buildValueBlock(); } @@ -61,16 +63,16 @@ public static ValueBlock createTestBlock() protected Object getGreaterValue(Object value) { // time zone doesn't matter for ordering - return LongTimestampWithTimeZone.fromEpochMillisAndFraction(((LongTimestampWithTimeZone) value).getEpochMillis() + 1, 0, getTimeZoneKeyForOffset(33)); + return fromEpochMillisAndFraction(((LongTimestampWithTimeZone) value).getEpochMillis() + 1, 0, getTimeZoneKeyForOffset(33)); } @Test public void testPreviousValue() { - LongTimestampWithTimeZone minValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY); - LongTimestampWithTimeZone nextToMinValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 1_000_000, UTC_KEY); - LongTimestampWithTimeZone previousToMaxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 998_000_000, UTC_KEY); - LongTimestampWithTimeZone maxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 999_000_000, UTC_KEY); + LongTimestampWithTimeZone minValue = fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY); + LongTimestampWithTimeZone nextToMinValue = fromEpochMillisAndFraction(Long.MIN_VALUE, 1_000_000, UTC_KEY); + LongTimestampWithTimeZone previousToMaxValue = fromEpochMillisAndFraction(Long.MAX_VALUE, 998_000_000, UTC_KEY); + LongTimestampWithTimeZone maxValue = fromEpochMillisAndFraction(Long.MAX_VALUE, 999_000_000, UTC_KEY); assertThat(type.getPreviousValue(minValue)) .isEqualTo(Optional.empty()); @@ -78,12 +80,12 @@ public void testPreviousValue() .isEqualTo(Optional.of(minValue)); assertThat(type.getPreviousValue(getSampleValue())) - .isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1110, 999_000_000, getTimeZoneKeyForOffset(0)))); - assertThat(type.getPreviousValue(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1483228800000L, 000_000_000, getTimeZoneKeyForOffset(0)))) - .isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1483228799999L, 999_000_000, getTimeZoneKeyForOffset(0)))); + .isEqualTo(Optional.of(fromEpochMillisAndFraction(1110, 999_000_000, getTimeZoneKeyForOffset(0)))); + assertThat(type.getPreviousValue(fromEpochMillisAndFraction(1483228800000L, 000_000_000, getTimeZoneKeyForOffset(0)))) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(1483228799999L, 999_000_000, getTimeZoneKeyForOffset(0)))); assertThat(type.getPreviousValue(previousToMaxValue)) - .isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 997_000_000, UTC_KEY))); + .isEqualTo(Optional.of(fromEpochMillisAndFraction(Long.MAX_VALUE, 997_000_000, UTC_KEY))); assertThat(type.getPreviousValue(maxValue)) .isEqualTo(Optional.of(previousToMaxValue)); } @@ -91,20 +93,20 @@ public void testPreviousValue() @Test public void testNextValue() { - LongTimestampWithTimeZone minValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY); - LongTimestampWithTimeZone nextToMinValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 1_000_000, UTC_KEY); - LongTimestampWithTimeZone previousToMaxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 998_000_000, UTC_KEY); - LongTimestampWithTimeZone maxValue = LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MAX_VALUE, 999_000_000, UTC_KEY); + LongTimestampWithTimeZone minValue = fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY); + LongTimestampWithTimeZone nextToMinValue = fromEpochMillisAndFraction(Long.MIN_VALUE, 1_000_000, UTC_KEY); + LongTimestampWithTimeZone previousToMaxValue = fromEpochMillisAndFraction(Long.MAX_VALUE, 998_000_000, UTC_KEY); + LongTimestampWithTimeZone maxValue = fromEpochMillisAndFraction(Long.MAX_VALUE, 999_000_000, UTC_KEY); assertThat(type.getNextValue(minValue)) .isEqualTo(Optional.of(nextToMinValue)); assertThat(type.getNextValue(nextToMinValue)) - .isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(Long.MIN_VALUE, 2_000_000, UTC_KEY))); + .isEqualTo(Optional.of(fromEpochMillisAndFraction(Long.MIN_VALUE, 2_000_000, UTC_KEY))); assertThat(type.getNextValue(getSampleValue())) - .isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1111, 1_000_000, getTimeZoneKeyForOffset(0)))); - assertThat(type.getNextValue(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1483228799999L, 999_000_000, getTimeZoneKeyForOffset(0)))) - .isEqualTo(Optional.of(LongTimestampWithTimeZone.fromEpochMillisAndFraction(1483228800000L, 000_000_000, getTimeZoneKeyForOffset(0)))); + .isEqualTo(Optional.of(fromEpochMillisAndFraction(1111, 1_000_000, getTimeZoneKeyForOffset(0)))); + assertThat(type.getNextValue(fromEpochMillisAndFraction(1483228799999L, 999_000_000, getTimeZoneKeyForOffset(0)))) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(1483228800000L, 000_000_000, getTimeZoneKeyForOffset(0)))); assertThat(type.getNextValue(previousToMaxValue)) .isEqualTo(Optional.of(maxValue)); @@ -114,43 +116,55 @@ public void testNextValue() @ParameterizedTest @MethodSource("testPreviousNextValueEveryPrecisionDataProvider") - public void testPreviousValueEveryPrecision(int precision, long minValue, long maxValue, long step) + public void testPreviousValueEveryPrecision(int precision, LongTimestampWithTimeZone minValue, LongTimestampWithTimeZone maxValue, int step) { - Type type = createTimestampType(precision); + long middleRangeEpochMillis = 123_456_789_000_000L; + Type type = createTimestampWithTimeZoneType(precision); + + LongTimestampWithTimeZone zeroValueType = fromEpochMillisAndFraction(0, 0, UTC_KEY); + LongTimestampWithTimeZone sequenceValueType = fromEpochMillisAndFraction(middleRangeEpochMillis, 0, UTC_KEY); + LongTimestampWithTimeZone nextToMinValueType = fromEpochMillisAndFraction(minValue.getEpochMillis(), step, UTC_KEY); + LongTimestampWithTimeZone previousToMaxValueType = fromEpochMillisAndFraction(maxValue.getEpochMillis(), PICOSECONDS_PER_MILLISECOND - (2 * step), UTC_KEY); assertThat(type.getPreviousValue(minValue)) .isEqualTo(Optional.empty()); - assertThat(type.getPreviousValue(minValue + step)) + assertThat(type.getPreviousValue(nextToMinValueType)) .isEqualTo(Optional.of(minValue)); - assertThat(type.getPreviousValue(0L)) - .isEqualTo(Optional.of(-step)); - assertThat(type.getPreviousValue(123_456_789_000_000L)) - .isEqualTo(Optional.of(123_456_789_000_000L - step)); + assertThat(type.getPreviousValue(zeroValueType)) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(-1, PICOSECONDS_PER_MILLISECOND - step, UTC_KEY))); + assertThat(type.getPreviousValue(sequenceValueType)) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(middleRangeEpochMillis - 1, PICOSECONDS_PER_MILLISECOND - step, UTC_KEY))); - assertThat(type.getPreviousValue(maxValue - step)) - .isEqualTo(Optional.of(maxValue - 2 * step)); assertThat(type.getPreviousValue(maxValue)) - .isEqualTo(Optional.of(maxValue - step)); + .isEqualTo(Optional.of(fromEpochMillisAndFraction(maxValue.getEpochMillis(), PICOSECONDS_PER_MILLISECOND - (2 * step), UTC_KEY))); + assertThat(type.getPreviousValue(previousToMaxValueType)) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(maxValue.getEpochMillis(), PICOSECONDS_PER_MILLISECOND - (3 * step), UTC_KEY))); } @ParameterizedTest @MethodSource("testPreviousNextValueEveryPrecisionDataProvider") - public void testNextValueEveryPrecision(int precision, long minValue, long maxValue, long step) + public void testNextValueEveryPrecision(int precision, LongTimestampWithTimeZone minValue, LongTimestampWithTimeZone maxValue, int step) { - Type type = createTimestampType(precision); + long middleRangeEpochMillis = 123_456_789_000_000L; + + Type type = createTimestampWithTimeZoneType(precision); + LongTimestampWithTimeZone zeroValueType = fromEpochMillisAndFraction(0, 0, UTC_KEY); + LongTimestampWithTimeZone sequenceValueType = fromEpochMillisAndFraction(middleRangeEpochMillis, 0, UTC_KEY); + LongTimestampWithTimeZone nextToMinValueType = fromEpochMillisAndFraction(minValue.getEpochMillis(), step, UTC_KEY); + LongTimestampWithTimeZone previousToMaxValueType = fromEpochMillisAndFraction(maxValue.getEpochMillis(), PICOSECONDS_PER_MILLISECOND - (2 * step), UTC_KEY); assertThat(type.getNextValue(minValue)) - .isEqualTo(Optional.of(minValue + step)); - assertThat(type.getNextValue(minValue + step)) - .isEqualTo(Optional.of(minValue + 2 * step)); + .isEqualTo(Optional.of(nextToMinValueType)); + assertThat(type.getNextValue(nextToMinValueType)) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(minValue.getEpochMillis(), 2 * step, UTC_KEY))); - assertThat(type.getNextValue(0L)) - .isEqualTo(Optional.of(step)); - assertThat(type.getNextValue(123_456_789_000_000L)) - .isEqualTo(Optional.of(123_456_789_000_000L + step)); + assertThat(type.getNextValue(zeroValueType)) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(0, step, UTC_KEY))); + assertThat(type.getNextValue(sequenceValueType)) + .isEqualTo(Optional.of(fromEpochMillisAndFraction(middleRangeEpochMillis, step, UTC_KEY))); - assertThat(type.getNextValue(maxValue - step)) + assertThat(type.getNextValue(previousToMaxValueType)) .isEqualTo(Optional.of(maxValue)); assertThat(type.getNextValue(maxValue)) .isEqualTo(Optional.empty()); @@ -159,13 +173,51 @@ public void testNextValueEveryPrecision(int precision, long minValue, long maxVa public static Stream testPreviousNextValueEveryPrecisionDataProvider() { return Stream.of( - Arguments.of(0, Long.MIN_VALUE + 775808, Long.MAX_VALUE - 775807, 1_000_000L), - Arguments.of(1, Long.MIN_VALUE + 75808, Long.MAX_VALUE - 75807, 100_000L), - Arguments.of(2, Long.MIN_VALUE + 5808, Long.MAX_VALUE - 5807, 10_000L), - Arguments.of(3, Long.MIN_VALUE + 808, Long.MAX_VALUE - 807, 1_000L), - Arguments.of(4, Long.MIN_VALUE + 8, Long.MAX_VALUE - 7, 100L), - Arguments.of(5, Long.MIN_VALUE + 8, Long.MAX_VALUE - 7, 10L), - Arguments.of(6, Long.MIN_VALUE, Long.MAX_VALUE, 1L)); + Arguments.of( + 4, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 100_000_000, UTC_KEY), + 100_000_000), + Arguments.of( + 5, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 10_000_000, UTC_KEY), + 10_000_000), + Arguments.of( + 6, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 1_000_000, UTC_KEY), + 1_000_000), + Arguments.of( + 7, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 100_000, UTC_KEY), + 100_000), + Arguments.of( + 8, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 10_000, UTC_KEY), + 10_000), + Arguments.of( + 9, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 1_000, UTC_KEY), + 1_000), + Arguments.of( + 10, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 100, UTC_KEY), + 100), + Arguments.of( + 11, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 10, UTC_KEY), + 10), + Arguments.of( + 12, + fromEpochMillisAndFraction(Long.MIN_VALUE, 0, UTC_KEY), + fromEpochMillisAndFraction(Long.MAX_VALUE, PICOSECONDS_PER_MILLISECOND - 1, UTC_KEY), + 1)); } @Test