Skip to content

Commit 0276aff

Browse files
committed
[fix](ivm) Estimate a stream read's row count from the table it reads
### What problem does this PR solve? Issue Number: None Related PR: None Problem Summary: A scan that reads a table stream is planned over a lightweight wrapper of the base table: OlapTableWrapper for the base-table reads (stream@reset, and the base part of stream@snapshot), OlapChangelogTableWrapper for the changelog part. The wrapper delegates partition lookups but holds no partitions of its own, while OlapTable derives a row count by iterating that map. Every scan over a wrapper was therefore estimated at zero rows, which StatsCalculator.computeOlapScan raises to one: a read of a four-billion-row table looked like a one-row scan. In cloud mode NormalizeOlapTableStreamScan wraps the base table for every read once the cloud read states are installed, so both stream@reset() and stream@snapshot() were estimated at one row. An incremental view maintenance refresh plans its reads the same way. With the wrong estimate the optimizer picks the real four-hundred-million-row side as the broadcast build, and the refresh exhausts the task memory limit. Reproduction: one duplicate-key table with seven rows and one stream (show_initial_rows=false, five rows written before the stream and two after). Before this change, the plans estimated the base table at 7 rows, stream@reset() at 1 row in cloud (7 rows in shared-nothing), and stream@snapshot() at 1 row in both. Fix: - OlapTableWrapper delegates getRowCountForIndex, getRowCountForPartitionIndex, getPartitionNum and getPartitionItemOrAnalysisException to the table it wraps. A reset read, and a snapshot read whose offsets already reached the end, see exactly that table's content; a snapshot read of an older offset is bounded by it. With the partition count in place, a partition-pruned scan takes the statistics calculator's per-partition path, which looks up the selected partitions' items to tighten the partition-key min/max. - OlapChangelogTableWrapper does not guess. A changelog scan reads the change rows of its offset range, not the table; a read that starts at the beginning of the stream returns about the table's rows while a delta returns a few, and FE cannot tell the two apart. It keeps the row count unknown (UNKNOWN_ROW_COUNT) instead of the table's rows or zero. This is unchanged behaviour from before the delegation. Results on a local cloud cluster, with the seven-row table and stream, stats= from the query profile: base table 7 -> 7, stream@reset() 1 -> 7, stream@snapshot() 1 -> 7. The incremental read keeps its previous estimate. ### Release note Fix stream reads (including incremental view maintenance refreshes) estimating every scan at one row, which could make the optimizer broadcast a very large build side and exhaust memory during a refresh. ### Check List (For Author) - Test: Unit test / Regression test / Manual test - Unit test: OlapTableWrapperTest, three new cases (row count delegation; the changelog wrapper keeps the change-row count unknown instead of inheriting the table's row count; partition item lookup delegation). - Regression test: changelog_p0/test_stream_scan_row_count, which runs in both the shared-nothing and the cloud P0 pipelines and asserts the estimates with explain physical plan. - Manual test: the reproduction above, verified through query profiles on a local cloud cluster before and after the change. - Behavior changed: Yes. Scans over a reset or snapshot stream read are now estimated from the table they read instead of one row. - Does this need documentation: No
1 parent 2a4191c commit 0276aff

4 files changed

Lines changed: 198 additions & 0 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/catalog/OlapChangelogTableWrapper.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535

