use arrow::datatypes::TimeUnit;
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum FieldType {
String,
Int64,
Float64,
Boolean,
Dictionary,
Date32,
Timestamp(TimeUnit),
}
impl FieldType {
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Option<Self> {
match s {
"string" => Some(FieldType::String),
"int64" => Some(FieldType::Int64),
"float64" => Some(FieldType::Float64),
"bool" | "boolean" => Some(FieldType::Boolean),
"dictionary" => Some(FieldType::Dictionary),
"date32" => Some(FieldType::Date32),
"timestamp" => Some(FieldType::Timestamp(TimeUnit::Microsecond)),
"timestamp[s]" => Some(FieldType::Timestamp(TimeUnit::Second)),
"timestamp[ms]" => Some(FieldType::Timestamp(TimeUnit::Millisecond)),
"timestamp[us]" | "timestamp[µs]" => Some(FieldType::Timestamp(TimeUnit::Microsecond)),
"timestamp[ns]" => Some(FieldType::Timestamp(TimeUnit::Nanosecond)),
_ => None,
}
}
}
#[derive(Clone, Debug, Default)]
pub struct ExecutionPlan {
pub field_map: HashMap<String, String>,
pub drop_fields: HashSet<String>,
pub field_types: HashMap<String, FieldType>,
pub dictionary_columns: HashSet<String>,
pub filter: Option<FilterPredicate>,
pub schema_order: Vec<String>,
pub auto_dict: bool,
pub dict_threshold: Option<f64>,
pub dict_max_size: Option<usize>,
}
impl ExecutionPlan {
pub fn new() -> Self {
Self::default()
}
pub fn rename(mut self, raw: impl Into<String>, output: impl Into<String>) -> Self {
self.field_map.insert(raw.into(), output.into());
self
}
pub fn drop(mut self, field: impl Into<String>) -> Self {
self.drop_fields.insert(field.into());
self
}
pub fn drop_many<I, S>(mut self, fields: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.drop_fields.extend(fields.into_iter().map(Into::into));
self
}
pub fn type_as(mut self, field: impl Into<String>, field_type: FieldType) -> Self {
self.field_types.insert(field.into(), field_type);
self
}
pub fn dictionary(mut self, field: impl Into<String>) -> Self {
self.dictionary_columns.insert(field.into());
self
}
pub fn filter_eq(mut self, field: impl Into<String>, value: impl Into<String>) -> Self {
self.filter = Some(FilterPredicate::Equal {
field: field.into(),
value: value.into(),
});
self
}
pub fn filter_ne(mut self, field: impl Into<String>, value: impl Into<String>) -> Self {
self.filter = Some(FilterPredicate::NotEqual {
field: field.into(),
value: value.into(),
});
self
}
pub fn filter_compare(
mut self,
field_a: impl Into<String>,
op: CompareOp,
field_b: impl Into<String>,
) -> Self {
self.filter = Some(FilterPredicate::Compare {
field_a: field_a.into(),
op,
field_b: field_b.into(),
});
self
}
pub fn schema_order<I, S>(mut self, order: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.schema_order = order.into_iter().map(Into::into).collect();
self
}
pub fn with_auto_dict(mut self, yes: bool) -> Self {
self.auto_dict = yes;
self
}
pub fn with_dict_threshold(mut self, threshold: f64) -> Self {
self.dict_threshold = Some(threshold);
self
}
pub fn with_dict_max_size(mut self, max_size: usize) -> Self {
self.dict_max_size = Some(max_size);
self
}
pub fn column_type(&self, name: &str) -> FieldType {
if let Some(ft) = self.field_types.get(name) {
return ft.clone();
}
if self.dictionary_columns.contains(name) {
return FieldType::Dictionary;
}
FieldType::String
}
pub fn resolve_field<'a>(&'a self, raw: &'a str) -> Option<&'a str> {
let resolved = self.field_map.get(raw).map_or(raw, |s| s.as_str());
if self.drop_fields.contains(resolved) {
return None;
}
Some(resolved)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CompareOp {
Gt,
Lt,
Ge,
Le,
Eq,
Ne,
}
impl CompareOp {
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Option<Self> {
match s {
">" | "gt" => Some(CompareOp::Gt),
"<" | "lt" => Some(CompareOp::Lt),
">=" | "ge" => Some(CompareOp::Ge),
"<=" | "le" => Some(CompareOp::Le),
"==" | "eq" => Some(CompareOp::Eq),
"!=" | "ne" => Some(CompareOp::Ne),
_ => None,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum FilterPredicate {
NotEqual { field: String, value: String },
Equal { field: String, value: String },
Compare {
field_a: String,
op: CompareOp,
field_b: String,
},
And(Box<FilterPredicate>, Box<FilterPredicate>),
Or(Box<FilterPredicate>, Box<FilterPredicate>),
Not(Box<FilterPredicate>),
}
impl FilterPredicate {
pub fn all(a: FilterPredicate, b: FilterPredicate) -> Self {
FilterPredicate::And(Box::new(a), Box::new(b))
}
pub fn any(a: FilterPredicate, b: FilterPredicate) -> Self {
FilterPredicate::Or(Box::new(a), Box::new(b))
}
#[allow(clippy::should_implement_trait)]
pub fn not(inner: FilterPredicate) -> Self {
FilterPredicate::Not(Box::new(inner))
}
}
impl FilterPredicate {
pub(crate) fn check(
&self,
columns: &[crate::columnar::ColumnBuilder],
field_index: &HashMap<String, usize>,
row_index: usize,
plan: &ExecutionPlan,
) -> bool {
match self {
FilterPredicate::NotEqual { field, value } => {
let actual = get_value(columns, field_index, field, plan, row_index);
actual.as_deref() != Some(value.as_str())
}
FilterPredicate::Equal { field, value } => {
let actual = get_value(columns, field_index, field, plan, row_index);
actual.as_deref() == Some(value.as_str())
}
FilterPredicate::Compare {
field_a,
op,
field_b,
} => {
let va = get_column(columns, field_index, resolve(field_a, plan))
.and_then(|b| b.get_typed_value(row_index));
let vb = get_column(columns, field_index, resolve(field_b, plan))
.and_then(|b| b.get_typed_value(row_index));
match (va, vb) {
(Some(a), Some(b)) => compare_typed(&a, *op, &b),
_ => false,
}
}
FilterPredicate::And(a, b) => {
let (first, second) =
if pred_ordinal(a, field_index, plan) <= pred_ordinal(b, field_index, plan) {
(a.as_ref(), b.as_ref())
} else {
(b.as_ref(), a.as_ref())
};
first.check(columns, field_index, row_index, plan)
&& second.check(columns, field_index, row_index, plan)
}
FilterPredicate::Or(a, b) => {
let (first, second) =
if pred_ordinal(a, field_index, plan) <= pred_ordinal(b, field_index, plan) {
(a.as_ref(), b.as_ref())
} else {
(b.as_ref(), a.as_ref())
};
first.check(columns, field_index, row_index, plan)
|| second.check(columns, field_index, row_index, plan)
}
FilterPredicate::Not(inner) => !inner.check(columns, field_index, row_index, plan),
}
}
}
fn resolve<'a>(field: &'a str, plan: &'a ExecutionPlan) -> &'a str {
plan.field_map.get(field).map_or(field, |s| s.as_str())
}
fn get_column<'a>(
columns: &'a [crate::columnar::ColumnBuilder],
field_index: &HashMap<String, usize>,
name: &str,
) -> Option<&'a crate::columnar::ColumnBuilder> {
field_index.get(name).map(|&i| &columns[i])
}
fn get_value(
columns: &[crate::columnar::ColumnBuilder],
field_index: &HashMap<String, usize>,
field: &str,
plan: &ExecutionPlan,
row_index: usize,
) -> Option<String> {
let col = get_column(columns, field_index, resolve(field, plan))?;
if let Some(s) = col.get_filter_view(row_index) {
return Some(s.to_owned());
}
col.get_filter_value(row_index)
}
fn compare_typed(
a: &crate::columnar::TypedValue<'_>,
op: CompareOp,
b: &crate::columnar::TypedValue<'_>,
) -> bool {
use crate::columnar::TypedValue as T;
let num_pair: Option<(f64, f64)> = match (a, b) {
(T::Int64(x), T::Int64(y)) => Some((*x as f64, *y as f64)),
(T::Float64(x), T::Float64(y)) => Some((*x, *y)),
(T::Int64(x), T::Float64(y)) | (T::Float64(y), T::Int64(x)) => Some((*x as f64, *y)),
_ => None,
};
if let Some((x, y)) = num_pair {
return apply_op(op, x.partial_cmp(&y));
}
match (a, b) {
(T::Str(x), T::Str(y)) => apply_op(op, x.cmp(y).into()),
(T::Bool(x), T::Bool(y)) => apply_op(op, x.cmp(y).into()),
(T::Date32(x), T::Date32(y)) => apply_op(op, x.cmp(y).into()),
(T::Timestamp(x), T::Timestamp(y)) => apply_op(op, x.cmp(y).into()),
_ => false,
}
}
fn apply_op(op: CompareOp, ord: Option<std::cmp::Ordering>) -> bool {
use std::cmp::Ordering::*;
let Some(ord) = ord else { return false };
match op {
CompareOp::Gt => ord == Greater,
CompareOp::Lt => ord == Less,
CompareOp::Ge => ord != Less,
CompareOp::Le => ord != Greater,
CompareOp::Eq => ord == Equal,
CompareOp::Ne => ord != Equal,
}
}
fn pred_ordinal(
pred: &FilterPredicate,
field_index: &HashMap<String, usize>,
plan: &ExecutionPlan,
) -> usize {
match pred {
FilterPredicate::Equal { field, .. } | FilterPredicate::NotEqual { field, .. } => {
field_index
.get(resolve(field, plan))
.copied()
.unwrap_or(usize::MAX)
}
FilterPredicate::Compare {
field_a, field_b, ..
} => {
let a = field_index
.get(resolve(field_a, plan))
.copied()
.unwrap_or(usize::MAX);
let b = field_index
.get(resolve(field_b, plan))
.copied()
.unwrap_or(usize::MAX);
a.min(b)
}
FilterPredicate::And(a, b) | FilterPredicate::Or(a, b) => {
pred_ordinal(a, field_index, plan).min(pred_ordinal(b, field_index, plan))
}
FilterPredicate::Not(inner) => pred_ordinal(inner, field_index, plan),
}
}