Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions crates/paimon/src/table/table_commit/auto_index_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,141 @@ async fn data_evolution_and_query_index_switches_do_not_gate_automatic_builds()
}
}

#[tokio::test]
async fn append_scalar_indexes_prune_rows_and_preserve_unindexed_matches() {
use crate::table::RowRange;

let table = table(Some(true), false)
.await
.copy_with_options(HashMap::from([(
"data-evolution.enabled".into(),
"false".into(),
)]));
TableCommit::new(table.clone(), "indexed".into())
.commit(messages(&table, vec![0, 3, 9]).await)
.await
.unwrap();
let unindexed = table.copy_with_options(HashMap::from([("write-only".into(), "true".into())]));
TableCommit::new(unindexed.clone(), "unindexed".into())
.commit(messages(&unindexed, vec![0, 3, 9]).await)
.await
.unwrap();

for mode in [None, Some("fast"), Some("full"), Some("detail")] {
let table = if let Some(mode) = mode {
table.copy_with_options(HashMap::from([(
"global-index.search-mode".into(),
mode.into(),
)]))
} else {
table.clone()
};
let fast = mode == Some("fast");
let mut read = table.new_read_builder();
read.with_filter(
crate::spec::PredicateBuilder::new(table.schema().fields())
.equal("id", Datum::Int(3))
.unwrap(),
);
let plan = read.new_scan().plan().await.unwrap();
let ranges = plan
.splits()
.iter()
.flat_map(|split| {
split
.row_ranges()
.expect("append scalar scans must produce indexed row ranges")
})
.cloned()
.collect::<Vec<_>>();
assert_eq!(
ranges,
if fast {
vec![RowRange::new(1, 1)]
} else {
vec![RowRange::new(1, 1), RowRange::new(3, 5)]
},
"{mode:?} must respect index coverage"
);
let batches: Vec<RecordBatch> = read
.new_read()
.unwrap()
.to_arrow(plan.splits())
.unwrap()
.try_collect()
.await
.unwrap();
let found = batches
.iter()
.flat_map(|batch| {
batch
.column_by_name("id")
.unwrap()
.as_any()
.downcast_ref::<Int32Array>()
.unwrap()
.values()
.to_vec()
})
.collect::<Vec<_>>();
assert_eq!(found, if fast { vec![3] } else { vec![3, 3] });
}
}

#[tokio::test]
async fn append_scalar_indexes_preserve_row_position_slices() {
let table = table(Some(true), false)
.await
.copy_with_options(HashMap::from([
("data-evolution.enabled".into(), "false".into()),
("source.split.target-size".into(), "1b".into()),
]));
let commit = TableCommit::new(table.clone(), "writer".into());
commit
.commit(messages(&table, vec![0, 4, 9]).await)
.await
.unwrap();
commit
.commit(messages(&table, vec![0, 3, 9]).await)
.await
.unwrap();
let mut read = table.new_read_builder();
read.with_filter(
crate::spec::PredicateBuilder::new(table.schema().fields())
.equal("id", Datum::Int(3))
.unwrap(),
);
let plan = read
.new_scan()
.with_row_position_slice(4, 5)
.unwrap()
.plan()
.await
.unwrap();
let batches: Vec<RecordBatch> = read
.new_read()
.unwrap()
.to_arrow(plan.splits())
.unwrap()
.try_collect()
.await
.unwrap();
let found = batches
.iter()
.flat_map(|batch| {
batch
.column_by_name("id")
.unwrap()
.as_any()
.downcast_ref::<Int32Array>()
.unwrap()
.values()
.to_vec()
})
.collect::<Vec<_>>();
assert_eq!(found, vec![3]);
}

#[tokio::test]
async fn append_only_adds_missing_ranges_and_empty_commits_do_nothing() {
let table = table(Some(true), false).await;
Expand Down
17 changes: 11 additions & 6 deletions crates/paimon/src/table/table_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,12 +861,12 @@ fn global_index_detail_data_ranges(entries: &[ManifestEntry]) -> Vec<RowRange> {

fn should_use_global_index_row_range_optimization(
row_range_optimization_disabled: bool,
data_evolution_enabled: bool,
supports_global_row_ranges: bool,
global_index_enabled: bool,
has_data_predicates: bool,
) -> bool {
!row_range_optimization_disabled
&& data_evolution_enabled
&& supports_global_row_ranges
&& global_index_enabled
&& has_data_predicates
}
Expand Down Expand Up @@ -2519,10 +2519,14 @@ impl<'a> PaimonTableScan<'a> {
core_options: &CoreOptions,
data_evolution_enabled: bool,
) -> crate::Result<Option<GlobalIndexScanSettings>> {
let supports_global_row_ranges = data_evolution_enabled
|| (self.table.schema().primary_keys().is_empty()
&& core_options.row_tracking_enabled()
&& core_options.bucket() == -1);
if !self.is_streaming()
&& should_use_global_index_row_range_optimization(
self.row_range_optimization_disabled,
data_evolution_enabled,
supports_global_row_ranges,
core_options.global_index_enabled(),
!self.data_predicates.is_empty(),
)
Expand Down Expand Up @@ -3375,9 +3379,10 @@ impl<'a> PaimonTableScan<'a> {

let split_row_ranges =
split_row_ranges_for_files(effective_row_ranges.as_deref(), &file_group)?;
if split_row_ranges
.as_deref()
.is_some_and(<[RowRange]>::is_empty)
if append_row_position_selection.is_none()
&& split_row_ranges
.as_deref()
.is_some_and(<[RowRange]>::is_empty)
{
continue;
}
Expand Down
Loading