3636
/**
3737
* A lightweight wrapper base for read changelog of table
38+
*
39+
* <p>A changelog scan reads the change rows of an offset range, not the table, and neither the
40+
* range nor the changelog index reports how many change rows it holds: a read that starts at the
41+
* beginning of the stream returns about the table's rows while a delta returns a few, and FE
42+
* cannot tell the two apart. The estimate is therefore left unknown rather than guessed as the
43+
* table's rows or as zero.
3844
*/
3945
public class OlapChangelogTableWrapper extends OlapTableWrapper {
4046

@@ -62,6 +68,20 @@ public long getBaseIndexId() {
6268
return changelogIndexMeta.getIndexId();
6369
}
6470

71+
/**
72+
* The change rows of an offset range are not derivable from FE metadata, so the row count is
73+
* unknown rather than zero or the table's rows. See the class comment.
74+
*/
75+
@Override
76+
public long getRowCountForIndex(long indexId, boolean strict) {
77+
return UNKNOWN_ROW_COUNT;
78+
}
79+
80+
@Override
81+
public long getRowCountForPartitionIndex(long partitionId, long indexId, boolean strict) {
82+
return UNKNOWN_ROW_COUNT;
83+
}
84+
6585
public static boolean isChangelogSyntheticColumn(Column column) {
6686
return column.getName().equals(Column.CHANGELOG_TSO_COL)
6787
|| column.getName().equals(Column.CHANGELOG_LSN_COL);

‎fe/fe-core/src/main/java/org/apache/doris/catalog/OlapTableWrapper.java‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.doris.catalog;
1919

2020
import org.apache.doris.catalog.stream.StreamReadMode;
21+
import org.apache.doris.common.AnalysisException;
2122
import org.apache.doris.common.Pair;
2223

2324
import com.google.common.collect.ImmutableMap;
@@ -149,6 +150,11 @@ public Partition getPartition(long partitionId) {
149150
return originTable.getPartition(partitionId);
150151
}
151152

153+
@Override
154+
public PartitionItem getPartitionItemOrAnalysisException(String partitionName) throws AnalysisException {
155+
return originTable.getPartitionItemOrAnalysisException(partitionName);
156+
}
157+
152158
@Override
153159
public Set<String> getPartitionNames() {
154160
return originTable.getPartitionNames();
@@ -190,6 +196,33 @@ public MaterializedIndex getBaseIndex() {
190196
return originTable.getBaseIndex();
191197
}
192198

199+
/**
200+
* Row counts are derived from the partitions of the table, and this wrapper holds none of its
201+
* own -- it only delegates partition lookups to the table it wraps. The inherited methods
202+
* iterate the wrapper's private partition map, so every scan over a wrapper would be estimated
203+
* at zero rows, which the statistics calculator raises to one; a reset read of a
204+
* four-billion-row table would then look like a one-row build side to the optimizer.
205+
*
206+
* <p>Delegating answers the read with the table it is over: a reset read, and a snapshot read
207+
* whose offsets already reached the end, see exactly that table's content, and a snapshot read
208+
* of an older offset is bounded by it (the table only grows for the duplicate-key tables this
209+
* wrapper serves in that way).
210+
*/
211+
@Override
212+
public long getRowCountForIndex(long indexId, boolean strict) {
213+
return originTable.getRowCountForIndex(indexId, strict);
214+
}
215+
216+
@Override
217+
public long getRowCountForPartitionIndex(long partitionId, long indexId, boolean strict) {
218+
return originTable.getRowCountForPartitionIndex(partitionId, indexId, strict);
219+
}
220+
221+
@Override
222+
public int getPartitionNum() {
223+
return originTable.getPartitionNum();
224+
}
225+
193226
public Pair<Long, Long> getPartitionOffset(long partitionId) {
194227
return partitionOffsetMap.get(partitionId);
195228
}

‎fe/fe-core/src/test/java/org/apache/doris/catalog/OlapTableWrapperTest.java‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,59 @@ public void testCloudChangelogWrapperWithFixedVisibleVersionsUsesOriginTable() {
186186
mockedPartition.verifyNoInteractions();
187187
}
188188
}
189+
190+
@Test
191+
public void testRowCountIsDelegatedToTheWrappedTable() {
192+
OlapTable table = newTestTable(BinlogTestUtils.newTestChangelogConfig(false, false));
193+
long partitionId = 200L;
194+
long rowCount = 42L;
195+
MaterializedIndex baseIndex = new MaterializedIndex(table.getBaseIndexId(), null);
196+
baseIndex.setRowCount(rowCount);
197+
baseIndex.setRowCountReported(true);
198+
table.addPartition(new Partition(partitionId, "p1", baseIndex, new RandomDistributionInfo(32)));
199+
200+
OlapTableWrapper wrapper = new OlapTableWrapper(table);
201+
202+
// The wrapper owns no partitions, so the inherited implementations would answer 0 and the
203+
// statistics calculator would raise that to one row.
204+
Assertions.assertEquals(rowCount, wrapper.getRowCountForIndex(table.getBaseIndexId(), true));
205+
Assertions.assertEquals(rowCount,
206+
wrapper.getRowCountForPartitionIndex(partitionId, table.getBaseIndexId(), true));
207+
Assertions.assertEquals(table.getPartitionNum(), wrapper.getPartitionNum());
208+
}
209+
210+
@Test
211+
public void testPartitionItemLookupIsDelegatedToTheWrappedTable() throws Exception {
212+
OlapTable table = Mockito.spy(newTestTable(BinlogTestUtils.newTestChangelogConfig(false, false)));
213+
PartitionItem item = Mockito.mock(PartitionItem.class);
214+
Mockito.doReturn(item).when(table).getPartitionItemOrAnalysisException("p1");
215+
216+
OlapTableWrapper wrapper = new OlapTableWrapper(table);
217+
218+
// The inherited implementation reads the wrapper's own partition name map, which is empty,
219+
// and answers "partition not found" for every name.
220+
Assertions.assertSame(item, wrapper.getPartitionItemOrAnalysisException("p1"));
221+
}
222+
223+
@Test
224+
public void testChangelogWrapperKeepsTheChangeRowCountUnknown() {
225+
ChangelogConfig changelogConfig = BinlogTestUtils.newTestChangelogConfig(true, true);
226+
changelogConfig.setTtlSeconds(10);
227+
OlapTable table = newTestTable(changelogConfig);
228+
long partitionId = 300L;
229+
MaterializedIndex baseIndex = new MaterializedIndex(table.getBaseIndexId(), null);
230+
baseIndex.setRowCount(42L);
231+
baseIndex.setRowCountReported(true);
232+
table.addPartition(new Partition(partitionId, "p1", baseIndex, new RandomDistributionInfo(32)));
233+
234+
OlapChangelogTableWrapper wrapper = new OlapChangelogTableWrapper(table);
235+
236+
// A changelog scan reads the change rows of its offset range: that range is neither the
237+
// table's rows (a delta is much smaller) nor zero, so the estimate stays unknown instead
238+
// of being guessed from the table's row count, which is known here.
239+
Assertions.assertEquals(TableIf.UNKNOWN_ROW_COUNT,
240+
wrapper.getRowCountForIndex(wrapper.getBaseIndexId(), true));
241+
Assertions.assertEquals(TableIf.UNKNOWN_ROW_COUNT,
242+
wrapper.getRowCountForPartitionIndex(partitionId, wrapper.getBaseIndexId(), true));
243+
}
189244
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
import org.awaitility.Awaitility
19+
20+
import static java.util.concurrent.TimeUnit.SECONDS
21+
22+
/**
23+
* The optimizer must estimate a stream read from the table it reads, not at one row.
24+
*
25+
* <p>A stream read is planned over a lightweight wrapper of the base table. The wrapper holds no
26+
* partitions of its own, so a row count derived from its own partitions collapsed to zero and the
27+
* statistics calculator raised that to one: a stream read of a large table looked like a one-row
28+
* scan, and the optimizer could pick the real large side as a broadcast build.
29+
*
30+
* <p>This suite runs in both shared-nothing and cloud mode, and that matters: in cloud every read
31+
* of a stream wraps the base table once the cloud read states are installed, so even a reset read
32+
* goes through the wrapper; in shared-nothing a reset read scans the real table and only a
33+
* snapshot read uses a wrapper. Both must be estimated from the table.
34+
*/
35+
suite("test_stream_scan_row_count") {
36+
sql "DROP STREAM IF EXISTS stream_read_rowcount_stream FORCE"
37+
sql "DROP TABLE IF EXISTS stream_read_rowcount_tbl"
38+
sql """
39+
CREATE TABLE stream_read_rowcount_tbl (
40+
id BIGINT NOT NULL,
41+
v BIGINT
42+
)
43+
DUPLICATE KEY(id)
44+
DISTRIBUTED BY HASH(id) BUCKETS 1
45+
PROPERTIES (
46+
"replication_num" = "1",
47+
"changelog.enable" = "true",
48+
"changelog.ttl_seconds" = "86400"
49+
)
50+
"""
51+
// Five rows exist when the stream is created; the last two are written afterwards, so the
52+
// snapshot image holds five rows while the table holds seven. The syncs make sure the rows are
53+
// published, so the stream records their offsets rather than an unset one.
54+
sql "INSERT INTO stream_read_rowcount_tbl VALUES (1,10),(2,20),(3,30),(4,40),(5,50)"
55+
sql "sync"
56+
sql """
57+
CREATE STREAM stream_read_rowcount_stream ON TABLE stream_read_rowcount_tbl
58+
PROPERTIES ('show_initial_rows'='false')
59+
"""
60+
sql "INSERT INTO stream_read_rowcount_tbl VALUES (6,60),(7,70)"
61+
sql "sync"
62+
63+
// The estimated row count of the base table's scan in the physical plan.
64+
def scanRowCount = { String query ->
65+
def planLines = sql "explain physical plan ${query}"
66+
def scanLine = planLines.collect { it[0] }.find {
67+
it.contains("PhysicalOlapScan") && it.contains("table=stream_read_rowcount_tbl")
68+
}
69+
assertTrue(scanLine != null, "no scan of stream_read_rowcount_tbl in the plan of [${query}]: ${planLines}")
70+
def stats = (scanLine =~ /stats=([0-9]+)/)
71+
assertTrue(stats.find(), "no row count in the scan line: ${scanLine}")
72+
return stats.group(1) as long
73+
}
74+
75+
// Tablet row counts reach the FE asynchronously. Wait for the base table itself to be
76+
// estimated at its seven rows before comparing the stream reads with it; an estimate of one
77+
// on the base table would come from unreported rows, not from this problem.
78+
Awaitility.await().atMost(180, SECONDS).pollInterval(2, SECONDS).until({
79+
scanRowCount("select * from stream_read_rowcount_tbl") == 7L
80+
})
81+
82+
assertEquals(7L, scanRowCount("select * from stream_read_rowcount_tbl"))
83+
84+
// stream@reset() reads the table as it is now, in both modes.
85+
assertEquals(7L, scanRowCount("select * from stream_read_rowcount_stream@reset()"))
86+
87+
// stream@snapshot() answers with the image at the stream's offset (five rows). Its estimate is
88+
// the current table, an upper bound by design -- but it must not collapse to one row.
89+
assertEquals(7L, scanRowCount("select * from stream_read_rowcount_stream@snapshot()"))
90+
}

0 commit comments

Comments
 (0)