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 57b084bc283 Fix null result in federated query for a single projected
column (#32444)
57b084bc283 is described below
commit 57b084bc283c792d8b0b8a269991eff48d570e20
Author: niu niu <[email protected]>
AuthorDate: Mon Aug 12 10:00:15 2024 +0800
Fix null result in federated query for a single projected column (#32444)
---
.../sqlfederation/resultset/SQLFederationResultSet.java | 2 +-
.../optimizer/function/mysql/impl/MySQLBinFunction.java | 6 +++---
.../function/mysql/impl/MySQLBitCountFunction.java | 2 +-
.../sql/src/test/resources/cases/dql/e2e-dql-select.xml | 16 ++++++++++++++++
4 files changed, 21 insertions(+), 5 deletions(-)
diff --git
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSet.java
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSet.java
index c1519501f31..46903e425e0 100644
---
a/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSet.java
+++
b/kernel/sql-federation/core/src/main/java/org/apache/shardingsphere/sqlfederation/resultset/SQLFederationResultSet.java
@@ -105,7 +105,7 @@ public final class SQLFederationResultSet extends
AbstractUnsupportedOperationSQ
if (result && null != enumerator.current()) {
currentRows = enumerator.current().getClass().isArray() &&
!(enumerator.current() instanceof byte[]) ? (Object[]) enumerator.current() :
new Object[]{enumerator.current()};
} else {
- currentRows = new Object[]{};
+ currentRows = new Object[]{null};
}
return result;
}
diff --git
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBinFunction.java
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBinFunction.java
index dac2122a1cc..fa6106fc4a1 100644
---
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBinFunction.java
+++
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBinFunction.java
@@ -42,14 +42,14 @@ public final class MySQLBinFunction {
if (value instanceof Number && !(value instanceof BigInteger)) {
return Long.toBinaryString(((Number) value).longValue());
}
+ if (value instanceof String && ((String) value).isEmpty()) {
+ return null;
+ }
BigInteger bigIntegerValue = value instanceof BigInteger ?
(BigInteger) value : new BigInteger(getFirstNumbers(String.valueOf(value)));
return -1 == bigIntegerValue.signum() ?
bigIntegerValue.add(BigInteger.ONE.shiftLeft(Long.SIZE).subtract(BigInteger.ONE)).add(BigInteger.ONE).toString(2)
: bigIntegerValue.toString(2);
}
private static String getFirstNumbers(final String value) {
- if (value.isEmpty()) {
- return "0";
- }
boolean isNegative = '-' == value.charAt(0);
StringBuilder result = new StringBuilder();
if (isNegative) {
diff --git
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBitCountFunction.java
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBitCountFunction.java
index a36ab752e78..65661d8cb07 100644
---
a/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBitCountFunction.java
+++
b/kernel/sql-federation/optimizer/src/main/java/org/apache/shardingsphere/sqlfederation/optimizer/function/mysql/impl/MySQLBitCountFunction.java
@@ -39,7 +39,7 @@ public final class MySQLBitCountFunction {
@SuppressWarnings("unused")
public static Object bitCount(final Object value) {
if (null == value) {
- return 0;
+ return null;
}
if (value instanceof byte[]) {
return bitCount((byte[]) value);
diff --git a/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select.xml
b/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select.xml
index ccd33ebc19c..1d45a8f2189 100644
--- a/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select.xml
+++ b/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select.xml
@@ -306,6 +306,10 @@
<assertion expected-data-source-name="read_dataset" />
</test-case>
+ <test-case sql="SELECT BIT_COUNT(NULL)" db-types="MySQL"
scenario-types="db_tbl_sql_federation">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
+
<test-case sql="SELECT bit_count(123456), bit_count('123456'),
bit_count('abcdefg'), BIT_COUNT('abcdef1234'), bit_count(''), bit_count(1 + 1)"
db-types="MySQL" scenario-types="db_tbl_sql_federation">
<assertion expected-data-source-name="read_dataset" />
</test-case>
@@ -370,4 +374,16 @@
INNER JOIN t_order_item t3 ON t2.product_id =
t3.product_id INNER JOIN t_order t4 ON t4.order_id = t3.order_id order by
t1.product_id" db-types="MySQL" scenario-types="db_tbl_sql_federation">
<assertion expected-data-source-name="read_dataset" />
</test-case>
+
+ <test-case sql="SELECT BIN(NULL)" db-types="MySQL"
scenario-types="db_tbl_sql_federation">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
+
+ <test-case sql="SELECT BIN('')" db-types="MySQL"
scenario-types="db_tbl_sql_federation">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
+
+ <test-case sql="SELECT BIN(NULL), BIN(''), BIN(' ')" db-types="MySQL"
scenario-types="db_tbl_sql_federation">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
</e2e-test-cases>