zabetak commented on code in PR #4356:
URL: https://github.com/apache/hive/pull/4356#discussion_r1214004385
##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/Filter.g:
##########
@@ -52,34 +53,36 @@ import java.util.regex.Pattern;
private static final Pattern datePattern =
Pattern.compile(".*(\\d\\d\\d\\d-\\d\\d-\\d\\d).*");
private static final Pattern timestampPattern =
Pattern.compile(".*(\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d).*");
- private static final ThreadLocal<SimpleDateFormat> dateFormat =
- new ThreadLocal<SimpleDateFormat>() {
- @Override
- protected SimpleDateFormat initialValue() {
- SimpleDateFormat val = new SimpleDateFormat("yyyy-MM-dd");
- val.setLenient(false); // Without this, 2020-20-20 becomes 2021-08-20.
Review Comment:
The old date parser was STRICT and the timestamp parser was
`ResolverStyle.SMART`(the default).
The new parsers are both `ResolverStyle.SMART`. In other parts of Hive there
is a move to slowly shift date/timestamp parsers to be STRICT so it may make
sense to keep them strict here as well.
If we want to keep the old behavior we can do that as well but in either
case let's add a few tests with invalid/out-of-range dates & timestamps.
```
2023-06-02 99:35:00
2023-06-32 10:35:00
2023-06-32
```
##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/Filter.g:
##########
@@ -52,34 +53,36 @@ import java.util.regex.Pattern;
private static final Pattern datePattern =
Pattern.compile(".*(\\d\\d\\d\\d-\\d\\d-\\d\\d).*");
private static final Pattern timestampPattern =
Pattern.compile(".*(\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d).*");
- private static final ThreadLocal<SimpleDateFormat> dateFormat =
- new ThreadLocal<SimpleDateFormat>() {
- @Override
- protected SimpleDateFormat initialValue() {
- SimpleDateFormat 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;
- };
- };
- private static final ThreadLocal<DateTimeFormatter> timestampFormat =
- new ThreadLocal<DateTimeFormatter>() {
- @Override
- protected DateTimeFormatter initialValue() {
- DateTimeFormatter val = DateTimeFormatter.ofPattern("yyyy-MM-dd
HH:mm:ss")
- .withZone(TimeZone.getTimeZone("UTC").toZoneId());
- return val;
- };
- };
- public static java.sql.Date extractDate(String input) {
+ private static final ThreadLocal<DateTimeFormatter> dateFormat =
createDateTimeFormatter("yyyy-MM-dd");
+ private static final ThreadLocal<DateTimeFormatter> timestampFormat =
createDateTimeFormatter("yyyy-MM-dd HH:mm:ss");
+
+ private static ThreadLocal<DateTimeFormatter> createDateTimeFormatter(String
format) {
+ return new ThreadLocal<DateTimeFormatter>() {
+ @Override
+ protected DateTimeFormatter initialValue() {
+ DateTimeFormatter val = DateTimeFormatter.ofPattern(format)
+ .withZone(TimeZone.getTimeZone("UTC").toZoneId());
Review Comment:
Since we are using `LocalDate` and `LocalDateTime` I don't know how much
sense it makes to use a zone here. However, it was like that before so we can
keep it as is.
##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/Filter.g:
##########
@@ -549,7 +549,6 @@ DateLiteral
TimestampLiteral
:
KW_TIMESTAMP '\'' TimestampString '\'' { extractTimestamp(getText()) !=
null }?
- | TimestampString { extractTimestamp(getText()) != null }?
Review Comment:
I now understand better the problem with the conflict; I forgot that rule
order does not matter.
##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/parser/Filter.g:
##########
@@ -37,6 +37,7 @@ package org.apache.hadoop.hive.metastore.parser;
import java.sql.Date;
import java.sql.Timestamp;
+import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.text.ParseException;
Review Comment:
Unused imports?
##########
standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/TestPartFilterExprUtil.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.hadoop.hive.metastore;
+
+import org.apache.hadoop.hive.metastore.annotation.MetastoreUnitTest;
+import org.apache.hadoop.hive.metastore.api.MetaException;
+import org.apache.hadoop.hive.metastore.parser.ExpressionTree;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+@Category(MetastoreUnitTest.class)
+public class TestPartFilterExprUtil {
Review Comment:
Actually I didn't have in mind generating the expressions but having
parameters as {expression, expectedTree} pairs.
```
new Object[] {"(j) in (1990-11-10)", "LeafNode{keyName='j', operator='=',
value=1990-11-10}"},
new Object[] {"(j) IN (DATE'1990-11-10')", "LeafNode{keyName='j',
operator='=', value=1990-11-10}"},
...
```
Parameters could also be loaded from a CSV file which could further cut down
the need for double quotes etc
(https://junit.org/junit5/docs/snapshot/user-guide/#writing-tests-parameterized-tests-sources-CsvFileSource):
```
(j) in (1990-11-10); LeafNode{keyName='j', operator='=', value=1990-11-10}
(j) IN (DATE'1990-11-10'); LeafNode{keyName='j', operator='=',
value=1990-11-10}
```
Anyways the test is completely fine as it is and I like that its name
denotes exactly what it does. I just wanted to share what I had in mind.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]