diff --git common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java index 4708d35a78..6629edfa65 100644 --- common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java +++ common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java @@ -124,12 +124,14 @@ public static TimestampTZ parseOrNull(String s, ZoneId defaultTimeZone) { // Converts Date to TimestampTZ. public static TimestampTZ convert(Date date, ZoneId defaultTimeZone) { - return parse(date.toString(), defaultTimeZone); + ZonedDateTime dateUtc = Instant.ofEpochMilli(date.toEpochMilli()).atZone(ZoneOffset.UTC); + return new TimestampTZ(dateUtc.withZoneSameLocal(defaultTimeZone)); } // Converts Timestamp to TimestampTZ. public static TimestampTZ convert(Timestamp ts, ZoneId defaultTimeZone) { - return parse(ts.toString(), defaultTimeZone); + ZonedDateTime tsUtc = Instant.ofEpochSecond(ts.toEpochSecond(), ts.getNanos()).atZone(ZoneOffset.UTC); + return new TimestampTZ(tsUtc.withZoneSameLocal(defaultTimeZone)); } public static ZoneId parseTimeZone(String timeZoneStr) { diff --git common/src/test/org/apache/hadoop/hive/common/type/TestTimestampTZ.java common/src/test/org/apache/hadoop/hive/common/type/TestTimestampTZ.java index cd23abebfa..6cea6722c3 100644 --- common/src/test/org/apache/hadoop/hive/common/type/TestTimestampTZ.java +++ common/src/test/org/apache/hadoop/hive/common/type/TestTimestampTZ.java @@ -115,6 +115,25 @@ public void testConvertFromTimestamp() { Timestamp.valueOf(s), TimeZone.getTimeZone("America/Los_Angeles").toZoneId()); Assert.assertTrue(tstz1.compareTo(tstz2) < 0); + Assert.assertEquals(tstz1.getZonedDateTime().toLocalDateTime(), tstz2.getZonedDateTime().toLocalDateTime()); + } finally { + TimeZone.setDefault(defaultZone); + } + } + + @Test + public void testConvertFromDate() { + TimeZone defaultZone = TimeZone.getDefault(); + try { + String s = "2017-06-12"; + TimestampTZ tstz1 = TimestampTZUtil.convert( + Date.valueOf(s), + TimeZone.getTimeZone("Europe/London").toZoneId()); + TimestampTZ tstz2 = TimestampTZUtil.convert( + Date.valueOf(s), + TimeZone.getTimeZone("America/Los_Angeles").toZoneId()); + Assert.assertTrue(tstz1.compareTo(tstz2) < 0); + Assert.assertEquals(tstz1.getZonedDateTime().toLocalDate(), tstz2.getZonedDateTime().toLocalDate()); } finally { TimeZone.setDefault(defaultZone); } diff --git itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/type/TimestampTZUtilBench.java itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/type/TimestampTZUtilBench.java new file mode 100644 index 0000000000..ec7ce791f8 --- /dev/null +++ itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/type/TimestampTZUtilBench.java @@ -0,0 +1,90 @@ +/* + * 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.hive.benchmark.common.type; + +import org.apache.hadoop.hive.common.type.Date; +import org.apache.hadoop.hive.common.type.Timestamp; +import org.apache.hadoop.hive.common.type.TimestampTZ; +import org.apache.hadoop.hive.common.type.TimestampTZUtil; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.Warmup; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.time.ZoneOffset; +import java.util.concurrent.TimeUnit; + +/** + * This benchmark measures the performance of conversion values of type {@link Timestamp} and {@link Date} + * to {@link TimestampTZ} type. + *

+ * + * Build with "{@code mvn clean install -DskipTests -Pdist,itests}" at main hive directory. + * Then from itests/hive-jmh directory, run: + *

+ * java -jar target/benchmarks.jar org.apache.hive.benchmark.common.type.TimestampTZUtilBench
+ * 
+ */ +@BenchmarkMode(Mode.AverageTime) +@Fork(1) +@State(Scope.Thread) +@OutputTimeUnit(TimeUnit.NANOSECONDS) +public class TimestampTZUtilBench { + + private Timestamp timestamp; + private Date date; + + @Setup + public void prepare() { + timestamp = new Timestamp(); + timestamp.setTimeInMillis(System.currentTimeMillis()); + date = new Date(); + date.setTimeInMillis(System.currentTimeMillis()); + } + + @Benchmark + @Warmup(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS) + @Measurement(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS) + public TimestampTZ convertDate() { + return TimestampTZUtil.convert(date, ZoneOffset.UTC); + } + + @Benchmark + @Warmup(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS) + @Measurement(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS) + public TimestampTZ convertTimestamp() { + return TimestampTZUtil.convert(timestamp, ZoneOffset.UTC); + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder().include( + ".*" + TimestampTZUtilBench.class.getSimpleName() + ".*").build(); + new Runner(opt).run(); + } +} diff --git itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/type/package-info.java itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/type/package-info.java new file mode 100644 index 0000000000..e1e900ab98 --- /dev/null +++ itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/type/package-info.java @@ -0,0 +1,22 @@ +/* + * 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 for the common type benchmarks. + */ +package org.apache.hive.benchmark.common.type; \ No newline at end of file