Skip to content

Commit a90032a

Browse files
authored
fix(storage): analyze table get out of index (#18877)
* fix analyze table out of index * fix
1 parent 16c7380 commit a90032a

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/query/storages/fuse/src/operations/analyze/collect_ndv_source.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ impl AnalyzeCollectNDVSource {
142142
let block_reader =
143143
table.create_block_reader(ctx.clone(), projection, false, false, false)?;
144144

145+
// Rebuild `ndv_columns_map` so that keys correspond to the column order in the projection.
146+
//
147+
// The original `BTreeMap<FieldIndex, TableField>` is ordered by field index (ascending),
148+
// but after projection, columns are loaded in that order and accessed by *position*.
149+
// Therefore, we re-index it with `enumerate()` to align with block column offsets (0..N).
150+
let ndv_columns_map = ndv_columns_map.values().cloned().enumerate().collect();
145151
let dal = table.get_operator();
146152
let settings = ReadSettings::from_ctx(&ctx)?;
147153
let segment_reader = MetaReaders::segment_info_reader(dal.clone(), table_schema.clone());
@@ -359,7 +365,7 @@ impl Processor for AnalyzeCollectNDVSource {
359365

360366
let joint = futures::future::try_join_all(handlers).await.map_err(|e| {
361367
ErrorCode::StorageOther(format!(
362-
"[BLOCK-COMPACT] Failed to deserialize segment blocks: {}",
368+
"[ANALYZE-TABLE] Failed to build NDV statistics: {}",
363369
e
364370
))
365371
})?;

tests/sqllogictests/suites/base/09_fuse_engine/09_0020_analyze.test

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,16 @@ statement ok
220220
DROP TABLE t2;
221221

222222
statement ok
223-
CREATE TABLE t3(a int, b string) approx_distinct_columns = '';
223+
CREATE TABLE t3(a int, b string, c int) approx_distinct_columns = '';
224224

225225
statement ok
226-
insert into t3 values(1, 'a'), (2, 'b');
226+
insert into t3 values(1, 'a', 1), (2, 'b', 2);
227227

228228
statement ok
229229
alter table t3 set options(approx_distinct_columns = 'a, b');
230230

231231
statement ok
232-
insert into t3 values(4, 'c'), (3, 'b');
232+
insert into t3 values(4, 'c', 4), (3, 'b', 4);
233233

234234
query B
235235
select segment_stats_size is null from fuse_segment('db_09_0020','t3') order by file_location;
@@ -251,6 +251,10 @@ select column_name, distinct_count from fuse_statistic('db_09_0020', 't3') order
251251
----
252252
a 4
253253
b 3
254+
c 3
255+
256+
statement ok
257+
alter table t3 set options(approx_distinct_columns = 'a, c');
254258

255259
statement ok
256260
analyze table t3;
@@ -260,6 +264,14 @@ select count() from fuse_snapshot('db_09_0020', 't3');
260264
----
261265
4
262266

267+
statement ok
268+
analyze table t3;
269+
270+
query I
271+
select count() from fuse_snapshot('db_09_0020', 't3');
272+
----
273+
5
274+
263275
statement ok
264276
DROP TABLE t3;
265277

0 commit comments

Comments
 (0)