zabetak commented on code in PR #4760: URL: https://github.com/apache/hive/pull/4760#discussion_r1341952353
########## standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/MetaStoreUtils.java: ########## @@ -74,25 +77,40 @@ import com.google.common.base.Joiner; public class MetaStoreUtils { - /** A fixed date format to be used for hive partition column values. */ - public static final ThreadLocal<DateFormat> PARTITION_DATE_FORMAT = - new ThreadLocal<DateFormat>() { - @Override - protected DateFormat initialValue() { - DateFormat val = new SimpleDateFormat("yyyy-MM-dd"); - val.setLenient(false); // Without this, 2020-20-20 becomes 2021-08-20. - val.setTimeZone(TimeZone.getTimeZone("UTC")); - return val; - } - }; - public static final ThreadLocal<DateTimeFormatter> PARTITION_TIMESTAMP_FORMAT = - new ThreadLocal<DateTimeFormatter>() { - @Override - protected DateTimeFormatter initialValue() { - return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"). - withZone(TimeZone.getTimeZone("UTC").toZoneId()); - } - }; + + private static final DateTimeFormatter DATE_FORMATTER = createDateTimeFormatter("uuuu-MM-dd"); + + private static final DateTimeFormatter TIMESTAMP_FORMATTER = createDateTimeFormatter("uuuu-MM-dd HH:mm:ss"); + + private static DateTimeFormatter createDateTimeFormatter(String format) { + return DateTimeFormatter.ofPattern(format) + .withZone(TimeZone.getTimeZone("UTC").toZoneId()) + .withResolverStyle(ResolverStyle.STRICT); + } + + /** + * Converts java.sql.Date to String format. + * @param date - java.sql.Date object. + * @return Date in string format. + */ + public static String convertDateToString(Date date) { + return DATE_FORMATTER.format(date.toLocalDate()); + } + + public static Date convertStringToDate(String date) { + LocalDate val = LocalDate.parse(date, DATE_FORMATTER); + return java.sql.Date.valueOf(val); + } + + public static String convertTimestampToString(Timestamp timestamp) { + return TIMESTAMP_FORMATTER.format(timestamp.toLocalDateTime()); + } + + public static Timestamp convertStringToTimestamp(String timestamp) { + LocalDateTime val = LocalDateTime.from(TIMESTAMP_FORMATTER.parse(timestamp)); + return Timestamp.valueOf(val); + } + Review Comment: To avoid any kind of unpleasant surprises we should use the same methods and formatters in `PartFilterVisitor`. ########## ql/src/test/results/clientpositive/llap/date_timestamp_partition_filter.q.out: ########## @@ -0,0 +1,214 @@ +PREHOOK: query: CREATE EXTERNAL TABLE testpd(col1 string, col2 String) PARTITIONED BY(PartitionDate DATE) STORED AS ORC Review Comment: Is there a part in this output that shows that partition pruning is working and there are no hidden exceptions that just prevent it from running? ########## standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/ExpressionTree.java: ########## @@ -18,7 +18,7 @@ package org.apache.hadoop.hive.metastore.parser; import java.sql.Timestamp; -import java.util.Date; Review Comment: That's a sneaky change since `java.util.Date` is a super class of `java.sql.Date` so the behavior of `instanceof` and other operators may be affected. Do we have test coverage for this class? Are we creating anywhere else instances of `java.util.Date` that may end up here? ########## ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java: ########## @@ -1804,9 +1803,9 @@ private static String normalizeDateCol(Object colValue) throws SemanticException throw new SemanticException("Unexpected date type " + colValue.getClass()); } try { - return MetaStoreUtils.PARTITION_DATE_FORMAT.get().format( - MetaStoreUtils.PARTITION_DATE_FORMAT.get().parse(value.toString())); - } catch (ParseException e) { + return MetaStoreUtils.convertDateToString( + MetaStoreUtils.convertStringToDate(value.toString())); + } catch (Exception e) { throw new SemanticException(e); } } Review Comment: Interestingly this seems to be dead code. We probably don't need to do something as part of this PR but I was wondering why it was left here. We can investigate later on after we are done with this. ########## standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java: ########## @@ -0,0 +1,78 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.hive.metastore.utils; + +import org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.sql.Date; +import java.sql.Timestamp; +import java.time.ZoneId; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +@Category(MetastoreUnitTest.class) +public class TestMetaStoreUtils { + + @Test + public void testConvertDateToString() { + TimeZone defaultTZ = TimeZone.getDefault(); + try { + TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.of("Asia/Hong_Kong"))); + String date = MetaStoreUtils.convertDateToString(Date.valueOf("2023-01-01")); + assertEquals("2023-01-01", date); + } finally { + TimeZone.setDefault(defaultTZ); + } + } + + @Test + public void testcConvertTimestampToString() { + TimeZone defaultTZ = TimeZone.getDefault(); + try { + String date = MetaStoreUtils.convertTimestampToString(Timestamp.valueOf("2023-01-01 10:20:30")); + assertEquals("2023-01-01 10:20:30", date); + } finally { + TimeZone.setDefault(defaultTZ); + } Review Comment: Why do we need this? Neither the test nor the called method modify the default timezone. ########## ql/src/test/queries/clientpositive/date_timestamp_partition_filter.q: ########## @@ -0,0 +1,14 @@ +--! qt:timezone:Asia/Hong_Kong + +CREATE EXTERNAL TABLE testpd(col1 string, col2 String) PARTITIONED BY(PartitionDate DATE) STORED AS ORC; +INSERT into testpd(PartitionDate, col1, col2) VALUES('2023-01-01','Value11','Value12'); +INSERT into testpd(PartitionDate, col1, col2) VALUES('2023-01-02','Value21','Value22'); +explain extended select * from testpd where PartitionDate = '2023-01-01'; +select * from testpd where PartitionDate = '2023-01-01'; + + +CREATE EXTERNAL TABLE testpt(col1 string, col2 String) PARTITIONED BY(PartitionTimestamp TIMESTAMP) STORED AS ORC; +INSERT into testpt(PartitionTimestamp, col1, col2) VALUES('2023-01-01 10:20:30','Value11','Value12'); +INSERT into testpt(PartitionTimestamp, col1, col2) VALUES('2023-01-02 20:30:40','Value21','Value22'); +explain extended select * from testpt where PartitionTimestamp = '2023-01-01 10:20:30'; +select * from testpt where PartitionTimestamp = '2023-01-01 10:20:30'; Review Comment: nit: Missing new line at the EOF. ########## standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java: ########## @@ -0,0 +1,78 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.hive.metastore.utils; + +import org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.sql.Date; +import java.sql.Timestamp; +import java.time.ZoneId; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +@Category(MetastoreUnitTest.class) +public class TestMetaStoreUtils { Review Comment: Would some of these tests fail without the new changes introduced in this PR? ########## standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/utils/TestMetaStoreUtils.java: ########## @@ -0,0 +1,78 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.hive.metastore.utils; + +import org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.sql.Date; +import java.sql.Timestamp; +import java.time.ZoneId; +import java.util.TimeZone; + +import static org.junit.Assert.assertEquals; + +@Category(MetastoreUnitTest.class) +public class TestMetaStoreUtils { Review Comment: Can we also add tests for: * String -> Date -> String * String -> Timestamp -> String -- 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. To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org For additional commands, e-mail: gitbox-h...@hive.apache.org