|
18 | 18 | package org.apache.fluss.client.converter; |
19 | 19 |
|
20 | 20 | import org.apache.fluss.row.GenericRow; |
| 21 | +import org.apache.fluss.row.TimestampLtz; |
| 22 | +import org.apache.fluss.row.TimestampNtz; |
21 | 23 | import org.apache.fluss.types.DataTypes; |
22 | 24 | import org.apache.fluss.types.RowType; |
23 | 25 |
|
24 | 26 | import org.junit.jupiter.api.Test; |
25 | 27 |
|
| 28 | +import java.time.Instant; |
| 29 | +import java.time.LocalDateTime; |
| 30 | + |
26 | 31 | import static org.assertj.core.api.Assertions.assertThat; |
27 | 32 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
28 | 33 |
|
@@ -168,4 +173,157 @@ public void testUnsupportedSchemaFieldTypeThrows() { |
168 | 173 | .hasMessageContaining("MAP") |
169 | 174 | .hasMessageContaining("mapField"); |
170 | 175 | } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void testTimestampPrecision3() { |
| 179 | + // Test with precision 3 milliseconds |
| 180 | + RowType table = |
| 181 | + RowType.builder() |
| 182 | + .field("timestampNtzField", DataTypes.TIMESTAMP(3)) |
| 183 | + .field("timestampLtzField", DataTypes.TIMESTAMP_LTZ(3)) |
| 184 | + .build(); |
| 185 | + |
| 186 | + PojoToRowConverter<TimestampPojo> writer = |
| 187 | + PojoToRowConverter.of(TimestampPojo.class, table, table); |
| 188 | + |
| 189 | + // 123.456789 |
| 190 | + LocalDateTime ldt = LocalDateTime.of(2025, 7, 23, 15, 1, 30, 123456789); |
| 191 | + Instant instant = Instant.parse("2025-07-23T15:01:30.123456789Z"); |
| 192 | + |
| 193 | + TimestampPojo pojo = new TimestampPojo(ldt, instant); |
| 194 | + GenericRow row = writer.toRow(pojo); |
| 195 | + |
| 196 | + // truncate to 123.000000 |
| 197 | + TimestampNtz expectedNtz = TimestampNtz.fromLocalDateTime(ldt.withNano(123000000)); |
| 198 | + TimestampLtz expectedLtz = |
| 199 | + TimestampLtz.fromInstant( |
| 200 | + Instant.ofEpochSecond(instant.getEpochSecond(), 123000000)); |
| 201 | + |
| 202 | + assertThat(row.getTimestampNtz(0, 3)).isEqualTo(expectedNtz); |
| 203 | + assertThat(row.getTimestampLtz(1, 3)).isEqualTo(expectedLtz); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void testTimestampPrecision6() { |
| 208 | + // Test with precision 6 microseconds |
| 209 | + RowType table = |
| 210 | + RowType.builder() |
| 211 | + .field("timestampNtzField", DataTypes.TIMESTAMP(6)) |
| 212 | + .field("timestampLtzField", DataTypes.TIMESTAMP_LTZ(6)) |
| 213 | + .build(); |
| 214 | + |
| 215 | + PojoToRowConverter<TimestampPojo> writer = |
| 216 | + PojoToRowConverter.of(TimestampPojo.class, table, table); |
| 217 | + |
| 218 | + // 123.456789 |
| 219 | + LocalDateTime ldt = LocalDateTime.of(2025, 7, 23, 15, 1, 30, 123456789); |
| 220 | + Instant instant = Instant.parse("2025-07-23T15:01:30.123456789Z"); |
| 221 | + |
| 222 | + TimestampPojo pojo = new TimestampPojo(ldt, instant); |
| 223 | + GenericRow row = writer.toRow(pojo); |
| 224 | + |
| 225 | + // truncate to 123.456000 |
| 226 | + TimestampNtz expectedNtz = TimestampNtz.fromLocalDateTime(ldt.withNano(123456000)); |
| 227 | + TimestampLtz expectedLtz = |
| 228 | + TimestampLtz.fromInstant( |
| 229 | + Instant.ofEpochSecond(instant.getEpochSecond(), 123456000)); |
| 230 | + |
| 231 | + assertThat(row.getTimestampNtz(0, 6)).isEqualTo(expectedNtz); |
| 232 | + assertThat(row.getTimestampLtz(1, 6)).isEqualTo(expectedLtz); |
| 233 | + } |
| 234 | + |
| 235 | + @Test |
| 236 | + public void testTimestampPrecision9() { |
| 237 | + // Test with precision 9 nanoseconds |
| 238 | + RowType table = |
| 239 | + RowType.builder() |
| 240 | + .field("timestampNtzField", DataTypes.TIMESTAMP(9)) |
| 241 | + .field("timestampLtzField", DataTypes.TIMESTAMP_LTZ(9)) |
| 242 | + .build(); |
| 243 | + |
| 244 | + PojoToRowConverter<TimestampPojo> writer = |
| 245 | + PojoToRowConverter.of(TimestampPojo.class, table, table); |
| 246 | + |
| 247 | + LocalDateTime ldt = LocalDateTime.of(2025, 7, 23, 15, 1, 30, 123456789); |
| 248 | + Instant instant = Instant.parse("2025-07-23T15:01:30.123456789Z"); |
| 249 | + |
| 250 | + TimestampPojo pojo = new TimestampPojo(ldt, instant); |
| 251 | + GenericRow row = writer.toRow(pojo); |
| 252 | + |
| 253 | + TimestampNtz expectedNtz = TimestampNtz.fromLocalDateTime(ldt); |
| 254 | + TimestampLtz expectedLtz = TimestampLtz.fromInstant(instant); |
| 255 | + |
| 256 | + assertThat(row.getTimestampNtz(0, 9)).isEqualTo(expectedNtz); |
| 257 | + assertThat(row.getTimestampLtz(1, 9)).isEqualTo(expectedLtz); |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + public void testTimestampPrecisionRoundTrip() { |
| 262 | + testRoundTripWithPrecision(3); |
| 263 | + testRoundTripWithPrecision(6); |
| 264 | + testRoundTripWithPrecision(9); |
| 265 | + } |
| 266 | + |
| 267 | + private void testRoundTripWithPrecision(int precision) { |
| 268 | + RowType table = |
| 269 | + RowType.builder() |
| 270 | + .field("timestampNtzField", DataTypes.TIMESTAMP(precision)) |
| 271 | + .field("timestampLtzField", DataTypes.TIMESTAMP_LTZ(precision)) |
| 272 | + .build(); |
| 273 | + |
| 274 | + PojoToRowConverter<TimestampPojo> writer = |
| 275 | + PojoToRowConverter.of(TimestampPojo.class, table, table); |
| 276 | + RowToPojoConverter<TimestampPojo> reader = |
| 277 | + RowToPojoConverter.of(TimestampPojo.class, table, table); |
| 278 | + |
| 279 | + LocalDateTime originalLdt = LocalDateTime.of(2025, 7, 23, 15, 1, 30, 123456789); |
| 280 | + Instant originalInstant = Instant.parse("2025-07-23T15:01:30.123456789Z"); |
| 281 | + |
| 282 | + TimestampPojo originalPojo = new TimestampPojo(originalLdt, originalInstant); |
| 283 | + |
| 284 | + // Convert POJO -> Row -> POJO |
| 285 | + GenericRow row = writer.toRow(originalPojo); |
| 286 | + TimestampPojo resultPojo = reader.fromRow(row); |
| 287 | + |
| 288 | + LocalDateTime expectedLdt = truncateLocalDateTime(originalLdt, precision); |
| 289 | + Instant expectedInstant = truncateInstant(originalInstant, precision); |
| 290 | + |
| 291 | + assertThat(resultPojo.timestampNtzField) |
| 292 | + .as("Round-trip LocalDateTime with precision %d", precision) |
| 293 | + .isEqualTo(expectedLdt); |
| 294 | + assertThat(resultPojo.timestampLtzField) |
| 295 | + .as("Round-trip Instant with precision %d", precision) |
| 296 | + .isEqualTo(expectedInstant); |
| 297 | + } |
| 298 | + |
| 299 | + private LocalDateTime truncateLocalDateTime(LocalDateTime ldt, int precision) { |
| 300 | + if (precision >= 9) { |
| 301 | + return ldt; |
| 302 | + } |
| 303 | + int divisor = (int) Math.pow(10, 9 - precision); |
| 304 | + int truncatedNanos = (ldt.getNano() / divisor) * divisor; |
| 305 | + return ldt.withNano(truncatedNanos); |
| 306 | + } |
| 307 | + |
| 308 | + private Instant truncateInstant(Instant instant, int precision) { |
| 309 | + if (precision >= 9) { |
| 310 | + return instant; |
| 311 | + } |
| 312 | + int divisor = (int) Math.pow(10, 9 - precision); |
| 313 | + int truncatedNanos = (instant.getNano() / divisor) * divisor; |
| 314 | + return Instant.ofEpochSecond(instant.getEpochSecond(), truncatedNanos); |
| 315 | + } |
| 316 | + |
| 317 | + /** POJO for testing timestamp precision. */ |
| 318 | + public static class TimestampPojo { |
| 319 | + public LocalDateTime timestampNtzField; |
| 320 | + public Instant timestampLtzField; |
| 321 | + |
| 322 | + public TimestampPojo() {} |
| 323 | + |
| 324 | + public TimestampPojo(LocalDateTime timestampNtzField, Instant timestampLtzField) { |
| 325 | + this.timestampNtzField = timestampNtzField; |
| 326 | + this.timestampLtzField = timestampLtzField; |
| 327 | + } |
| 328 | + } |
171 | 329 | } |
0 commit comments