mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2026-11-20 17:44:15 +00:00
Trim the jitexpr input column API
Remove the one-line accessors with a single caller: read the `var_type`, `column` and `first_values` fields directly (visible to the jitexpr value source module only), and fold `value_for_doc` into `value_of`, which now takes an `Option<u64>` and returns null for `None`. `InputColumn` goes from 11 to 6 methods. No behavior change.
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
//! The fast-field columns bound to the inputs of a compiled jitexpr expression.
|
||||
|
||||
use columnar::{
|
||||
Column, ColumnIndex, DynamicColumn, DynamicColumnHandle, MonotonicallyMappableToU64,
|
||||
};
|
||||
use columnar::{Column, DynamicColumn, DynamicColumnHandle, MonotonicallyMappableToU64};
|
||||
use jitexpr::types::{VarType, VariableValue};
|
||||
|
||||
use super::term_resolver::TermResolver;
|
||||
@@ -11,11 +9,12 @@ use crate::{DocId, TantivyError, COLLECT_BLOCK_BUFFER_LEN};
|
||||
|
||||
/// A fast-field column bound to an input of the compiled expression.
|
||||
pub(super) struct InputColumn {
|
||||
var_type: VarType,
|
||||
/// Values in their monotonic `u64` mapping. Term ords for a `Str` input.
|
||||
column: Column<u64>,
|
||||
pub(super) var_type: VarType,
|
||||
/// Values in their monotonic `u64` mapping. Term ords for a `Str` input. Its index tells
|
||||
/// which docs have a value.
|
||||
pub(super) column: Column<u64>,
|
||||
/// `first_values[i]` is the first value of `docs[i]`, for the docs of the last block.
|
||||
first_values: Vec<Option<u64>>,
|
||||
pub(super) first_values: Vec<Option<u64>>,
|
||||
/// The terms of `first_values`. Not `TermResolver::None` if and only if `var_type` is `Str`.
|
||||
term_resolver: TermResolver,
|
||||
}
|
||||
@@ -57,20 +56,17 @@ impl InputColumn {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn var_type(&self) -> VarType {
|
||||
self.var_type
|
||||
}
|
||||
|
||||
/// Returns the number of terms of the dictionary of a `Str` input, or `None` for the other
|
||||
/// types.
|
||||
pub(super) fn num_terms(&self) -> Option<usize> {
|
||||
self.term_resolver.num_terms()
|
||||
}
|
||||
|
||||
/// Loads the first value of each doc of `docs`, without resolving their terms.
|
||||
/// Loads the first value of each doc of `docs` in `first_values`, without resolving their
|
||||
/// terms.
|
||||
///
|
||||
/// Afterwards, read the values with `first_value`, and their terms with `value_of`: `value`
|
||||
/// must not be called before the next `load_block`.
|
||||
/// Afterwards, resolve the terms with `value_of`: `value` must not be called before the next
|
||||
/// `load_block`.
|
||||
pub(super) fn load_block_values(&mut self, docs: &[DocId]) {
|
||||
// `first_vals` does not write the entries of the docs without a value: we reset them.
|
||||
self.first_values.clear();
|
||||
@@ -78,20 +74,6 @@ impl InputColumn {
|
||||
self.column.first_vals(docs, &mut self.first_values);
|
||||
}
|
||||
|
||||
/// Returns the first value of `docs[doc_pos]` in its monotonic `u64` mapping (a term ord for
|
||||
/// a `Str` input), for the `docs` of the last `load_block_values` call.
|
||||
#[inline]
|
||||
pub(super) fn first_value(&self, doc_pos: usize) -> Option<u64> {
|
||||
self.first_values[doc_pos]
|
||||
}
|
||||
|
||||
/// Returns the first value of `doc` in its monotonic `u64` mapping, without going through a
|
||||
/// block.
|
||||
#[inline]
|
||||
pub(super) fn first_value_for_doc(&self, doc: DocId) -> Option<u64> {
|
||||
self.column.first(doc)
|
||||
}
|
||||
|
||||
/// Loads the first value of each doc of `docs`.
|
||||
///
|
||||
/// Panics if the dictionary of a text column is corrupted.
|
||||
@@ -116,29 +98,18 @@ impl InputColumn {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the value of `doc`, without going through `load_block`.
|
||||
/// Returns the variable value of `value`, a value of the column in its monotonic `u64`
|
||||
/// mapping, or null. Resolves the term of a `Str` input outside of the block path.
|
||||
///
|
||||
/// Invalidates the last `load_block` call: `value` must not be called before the next
|
||||
/// `load_block`.
|
||||
///
|
||||
/// Panics if the dictionary of a text column is corrupted.
|
||||
#[inline]
|
||||
pub(super) fn value_for_doc(&mut self, doc: DocId) -> VariableValue<'_> {
|
||||
let Some(value) = self.column.first(doc) else {
|
||||
pub(super) fn value_of(&mut self, value: Option<u64>) -> VariableValue<'_> {
|
||||
let Some(value) = value else {
|
||||
return VariableValue::none();
|
||||
};
|
||||
self.value_of(value)
|
||||
}
|
||||
|
||||
/// Returns the variable value of `value`, a value of the column in its monotonic `u64`
|
||||
/// mapping. Resolves the term of a `Str` input outside of the block path.
|
||||
///
|
||||
/// Invalidates the last `load_block` call: `value` must not be called before the next
|
||||
/// `load_block`.
|
||||
///
|
||||
/// Panics if the dictionary of a text column is corrupted.
|
||||
#[inline]
|
||||
pub(super) fn value_of(&mut self, value: u64) -> VariableValue<'_> {
|
||||
match self.var_type {
|
||||
VarType::Bool => VariableValue::from(bool::from_u64(value)),
|
||||
VarType::I64 => VariableValue::from(i64::from_u64(value)),
|
||||
@@ -148,9 +119,4 @@ impl InputColumn {
|
||||
VarType::None => VariableValue::none(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the index of the column: which docs have a value.
|
||||
pub(super) fn column_index(&self) -> &ColumnIndex {
|
||||
&self.column.index
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,10 +150,7 @@ fn eval_single_input(
|
||||
) -> Option<u64> {
|
||||
let result_type: VarType = compiled_fn.result_type();
|
||||
let mut args = args.borrow::<VariableValue>();
|
||||
args.push(match input_value {
|
||||
Some(value) => input.value_of(value),
|
||||
None => VariableValue::none(),
|
||||
});
|
||||
args.push(input.value_of(input_value));
|
||||
// SAFETY: `args` follow the compiled inputs, with their types (precondition). The string is
|
||||
// borrowed from `input`, which is not modified during the call.
|
||||
let result: VariableValue = unsafe { compiled_fn.call(&args) };
|
||||
@@ -173,7 +170,7 @@ impl JitExprValueSource {
|
||||
// Evaluating the function with inputs of the wrong type is undefined behavior.
|
||||
assert_eq!(compiled_fn.inputs().len(), inputs.len());
|
||||
for (compiled_input, input) in compiled_fn.inputs().iter().zip(&inputs) {
|
||||
assert_eq!(compiled_input.r#type, input.var_type());
|
||||
assert_eq!(compiled_input.r#type, input.var_type);
|
||||
}
|
||||
let num_inputs = inputs.len();
|
||||
let evaluation = match inputs.as_slice() {
|
||||
@@ -213,7 +210,7 @@ impl JitExprValueSource {
|
||||
} = self;
|
||||
if let Evaluation::PerTermOrd(results) = evaluation {
|
||||
let input = &mut inputs[0];
|
||||
let input_value = input.first_value_for_doc(doc);
|
||||
let input_value = input.column.first(doc);
|
||||
return results.get_or_eval(input_value, || {
|
||||
eval_single_input(compiled_fn, args, term_dictionary, input, input_value)
|
||||
});
|
||||
@@ -222,7 +219,7 @@ impl JitExprValueSource {
|
||||
let mut args = args.borrow::<VariableValue>();
|
||||
// Each input is borrowed once: the values can borrow their input.
|
||||
for input in inputs.iter_mut() {
|
||||
args.push(input.value_for_doc(doc));
|
||||
args.push(input.value_of(input.column.first(doc)));
|
||||
}
|
||||
// SAFETY: `args` follow the compiled inputs, with their types (see `new`). The strings
|
||||
// are borrowed from `inputs`, which are not modified during the call.
|
||||
@@ -238,7 +235,7 @@ impl JitExprValueSource {
|
||||
.iter()
|
||||
.zip(&self.inputs)
|
||||
.map(|(compiled_input, input)| {
|
||||
(compiled_input.variable_name.as_ref(), input.column_index())
|
||||
(compiled_input.variable_name.as_ref(), &input.column.index)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -280,7 +277,7 @@ impl ValueSource for JitExprValueSource {
|
||||
// Terms are only resolved for the term ords evaluated for the first time.
|
||||
input.load_block_values(docs);
|
||||
for (doc_pos, &doc) in docs.iter().enumerate() {
|
||||
let input_value = input.first_value(doc_pos);
|
||||
let input_value = input.first_values[doc_pos];
|
||||
let value_opt = results.get_or_eval(input_value, || {
|
||||
eval_single_input(compiled_fn, args, term_dictionary, input, input_value)
|
||||
});
|
||||
|
||||
@@ -186,7 +186,7 @@ impl JitExprValueSourceProvider {
|
||||
)));
|
||||
};
|
||||
let input = InputColumn::open(&handle)?;
|
||||
if input.var_type() != compiled_input.r#type {
|
||||
if input.var_type != compiled_input.r#type {
|
||||
return Err(TantivyError::InternalError(format!(
|
||||
"the compiled input `{}` expects {:?}, but its column has type {}",
|
||||
compiled_input.variable_name,
|
||||
|
||||
Reference in New Issue
Block a user