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 itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/TimestampTZUtilBench.java itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/TimestampTZUtilBench.java new file mode 100644 index 0000000000..28913a3403 --- /dev/null +++ itests/hive-jmh/src/main/java/org/apache/hive/benchmark/common/TimestampTZUtilBench.java @@ -0,0 +1,74 @@ +/* +* Licensed 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; + +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; + +@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(); + } +}