This is an automated email from the ASF dual-hosted git repository.
yujun777 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new a564c02674b [fix](ivm) Fall back to complete refresh on negative
aggregate counts (#70804)
a564c02674b is described below
commit a564c02674b480a98fc3b2c1edaded9dd6c76423
Author: yujun <[email protected]>
AuthorDate: Mon Nov 23 12:30:33 2026 +0800
[fix](ivm) Fall back to complete refresh on negative aggregate counts
(#70804)
### Background
An incremental aggregate materialized view applies signed changes from
its base tables to the counts it already stores. Runtime guards reject a
merge that would make a count negative. A refresh request that allows
fallback can rebuild the view when the stored state cannot be used to
apply the change.
### What problem does this PR solve?
Issue Number: None
Related PR: #62606 (introduced the aggregate guards), #69772 (uses the
same recovery path for expired stream offsets)
PR Type: Bugfix
Problem Summary:
An incremental refresh that calculates a negative count fails even when
the request allows fallback. This can be reproduced by moving facts to
an already populated group through an excluded dimension, then deleting
more facts than the view has recorded for that group. The refresh now
rebuilds the view and its stream baselines when the request allows
fallback.
### Details
- **Frequency:** Reproduced with SQL when the delete delta for a group
exceeds the count stored by the view. Production frequency is unknown.
- **Root cause:** The aggregate assertions returned ordinary query
errors that IVM's execution failure classifier did not recognize. The
incremental attempt propagated the error and the refresh task never
reached the fallback path.
- **Why existing tests missed it:** `IvmIncrRefreshManagerTest` covers
other recognized executor errors. `test_ivm_minmax_runtime_fallback`
reaches the fallback path through a MIN/MAX boundary guard and does not
make an aggregate count negative.
- **Fix:** Tag the shared non-negative count assertion and classify it
as `NEGATIVE_AGGREGATE_COUNT`. Use the existing direct-to-`COMPLETE`
route to recompute view state and reset the stream baselines together.
- **Other paths and branches:** The shared guard covers the group count,
hidden non-null counts, and `COUNT(expr)` in both deployment modes. The
Apache Doris 3.1, 4.0, 4.1, and 4.2 release branches checked do not
contain this classifier.
- **Behavior change:** `INCREMENTAL FALLBACK` and `AUTO` try `COMPLETE`
after this guard trips. Strict `INCREMENTAL` reports
`NEGATIVE_AGGREGATE_COUNT` and includes the assertion detail.
---
.../doris/mtmv/ivm/IvmFailureClassifier.java | 4 +
.../apache/doris/mtmv/ivm/IvmFailureReason.java | 2 +
.../mtmv/ivm/agg/IvmAggExpressionBuilder.java | 5 +-
.../doris/mtmv/ivm/IvmIncrRefreshManagerTest.java | 11 +++
.../test_ivm_negative_aggregate_count_fallback.out | 21 ++++++
...st_ivm_negative_aggregate_count_fallback.groovy | 87 ++++++++++++++++++++++
6 files changed, 129 insertions(+), 1 deletion(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureClassifier.java
b/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureClassifier.java
index 12f9e3c5db6..1e302783e17 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureClassifier.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureClassifier.java
@@ -27,6 +27,7 @@ import java.util.Optional;
*/
public final class IvmFailureClassifier {
public static final String MIN_MAX_BOUNDARY_MSG_PREFIX = "IVM fallback:
min/max boundary hit";
+ public static final String NEGATIVE_AGGREGATE_COUNT_MSG_PREFIX = "IVM
fallback: negative aggregate count: ";
public static final String BITMAP_AGG_DELETE_MSG_PREFIX =
"IVM: deleted row affects BITMAP aggregate, requires COMPLETE";
public static final String NON_DETERMINISTIC_ROW_ID_MSG_PREFIX =
@@ -45,6 +46,9 @@ public final class IvmFailureClassifier {
if (detail.contains(MIN_MAX_BOUNDARY_MSG_PREFIX)) {
return Optional.of(IvmFailureReason.MIN_MAX_BOUNDARY_HIT);
}
+ if (detail.contains(NEGATIVE_AGGREGATE_COUNT_MSG_PREFIX)) {
+ return Optional.of(IvmFailureReason.NEGATIVE_AGGREGATE_COUNT);
+ }
if (detail.contains(BITMAP_AGG_DELETE_MSG_PREFIX)) {
return Optional.of(IvmFailureReason.BITMAP_AGG_DELETE);
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureReason.java
b/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureReason.java
index af2f5b358aa..490b603a142 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureReason.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/IvmFailureReason.java
@@ -37,6 +37,7 @@ public enum IvmFailureReason {
INCREMENTAL_EXECUTION_FAILED,
AGG_UNSUPPORTED,
MIN_MAX_BOUNDARY_HIT,
+ NEGATIVE_AGGREGATE_COUNT,
BITMAP_AGG_DELETE,
PLAN_SIGNATURE_MISMATCH,
/** A previous IVM refresh txn committed but its data is not yet visible.
*/
@@ -51,6 +52,7 @@ public enum IvmFailureReason {
return this == CHANGELOG_BROKEN
|| this == STREAM_UNSUPPORTED
|| this == MIN_MAX_BOUNDARY_HIT
+ || this == NEGATIVE_AGGREGATE_COUNT
|| this == BITMAP_AGG_DELETE
|| this == PLAN_SIGNATURE_MISMATCH;
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/agg/IvmAggExpressionBuilder.java
b/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/agg/IvmAggExpressionBuilder.java
index 24990b7a72b..d20450f9750 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/agg/IvmAggExpressionBuilder.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/mtmv/ivm/agg/IvmAggExpressionBuilder.java
@@ -19,6 +19,7 @@ package org.apache.doris.mtmv.ivm.agg;
import org.apache.doris.catalog.Column;
import org.apache.doris.mtmv.ivm.IvmException;
+import org.apache.doris.mtmv.ivm.IvmFailureClassifier;
import org.apache.doris.mtmv.ivm.IvmFailureReason;
import org.apache.doris.mtmv.ivm.IvmUtil;
import org.apache.doris.nereids.trees.expressions.Expression;
@@ -76,8 +77,10 @@ public class IvmAggExpressionBuilder {
/** Guards a count-like expression from going negative while keeping an
expression-typed result. */
public Expression assertNonNegative(Expression expr, String message) {
+ StringLiteral errorMessage = new
StringLiteral(IvmFailureClassifier.NEGATIVE_AGGREGATE_COUNT_MSG_PREFIX
+ + message);
return new If(new AssertTrue(new GreaterThanEqual(expr,
- new BigIntLiteral(0)), new StringLiteral(message)),
+ new BigIntLiteral(0)), errorMessage),
expr, new NullLiteral(expr.getDataType()));
}
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/mtmv/ivm/IvmIncrRefreshManagerTest.java
b/fe/fe-core/src/test/java/org/apache/doris/mtmv/ivm/IvmIncrRefreshManagerTest.java
index 7d0a9ec2c8c..fa8db00fc63 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/mtmv/ivm/IvmIncrRefreshManagerTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/mtmv/ivm/IvmIncrRefreshManagerTest.java
@@ -174,6 +174,17 @@ public class IvmIncrRefreshManagerTest {
IvmFailureClassifier.NON_DETERMINISTIC_ROW_ID_MSG_PREFIX);
}
+ @Test
+ public void testNegativeAggregateCountRequiresCompleteRefresh() throws
Exception {
+ IvmFailureReason reason = IvmFailureReason.NEGATIVE_AGGREGATE_COUNT;
+ for (String detail : List.of("negative group count", "negative hidden
count for sum_score",
+ "negative count for score_count")) {
+ assertKnownExecutionFailureFallback(reason,
+ "(Invalid argument) " +
IvmFailureClassifier.NEGATIVE_AGGREGATE_COUNT_MSG_PREFIX + detail);
+ }
+ Assertions.assertTrue(reason.requiresCompleteRefresh());
+ }
+
@Test
public void
testManagerReturnsChangelogBrokenFallbackForExpiredStreamOffset() throws
Exception {
// What planning a MIN_DELTA read throws once changelog.ttl_seconds
has expired the stream offset.
diff --git
a/regression-test/data/mtmv_p0/ivm/test_ivm_negative_aggregate_count_fallback.out
b/regression-test/data/mtmv_p0/ivm/test_ivm_negative_aggregate_count_fallback.out
new file mode 100644
index 00000000000..a3faf25504b
--- /dev/null
+++
b/regression-test/data/mtmv_p0/ivm/test_ivm_negative_aggregate_count_fallback.out
@@ -0,0 +1,21 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !initial --
+10 3
+20 1
+30 1
+
+-- !after_strict_failure --
+10 3
+20 1
+30 1
+
+-- !after_fallback --
+20 2
+30 1
+
+-- !after_auto --
+30 1
+
+-- !after_incremental --
+30 2
+
diff --git
a/regression-test/suites/mtmv_p0/ivm/test_ivm_negative_aggregate_count_fallback.groovy
b/regression-test/suites/mtmv_p0/ivm/test_ivm_negative_aggregate_count_fallback.groovy
new file mode 100644
index 00000000000..75d96816f63
--- /dev/null
+++
b/regression-test/suites/mtmv_p0/ivm/test_ivm_negative_aggregate_count_fallback.groovy
@@ -0,0 +1,87 @@
+// 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("test_ivm_negative_aggregate_count_fallback") {
+ sql "SET enable_materialized_view_rewrite = false"
+ sql "SET enable_dml_materialized_view_rewrite = false"
+ sql "DROP MATERIALIZED VIEW IF EXISTS ivm_negative_count_mv"
+ sql "DROP TABLE IF EXISTS ivm_negative_count_fact"
+ sql "DROP TABLE IF EXISTS ivm_negative_count_dim"
+
+ sql """
+ CREATE TABLE ivm_negative_count_fact (id INT NOT NULL, dim_id INT NOT
NULL)
+ UNIQUE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1
+ PROPERTIES (
+ 'replication_num' = '1',
+ 'enable_unique_key_merge_on_write' = 'true',
+ 'changelog.enable' = 'true',
+ 'changelog.need_historical_value' = 'true'
+ )
+ """
+ sql """
+ CREATE TABLE ivm_negative_count_dim (id INT NOT NULL, category INT NOT
NULL)
+ UNIQUE KEY(id) DISTRIBUTED BY HASH(id) BUCKETS 1
+ PROPERTIES ('replication_num' = '1',
'enable_unique_key_merge_on_write' = 'true')
+ """
+ sql "INSERT INTO ivm_negative_count_fact VALUES (1, 1), (2, 1), (3, 1),
(4, 2), (5, 3)"
+ sql "INSERT INTO ivm_negative_count_dim VALUES (1, 10), (2, 20), (3, 30)"
+ sql """
+ CREATE MATERIALIZED VIEW ivm_negative_count_mv
+ BUILD DEFERRED REFRESH INCREMENTAL FALLBACK ON MANUAL
+ KEY(category) DISTRIBUTED BY HASH(category) BUCKETS 1
+ PROPERTIES ('replication_num' = '1', 'excluded_trigger_tables' =
'ivm_negative_count_dim')
+ AS SELECT d.category, COUNT(*) AS row_count
+ FROM ivm_negative_count_fact f JOIN ivm_negative_count_dim d ON
f.dim_id = d.id
+ GROUP BY d.category
+ """
+ refreshIvm("ivm_negative_count_mv", "REFRESH MATERIALIZED VIEW
ivm_negative_count_mv COMPLETE")
+ assertEquals("SUCCESS", latestIvmTask("ivm_negative_count_mv").Status)
+ order_qt_initial "SELECT category, row_count FROM ivm_negative_count_mv"
+
+ // The excluded dimension contributes no delta. Both deleted facts now
join category 20,
+ // which has only one row in the MV baseline, so applying the delta makes
its count negative.
+ sql "INSERT INTO ivm_negative_count_dim VALUES (1, 20)"
+ sql "DELETE FROM ivm_negative_count_fact WHERE id IN (1, 2)"
+ refreshIvm("ivm_negative_count_mv", "REFRESH MATERIALIZED VIEW
ivm_negative_count_mv INCREMENTAL")
+ def strictTask = latestIvmTask("ivm_negative_count_mv")
+ assertEquals("FAILED", strictTask.Status)
+ assertTrue(strictTask.ErrorMsg.toString().contains("negative group count"))
+ assertEquals("NEGATIVE_AGGREGATE_COUNT", strictTask.IvmFallbackReason)
+ order_qt_after_strict_failure "SELECT category, row_count FROM
ivm_negative_count_mv"
+
+ refreshIvm("ivm_negative_count_mv",
+ "REFRESH MATERIALIZED VIEW ivm_negative_count_mv INCREMENTAL
FALLBACK")
+ def fallbackTask = latestIvmTask("ivm_negative_count_mv")
+ assertEquals("SUCCESS", fallbackTask.Status)
+ assertEquals("COMPLETE", fallbackTask.RefreshMode)
+ assertEquals("NEGATIVE_AGGREGATE_COUNT", fallbackTask.IvmFallbackReason)
+ order_qt_after_fallback "SELECT category, row_count FROM
ivm_negative_count_mv"
+
+ sql "INSERT INTO ivm_negative_count_dim VALUES (1, 30), (2, 30)"
+ sql "DELETE FROM ivm_negative_count_fact WHERE id IN (3, 4)"
+ refreshIvm("ivm_negative_count_mv", "REFRESH MATERIALIZED VIEW
ivm_negative_count_mv AUTO")
+ def autoTask = latestIvmTask("ivm_negative_count_mv")
+ assertEquals("SUCCESS", autoTask.Status)
+ assertEquals("COMPLETE", autoTask.RefreshMode)
+ assertEquals("NEGATIVE_AGGREGATE_COUNT", autoTask.IvmFallbackReason)
+ order_qt_after_auto "SELECT category, row_count FROM ivm_negative_count_mv"
+
+ sql "INSERT INTO ivm_negative_count_fact VALUES (6, 3)"
+ refreshIvm("ivm_negative_count_mv", "REFRESH MATERIALIZED VIEW
ivm_negative_count_mv INCREMENTAL")
+ assertEquals("SUCCESS", latestIvmTask("ivm_negative_count_mv").Status)
+ order_qt_after_incremental "SELECT category, row_count FROM
ivm_negative_count_mv"
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]