Skip to content

Commit

Permalink
JIT-compille DataFusion expression with column name (#2124)
Browse files Browse the repository at this point in the history
* Add code to create expression based on schema information

* Add spaces

* Move to top

* Clippy
  • Loading branch information
Dandandan authored Mar 31, 2022
1 parent f619d43 commit 4c2320e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 8 deletions.
1 change: 1 addition & 0 deletions datafusion/jit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ path = "src/lib.rs"
jit = []

[dependencies]
arrow = { version = "11" }
cranelift = "0.82.0"
cranelift-jit = "0.82.0"
cranelift-module = "0.82.0"
Expand Down
32 changes: 27 additions & 5 deletions datafusion/jit/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// under the License.

use cranelift::codegen::ir;
use datafusion_common::{DataFusionError, ScalarValue};
use datafusion_common::{DFSchemaRef, DataFusionError, ScalarValue};
use std::fmt::{Display, Formatter};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -138,11 +138,13 @@ pub enum Literal {
Typed(TypedLit),
}

impl TryFrom<datafusion_expr::Expr> for Expr {
impl TryFrom<(datafusion_expr::Expr, DFSchemaRef)> for Expr {
type Error = DataFusionError;

// Try to JIT compile the Expr for faster evaluation
fn try_from(value: datafusion_expr::Expr) -> Result<Self, Self::Error> {
fn try_from(
(value, schema): (datafusion_expr::Expr, DFSchemaRef),
) -> Result<Self, Self::Error> {
match &value {
datafusion_expr::Expr::BinaryExpr { left, op, right } => {
let op = match op {
Expand All @@ -164,10 +166,30 @@ impl TryFrom<datafusion_expr::Expr> for Expr {
}
};
Ok(Expr::Binary(op(
Box::new((*left.clone()).try_into()?),
Box::new((*right.clone()).try_into()?),
Box::new((*left.clone(), schema.clone()).try_into()?),
Box::new((*right.clone(), schema).try_into()?),
)))
}
datafusion_expr::Expr::Column(col) => {
let field = schema.field_from_column(col)?;
let ty = field.data_type();

let jit_type = match ty {
arrow::datatypes::DataType::Int64 => I64,
arrow::datatypes::DataType::Float32 => F32,
arrow::datatypes::DataType::Float64 => F64,
arrow::datatypes::DataType::Boolean => BOOL,

_ => {
return Err(DataFusionError::NotImplemented(format!(
"Compiling Expression with type {} not yet supported in JIT mode",
ty
)))
}
};

Ok(Expr::Identifier(field.qualified_name(), jit_type))
}
datafusion_expr::Expr::Literal(s) => {
let lit = match s {
ScalarValue::Boolean(Some(b)) => TypedLit::Bool(*b),
Expand Down
31 changes: 28 additions & 3 deletions datafusion/jit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ pub mod jit;

#[cfg(test)]
mod tests {
use std::collections::HashMap;
use std::sync::Arc;

use crate::api::{Assembler, GeneratedFunction};
use crate::ast::{BinaryExpr, Expr, Literal, TypedLit, I64};
use crate::jit::JIT;
use datafusion_common::Result;
use datafusion_expr::lit;
use arrow::datatypes::DataType;
use datafusion_common::{DFField, DFSchema, Result};
use datafusion_expr::{col, lit};

#[test]
fn iterative_fib() -> Result<()> {
Expand Down Expand Up @@ -89,7 +93,8 @@ mod tests {
#[test]
fn from_datafusion_expression() -> Result<()> {
let df_expr = lit(1.0f32) + lit(2.0f32);
let jit_expr: crate::ast::Expr = df_expr.try_into()?;
let schema = Arc::new(DFSchema::empty());
let jit_expr: crate::ast::Expr = (df_expr, schema).try_into()?;

assert_eq!(
jit_expr,
Expand All @@ -102,6 +107,26 @@ mod tests {
Ok(())
}

#[test]
fn from_datafusion_expression_schema() -> Result<()> {
let df_expr = col("a") + lit(1i64);
let schema = Arc::new(DFSchema::new_with_metadata(
vec![DFField::new(Some("table1"), "a", DataType::Int64, false)],
HashMap::new(),
)?);
let jit_expr: crate::ast::Expr = (df_expr, schema).try_into()?;

assert_eq!(
jit_expr,
Expr::Binary(BinaryExpr::Add(
Box::new(Expr::Identifier("table1.a".to_string(), I64)),
Box::new(Expr::Literal(Literal::Typed(TypedLit::Int(1))))
)),
);

Ok(())
}

unsafe fn run_code<I, O>(
jit: &mut JIT,
code: GeneratedFunction,
Expand Down

0 comments on commit 4c2320e

Please sign in to comment.