This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 09ea447a893 Support parsing more PostgreSQL time format (#23794)
09ea447a893 is described below

commit 09ea447a8930536d1084d79ee01cff32c293c894
Author: 吴伟杰 <[email protected]>
AuthorDate: Mon Jan 30 11:08:07 2023 +0800

    Support parsing more PostgreSQL time format (#23794)
---
 .../extended/bind/OpenGaussComBatchBindPacket.java |  4 +-
 .../extended/bind/PostgreSQLComBindPacket.java     |  4 +-
 .../bind/protocol/PostgreSQLTextTimeUtils.java     | 51 +++++++++++++++
 .../bind/protocol/PostgreSQLTextTimeUtilsTest.java | 72 ++++++++++++++++++++++
 4 files changed, 127 insertions(+), 4 deletions(-)

diff --git 
a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacket.java
 
b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacket.java
index 5d71be310b9..9d7ebd20b41 100644
--- 
a/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacket.java
+++ 
b/db-protocol/opengauss/src/main/java/org/apache/shardingsphere/db/protocol/opengauss/packet/command/query/extended/bind/OpenGaussComBatchBindPacket.java
@@ -26,12 +26,12 @@ import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLValue
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLBinaryProtocolValue;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLBinaryProtocolValueFactory;
+import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLTextTimeUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLTextTimestampUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
 
 import java.math.BigDecimal;
 import java.sql.Date;
-import java.sql.Time;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -139,7 +139,7 @@ public final class OpenGaussComBatchBindPacket extends 
OpenGaussCommandPacket {
             case POSTGRESQL_TYPE_DATE:
                 return Date.valueOf(textValue);
             case POSTGRESQL_TYPE_TIME:
-                return Time.valueOf(textValue);
+                return PostgreSQLTextTimeUtils.parse(textValue);
             case POSTGRESQL_TYPE_TIMESTAMP:
             case POSTGRESQL_TYPE_TIMESTAMPTZ:
                 return PostgreSQLTextTimestampUtils.parse(textValue);
diff --git 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
index 1b8f0167997..2335f3bc635 100644
--- 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
+++ 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/PostgreSQLComBindPacket.java
@@ -25,13 +25,13 @@ import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.PostgreSQ
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.PostgreSQLColumnType;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLBinaryProtocolValue;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLBinaryProtocolValueFactory;
+import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLTextTimeUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol.PostgreSQLTextTimestampUtils;
 import 
org.apache.shardingsphere.db.protocol.postgresql.packet.identifier.PostgreSQLIdentifierTag;
 import 
org.apache.shardingsphere.db.protocol.postgresql.payload.PostgreSQLPacketPayload;
 
 import java.math.BigDecimal;
 import java.sql.Date;
-import java.sql.Time;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
@@ -127,7 +127,7 @@ public final class PostgreSQLComBindPacket extends 
PostgreSQLCommandPacket {
             case POSTGRESQL_TYPE_DATE:
                 return Date.valueOf(textValue);
             case POSTGRESQL_TYPE_TIME:
-                return Time.valueOf(textValue);
+                return PostgreSQLTextTimeUtils.parse(textValue);
             case POSTGRESQL_TYPE_TIMESTAMP:
             case POSTGRESQL_TYPE_TIMESTAMPTZ:
                 return PostgreSQLTextTimestampUtils.parse(textValue);
diff --git 
a/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimeUtils.java
 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimeUtils.java
new file mode 100644
index 00000000000..ed19f2ad901
--- /dev/null
+++ 
b/db-protocol/postgresql/src/main/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimeUtils.java
@@ -0,0 +1,51 @@
+/*
+ * 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.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol;
+
+import 
org.apache.shardingsphere.infra.util.exception.external.sql.type.generic.UnsupportedSQLOperationException;
+
+import java.time.LocalTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+
+/**
+ * Utils for PostgreSQL time.
+ */
+public final class PostgreSQLTextTimeUtils {
+    
+    private static final DateTimeFormatter POSTGRESQL_TIME_FORMATTER = 
DateTimeFormatter.ofPattern(
+            "[HH:mm:ss][HHmmss][HH:mm][HHmm]"
+                    + 
"[.SSSSSSSSS][.SSSSSSSS][.SSSSSSS][.SSSSSS][.SSSSS][.SSSS][.SSS][.SS][.S]"
+                    + "[ ]"
+                    + "[XXXXX][XXXX][XXX][XX][X]");
+    
+    /**
+     * Parse time in PostgreSQL text format.<br>
+     * NOTICE: PostgreSQL allows 6 fractional digits retained in the seconds 
field, which is unsupported by {@link java.sql.Time}.
+     *
+     * @param value text value to be parsed
+     * @return time
+     */
+    public static LocalTime parse(final String value) {
+        try {
+            return POSTGRESQL_TIME_FORMATTER.parse(value, LocalTime::from);
+        } catch (final DateTimeParseException ignored) {
+            throw new UnsupportedSQLOperationException("Unsupported time 
format: [" + value + "]");
+        }
+    }
+}
diff --git 
a/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimeUtilsTest.java
 
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimeUtilsTest.java
new file mode 100644
index 00000000000..b47a3ec8413
--- /dev/null
+++ 
b/db-protocol/postgresql/src/test/java/org/apache/shardingsphere/db/protocol/postgresql/packet/command/query/extended/bind/protocol/PostgreSQLTextTimeUtilsTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.shardingsphere.db.protocol.postgresql.packet.command.query.extended.bind.protocol;
+
+import lombok.RequiredArgsConstructor;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import java.time.LocalTime;
+import java.util.Arrays;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+@RunWith(Parameterized.class)
+@RequiredArgsConstructor
+public final class PostgreSQLTextTimeUtilsTest {
+    
+    private final String input;
+    
+    private final LocalTime expected;
+    
+    @Parameters(name = "{0}")
+    public static Iterable<Object[]> textValues() {
+        return Arrays.asList(
+                new Object[]{"2323", LocalTime.of(23, 23, 0)},
+                new Object[]{"23:23", LocalTime.of(23, 23, 0)},
+                new Object[]{"232323", LocalTime.of(23, 23, 23)},
+                new Object[]{"23:23:23", LocalTime.of(23, 23, 23)},
+                new Object[]{"23:23:23.1", LocalTime.of(23, 23, 23, 
100_000_000)},
+                new Object[]{"23:23:23.12", LocalTime.of(23, 23, 23, 
120_000_000)},
+                new Object[]{"23:23:23.123", LocalTime.of(23, 23, 23, 
123_000_000)},
+                new Object[]{"23:23:23.123+08", LocalTime.of(23, 23, 23, 
123_000_000)},
+                new Object[]{"23:23:23.123+08", LocalTime.of(23, 23, 23, 
123_000_000)},
+                new Object[]{"23:23:23.12345", LocalTime.of(23, 23, 23, 
123_450_000)},
+                new Object[]{"23:23:23.12345+0800", LocalTime.of(23, 23, 23, 
123_450_000)},
+                new Object[]{"23:23:23.12345+0800", LocalTime.of(23, 23, 23, 
123_450_000)},
+                new Object[]{"23:23:23.12345+08:00:00", LocalTime.of(23, 23, 
23, 123_450_000)},
+                new Object[]{"23:23:23.12345+08:00:00", LocalTime.of(23, 23, 
23, 123_450_000)},
+                new Object[]{"23:23:23.12345+08:00:00", LocalTime.of(23, 23, 
23, 123_450_000)},
+                new Object[]{"23:23:23.123456", LocalTime.of(23, 23, 23, 
123_456_000)},
+                new Object[]{"23:23:23.1234567", LocalTime.of(23, 23, 23, 
123_456_700)},
+                new Object[]{"23:23:23.12345678", LocalTime.of(23, 23, 23, 
123_456_780)},
+                new Object[]{"23:23:23.123456789", LocalTime.of(23, 23, 23, 
123_456_789)},
+                new Object[]{"23:23:23.123456 +08:00", LocalTime.of(23, 23, 
23, 123_456_000)},
+                new Object[]{"23:23:23.123456 -08:00", LocalTime.of(23, 23, 
23, 123_456_000)},
+                new Object[]{"23:23:23.123456789 +08:00", LocalTime.of(23, 23, 
23, 123_456_789)},
+                new Object[]{"23:23:23.123456", LocalTime.of(23, 23, 23, 
123_456_000)});
+    }
+    
+    @Test
+    public void assertParse() {
+        assertThat(PostgreSQLTextTimeUtils.parse(input), is(expected));
+    }
+}

Reply via email to