use super::ResolvedBoolColumn;
use crate::core::SymbolId;
use datafusion::arrow::array::{Array, BooleanArray};
use datafusion_common::Result;
use std::ops::Range;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct RowLocation {
pub(crate) local_row: usize,
}
pub(crate) trait BoolColumnProvider {
fn locate_with_work(
&self,
row: usize,
completed: &mut dyn FnMut(usize) -> Result<()>,
) -> Result<Option<RowLocation>>;
fn get_bool_with_loc(&self, symbol_id: SymbolId, loc: RowLocation) -> bool;
fn get_bool_from_col_with_loc(
&self,
column: ResolvedBoolColumn,
loc: RowLocation,
) -> bool;
fn precheck_is_false(&self, _column: ResolvedBoolColumn, _loc: RowLocation) -> bool {
false
}
fn symbol_is_present(
&self,
symbol_id: SymbolId,
completed: &mut impl FnMut(usize) -> Result<()>,
) -> Result<bool> {
self.symbol_is_present_in_range(symbol_id, 0..self.num_rows(), completed)
}
fn symbol_is_present_in_range(
&self,
symbol_id: SymbolId,
range: Range<usize>,
completed: &mut impl FnMut(usize) -> Result<()>,
) -> Result<bool> {
if range.start > range.end || range.end > self.num_rows() {
return datafusion_common::internal_err!(
"MATCH_RECOGNIZE presence range {range:?} is outside its Boolean provider"
);
}
for row in range {
let location = self.locate_with_work(row, completed)?.ok_or_else(|| {
datafusion_common::internal_datafusion_err!(
"MATCH_RECOGNIZE absence scan row is missing from its Boolean provider"
)
})?;
let present = self.get_bool_with_loc(symbol_id, location);
completed(1)?;
if present {
return Ok(true);
}
}
Ok(false)
}
fn num_rows(&self) -> usize;
}
#[inline(always)]
pub(crate) fn is_true(column: &BooleanArray, row: usize) -> bool {
column.is_valid(row) && column.value(row)
}
#[inline]
pub(super) fn packed_union(words: &[u64]) -> u64 {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
if std::is_x86_feature_detected!("avx2") {
return unsafe { avx2_union(words) };
}
words.iter().fold(0, |union, word| union | word)
}
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[target_feature(enable = "avx2")]
unsafe fn avx2_union(words: &[u64]) -> u64 {
words.iter().fold(0, |union, word| union | word)
}