pvary commented on a change in pull request #2254: URL: https://github.com/apache/iceberg/pull/2254#discussion_r582881345
########## File path: mr/src/test/java/org/apache/iceberg/mr/hive/TestHiveIcebergStorageHandlerTimezone.java ########## @@ -0,0 +1,165 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iceberg.mr.hive; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Collection; +import java.util.List; +import java.util.TimeZone; +import org.apache.hadoop.hive.serde2.io.DateWritable; +import org.apache.hadoop.hive.serde2.io.TimestampWritable; +import org.apache.iceberg.FileFormat; +import org.apache.iceberg.Schema; +import org.apache.iceberg.data.Record; +import org.apache.iceberg.mr.TestHelper; +import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; +import org.apache.iceberg.types.Types; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.apache.iceberg.types.Types.NestedField.optional; +import static org.junit.runners.Parameterized.Parameter; +import static org.junit.runners.Parameterized.Parameters; + +@RunWith(Parameterized.class) +public class TestHiveIcebergStorageHandlerTimezone { + + @Parameters(name = "timezone={0}") + public static Collection<Object[]> parameters() { + Collection<Object[]> testParams = ImmutableList.of( + new String[] {"America/New_York"}, + new String[] {"Asia/Kolkata"}, + new String[] {"UTC/Greenwich"} + ); + + return testParams; + } + + private TestHiveShell shell; + + private TestTables testTables; + + private TimeZone originalTz = TimeZone.getDefault(); + + @Parameter(0) + public String timezoneString; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Before + public void before() throws IOException, IllegalAccessException { + TimeZone.setDefault(TimeZone.getTimeZone(timezoneString)); + + try { + // Magic to update cached stuff for Hive where the default timezone is used + Field formatterField = TimestampWritable.class.getDeclaredField("threadLocalDateFormat"); + formatterField.setAccessible(true); + ThreadLocal<DateFormat> formatter = (ThreadLocal<DateFormat>) formatterField.get(null); + formatter.set(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); + + Field localTimeZoneField = DateWritable.class.getDeclaredField("LOCAL_TIMEZONE"); + localTimeZoneField.setAccessible(true); + ThreadLocal<TimeZone> localTimeZone = (ThreadLocal<TimeZone>) localTimeZoneField.get(null); + localTimeZone.set(TimeZone.getDefault()); + } catch (NoSuchFieldException nse) { + // Hive2 has thread local cache we have to clean, so the new default timezone is used when formatting + // Hive3 we do not have this for Date and Timestamp + } + + this.shell = HiveIcebergStorageHandlerTestUtils.shell(); + this.testTables = HiveIcebergStorageHandlerTestUtils.testTables(shell, TestTables.TestTableType.HIVE_CATALOG, temp); + + // Uses spark as an engine so we can detect if we unintentionally try to use any execution engines + HiveIcebergStorageHandlerTestUtils.init(shell, testTables, temp, "spark"); + } + + @After + public void after() { + shell.stop(); + TimeZone.setDefault(originalTz); Review comment: Done ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org