This is an automated email from the ASF dual-hosted git repository.
chengzhang 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 d10aa6fbb6b Fix avg, sum, min, max function return empty data when no
query result return (#33449)
d10aa6fbb6b is described below
commit d10aa6fbb6b3b1ad3b6ff2caeca9384f5ebe7937
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Tue Oct 29 12:32:52 2024 +0800
Fix avg, sum, min, max function return empty data when no query result
return (#33449)
* Fix avg, sum, min, max function return empty data when no query result
* Update release note
---
RELEASE-NOTES.md | 7 +++---
.../dql/groupby/GroupByMemoryMergedResult.java | 8 +++----
.../cases/dql/e2e-dql-select-group-by.xml | 25 ++++++++++++++++++++++
3 files changed, 32 insertions(+), 8 deletions(-)
diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md
index dc1a84afae7..3a3108766e8 100644
--- a/RELEASE-NOTES.md
+++ b/RELEASE-NOTES.md
@@ -12,8 +12,8 @@
1. SQL Federation: Upgrade calcite version to 1.38.0 and update all license
info in LICENSE file -
[#33279](https://github.com/apache/shardingsphere/pull/33279)
1. SQL Parser: Support parsing Doris INSTR -
[#33289](https://github.com/apache/shardingsphere/pull/33289)
1. Agent: Simplify the use of Agent's Docker Image -
[#33356](https://github.com/apache/shardingsphere/pull/33356)
-1. Add arguments not null check when creating RouteUnit -
[#33382](https://github.com/apache/shardingsphere/pull/33382)
-1. Add index columns not empty judgement for IndexColumnTokenGenerator -
[#33384](https://github.com/apache/shardingsphere/pull/33384)
+1. Kernel: Add arguments not null check when creating RouteUnit -
[#33382](https://github.com/apache/shardingsphere/pull/33382)
+1. Kernel: Add index columns not empty judgement for IndexColumnTokenGenerator
- [#33384](https://github.com/apache/shardingsphere/pull/33384)
1. SQL Parser: Support parsing Doris STRRIGHT -
[#33393](https://github.com/apache/shardingsphere/pull/33393)
1. JDBC: Add show database name for JDBC when execute SHOW COMPUTE NODES -
[#33437](https://github.com/apache/shardingsphere/pull/33437)
@@ -25,7 +25,8 @@
1. SQL Binder: Fix SQL performance issues caused by repeated subquery fetches
- [#33361](https://github.com/apache/shardingsphere/pull/33361)
1. Sharding: Remove ShardingRouteAlgorithmException check logic temporarily to
support different actual table name config -
[#33367](https://github.com/apache/shardingsphere/pull/33367)
1. SQL Binder: Fix table does not exist exception when use
HintManager#setDatabaseName to transparent -
[#33370](https://github.com/apache/shardingsphere/pull/33370)
-1. Fix LiteralExpressionSegment cast exception in sql parser. -
[#33332](https://github.com/apache/shardingsphere/pull/33332)
+1. SQL Parser: Fix LiteralExpressionSegment cast exception in sql parser. -
[#33332](https://github.com/apache/shardingsphere/pull/33332)
+1. Sharding: Fix avg, sum, min, max function return empty data when no query
result return - [#33449](https://github.com/apache/shardingsphere/pull/33449)
### Change Logs
diff --git
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
index 6c456bfca7b..226f48fab8f 100644
---
a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
+++
b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dql/groupby/GroupByMemoryMergedResult.java
@@ -17,18 +17,18 @@
package org.apache.shardingsphere.sharding.merge.dql.groupby;
-import
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.AggregationDistinctProjection;
import
org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.AggregationProjection;
import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
+import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.infra.exception.dialect.exception.syntax.table.NoSuchTableException;
import
org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import
org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryMergedResult;
import
org.apache.shardingsphere.infra.merge.result.impl.memory.MemoryQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
-import
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import
org.apache.shardingsphere.sharding.exception.data.NotImplementComparableValueException;
import
org.apache.shardingsphere.sharding.merge.dql.groupby.aggregation.AggregationUnit;
import
org.apache.shardingsphere.sharding.merge.dql.groupby.aggregation.AggregationUnitFactory;
@@ -38,14 +38,12 @@ import
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.table
import java.sql.SQLException;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -144,7 +142,7 @@ public final class GroupByMemoryMergedResult extends
MemoryMergedResult<Sharding
final
Map<GroupByValue, MemoryQueryResultRow> dataMap, final List<Boolean>
valueCaseSensitive) {
if (dataMap.isEmpty()) {
Object[] data = generateReturnData(selectStatementContext);
- return Arrays.stream(data).anyMatch(Objects::nonNull) ?
Collections.singletonList(new MemoryQueryResultRow(data)) :
Collections.emptyList();
+ return
selectStatementContext.getProjectionsContext().getAggregationProjections().isEmpty()
? Collections.emptyList() : Collections.singletonList(new
MemoryQueryResultRow(data));
}
List<MemoryQueryResultRow> result = new ArrayList<>(dataMap.values());
result.sort(new GroupByRowComparator(selectStatementContext,
valueCaseSensitive));
diff --git
a/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select-group-by.xml
b/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select-group-by.xml
index aa525aeb344..0a12d650c70 100644
--- a/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select-group-by.xml
+++ b/test/e2e/sql/src/test/resources/cases/dql/e2e-dql-select-group-by.xml
@@ -52,4 +52,29 @@
scenario-comments="Test single table's LIKE operator underscore
wildcard in select group by statement when use sharding feature.|Test encrypt
table's LIKE operator underscore wildcard in select group by statement when use
encrypt feature.">
<assertion expected-data-source-name="read_dataset" />
</test-case>
+
+ <test-case sql="SELECT AVG(DISTINCT order_id) AS avg_id FROM t_order WHERE
1 = 2" db-types="MySQL,PostgreSQL,openGauss" scenario-types="db,tbl"
+ scenario-comments="Test AVG DISTINCT returns NULL when no data
matches">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
+
+ <test-case sql="SELECT SUM(DISTINCT order_id) AS sum_id FROM t_order WHERE
1 = 2" db-types="MySQL,PostgreSQL,openGauss" scenario-types="db,tbl"
+ scenario-comments="Test SUM DISTINCT returns NULL when no data
matches">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
+
+ <test-case sql="SELECT COUNT(DISTINCT order_id) AS count_id FROM t_order
WHERE 1 = 2" db-types="MySQL,PostgreSQL,openGauss" scenario-types="db,tbl"
+ scenario-comments="Test COUNT DISTINCT returns 0 when no data
matches">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
+
+ <test-case sql="SELECT MIN(DISTINCT order_id) AS min_id FROM t_order WHERE
1 = 2" db-types="MySQL,PostgreSQL,openGauss" scenario-types="db,tbl"
+ scenario-comments="Test MIN DISTINCT returns NULL when no data
matches">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
+
+ <test-case sql="SELECT MAX(DISTINCT order_id) AS max_id FROM t_order WHERE
1 = 2" db-types="MySQL,PostgreSQL,openGauss" scenario-types="db,tbl"
+ scenario-comments="Test MAX DISTINCT returns NULL when no data
matches">
+ <assertion expected-data-source-name="read_dataset" />
+ </test-case>
</e2e-test-cases>