Skip to content

Apache DataFusion SQL Query Engine

ARROW-5013: [Rust] [DataFusion] Refactor runtime expression support

Andy Grovecb6ccaea9662
diff --git a/rust/datafusion/src/execution/aggregate.rs b/rust/datafusion/src/execution/aggregate.rs
index aec187f24b..4417d13bc5 100644
--- a/rust/datafusion/src/execution/aggregate.rs
+++ b/rust/datafusion/src/execution/aggregate.rs
@@ -30,10 +30,12 @@ use arrow::datatypes::{DataType, Schema};
use arrow::record_batch::RecordBatch;
use crate::error::{ExecutionError, Result};
-use crate::execution::expression::{AggregateType, RuntimeExpr};
+use crate::execution::expression::AggregateType;
use crate::execution::relation::Relation;
use crate::logicalplan::ScalarValue;
+use crate::execution::expression::CompiledAggregateExpression;
+use crate::execution::expression::CompiledExpr;
use fnv::FnvHashMap;
/// An aggregate relation is made up of zero or more grouping expressions and one
@@ -41,8 +43,8 @@ use fnv::FnvHashMap;
pub(super) struct AggregateRelation {
schema: Arc<Schema>,
input: Rc<RefCell<Relation>>,
- group_expr: Vec<RuntimeExpr>,
- aggr_expr: Vec<RuntimeExpr>,
+ group_expr: Vec<CompiledExpr>,
+ aggr_expr: Vec<CompiledAggregateExpression>,
end_of_results: bool,
}
@@ -50,8 +52,8 @@ impl AggregateRelation {
pub fn new(
schema: Arc<Schema>,
input: Rc<RefCell<Relation>>,
- group_expr: Vec<RuntimeExpr>,
- aggr_expr: Vec<RuntimeExpr>,
+ group_expr: Vec<CompiledExpr>,
+ aggr_expr: Vec<CompiledAggregateExpression>,
) -> Self {
AggregateRelation {
schema,
@@ -337,26 +339,29 @@ struct MapEntry {
}
/// Create an initial aggregate entry
-fn create_accumulators(aggr_expr: &Vec<RuntimeExpr>) -> Result<AccumulatorSet> {
+fn create_accumulators(
+ aggr_expr: &Vec<CompiledAggregateExpression>,
+) -> Result<AccumulatorSet> {
let aggr_values: Vec<Rc<RefCell<AggregateFunction>>> = aggr_expr
.iter()
- .map(|e| match e {
- RuntimeExpr::AggregateFunction { ref f, ref t, .. } => match f {
- AggregateType::Min => Ok(Rc::new(RefCell::new(MinFunction::new(t)))
- as Rc<RefCell<AggregateFunction>>),
- AggregateType::Max => Ok(Rc::new(RefCell::new(MaxFunction::new(t)))
- as Rc<RefCell<AggregateFunction>>),
- AggregateType::Sum => Ok(Rc::new(RefCell::new(SumFunction::new(t)))
- as Rc<RefCell<AggregateFunction>>),
- _ => Err(ExecutionError::ExecutionError(
- "unsupported aggregate function".to_string(),
- )),
- },
+ .map(|e| match e.aggr_type() {
+ AggregateType::Min => {
+ Ok(Rc::new(RefCell::new(MinFunction::new(e.data_type())))
+ as Rc<RefCell<AggregateFunction>>)
+ }
+ AggregateType::Max => {
+ Ok(Rc::new(RefCell::new(MaxFunction::new(e.data_type())))
+ as Rc<RefCell<AggregateFunction>>)
+ }
+ AggregateType::Sum => {
+ Ok(Rc::new(RefCell::new(SumFunction::new(e.data_type())))
+ as Rc<RefCell<AggregateFunction>>)
+ }
_ => Err(ExecutionError::ExecutionError(
- "invalid aggregate expression".to_string(),
+ "unsupported aggregate function".to_string(),
)),
})
- .collect::<Result<Vec<Rc<RefCell<AggregateFunction>>>>>()?;
+ .collect::<Result<Vec<Rc<RefCell<_>>>>>()?;
Ok(AccumulatorSet { aggr_values })
}
@@ -569,88 +574,63 @@ fn update_accumulators(
batch: &RecordBatch,
row: usize,
accumulator_set: &mut AccumulatorSet,
- aggr_expr: &Vec<RuntimeExpr>,
+ aggr_expr: &Vec<CompiledAggregateExpression>,
) -> Result<()> {
// update the accumulators
for j in 0..accumulator_set.aggr_values.len() {
- match &aggr_expr[j] {
- RuntimeExpr::AggregateFunction { args, t, .. } => {
- // evaluate argument to aggregate function
- match args[0](&batch) {
- Ok(array) => {
- let value: Option<ScalarValue> = match t {
- DataType::UInt8 => {
- let z =
- array.as_any().downcast_ref::<UInt8Array>().unwrap();
- Some(ScalarValue::UInt8(z.value(row)))
- }
- DataType::UInt16 => {
- let z =
- array.as_any().downcast_ref::<UInt16Array>().unwrap();
- Some(ScalarValue::UInt16(z.value(row)))
- }
- DataType::UInt32 => {
- let z =
- array.as_any().downcast_ref::<UInt32Array>().unwrap();
- Some(ScalarValue::UInt32(z.value(row)))
- }
- DataType::UInt64 => {
- let z =
- array.as_any().downcast_ref::<UInt64Array>().unwrap();
- Some(ScalarValue::UInt64(z.value(row)))
- }
- DataType::Int8 => {
- let z =
- array.as_any().downcast_ref::<Int8Array>().unwrap();
- Some(ScalarValue::Int8(z.value(row)))
- }
- DataType::Int16 => {
- let z =
- array.as_any().downcast_ref::<Int16Array>().unwrap();
- Some(ScalarValue::Int16(z.value(row)))
- }
- DataType::Int32 => {
- let z =
- array.as_any().downcast_ref::<Int32Array>().unwrap();
- Some(ScalarValue::Int32(z.value(row)))
- }
- DataType::Int64 => {
- let z =
- array.as_any().downcast_ref::<Int64Array>().unwrap();
- Some(ScalarValue::Int64(z.value(row)))
- }
- DataType::Float32 => {
- let z = array
- .as_any()
- .downcast_ref::<Float32Array>()
- .unwrap();
- Some(ScalarValue::Float32(z.value(row)))
- }
- DataType::Float64 => {
- let z = array
- .as_any()
- .downcast_ref::<Float64Array>()
- .unwrap();
- Some(ScalarValue::Float64(z.value(row)))
- }
- other => return Err(ExecutionError::ExecutionError(format!("Unsupported data type {:?} for result of aggregate expression", other))),
- };
- accumulator_set.accumulate_scalar(j, value)?;
- }
- Err(e) => {
- return Err(ExecutionError::ExecutionError(format!(
- "Failed to evaluate aggregate expression: {:?}",
- e
- )));
- }
- }
+ // evaluate the argument to the aggregate function
+ let array = aggr_expr[j].invoke(batch)?;
+
+ let value: Option<ScalarValue> = match aggr_expr[j].data_type() {
+ DataType::UInt8 => {
+ let z = array.as_any().downcast_ref::<UInt8Array>().unwrap();
+ Some(ScalarValue::UInt8(z.value(row)))
}
- _ => {
- return Err(ExecutionError::InternalError(
- "Invalid aggregate expression in accumulator".to_string(),
- ));
+ DataType::UInt16 => {
+ let z = array.as_any().downcast_ref::<UInt16Array>().unwrap();
+ Some(ScalarValue::UInt16(z.value(row)))
}
- }
+ DataType::UInt32 => {
+ let z = array.as_any().downcast_ref::<UInt32Array>().unwrap();
+ Some(ScalarValue::UInt32(z.value(row)))
+ }
+ DataType::UInt64 => {
+ let z = array.as_any().downcast_ref::<UInt64Array>().unwrap();
+ Some(ScalarValue::UInt64(z.value(row)))
+ }
+ DataType::Int8 => {
+ let z = array.as_any().downcast_ref::<Int8Array>().unwrap();
+ Some(ScalarValue::Int8(z.value(row)))
+ }
+ DataType::Int16 => {
+ let z = array.as_any().downcast_ref::<Int16Array>().unwrap();
+ Some(ScalarValue::Int16(z.value(row)))
+ }
+ DataType::Int32 => {
+ let z = array.as_any().downcast_ref::<Int32Array>().unwrap();
+ Some(ScalarValue::Int32(z.value(row)))
+ }
+ DataType::Int64 => {
+ let z = array.as_any().downcast_ref::<Int64Array>().unwrap();
+ Some(ScalarValue::Int64(z.value(row)))
+ }
+ DataType::Float32 => {
+ let z = array.as_any().downcast_ref::<Float32Array>().unwrap();
+ Some(ScalarValue::Float32(z.value(row)))
+ }
+ DataType::Float64 => {
+ let z = array.as_any().downcast_ref::<Float64Array>().unwrap();
+ Some(ScalarValue::Float64(z.value(row)))
+ }
+ other => {
+ return Err(ExecutionError::ExecutionError(format!(
+ "Unsupported data type {:?} for result of aggregate expression",
+ other
+ )));
+ }
+ };
+
+ accumulator_set.accumulate_scalar(j, value)?;
}
Ok(())
}
@@ -751,34 +731,24 @@ impl AggregateRelation {
while let Some(batch) = self.input.borrow_mut().next()? {
for i in 0..aggr_expr_count {
- match &self.aggr_expr[i] {
- RuntimeExpr::AggregateFunction { f, args, t, .. } => {
- // evaluate argument to aggregate function
- match args[0](&batch) {
- Ok(array) => match f {
- AggregateType::Min => accumulator_set
- .accumulate_scalar(i, array_min(array, &t)?)?,
- AggregateType::Max => accumulator_set
- .accumulate_scalar(i, array_max(array, &t)?)?,
- AggregateType::Sum => accumulator_set
- .accumulate_scalar(i, array_sum(array, &t)?)?,
- _ => {
- return Err(ExecutionError::NotImplemented(
- "Unsupported aggregate function".to_string(),
- ));
- }
- },
- Err(_) => {
- return Err(ExecutionError::ExecutionError(
- "Failed to evaluate argument to aggregate function"
- .to_string(),
- ));
- }
- }
+ // evaluate the argument to the aggregate function
+ let array = self.aggr_expr[i].invoke(&batch)?;
+
+ let t = self.aggr_expr[i].data_type();
+
+ match self.aggr_expr[i].aggr_type() {
+ AggregateType::Min => {
+ accumulator_set.accumulate_scalar(i, array_min(array, &t)?)?
+ }
+ AggregateType::Max => {
+ accumulator_set.accumulate_scalar(i, array_max(array, &t)?)?
+ }
+ AggregateType::Sum => {
+ accumulator_set.accumulate_scalar(i, array_sum(array, &t)?)?
}
_ => {
- return Err(ExecutionError::General(
- "Invalid aggregate expression".to_string(),
+ return Err(ExecutionError::NotImplemented(
+ "Unsupported aggregate function".to_string(),
));
}
}
@@ -849,7 +819,7 @@ impl AggregateRelation {
let group_by_keys: Vec<ArrayRef> = self
.group_expr
.iter()
- .map(|e| e.get_func()?(&batch))
+ .map(|e| e.invoke(&batch))
.collect::<Result<Vec<ArrayRef>>>()?;
// iterate over each row in the batch
@@ -962,7 +932,7 @@ impl AggregateRelation {
// grouping values
for i in 0..self.group_expr.len() {
- let array: Result<ArrayRef> = match self.group_expr[i].get_type() {
+ let array: Result<ArrayRef> = match self.group_expr[i].data_type() {
DataType::UInt8 => {
group_array_from_map_entries!(UInt8Builder, UInt8, entries, i)
}
@@ -1006,7 +976,7 @@ impl AggregateRelation {
// aggregate values
for i in 0..self.aggr_expr.len() {
- let array = match self.aggr_expr[i].get_type() {
+ let array = match self.aggr_expr[i].data_type() {
DataType::UInt8 => {
aggr_array_from_map_entries!(UInt8Builder, UInt8, entries, i)
}
@@ -1068,7 +1038,7 @@ mod tests {
let relation = load_csv("../../testing/data/csv/aggregate_test_100.csv", &schema);
let context = ExecutionContext::new();
- let aggr_expr = vec![expression::compile_expr(
+ let aggr_expr = vec![expression::compile_aggregate_expr(
&context,
&Expr::AggregateFunction {
name: String::from("min"),
@@ -1103,7 +1073,7 @@ mod tests {
let relation = load_csv("../../testing/data/csv/aggregate_test_100.csv", &schema);
let context = ExecutionContext::new();
- let aggr_expr = vec![expression::compile_expr(
+ let aggr_expr = vec![expression::compile_aggregate_expr(
&context,
&Expr::AggregateFunction {
name: String::from("max"),
@@ -1142,7 +1112,7 @@ mod tests {
let group_by_expr =
expression::compile_expr(&context, &Expr::Column(1), &schema).unwrap();
- let min_expr = expression::compile_expr(
+ let min_expr = expression::compile_aggregate_expr(
&context,
&Expr::AggregateFunction {
name: String::from("min"),
@@ -1153,7 +1123,7 @@ mod tests {
)
.unwrap();
- let max_expr = expression::compile_expr(
+ let max_expr = expression::compile_aggregate_expr(
&context,
&Expr::AggregateFunction {
name: String::from("max"),
@@ -1164,7 +1134,7 @@ mod tests {
)
.unwrap();
- let sum_expr = expression::compile_expr(
+ let sum_expr = expression::compile_aggregate_expr(
&context,
&Expr::AggregateFunction {
name: String::from("sum"),
diff --git a/rust/datafusion/src/execution/context.rs b/rust/datafusion/src/execution/context.rs
index 4d0071c2c3..a18d7690f8 100644
--- a/rust/datafusion/src/execution/context.rs
+++ b/rust/datafusion/src/execution/context.rs
@@ -175,12 +175,8 @@ impl ExecutionContext {
} => {
let input_rel = self.execute(input, batch_size)?;
let input_schema = input_rel.as_ref().borrow().schema().clone();
- let runtime_expr = compile_scalar_expr(&self, expr, &input_schema)?;
- let rel = FilterRelation::new(
- input_rel,
- runtime_expr, /* .get_func()?.clone() */
- input_schema,
- );
+ let runtime_expr = compile_expr(&self, expr, &input_schema)?;
+ let rel = FilterRelation::new(input_rel, runtime_expr, input_schema);
Ok(Rc::new(RefCell::new(rel)))
}
LogicalPlan::Projection {
@@ -197,9 +193,9 @@ impl ExecutionContext {
let project_schema = Arc::new(Schema::new(project_columns));
- let compiled_expr: Result<Vec<RuntimeExpr>> = expr
+ let compiled_expr: Result<Vec<CompiledExpr>> = expr
.iter()
- .map(|e| compile_scalar_expr(&self, e, &input_schema))
+ .map(|e| compile_expr(&self, e, &input_schema))
.collect();
let rel = ProjectRelation::new(input_rel, compiled_expr?, project_schema);
@@ -216,16 +212,17 @@ impl ExecutionContext {
let input_schema = input_rel.as_ref().borrow().schema().clone();
- let compiled_group_expr_result: Result<Vec<RuntimeExpr>> = group_expr
+ let compiled_group_expr_result: Result<Vec<CompiledExpr>> = group_expr
.iter()
- .map(|e| compile_scalar_expr(&self, e, &input_schema))
+ .map(|e| compile_expr(&self, e, &input_schema))
.collect();
let compiled_group_expr = compiled_group_expr_result?;
- let compiled_aggr_expr_result: Result<Vec<RuntimeExpr>> = aggr_expr
- .iter()
- .map(|e| compile_expr(&self, e, &input_schema))
- .collect();
+ let compiled_aggr_expr_result: Result<Vec<CompiledAggregateExpression>> =
+ aggr_expr
+ .iter()
+ .map(|e| compile_aggregate_expr(&self, e, &input_schema))
+ .collect();
let compiled_aggr_expr = compiled_aggr_expr_result?;
let mut output_fields: Vec<Field> = vec![];
diff --git a/rust/datafusion/src/execution/expression.rs b/rust/datafusion/src/execution/expression.rs
index 5a593ba1b9..f152a14e80 100644
--- a/rust/datafusion/src/execution/expression.rs
+++ b/rust/datafusion/src/execution/expression.rs
@@ -29,10 +29,10 @@ use crate::error::{ExecutionError, Result};
use crate::execution::context::ExecutionContext;
use crate::logicalplan::{Expr, Operator, ScalarValue};
-/// Compiled Expression (basically just a closure to evaluate the expression at runtime)
-pub type CompiledExpr = Rc<Fn(&RecordBatch) -> Result<ArrayRef>>;
+/// Function that accepts a RecordBatch and returns an ArrayRef
+pub type ArrayFunction = Rc<Fn(&RecordBatch) -> Result<ArrayRef>>;
-/// Similar to `CompiledExpr` but the closure transforms `ArrayRef` to another `ArrayRef`
+/// Function that accepts an ArrayRef and returns an ArrayRef
pub type CompiledCastFunction = Rc<Fn(&ArrayRef) -> Result<ArrayRef>>;
/// Enumeration of supported aggregate functions
@@ -46,52 +46,63 @@ pub enum AggregateType {
Avg,
}
-/// Runtime expression
-pub(super) enum RuntimeExpr {
- Compiled {
- name: String,
- f: CompiledExpr,
- t: DataType,
- },
- AggregateFunction {
- name: String,
- f: AggregateType,
- args: Vec<CompiledExpr>,
- t: DataType,
- },
+/// Compiled expression
+pub(super) struct CompiledExpr {
+ name: String,
+ f: ArrayFunction,
+ t: DataType,
}
-impl RuntimeExpr {
- pub fn get_func(&self) -> Result<CompiledExpr> {
- match self {
- &RuntimeExpr::Compiled { ref f, .. } => Ok(f.clone()),
- _ => Err(ExecutionError::InternalError(
- "Invalid runtime expression".to_string(),
- )),
- }
+impl CompiledExpr {
+ /// get the name of the expression
+ pub fn name(&self) -> &str {
+ &self.name
}
- pub fn get_name(&self) -> &String {
- match self {
- &RuntimeExpr::Compiled { ref name, .. } => name,
- &RuntimeExpr::AggregateFunction { ref name, .. } => name,
- }
+ /// Get the data type of the expression
+ pub fn data_type(&self) -> &DataType {
+ &self.t
}
- pub fn get_type(&self) -> DataType {
- match self {
- &RuntimeExpr::Compiled { ref t, .. } => t.clone(),
- &RuntimeExpr::AggregateFunction { ref t, .. } => t.clone(),
- }
+ /// invoke the compiled expression
+ pub fn invoke(&self, batch: &RecordBatch) -> Result<ArrayRef> {
+ (self.f)(batch)
}
}
-/// Compiles a scalar expression into a closure
-pub(super) fn compile_expr(
+/// Compiled aggregate expression
+pub(super) struct CompiledAggregateExpression {
+ /// Aggregate type
+ aggr_type: AggregateType,
+ /// Arguments to the aggregate expression
+ args: Vec<CompiledExpr>,
+ /// Return type of aggregate expression
+ t: DataType,
+}
+
+impl CompiledAggregateExpression {
+ /// get the aggregate type (Min, Max, Count, etc)
+ pub fn aggr_type(&self) -> &AggregateType {
+ &self.aggr_type
+ }
+
+ /// Get the data type of the expression
+ pub fn data_type(&self) -> &DataType {
+ &self.t
+ }
+
+ /// invoke the compiled expression
+ pub fn invoke(&self, batch: &RecordBatch) -> Result<ArrayRef> {
+ self.args[0].invoke(batch)
+ }
+}
+
+/// Compiles an aggregate expression into a closure
+pub(super) fn compile_aggregate_expr(
ctx: &ExecutionContext,
expr: &Expr,
input_schema: &Schema,
-) -> Result<RuntimeExpr> {
+) -> Result<CompiledAggregateExpression> {
match *expr {
Expr::AggregateFunction {
ref name,
@@ -100,10 +111,10 @@ pub(super) fn compile_expr(
} => {
assert_eq!(1, args.len());
- let compiled_args: Result<Vec<RuntimeExpr>> = args
+ let compiled_args: Vec<CompiledExpr> = args
.iter()
- .map(|e| compile_scalar_expr(&ctx, e, input_schema))
- .collect();
+ .map(|e| compile_expr(&ctx, e, input_schema))
+ .collect::<Result<Vec<_>>>()?;
let func = match name.to_lowercase().as_ref() {
"min" => Ok(AggregateType::Min),
@@ -116,19 +127,13 @@ pub(super) fn compile_expr(
))),
};
- let mut args = vec![];
- for arg in compiled_args? {
- args.push(arg.get_func()?.clone());
- }
-
- Ok(RuntimeExpr::AggregateFunction {
- name: name.to_string(),
- f: func?,
- args,
+ Ok(CompiledAggregateExpression {
+ args: compiled_args,
t: return_type.clone(),
+ aggr_type: func?,
})
}
- _ => Ok(compile_scalar_expr(&ctx, expr, input_schema)?),
+ _ => panic!(),
}
}
@@ -142,8 +147,8 @@ macro_rules! binary_op {
macro_rules! math_ops {
($LEFT:expr, $RIGHT:expr, $BATCH:expr, $OP:ident) => {{
- let left_values = $LEFT.get_func()?($BATCH)?;
- let right_values = $RIGHT.get_func()?($BATCH)?;
+ let left_values = $LEFT.invoke($BATCH)?;
+ let right_values = $RIGHT.invoke($BATCH)?;
match (left_values.data_type(), right_values.data_type()) {
(DataType::Int8, DataType::Int8) => {
binary_op!(left_values, right_values, $OP, Int8Array)
@@ -182,8 +187,8 @@ macro_rules! math_ops {
macro_rules! comparison_ops {
($LEFT:expr, $RIGHT:expr, $BATCH:expr, $OP:ident) => {{
- let left_values = $LEFT.get_func()?($BATCH)?;
- let right_values = $RIGHT.get_func()?($BATCH)?;
+ let left_values = $LEFT.invoke($BATCH)?;
+ let right_values = $RIGHT.invoke($BATCH)?;
match (left_values.data_type(), right_values.data_type()) {
(DataType::Int8, DataType::Int8) => {
binary_op!(left_values, right_values, $OP, Int8Array)
@@ -223,8 +228,8 @@ macro_rules! comparison_ops {
macro_rules! boolean_ops {
($LEFT:expr, $RIGHT:expr, $BATCH:expr, $OP:ident) => {{
- let left_values = $LEFT.get_func()?($BATCH)?;
- let right_values = $RIGHT.get_func()?($BATCH)?;
+ let left_values = $LEFT.invoke($BATCH)?;
+ let right_values = $RIGHT.invoke($BATCH)?;
Ok(Arc::new(compute::$OP(
left_values.as_any().downcast_ref::<BooleanArray>().unwrap(),
right_values
@@ -238,7 +243,7 @@ macro_rules! boolean_ops {
macro_rules! literal_array {
($VALUE:expr, $ARRAY_TYPE:ident, $TY:ident) => {{
let nn = *$VALUE;
- Ok(RuntimeExpr::Compiled {
+ Ok(CompiledExpr {
name: format!("{}", nn),
f: Rc::new(move |batch: &RecordBatch| {
let capacity = batch.num_rows();
@@ -255,11 +260,11 @@ macro_rules! literal_array {
}
/// Compiles a scalar expression into a closure
-pub(super) fn compile_scalar_expr(
+pub(super) fn compile_expr(
ctx: &ExecutionContext,
expr: &Expr,
input_schema: &Schema,
-) -> Result<RuntimeExpr> {
+) -> Result<CompiledExpr> {
match expr {
&Expr::Literal(ref value) => match value {
//NOTE: this is a temporary hack .. due to the way expressions like 'a > 1'
@@ -284,7 +289,7 @@ pub(super) fn compile_scalar_expr(
other
))),
},
- &Expr::Column(index) => Ok(RuntimeExpr::Compiled {
+ &Expr::Column(index) => Ok(CompiledExpr {
name: input_schema.field(index).name().clone(),
f: Rc::new(move |batch: &RecordBatch| Ok((*batch.column(index)).clone())),
t: input_schema.field(index).data_type().clone(),
@@ -296,7 +301,7 @@ pub(super) fn compile_scalar_expr(
&Expr::Column(index) => {
let col = input_schema.field(index);
let dt = data_type.clone();
- Ok(RuntimeExpr::Compiled {
+ Ok(CompiledExpr {
name: col.name().clone(),
t: col.data_type().clone(),
f: Rc::new(move |batch: &RecordBatch| {
@@ -312,7 +317,7 @@ pub(super) fn compile_scalar_expr(
ScalarValue::Int64(n) => {
let nn = *n;
match data_type {
- DataType::Float64 => Ok(RuntimeExpr::Compiled {
+ DataType::Float64 => Ok(CompiledExpr {
name: "lit".to_string(),
f: Rc::new(move |batch: &RecordBatch| {
let mut b = Float64Array::builder(batch.num_rows());
@@ -345,89 +350,89 @@ pub(super) fn compile_scalar_expr(
ref op,
ref right,
} => {
- let left_expr = compile_scalar_expr(ctx, left, input_schema)?;
- let right_expr = compile_scalar_expr(ctx, right, input_schema)?;
+ let left_expr = compile_expr(ctx, left, input_schema)?;
+ let right_expr = compile_expr(ctx, right, input_schema)?;
let name = format!("{:?} {:?} {:?}", left, op, right);
- let op_type = left_expr.get_type().clone();
+ let op_type = left_expr.data_type().clone();
match op {
- &Operator::Eq => Ok(RuntimeExpr::Compiled {
+ &Operator::Eq => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
comparison_ops!(left_expr, right_expr, batch, eq)
}),
t: DataType::Boolean,
}),
- &Operator::NotEq => Ok(RuntimeExpr::Compiled {
+ &Operator::NotEq => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
comparison_ops!(left_expr, right_expr, batch, neq)
}),
t: DataType::Boolean,
}),
- &Operator::Lt => Ok(RuntimeExpr::Compiled {
+ &Operator::Lt => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
comparison_ops!(left_expr, right_expr, batch, lt)
}),
t: DataType::Boolean,
}),
- &Operator::LtEq => Ok(RuntimeExpr::Compiled {
+ &Operator::LtEq => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
comparison_ops!(left_expr, right_expr, batch, lt_eq)
}),
t: DataType::Boolean,
}),
- &Operator::Gt => Ok(RuntimeExpr::Compiled {
+ &Operator::Gt => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
comparison_ops!(left_expr, right_expr, batch, gt)
}),
t: DataType::Boolean,
}),
- &Operator::GtEq => Ok(RuntimeExpr::Compiled {
+ &Operator::GtEq => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
comparison_ops!(left_expr, right_expr, batch, gt_eq)
}),
t: DataType::Boolean,
}),
- &Operator::And => Ok(RuntimeExpr::Compiled {
+ &Operator::And => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
boolean_ops!(left_expr, right_expr, batch, and)
}),
t: DataType::Boolean,
}),
- &Operator::Or => Ok(RuntimeExpr::Compiled {
+ &Operator::Or => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
boolean_ops!(left_expr, right_expr, batch, or)
}),
t: DataType::Boolean,
}),
- &Operator::Plus => Ok(RuntimeExpr::Compiled {
+ &Operator::Plus => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
math_ops!(left_expr, right_expr, batch, add)
}),
t: op_type,
}),
- &Operator::Minus => Ok(RuntimeExpr::Compiled {
+ &Operator::Minus => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
math_ops!(left_expr, right_expr, batch, subtract)
}),
t: op_type,
}),
- &Operator::Multiply => Ok(RuntimeExpr::Compiled {
+ &Operator::Multiply => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
math_ops!(left_expr, right_expr, batch, multiply)
}),
t: op_type,
}),
- &Operator::Divide => Ok(RuntimeExpr::Compiled {
+ &Operator::Divide => Ok(CompiledExpr {
name,
f: Rc::new(move |batch: &RecordBatch| {
math_ops!(left_expr, right_expr, batch, divide)
diff --git a/rust/datafusion/src/execution/filter.rs b/rust/datafusion/src/execution/filter.rs
index 395a8946c9..5ff62cf829 100644
--- a/rust/datafusion/src/execution/filter.rs
+++ b/rust/datafusion/src/execution/filter.rs
@@ -27,7 +27,7 @@ use arrow::datatypes::Schema;
use arrow::record_batch::RecordBatch;
use crate::error::{ExecutionError, Result};
-use crate::execution::expression::RuntimeExpr;
+use crate::execution::expression::CompiledExpr;
use crate::execution::relation::Relation;
/// Implementation of a filter relation
@@ -37,13 +37,13 @@ pub(super) struct FilterRelation {
/// Relation that is being filtered
input: Rc<RefCell<Relation>>,
/// Filter expression
- expr: RuntimeExpr,
+ expr: CompiledExpr,
}
impl FilterRelation {
pub fn new(
input: Rc<RefCell<Relation>>,
- expr: RuntimeExpr,
+ expr: CompiledExpr,
schema: Arc<Schema>,
) -> Self {
Self {
@@ -59,7 +59,9 @@ impl Relation for FilterRelation {
match self.input.borrow_mut().next()? {
Some(batch) => {
// evaluate the filter expression against the batch
- match self.expr.get_func()?(&batch)?
+ match self
+ .expr
+ .invoke(&batch)?
.as_any()
.downcast_ref::<BooleanArray>()
{
diff --git a/rust/datafusion/src/execution/projection.rs b/rust/datafusion/src/execution/projection.rs
index 2309897a9d..cf91409dc8 100644
--- a/rust/datafusion/src/execution/projection.rs
+++ b/rust/datafusion/src/execution/projection.rs
@@ -29,7 +29,7 @@ use arrow::datatypes::{Field, Schema};
use arrow::record_batch::RecordBatch;
use crate::error::Result;
-use crate::execution::expression::RuntimeExpr;
+use crate::execution::expression::CompiledExpr;
use crate::execution::relation::Relation;
/// Projection relation
@@ -39,13 +39,13 @@ pub(super) struct ProjectRelation {
/// The relation that the projection is being applied to
input: Rc<RefCell<Relation>>,
/// Projection expressions
- expr: Vec<RuntimeExpr>,
+ expr: Vec<CompiledExpr>,
}
impl ProjectRelation {
pub fn new(
input: Rc<RefCell<Relation>>,
- expr: Vec<RuntimeExpr>,
+ expr: Vec<CompiledExpr>,
schema: Arc<Schema>,
) -> Self {
ProjectRelation {
@@ -61,12 +61,12 @@ impl Relation for ProjectRelation {
match self.input.borrow_mut().next()? {
Some(batch) => {
let projected_columns: Result<Vec<ArrayRef>> =
- self.expr.iter().map(|e| e.get_func()?(&batch)).collect();
+ self.expr.iter().map(|e| e.invoke(&batch)).collect();
let schema = Schema::new(
self.expr
.iter()
- .map(|e| Field::new(&e.get_name(), e.get_type(), true))
+ .map(|e| Field::new(&e.name(), e.data_type().clone(), true))
.collect(),
);