|
| 1 | +use arrow::datatypes::{DataType, Field}; |
| 2 | +use datafusion::common::{DFSchema, ExprSchema as _}; |
| 3 | +use datafusion::prelude::{Column, Expr, array_has, array_to_string, col, contains, lit, lower}; |
| 4 | + |
| 5 | +#[derive(Debug, Clone, thiserror::Error)] |
| 6 | +pub enum FilterError { |
| 7 | + #[error("column {0} was not found")] |
| 8 | + ColumnNotFound(Column), |
| 9 | + |
| 10 | + #[error("invalid filter operation {0:?} for field {1}")] |
| 11 | + InvalidFilterOperation(FilterOperation, Box<Field>), |
| 12 | +} |
| 13 | + |
| 14 | +/// A filter applied to a table. |
| 15 | +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)] |
| 16 | +pub struct Filter { |
| 17 | + pub column_name: String, |
| 18 | + pub operation: FilterOperation, |
| 19 | +} |
| 20 | + |
| 21 | +impl Filter { |
| 22 | + pub fn new(column_name: impl Into<String>, operation: FilterOperation) -> Self { |
| 23 | + Self { |
| 24 | + column_name: column_name.into(), |
| 25 | + operation, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Convert to an [`Expr`]. |
| 30 | + /// |
| 31 | + /// The expression is used for filtering and should thus evaluate to a boolean. |
| 32 | + pub fn as_filter_expression(&self, schema: &DFSchema) -> Result<Expr, FilterError> { |
| 33 | + let column = Column::from(self.column_name.clone()); |
| 34 | + let Ok(field) = schema.field_from_column(&column) else { |
| 35 | + return Err(FilterError::ColumnNotFound(column)); |
| 36 | + }; |
| 37 | + |
| 38 | + self.operation.as_filter_expression(&column, field) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// The kind of filter operation |
| 43 | +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)] |
| 44 | +pub enum FilterOperation { |
| 45 | + //TODO(ab): parameterise that over multiple string ops, e.g. "contains", "starts with", etc. |
| 46 | + StringContains(String), |
| 47 | + |
| 48 | + BooleanEquals(bool), |
| 49 | +} |
| 50 | + |
| 51 | +impl FilterOperation { |
| 52 | + pub fn default_for_datatype(data_type: &DataType) -> Option<Self> { |
| 53 | + match data_type { |
| 54 | + DataType::Utf8 | DataType::Utf8View => Some(Self::StringContains(String::new())), |
| 55 | + DataType::List(field) | DataType::ListView(field) |
| 56 | + if field.data_type() == &DataType::Utf8 |
| 57 | + || field.data_type() == &DataType::Utf8View => |
| 58 | + { |
| 59 | + Some(Self::StringContains(String::new())) |
| 60 | + } |
| 61 | + |
| 62 | + DataType::Boolean => Some(Self::BooleanEquals(true)), |
| 63 | + DataType::List(fields) | DataType::ListView(fields) |
| 64 | + if fields.data_type() == &DataType::Boolean => |
| 65 | + { |
| 66 | + Some(Self::BooleanEquals(true)) |
| 67 | + } |
| 68 | + |
| 69 | + _ => None, |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /// Convert to an [`Expr`]. |
| 74 | + /// |
| 75 | + /// The expression is used for filtering and should thus evaluate to a boolean. |
| 76 | + pub fn as_filter_expression( |
| 77 | + &self, |
| 78 | + column: &Column, |
| 79 | + field: &Field, |
| 80 | + ) -> Result<Expr, FilterError> { |
| 81 | + match self { |
| 82 | + Self::StringContains(query_string) => { |
| 83 | + if query_string.is_empty() { |
| 84 | + return Ok(lit(true)); |
| 85 | + } |
| 86 | + |
| 87 | + let operand = match field.data_type() { |
| 88 | + DataType::Utf8 | DataType::Utf8View => col(column.clone()), |
| 89 | + |
| 90 | + DataType::List(field) | DataType::ListView(field) |
| 91 | + if field.data_type() == &DataType::Utf8 |
| 92 | + || field.data_type() == &DataType::Utf8View => |
| 93 | + { |
| 94 | + // for List[Utf8], we concatenate all the instances into a single logical |
| 95 | + // string |
| 96 | + array_to_string(col(column.clone()), lit(" ")) |
| 97 | + } |
| 98 | + |
| 99 | + _ => { |
| 100 | + return Err(FilterError::InvalidFilterOperation( |
| 101 | + self.clone(), |
| 102 | + field.clone().into(), |
| 103 | + )); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + Ok(contains(lower(operand), lower(lit(query_string)))) |
| 108 | + } |
| 109 | + |
| 110 | + Self::BooleanEquals(value) => match field.data_type() { |
| 111 | + DataType::Boolean => Ok(col(column.clone()).eq(lit(*value))), |
| 112 | + |
| 113 | + DataType::List(field) | DataType::ListView(field) |
| 114 | + if field.data_type() == &DataType::Boolean => |
| 115 | + { |
| 116 | + // all instances must be equal to the filter value |
| 117 | + Ok(!array_has(col(column.clone()), lit(!*value))) |
| 118 | + } |
| 119 | + |
| 120 | + _ => Err(FilterError::InvalidFilterOperation( |
| 121 | + self.clone(), |
| 122 | + field.clone().into(), |
| 123 | + )), |
| 124 | + }, |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments