|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.hudi; |
| 19 | + |
| 20 | +import org.apache.doris.common.jni.vec.ColumnType; |
| 21 | + |
| 22 | +import org.apache.hadoop.hive.common.type.Timestamp; |
| 23 | +import org.apache.hadoop.hive.serde2.io.TimestampWritableV2; |
| 24 | +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; |
| 25 | +import org.apache.hadoop.io.LongWritable; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.time.LocalDateTime; |
| 30 | +import java.time.ZoneId; |
| 31 | + |
| 32 | +public class HadoopHudiColumnValueTest { |
| 33 | + @Test |
| 34 | + public void testInstantCarriersAcrossTimezonesAndPrecisions() { |
| 35 | + java.util.TimeZone original = java.util.TimeZone.getDefault(); |
| 36 | + try { |
| 37 | + for (String zone : new String[] {"UTC", "Asia/Shanghai", "America/New_York"}) { |
| 38 | + java.util.TimeZone.setDefault(java.util.TimeZone.getTimeZone(zone)); |
| 39 | + for (int precision : new int[] {3, 6}) { |
| 40 | + for (long epoch : new long[] {-1, 0, 1636263000123L, 1636266600123L}) { |
| 41 | + if (precision == 6 && epoch > 0) { |
| 42 | + epoch = epoch * 1000 + 456; |
| 43 | + } |
| 44 | + long units = precision == 3 ? 1000L : 1_000_000L; |
| 45 | + java.time.Instant instant = java.time.Instant.ofEpochSecond(Math.floorDiv(epoch, units), |
| 46 | + Math.floorMod(epoch, units) * (1_000_000_000L / units)); |
| 47 | + LocalDateTime expected = LocalDateTime.ofInstant(instant, java.time.ZoneOffset.UTC); |
| 48 | + HadoopHudiColumnValue value = new HadoopHudiColumnValue(ZoneId.of(zone)); |
| 49 | + value.setField(ColumnType.parseType("ts", "timestamptz(" + precision + ")"), |
| 50 | + PrimitiveObjectInspectorFactory.writableTimestampObjectInspector); |
| 51 | + value.setRow(new LongWritable(epoch)); |
| 52 | + Assert.assertEquals(expected, value.getTimeStampTz()); |
| 53 | + value.setRow(java.sql.Timestamp.from(instant)); |
| 54 | + Assert.assertEquals(expected, value.getTimeStampTz()); |
| 55 | + value.setRow(new TimestampWritableV2(Timestamp.ofEpochSecond( |
| 56 | + instant.getEpochSecond(), instant.getNano()))); |
| 57 | + Assert.assertEquals(expected, value.getTimeStampTz()); |
| 58 | + value.setRow(null); |
| 59 | + Assert.assertTrue(value.isNull()); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } finally { |
| 64 | + java.util.TimeZone.setDefault(original); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testInstantTimestampUsesUtcComponents() { |
| 70 | + HadoopHudiColumnValue value = new HadoopHudiColumnValue(ZoneId.of("Asia/Shanghai")); |
| 71 | + value.setField(ColumnType.parseType("ts", "timestamptz(6)"), null); |
| 72 | + value.setRow(new LongWritable(-1)); |
| 73 | + Assert.assertEquals(LocalDateTime.of(1969, 12, 31, 23, 59, 59, 999999000), value.getTimeStampTz()); |
| 74 | + value.setField(ColumnType.parseType("ts", "timestamptz(6)"), |
| 75 | + PrimitiveObjectInspectorFactory.writableTimestampObjectInspector); |
| 76 | + value.setRow(new TimestampWritableV2(Timestamp.ofEpochSecond(1, 111333000))); |
| 77 | + Assert.assertEquals(LocalDateTime.of(1970, 1, 1, 0, 0, 1, 111333000), value.getTimeStampTz()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testInt64LocalTimestampIgnoresSessionTimezone() { |
| 82 | + HadoopHudiColumnValue value = new HadoopHudiColumnValue(ZoneId.of("America/Los_Angeles")); |
| 83 | + value.setField(ColumnType.parseType("ts", "datetimev2(6)"), null); |
| 84 | + value.setRow(new LongWritable(0)); |
| 85 | + |
| 86 | + Assert.assertEquals(LocalDateTime.of(1970, 1, 1, 0, 0), value.getDateTime()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testInt96LocalTimestampIgnoresSessionTimezone() { |
| 91 | + HadoopHudiColumnValue value = new HadoopHudiColumnValue(ZoneId.of("America/Los_Angeles")); |
| 92 | + value.setField(ColumnType.parseType("ts", "datetimev2(6)"), |
| 93 | + PrimitiveObjectInspectorFactory.writableTimestampObjectInspector); |
| 94 | + value.setRow(new TimestampWritableV2(Timestamp.ofEpochSecond(0))); |
| 95 | + |
| 96 | + Assert.assertEquals(LocalDateTime.of(1970, 1, 1, 0, 0), value.getDateTime()); |
| 97 | + } |
| 98 | +} |
0 commit comments