|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.iceberg.manifest; |
| 20 | + |
| 21 | +import org.apache.paimon.data.Timestamp; |
| 22 | +import org.apache.paimon.types.DataTypes; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.DisplayName; |
| 25 | +import org.junit.jupiter.params.ParameterizedTest; |
| 26 | +import org.junit.jupiter.params.provider.Arguments; |
| 27 | +import org.junit.jupiter.params.provider.MethodSource; |
| 28 | + |
| 29 | +import java.nio.ByteBuffer; |
| 30 | +import java.nio.ByteOrder; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 35 | + |
| 36 | +class IcebergConversionsTimestampTest { |
| 37 | + |
| 38 | + @ParameterizedTest |
| 39 | + @MethodSource("provideTimestampConversionCases") |
| 40 | + void testTimestampToByteBuffer(int precision, Timestamp input, long expectedMicros) { |
| 41 | + ByteBuffer buffer = IcebergConversions.toByteBuffer(DataTypes.TIMESTAMP(precision), input); |
| 42 | + assertThat(buffer.order()).isEqualTo(ByteOrder.LITTLE_ENDIAN); |
| 43 | + assertThat(buffer.getLong(0)).isEqualTo(expectedMicros); |
| 44 | + } |
| 45 | + |
| 46 | + private static Stream<Arguments> provideTimestampConversionCases() { |
| 47 | + Timestamp tsMillis = |
| 48 | + Timestamp.fromEpochMillis(1682164983524L); // 2023-04-22T13:03:03.524 (p=3) |
| 49 | + Timestamp tsMicros = Timestamp.fromMicros(1683849603123456L); // 2023-05-12T00:00:03.123456 |
| 50 | + |
| 51 | + return Stream.of( |
| 52 | + // For p=3..6 we encode microseconds per Iceberg spec |
| 53 | + Arguments.of(3, tsMillis, 1682164983524000L), // micros from millis |
| 54 | + Arguments.of(4, tsMillis, 1682164983524000L), |
| 55 | + Arguments.of(5, tsMillis, 1682164983524000L), |
| 56 | + Arguments.of(6, tsMillis, 1682164983524000L), |
| 57 | + Arguments.of(6, tsMicros, 1683849603123456L)); // passthrough |
| 58 | + } |
| 59 | + |
| 60 | + @ParameterizedTest |
| 61 | + @MethodSource("provideInvalidPrecisions") |
| 62 | + @DisplayName("Invalid timestamp precisions for ByteBuffer conversion") |
| 63 | + void testTimestampToByteBufferInvalidPrecisions(int precision) { |
| 64 | + Timestamp timestamp = Timestamp.fromEpochMillis(1682164983524L); |
| 65 | + |
| 66 | + assertThatThrownBy( |
| 67 | + () -> |
| 68 | + IcebergConversions.toByteBuffer( |
| 69 | + DataTypes.TIMESTAMP(precision), timestamp)) |
| 70 | + .isInstanceOf(IllegalArgumentException.class) |
| 71 | + .hasMessageContaining( |
| 72 | + "Paimon Iceberg compatibility only support timestamp type with precision from 3 to 6."); |
| 73 | + } |
| 74 | + |
| 75 | + private static Stream<Arguments> provideInvalidPrecisions() { |
| 76 | + return Stream.of( |
| 77 | + Arguments.of(0), |
| 78 | + Arguments.of(1), |
| 79 | + Arguments.of(2), |
| 80 | + Arguments.of(7), |
| 81 | + Arguments.of(8), |
| 82 | + Arguments.of(9)); |
| 83 | + } |
| 84 | + |
| 85 | + // ------------------------------------------------------------------------ |
| 86 | + // toPaimonObject tests |
| 87 | + // ------------------------------------------------------------------------ |
| 88 | + |
| 89 | + @ParameterizedTest |
| 90 | + @MethodSource("provideTimestampToPaimonCases") |
| 91 | + void testToPaimonObjectForTimestamp(int precision, long serializedMicros, String expectedTs) { |
| 92 | + byte[] bytes = new byte[8]; |
| 93 | + ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).putLong(serializedMicros); |
| 94 | + |
| 95 | + Timestamp actual = |
| 96 | + (Timestamp) |
| 97 | + IcebergConversions.toPaimonObject(DataTypes.TIMESTAMP(precision), bytes); |
| 98 | + |
| 99 | + assertThat(actual.toString()).isEqualTo(expectedTs); |
| 100 | + } |
| 101 | + |
| 102 | + private static Stream<Arguments> provideTimestampToPaimonCases() { |
| 103 | + return Stream.of( |
| 104 | + // Provide binary in micros; p=3..6 should all parse as micros |
| 105 | + Arguments.of(3, -1356022717123L, "1927-01-12T07:01:22.877"), |
| 106 | + Arguments.of(3, 1713790983524L, "2024-04-22T13:03:03.524"), |
| 107 | + Arguments.of(6, 1640690931207203L, "2021-12-28T11:28:51.207203")); |
| 108 | + } |
| 109 | + |
| 110 | + @ParameterizedTest |
| 111 | + @MethodSource("provideInvalidTimestampCases") |
| 112 | + void testToPaimonObjectTimestampInvalid(int precision, long serializedMicros) { |
| 113 | + byte[] bytes = new byte[8]; |
| 114 | + ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).putLong(serializedMicros); |
| 115 | + |
| 116 | + assertThatThrownBy( |
| 117 | + () -> |
| 118 | + IcebergConversions.toPaimonObject( |
| 119 | + DataTypes.TIMESTAMP(precision), bytes)) |
| 120 | + .isInstanceOf(IllegalArgumentException.class) |
| 121 | + .hasMessageContaining( |
| 122 | + "Paimon Iceberg compatibility only support timestamp type with precision from 3 to 6."); |
| 123 | + } |
| 124 | + |
| 125 | + private static Stream<Arguments> provideInvalidTimestampCases() { |
| 126 | + return Stream.of(Arguments.of(0, 1698686153L), Arguments.of(9, 1698686153123456789L)); |
| 127 | + } |
| 128 | +} |
0 commit comments