Checking Doris COUNT plus an offset: retain the raw count and the arithmetic
A correct COUNT optimization does not automatically preserve an expression that contains COUNT.
Post-aggregation arithmetic is part of the contract
Validate arithmetic applied after aggregation alongside the raw aggregate. Supplying the raw count, business offset and final value to a Truthound relation check explains a failure more clearly than checking one number's range. [1, 2, 3]
A partial rewrite of a compound expression
The public Doris Nereids reproduction describes PushCountIntoUnionAll admitting a compound expression containing COUNT while its actual rewrite handles only a direct COUNT form. A count with an added constant then differs from the expected result. A successful plain COUNT on the same input does not establish correctness of the compound expression. [1]
Derive the reference from the input
In an illustrative fixture, define a raw count of 6 and a business offset of 10. The expected final value is 16. A final COUNT-plus-offset value of 16 preserves the raw count plus the business offset. A final value of 12 violates that arithmetic relationship. A positive, plausible-looking 12 is not sufficient for approval. If the raw count uses the same affected rewrite, it is not independent evidence; count a small controlled input directly or use another verified path. [1, 3]
Keep the audit table after remediation
Export branch counts and final COUNT-plus-offset results together. Include repeated keys when checking the rewrite's conservation rule. Truthound Core's ExpressionValidator can verify that the final value equals the raw count plus the offset. It does not replace the Nereids rewrite. Separating the raw aggregation into another stage remains an alternative only if group keys and NULL COUNT semantics are preserved. [1, 2, 3]
Application conditions
Obtain raw_count independently for each same group_key and record it with business_offset and published_result.
Alternatives
Calculate raw COUNT in an independent stage, then add the business offset.
Limitations
Copying raw_count from the same faulty rewrite does not provide an independent reference. Truthound does not replace the Nereids rewrite.
Evidence
- Event checked
- Subject checked
- Modified
