You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@fluss.apache.org by "beryllw (via GitHub)" <gi...@apache.org> on 2026/05/08 07:58:41 UTC

[PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

beryllw opened a new pull request, #3271:
URL: https://github.com/apache/fluss/pull/3271

   <!--
   *Thank you very much for contributing to Fluss - we are happy that you want to help us improve Fluss. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [GitHub issue](https://github.com/apache/fluss/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no issue.
   
     - Name the pull request in the format "[component] Title of the pull request", where *[component]* should be replaced by the name of the component being changed. Typically, this corresponds to the component label assigned to the issue (e.g., [kv], [log], [client], [flink]). Skip *[component]* if you are unsure about which is the best component.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
   
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes.
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
   
     - **Generative AI disclosure:** Indicate whether generative AI tools were used in authoring this PR. If yes, specify the tool below.
       - [ ] No generative AI tools used
       - [ ] Yes (please specify the tool below)
   
   **(The sections below can be removed for hotfixes or typos)**
   -->
   
   <!--
   Generated-by: [Tool Name and Version] following [the guidelines](https://github.com/apache/fluss/blob/main/AGENTS.md)
   -->
   
   ### Purpose
   
   <!-- Linking this pull request to the issue -->
   Linked issue: close #3270
   
   Fixes the aggregate pushdown logic to correctly handle COUNT(column) on nullable columns.
   
   <!-- What is the purpose of the change -->
   
   ### Brief change log
   
   <!-- Please describe the changes made in this pull request and explain how they address the issue -->
   
   When the aggregate is CountAggFunction with a nullable column argument, the pushdown is rejected so that Flink handles the NULL-excluding count correctly.
   
   ### Tests
   
   <!-- List UT and IT cases to verify this change -->
   
   - FlinkTableSourceBatchITCase#testCountPushDownForPkTable
   - FlinkTableSourceBatchITCase#testCountPushDownForLogTable
   
   ### API and Format
   
   <!-- Does this change affect API or storage format -->
   
   ### Documentation
   
   <!-- Does this change introduce a new feature -->
   


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "beryllw (via GitHub)" <gi...@apache.org>.
beryllw commented on PR #3271:
URL: https://github.com/apache/fluss/pull/3271#issuecomment-4404919109

   @luoyuxia cc


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3271:
URL: https://github.com/apache/fluss/pull/3271#discussion_r3207530607


##########
fluss-flink/fluss-flink-common/src/test/java/org/apache/fluss/flink/source/FlinkTableSourceBatchITCase.java:
##########
@@ -536,11 +589,11 @@ private String prepareLogTable() throws Exception {
 
         TablePath tablePath = TablePath.of(DEFAULT_DB, tableName);
 
-        // prepare table data
+        // prepare table data with NULL values in address column
         try (Table table = conn.getTable(tablePath)) {
             AppendWriter appendWriter = table.newAppend().createWriter();
             for (int i = 1; i <= 5; i++) {
-                Object[] values = new Object[] {i, "address" + i, "name" + i};
+                Object[] values = new Object[] {i, i % 2 == 0 ? null : "address" + i, "name" + i};
                 appendWriter.append(row(values));
                 // make sure every bucket has records
                 appendWriter.flush();

Review Comment:
   `prepareLogTable()` now writes NULL values into the `address` column, but `testLimitLogTableScan` (which also uses `prepareLogTable()`) still builds its expected row set assuming `address2`/`address4` are non-null. This will make the `isSubsetOf(expected)` assertion flaky or fail depending on which rows are returned by the LIMIT scan. Consider keeping `prepareLogTable()`’s data non-null and introducing a dedicated helper for NULL-address data (or update the other test expectations accordingly).



##########
fluss-flink/fluss-flink-common/src/test/java/org/apache/fluss/flink/source/FlinkTableSourceBatchITCase.java:
##########
@@ -452,6 +479,32 @@ void testCountPushDownForLogTable(boolean partitionTable) throws Exception {
         List<String> expected = Collections.singletonList(String.format("+I[%s]", expectedRows));
         assertThat(collected).isEqualTo(expected);
 
+        // test COUNT(column) pushdown
+        query = String.format("SELECT COUNT(id) FROM %s", tableName);
+        assertThat(tEnv.explainSql(query))
+                .contains(
+                        "aggregates=[grouping=[], aggFunctions=[Count1AggFunction()]]]], fields=[count1$0]");
+        iterRows = tEnv.executeSql(query).collect();
+        collected = collectRowsWithTimeout(iterRows, 1);
+        assertThat(collected).isEqualTo(expected);
+
+        // test COUNT(column) with NULL values - should NOT push down for nullable columns
+        // This will fail because log table doesn't support full scan in batch mode
+        assertThatThrownBy(
+                        () ->
+                                tEnv.explainSql(
+                                        String.format("SELECT COUNT(address) FROM %s", tableName)))
+                .hasMessageContaining(
+                        "Currently, Fluss only support queries on table with datalake enabled or point queries on primary key when it's in batch execution mode.");
+        assertThatThrownBy(
+                        () ->
+                                tEnv.explainSql(
+                                        String.format(
+                                                "SELECT COUNT(DISTINCT address) FROM %s",
+                                                tableName)))
+                .hasMessageContaining(
+                        "Currently, Fluss only support queries on table with datalake enabled or point queries on primary key when it's in batch execution mode.");

Review Comment:
   The new “should NOT push down” checks for `COUNT(address)`/`COUNT(DISTINCT address)` only assert that planning fails in batch mode (because non-pushdown triggers an unsupported full scan). This validates rejection indirectly, but it doesn’t validate the actual correctness requirement from #3270 (i.e., that `COUNT(nullable_col)` returns the non-null count when scanning is supported). Consider adding a variant where scanning is allowed (e.g., a lake-enabled table or a streaming execution mode) and asserting the real results (e.g., `COUNT(address)` = 3 for the non-partitioned log table with two NULLs).



##########
fluss-flink/fluss-flink-common/src/main/java/org/apache/fluss/flink/source/FlinkTableSource.java:
##########
@@ -799,19 +798,33 @@ public boolean applyAggregates(
             return false;
         }
 
-        FunctionDefinition functionDefinition = aggregateExpressions.get(0).getFunctionDefinition();
-        if (!(functionDefinition
-                        .getClass()
-                        .getCanonicalName()
-                        .equals(
-                                "org.apache.flink.table.planner.functions.aggfunctions.CountAggFunction")
-                || functionDefinition
-                        .getClass()
-                        .getCanonicalName()
-                        .equals(
-                                "org.apache.flink.table.planner.functions.aggfunctions.Count1AggFunction"))) {
+        AggregateExpression aggExpr = aggregateExpressions.get(0);
+        String functionName = aggExpr.getFunctionDefinition().getClass().getCanonicalName();
+
+        // Verify that the aggregate function is COUNT(*) or COUNT(1)
+        // CountAggFunction: COUNT(*) or COUNT(column)
+        // Count1AggFunction: COUNT(1) with constant argument
+        boolean isCountAgg =
+                "org.apache.flink.table.planner.functions.aggfunctions.CountAggFunction"
+                        .equals(functionName);
+        boolean isCount1Agg =
+                "org.apache.flink.table.planner.functions.aggfunctions.Count1AggFunction"
+                        .equals(functionName);
+        if (!isCountAgg && !isCount1Agg) {
             return false;
         }
+
+        // For COUNT(column), reject if column is nullable (cannot handle NULL filtering)
+        if (isCountAgg) {
+            List<org.apache.flink.table.expressions.Expression> args = aggExpr.getChildren();
+            if (!args.isEmpty() && args.get(0) instanceof ResolvedExpression) {
+                ResolvedExpression arg = (ResolvedExpression) args.get(0);
+                if (arg.getOutputDataType().getLogicalType().isNullable()) {
+                    return false;
+                }
+            }
+        }

Review Comment:
   In `applyAggregates`, when `CountAggFunction` has an argument but that argument is not a `ResolvedExpression`, the nullability check is skipped and the code still enables row-count pushdown. That can produce wrong results for `COUNT(expr)` if the expression can be NULL (or if its nullability cannot be determined). Consider making this conservative: if there is a COUNT argument and you can’t reliably determine it is non-nullable, return `false` (don’t push down).



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "loserwang1024 (via GitHub)" <gi...@apache.org>.
loserwang1024 merged PR #3271:
URL: https://github.com/apache/fluss/pull/3271


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "beryllw (via GitHub)" <gi...@apache.org>.
beryllw commented on PR #3271:
URL: https://github.com/apache/fluss/pull/3271#issuecomment-4417925510

   > <img alt="image" width="1157" height="349" src="https://private-user-images.githubusercontent.com/125648852/590175631-db7ec384-4ae2-496e-a28b-f02f1b057bc7.png?jwt=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Nzg0NzkxMzUsIm5iZiI6MTc3ODQ3ODgzNSwicGF0aCI6Ii8xMjU2NDg4NTIvNTkwMTc1NjMxLWRiN2VjMzg0LTRhZTItNDk2ZS1hMjhiLWYwMmYxYjA1N2JjNy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjYwNTExJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI2MDUxMVQwNTUzNTVaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT0yZjYzNjU4MTBmMzBkZmQ2ZWY2ZGY3MTdlMTU0MWQ5NmEyOTZmMmExNGJkZmViZTZmMDFjYmViMjdlOGNlNjkwJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZyZXNwb25zZS1jb250ZW50LXR5cGU9aW1hZ2UlMkZwbmcifQ.xcIh2GU_rWpZ3GlqPVOYTOq5xegHKyBRJWWZ25-W1gM"> @beryllw test fails
   
   fixed


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "luoyuxia (via GitHub)" <gi...@apache.org>.
luoyuxia commented on PR #3271:
URL: https://github.com/apache/fluss/pull/3271#issuecomment-4411860565

   cc @loserwang1024 


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "loserwang1024 (via GitHub)" <gi...@apache.org>.
loserwang1024 commented on PR #3271:
URL: https://github.com/apache/fluss/pull/3271#issuecomment-4417071644

   <img width="1157" height="349" alt="image" src="https://github.com/user-attachments/assets/db7ec384-4ae2-496e-a28b-f02f1b057bc7" />
   @beryllw test fails


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "loserwang1024 (via GitHub)" <gi...@apache.org>.
loserwang1024 commented on PR #3271:
URL: https://github.com/apache/fluss/pull/3271#issuecomment-4426825089

   > When the aggregate is CountAggFunction with a nullable column argument, the pushdown is rejected so that Flink handles the NULL-excluding count correctly.
   
   Is this a bug in flink sql?


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [flink] Fix COUNT(column) aggregate pushdown to reject nullable columns [fluss]

Posted by "beryllw (via GitHub)" <gi...@apache.org>.
beryllw commented on PR #3271:
URL: https://github.com/apache/fluss/pull/3271#issuecomment-4427191187

   > Is this a bug in flink sql? Maybe we can take a flink jira for it?
   
   It's a Flink Fluss SQL Source bug.


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org