use super::{ColumnUse, IndexInfo, TableInfo, ROWID_ENTRY_SLOT};
pub(super) fn covering_slots(
table: &TableInfo,
index: &IndexInfo,
needed: &ColumnUse,
usable: bool,
) -> Option<Vec<(u16, usize)>> {
if needed.opaque || !usable || (table.without_rowid && index.root == table.root) {
return None;
}
let key = if table.without_rowid {
table.primary_key()
} else {
Vec::new()
};
let mut slots = Vec::with_capacity(needed.columns.len());
for slot in &needed.columns {
if table.rowid_alias == Some(*slot) && !table.without_rowid {
slots.push((*slot, ROWID_ENTRY_SLOT));
continue;
}
let held = index
.columns
.iter()
.position(|column| column.plain_column() == Some(*slot));
let position = match held {
Some(position) => position,
None if table.without_rowid => {
let offset = key.iter().position(|column| column == slot)?;
index.columns.len().saturating_add(offset)
}
None => return None,
};
slots.push((*slot, position));
}
Some(slots)
}