This is an automated email from the ASF dual-hosted git repository.
zhonghongsheng 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 c7b567b4f41 Revise #26930 (#26940)
c7b567b4f41 is described below
commit c7b567b4f4110be16f4bf3f382439caa2634c0ef
Author: Xinze Guo <[email protected]>
AuthorDate: Thu Jul 13 16:06:41 2023 +0800
Revise #26930 (#26940)
* Revise #26930, call resultSet.wasNull() after read value
* Add ColumnValueReaderEngineTest
* Fix Optional with null value
---
.../core/dumper/ColumnValueReaderEngine.java | 8 ++-
.../mysql/ingest/MySQLColumnValueReader.java | 50 +++++++++++++++++
...line.spi.ingest.dumper.DialectColumnValueReader | 18 +++++++
.../ingest/wal/OpenGaussColumnValueReader.java | 4 +-
.../ingest/PostgreSQLColumnValueReader.java | 2 +-
.../core/dump/ColumnValueReaderEngineTest.java | 63 ++++++++++++++++++++++
6 files changed, 137 insertions(+), 8 deletions(-)
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/dumper/ColumnValueReaderEngine.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/dumper/ColumnValueReaderEngine.java
index af08c516079..6ac258dbbd6 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/dumper/ColumnValueReaderEngine.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/dumper/ColumnValueReaderEngine.java
@@ -40,7 +40,7 @@ public final class ColumnValueReaderEngine {
/**
* Read column value.
- *
+ *
* @param resultSet result set
* @param metaData result set meta data
* @param columnIndex column index
@@ -48,11 +48,9 @@ public final class ColumnValueReaderEngine {
* @throws SQLException SQL exception
*/
public Object read(final ResultSet resultSet, final ResultSetMetaData
metaData, final int columnIndex) throws SQLException {
- if (resultSet.wasNull()) {
- return null;
- }
Optional<Object> dialectValue = readDialectValue(resultSet, metaData,
columnIndex);
- return dialectValue.isPresent() ? dialectValue :
readStandardValue(resultSet, metaData, columnIndex);
+ Object result = dialectValue.isPresent() ? dialectValue.get() :
readStandardValue(resultSet, metaData, columnIndex);
+ return resultSet.wasNull() ? null : result;
}
private Optional<Object> readDialectValue(final ResultSet resultSet, final
ResultSetMetaData metaData, final int columnIndex) throws SQLException {
diff --git
a/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLColumnValueReader.java
b/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLColumnValueReader.java
new file mode 100644
index 00000000000..39ba4fef81d
--- /dev/null
+++
b/kernel/data-pipeline/dialect/mysql/src/main/java/org/apache/shardingsphere/data/pipeline/mysql/ingest/MySQLColumnValueReader.java
@@ -0,0 +1,50 @@
+/*
+ * 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.data.pipeline.mysql.ingest;
+
+import
org.apache.shardingsphere.data.pipeline.spi.ingest.dumper.DialectColumnValueReader;
+
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.util.Optional;
+
+/**
+ * Column value reader for MySQL.
+ */
+public final class MySQLColumnValueReader implements DialectColumnValueReader {
+
+ private static final String YEAR_DATA_TYPE = "YEAR";
+
+ @Override
+ public Optional<Object> read(final ResultSet resultSet, final
ResultSetMetaData metaData, final int columnIndex) throws SQLException {
+ if (isYearDataType(metaData.getColumnTypeName(columnIndex))) {
+ return Optional.of(resultSet.getShort(columnIndex));
+ }
+ return Optional.empty();
+ }
+
+ private boolean isYearDataType(final String columnDataTypeName) {
+ return YEAR_DATA_TYPE.equalsIgnoreCase(columnDataTypeName);
+ }
+
+ @Override
+ public String getDatabaseType() {
+ return "MySQL";
+ }
+}
diff --git
a/kernel/data-pipeline/dialect/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.spi.ingest.dumper.DialectColumnValueReader
b/kernel/data-pipeline/dialect/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.spi.ingest.dumper.DialectColumnValueReader
new file mode 100644
index 00000000000..3c29e44c5a1
--- /dev/null
+++
b/kernel/data-pipeline/dialect/mysql/src/main/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.spi.ingest.dumper.DialectColumnValueReader
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.data.pipeline.mysql.ingest.MySQLColumnValueReader
diff --git
a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/wal/OpenGaussColumnValueReader.java
b/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/wal/OpenGaussColumnValueReader.java
index a3952b7cfd6..ab245d58620 100644
---
a/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/wal/OpenGaussColumnValueReader.java
+++
b/kernel/data-pipeline/dialect/opengauss/src/main/java/org/apache/shardingsphere/data/pipeline/opengauss/ingest/wal/OpenGaussColumnValueReader.java
@@ -39,11 +39,11 @@ public final class OpenGaussColumnValueReader implements
DialectColumnValueReade
@Override
public Optional<Object> read(final ResultSet resultSet, final
ResultSetMetaData metaData, final int columnIndex) throws SQLException {
if (isMoneyType(metaData, columnIndex)) {
- return Optional.of(resultSet.getBigDecimal(columnIndex));
+ return Optional.ofNullable(resultSet.getBigDecimal(columnIndex));
}
if (isBitType(metaData, columnIndex)) {
// openGauss JDBC driver can't parse bit(n) correctly when n > 1,
so JDBC url already add bitToString, there just return string
- return Optional.of(resultSet.getString(columnIndex));
+ return Optional.ofNullable(resultSet.getString(columnIndex));
}
if (isBoolType(metaData, columnIndex)) {
return Optional.of(resultSet.getBoolean(columnIndex));
diff --git
a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLColumnValueReader.java
b/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLColumnValueReader.java
index 183315d87dd..9388c6d7f0a 100644
---
a/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLColumnValueReader.java
+++
b/kernel/data-pipeline/dialect/postgresql/src/main/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLColumnValueReader.java
@@ -38,7 +38,7 @@ public final class PostgreSQLColumnValueReader implements
DialectColumnValueRead
@Override
public Optional<Object> read(final ResultSet resultSet, final
ResultSetMetaData metaData, final int columnIndex) throws SQLException {
if (isMoneyType(metaData, columnIndex)) {
- return Optional.of(resultSet.getBigDecimal(columnIndex));
+ return Optional.ofNullable(resultSet.getBigDecimal(columnIndex));
}
if (isBitType(metaData, columnIndex)) {
return Optional.of(getBitObject(resultSet, columnIndex));
diff --git
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/dump/ColumnValueReaderEngineTest.java
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/dump/ColumnValueReaderEngineTest.java
new file mode 100644
index 00000000000..edeae7568b7
--- /dev/null
+++
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/dump/ColumnValueReaderEngineTest.java
@@ -0,0 +1,63 @@
+/*
+ * 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.test.it.data.pipeline.core.dump;
+
+import com.zaxxer.hikari.HikariDataSource;
+import org.apache.commons.lang3.RandomStringUtils;
+import
org.apache.shardingsphere.data.pipeline.core.dumper.ColumnValueReaderEngine;
+import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Objects;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class ColumnValueReaderEngineTest {
+
+ @Test
+ void assertReadValue() throws SQLException {
+ ColumnValueReaderEngine columnValueReaderEngine = new
ColumnValueReaderEngine(new MySQLDatabaseType());
+ try (
+ HikariDataSource hikariDataSource =
createHikariDataSource(RandomStringUtils.randomAlphanumeric(6));
+ Connection connection = hikariDataSource.getConnection()) {
+ connection.createStatement().execute("CREATE TABLE t_order
(order_id INT PRIMARY KEY, user_id INT, status VARCHAR(12), c_year year)");
+ connection.createStatement().executeUpdate("INSERT INTO
t_order(order_id, user_id, status, c_year) VALUES (1, 2,'ok', null)");
+ ResultSet resultSet =
connection.createStatement().executeQuery("SELECT * FROM t_order");
+ resultSet.next();
+ assertThat(((Long)
Objects.requireNonNull(columnValueReaderEngine.read(resultSet,
resultSet.getMetaData(), 1))).intValue(), is(1));
+ assertThat(((Long)
Objects.requireNonNull(columnValueReaderEngine.read(resultSet,
resultSet.getMetaData(), 2))).intValue(), is(2));
+ assertThat(columnValueReaderEngine.read(resultSet,
resultSet.getMetaData(), 3), is("ok"));
+ assertNull(columnValueReaderEngine.read(resultSet,
resultSet.getMetaData(), 4));
+ }
+ }
+
+ private static HikariDataSource createHikariDataSource(final String
databaseName) {
+ HikariDataSource result = new HikariDataSource();
+
result.setJdbcUrl(String.format("jdbc:h2:mem:%s;DATABASE_TO_UPPER=false;MODE=MySQL",
databaseName));
+ result.setUsername("root");
+ result.setPassword("root");
+ result.setMaximumPoolSize(10);
+ result.setMinimumIdle(2);
+ return result;
+ }
+}