morrySnow opened a new pull request, #67732:
URL: https://github.com/apache/doris/pull/67732

   ## Problem
   
   Counting a projected alias over a nullable indexed column can return an 
incorrect non-zero result when the filter retains only null rows. Mixing the 
alias count with `COUNT(*)` or another count exposes the problem:
   
   ```sql
   SELECT COUNT(x), COUNT(*)
   FROM (SELECT k AS x FROM t WHERE k IS NULL) q;
   ```
   
   For two matching null rows, the correct result is `(0, 2)`, but the 
storage-layer index-count path can return `(2, 2)`.
   
   ## Root cause
   
   The FE implementation rule validates `IS NULL` and OR predicates before 
pushing count aggregation to the storage layer. In the Project variant, this 
validation used the aggregate-side alias slot, while the filter below the 
Project refers to the source slot. Their expression IDs differ, so the 
null-safety guard did not recognize that the filter and `COUNT` referenced the 
same nullable value.
   
   The rule normalized the aggregate argument to the source slot only later, 
after the safety decision had already been made.
   
   ## Reproduction
   
   ```sql
   CREATE TABLE t (
       id INT NOT NULL,
       k INT NULL,
       INDEX idx_k (k) USING INVERTED
   )
   DUPLICATE KEY(id)
   DISTRIBUTED BY HASH(id) BUCKETS 1
   PROPERTIES ("replication_num" = "1");
   
   INSERT INTO t VALUES (1, NULL), (2, NULL), (3, 1);
   
   SELECT COUNT(x), COUNT(*)
   FROM (SELECT k AS x FROM t WHERE k IS NULL) q;
   
   SELECT COUNT(x), COUNT(id)
   FROM (SELECT k AS x, id FROM t WHERE k IS NULL) q;
   ```
   
   Before this change, both queries return `(2, 2)` and the plan contains 
`pushAggOp=COUNT_ON_INDEX`. Both queries should return `(0, 2)`.
   
   ## Fix
   
   Normalize aggregate arguments through the Project before collecting the 
slots used by the predicate safety checks. The count slots and filter slots are 
now compared in the same source expression-ID domain. If `IS NULL` targets a 
counted source slot, the FE rejects the index-count pushdown and preserves the 
column's null values.
   
   This change is limited to the FE planner.
   
   ## Tests
   
   - Added a FE plan test for `COUNT(projected_alias) + COUNT(*)` above an `IS 
NULL` filter, verifying that the count-on-index implementation rule is rejected.
   - Ran `PhysicalStorageLayerAggregateTest`: 7 tests passed.
   - Deployed the FE to a local sandbox and reran both SQL reproductions. They 
return `(0, 2)`, and the scan plan reports `pushAggOp=NONE`.
   


-- 
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