-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from Fedomn/logical-filter
feat(planner): support logical filter
- Loading branch information
Showing
38 changed files
with
576 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use arrow::array::{Array, BooleanArray}; | ||
|
||
use crate::function::FunctionError; | ||
|
||
/// Downcast an Arrow Array to a concrete type | ||
macro_rules! downcast_value { | ||
($Value:expr, $Type:ident) => {{ | ||
use std::any::type_name; | ||
$Value.as_any().downcast_ref::<$Type>().ok_or_else(|| { | ||
FunctionError::CastError(format!("could not cast value to {}", type_name::<$Type>())) | ||
})? | ||
}}; | ||
} | ||
|
||
/// Downcast ArrayRef to BooleanArray | ||
pub fn as_boolean_array(array: &dyn Array) -> Result<&BooleanArray, FunctionError> { | ||
Ok(downcast_value!(array, BooleanArray)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod cast; | ||
mod create_info; | ||
|
||
pub use cast::*; | ||
pub use create_info::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use super::{PhysicalOperator, PhysicalOperatorBase}; | ||
use crate::execution::PhysicalPlanGenerator; | ||
use crate::planner_v2::{BoundConjunctionExpression, LogicalFilter}; | ||
|
||
#[derive(Clone)] | ||
pub struct PhysicalFilter { | ||
pub(crate) base: PhysicalOperatorBase, | ||
} | ||
|
||
impl PhysicalFilter { | ||
pub fn new(mut base: PhysicalOperatorBase) -> Self { | ||
let expression = | ||
BoundConjunctionExpression::try_build_and_conjunction_expression(base.expressioins); | ||
base.expressioins = vec![expression]; | ||
Self { base } | ||
} | ||
} | ||
|
||
impl PhysicalPlanGenerator { | ||
pub(crate) fn create_physical_filter(&self, op: LogicalFilter) -> PhysicalOperator { | ||
assert!(op.base.children.len() == 1); | ||
let base = self.create_physical_operator_base(op.base); | ||
PhysicalOperator::PhysicalFilter(PhysicalFilter::new(base)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,25 @@ | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
|
||
use arrow::datatypes::{Field, Schema, SchemaRef}; | ||
use arrow::datatypes::{DataType, Field, Schema, SchemaRef}; | ||
use arrow::record_batch::RecordBatch; | ||
use derive_new::new; | ||
use futures_async_stream::try_stream; | ||
|
||
use crate::execution::{ExecutionContext, ExecutorError, PhysicalDummyScan}; | ||
use crate::types_v2::ScalarValue; | ||
|
||
#[derive(new)] | ||
pub struct DummyScan { | ||
pub(crate) plan: PhysicalDummyScan, | ||
pub(crate) _plan: PhysicalDummyScan, | ||
} | ||
|
||
impl DummyScan { | ||
#[try_stream(boxed, ok = RecordBatch, error = ExecutorError)] | ||
pub async fn execute(self, _context: Arc<ExecutionContext>) { | ||
let mut fields = vec![]; | ||
for (idx, ty) in self.plan.base.types.iter().enumerate() { | ||
fields.push(Field::new( | ||
format!("col{}", idx).as_str(), | ||
ty.clone().into(), | ||
true, | ||
)); | ||
} | ||
let fields = vec![Field::new("dummy", DataType::Boolean, true)]; | ||
let schema = SchemaRef::new(Schema::new_with_metadata(fields, HashMap::new())); | ||
yield RecordBatch::new_empty(schema.clone()); | ||
let array = ScalarValue::Boolean(Some(true)).to_array(); | ||
yield RecordBatch::try_new(schema.clone(), vec![array])?; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::sync::Arc; | ||
|
||
use arrow::compute::filter_record_batch; | ||
use arrow::record_batch::RecordBatch; | ||
use derive_new::new; | ||
use futures_async_stream::try_stream; | ||
|
||
use crate::common::as_boolean_array; | ||
use crate::execution::{ | ||
BoxedExecutor, ExecutionContext, ExecutorError, ExpressionExecutor, PhysicalFilter, | ||
}; | ||
|
||
#[derive(new)] | ||
pub struct Filter { | ||
pub(crate) plan: PhysicalFilter, | ||
pub(crate) child: BoxedExecutor, | ||
} | ||
|
||
impl Filter { | ||
#[try_stream(boxed, ok = RecordBatch, error = ExecutorError)] | ||
pub async fn execute(self, _context: Arc<ExecutionContext>) { | ||
let exprs = self.plan.base.expressioins; | ||
|
||
#[for_await] | ||
for batch in self.child { | ||
let batch = batch?; | ||
let eval_mask = ExpressionExecutor::execute(&exprs, &batch)?; | ||
let predicate = as_boolean_array(&eval_mask[0])?; | ||
yield filter_record_batch(&batch, predicate)?; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.