@@ -419,13 +419,15 @@ Status CompactionMixin::do_compact_ordered_rowsets() {
419419 // link data to new rowset
420420 auto seg_id = 0 ;
421421 bool segments_key_bounds_truncated {false };
422+ bool any_input_aggregated {false };
422423 std::vector<KeyBoundsPB> segment_key_bounds;
423424 std::vector<uint32_t > num_segment_rows;
424425 for (auto rowset : _input_rowsets) {
425426 RETURN_IF_ERROR (rowset->link_files_to (tablet ()->tablet_path (),
426427 _output_rs_writer->rowset_id (), seg_id));
427428 seg_id += rowset->num_segments ();
428429 segments_key_bounds_truncated |= rowset->is_segments_key_bounds_truncated ();
430+ any_input_aggregated |= rowset->rowset_meta ()->is_segments_key_bounds_aggregated ();
429431 std::vector<KeyBoundsPB> key_bounds;
430432 RETURN_IF_ERROR (rowset->get_segments_key_bounds (&key_bounds));
431433 segment_key_bounds.insert (segment_key_bounds.end (), key_bounds.begin (), key_bounds.end ());
@@ -445,8 +447,12 @@ Status CompactionMixin::do_compact_ordered_rowsets() {
445447 rowset_meta->set_segments_overlap (NONOVERLAPPING);
446448 rowset_meta->set_rowset_state (VISIBLE);
447449 rowset_meta->set_segments_key_bounds_truncated (segments_key_bounds_truncated);
448- bool aggregate_key_bounds = config::enable_aggregate_non_mow_key_bounds &&
449- !_tablet->enable_unique_key_merge_on_write ();
450+ // If any input was already aggregated we have no way to recover per-segment
451+ // bounds, so force aggregation on the output to keep the layout consistent
452+ // with `num_segments` / the aggregated flag, even if the config is off now.
453+ bool aggregate_key_bounds =
454+ any_input_aggregated || (config::enable_aggregate_non_mow_key_bounds &&
455+ !_tablet->enable_unique_key_merge_on_write ());
450456 rowset_meta->set_segments_key_bounds (segment_key_bounds, aggregate_key_bounds);
451457 rowset_meta->set_num_segment_rows (num_segment_rows);
452458
0 commit comments