github-actions[bot] commented on code in PR #62548:
URL: https://github.com/apache/doris/pull/62548#discussion_r3092531111


##########
regression-test/suites/nereids_rules_p0/count_null_not_count_star/count_null_not_count_star.groovy:
##########
@@ -0,0 +1,116 @@
+// 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.
+
+suite("count_null_not_count_star") {
+    sql "SET enable_nereids_planner=true"
+    sql "SET enable_fallback_to_original_planner=false"
+
+    sql "DROP TABLE IF EXISTS count_null_test"
+
+    sql """
+        create table count_null_test(pk int, a int, b int) distributed by 
hash(pk) buckets 10
+        properties('replication_num' = '1');
+    """
+
+    sql """
+        insert into count_null_test values(1, 1, 1), (2, null, 2), (3, 3, 
null), (4, null, null);
+    """
+    sql "sync"
+
+    // count(null) should always return 0 regardless of row count
+    order_qt_count_null """select count(null) from count_null_test"""
+
+    // count(*) should return total number of rows
+    order_qt_count_star """select count(*) from count_null_test"""
+
+    // count(1) should be equivalent to count(*)
+    order_qt_count_literal """select count(1) from count_null_test"""
+
+    // count(null) with group by should return 0 for each group
+    order_qt_count_null_group_by """select pk, count(null) from 
count_null_test group by pk order by pk"""
+
+    // mixed: count(null) vs count(*) vs count(column)
+    order_qt_count_mixed """select count(null), count(*), count(a), count(b) 
from count_null_test"""
+
+    // count(null) in subquery with union all
+    order_qt_count_null_union """
+        select count(null) from (
+            select a from count_null_test
+            union all
+            select a from count_null_test
+        ) t
+    """
+
+    // count(null) as window function should still return 0
+    order_qt_count_null_window """

Review Comment:
   This window case does not reach `SimplifyWindowExpression`. That rule only 
rewrites when the `PARTITION BY` keys are unique, and an empty partition-key 
set is not treated as unique. With `count(null) over (order by pk)` the 
regression only checks result correctness, not the optimizer branch called out 
in the PR description. Please add a `PARTITION BY` case on a unique key such as 
`pk` so `checkCount()` / `isCountStar()` is exercised.



##########
regression-test/suites/nereids_rules_p0/count_null_not_count_star/count_null_not_count_star.groovy:
##########
@@ -0,0 +1,116 @@
+// 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.
+
+suite("count_null_not_count_star") {
+    sql "SET enable_nereids_planner=true"
+    sql "SET enable_fallback_to_original_planner=false"
+
+    sql "DROP TABLE IF EXISTS count_null_test"
+
+    sql """
+        create table count_null_test(pk int, a int, b int) distributed by 
hash(pk) buckets 10
+        properties('replication_num' = '1');
+    """
+
+    sql """
+        insert into count_null_test values(1, 1, 1), (2, null, 2), (3, 3, 
null), (4, null, null);
+    """
+    sql "sync"
+
+    // count(null) should always return 0 regardless of row count
+    order_qt_count_null """select count(null) from count_null_test"""
+
+    // count(*) should return total number of rows
+    order_qt_count_star """select count(*) from count_null_test"""
+
+    // count(1) should be equivalent to count(*)
+    order_qt_count_literal """select count(1) from count_null_test"""
+
+    // count(null) with group by should return 0 for each group
+    order_qt_count_null_group_by """select pk, count(null) from 
count_null_test group by pk order by pk"""
+
+    // mixed: count(null) vs count(*) vs count(column)
+    order_qt_count_mixed """select count(null), count(*), count(a), count(b) 
from count_null_test"""
+
+    // count(null) in subquery with union all
+    order_qt_count_null_union """

Review Comment:
   `count(null)` has already been rewritten to constant `0` by 
`CountLiteralRewrite` before the eager-aggregation stage reaches 
`PushCountIntoUnionAll`, so this query does not actually pin the union-all 
pushdown path mentioned in the PR description. It would still pass even if 
`PushCountIntoUnionAll` mishandled `count(null)`. Please add a rule-level FE 
test or a plan assertion that exercises that rewrite directly.



##########
regression-test/suites/nereids_rules_p0/count_null_not_count_star/count_null_not_count_star.groovy:
##########
@@ -0,0 +1,116 @@
+// 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.
+
+suite("count_null_not_count_star") {
+    sql "SET enable_nereids_planner=true"
+    sql "SET enable_fallback_to_original_planner=false"
+
+    sql "DROP TABLE IF EXISTS count_null_test"
+
+    sql """
+        create table count_null_test(pk int, a int, b int) distributed by 
hash(pk) buckets 10
+        properties('replication_num' = '1');
+    """
+
+    sql """
+        insert into count_null_test values(1, 1, 1), (2, null, 2), (3, 3, 
null), (4, null, null);
+    """
+    sql "sync"
+
+    // count(null) should always return 0 regardless of row count
+    order_qt_count_null """select count(null) from count_null_test"""
+
+    // count(*) should return total number of rows
+    order_qt_count_star """select count(*) from count_null_test"""
+
+    // count(1) should be equivalent to count(*)
+    order_qt_count_literal """select count(1) from count_null_test"""
+
+    // count(null) with group by should return 0 for each group
+    order_qt_count_null_group_by """select pk, count(null) from 
count_null_test group by pk order by pk"""
+
+    // mixed: count(null) vs count(*) vs count(column)
+    order_qt_count_mixed """select count(null), count(*), count(a), count(b) 
from count_null_test"""
+
+    // count(null) in subquery with union all
+    order_qt_count_null_union """
+        select count(null) from (
+            select a from count_null_test
+            union all
+            select a from count_null_test
+        ) t
+    """
+
+    // count(null) as window function should still return 0
+    order_qt_count_null_window """
+        select pk, count(null) over (order by pk) from count_null_test order 
by pk
+    """
+
+    // count(null) through join with GROUP BY: exercises 
PushDownAggThroughJoin path.
+    // With the bug, count(null) would be treated as count(*) and pushed down 
through join,
+    // producing wrong non-zero results. With the fix, it correctly returns 0.
+    sql "DROP TABLE IF EXISTS count_null_test2"
+    sql """
+        create table count_null_test2(pk int, c int) distributed by hash(pk) 
buckets 10
+        properties('replication_num' = '1');
+    """
+    sql """insert into count_null_test2 values(1, 10), (2, 20);"""
+    sql "sync"
+
+    order_qt_count_null_join_grouped """
+        select t1.pk, count(null), count(*)
+        from count_null_test t1 inner join count_null_test2 t2 on t1.pk = t2.pk
+        group by t1.pk order by t1.pk
+    """
+
+    // count(null) vs count(*) through join without GROUP BY for comparison
+    order_qt_count_star_join """
+        select count(*), count(null) from count_null_test t1 inner join 
count_null_test2 t2 on t1.pk = t2.pk
+    """
+
+    // COUNT_ON_INDEX path: count(null) on a table with inverted index should 
NOT use COUNT_ON_MATCH
+    sql "DROP TABLE IF EXISTS count_null_idx_test"
+    sql """
+        create table count_null_idx_test(
+            pk int,
+            content varchar(200),
+            INDEX idx_content (content) USING INVERTED PROPERTIES("parser" = 
"english")
+        ) duplicate key(pk)
+        distributed by hash(pk) buckets 1
+        properties('replication_num' = '1');
+    """
+    sql """insert into count_null_idx_test values(1, 'hello world'), (2, 
'doris test'), (3, 'hello doris');"""
+    sql "sync"
+
+    // count(*) with MATCH predicate may use COUNT_ON_MATCH optimization
+    // count(null) with same predicate must NOT — verify via explain
+    def explainCountStar = sql """

Review Comment:
   Only the negative explain is asserted here. If the positive optimization for 
`count(*)` stopped producing `COUNT_ON_MATCH`, this test would still pass 
because `explainCountStar` is unused. Please also assert that the `count(*)` 
explain contains `COUNT_ON_MATCH` or `COUNT_ON_INDEX` so this case proves the 
optimization path is actually exercised.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to