Skip to content

Apache DataFusion SQL Query Engine

Create jit-expression from datafusion expression (#2103)

Daniël Heresb7d3bb1e3f2a
diff --git a/datafusion/jit/Cargo.toml b/datafusion/jit/Cargo.toml
index 26780271dc..54c010812e 100644
--- a/datafusion/jit/Cargo.toml
+++ b/datafusion/jit/Cargo.toml
@@ -41,4 +41,6 @@ cranelift-jit = "0.82.0"
cranelift-module = "0.82.0"
cranelift-native = "0.82.0"
datafusion-common = { path = "../common", version = "7.0.0", features = ["jit"] }
+datafusion-expr = { path = "../expr", version = "7.0.0" }
+
parking_lot = "0.12"
diff --git a/datafusion/jit/src/ast.rs b/datafusion/jit/src/ast.rs
index 5d0e3bc404..8b9139a32e 100644
--- a/datafusion/jit/src/ast.rs
+++ b/datafusion/jit/src/ast.rs
@@ -16,6 +16,7 @@
// under the License.
use cranelift::codegen::ir;
+use datafusion_common::{DataFusionError, ScalarValue};
use std::fmt::{Display, Formatter};
#[derive(Clone, Debug)]
@@ -33,7 +34,7 @@ pub enum Stmt {
Declare(String, JITType),
}
-#[derive(Copy, Clone, Debug)]
+#[derive(Copy, Clone, Debug, PartialEq)]
/// Shorthand typed literals
pub enum TypedLit {
Bool(bool),
@@ -42,7 +43,7 @@ pub enum TypedLit {
Double(f64),
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
/// Expression
pub enum Expr {
/// literal
@@ -103,7 +104,7 @@ impl BinaryExpr {
}
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
/// Binary expression
pub enum BinaryExpr {
/// ==
@@ -128,7 +129,7 @@ pub enum BinaryExpr {
Div(Box<Expr>, Box<Expr>),
}
-#[derive(Clone, Debug)]
+#[derive(Clone, Debug, PartialEq)]
/// Literal
pub enum Literal {
/// Parsable literal with type
@@ -137,6 +138,59 @@ pub enum Literal {
Typed(TypedLit),
}
+impl TryFrom<datafusion_expr::Expr> 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> {
+ match &value {
+ datafusion_expr::Expr::BinaryExpr { left, op, right } => {
+ let op = match op {
+ datafusion_expr::Operator::Eq => BinaryExpr::Eq,
+ datafusion_expr::Operator::NotEq => BinaryExpr::Ne,
+ datafusion_expr::Operator::Lt => BinaryExpr::Lt,
+ datafusion_expr::Operator::LtEq => BinaryExpr::Le,
+ datafusion_expr::Operator::Gt => BinaryExpr::Gt,
+ datafusion_expr::Operator::GtEq => BinaryExpr::Ge,
+ datafusion_expr::Operator::Plus => BinaryExpr::Add,
+ datafusion_expr::Operator::Minus => BinaryExpr::Sub,
+ datafusion_expr::Operator::Multiply => BinaryExpr::Mul,
+ datafusion_expr::Operator::Divide => BinaryExpr::Div,
+ _ => {
+ return Err(DataFusionError::NotImplemented(format!(
+ "Compiling binary expression {} not yet supported",
+ value
+ )))
+ }
+ };
+ Ok(Expr::Binary(op(
+ Box::new((*left.clone()).try_into()?),
+ Box::new((*right.clone()).try_into()?),
+ )))
+ }
+ datafusion_expr::Expr::Literal(s) => {
+ let lit = match s {
+ ScalarValue::Boolean(Some(b)) => TypedLit::Bool(*b),
+ ScalarValue::Float32(Some(f)) => TypedLit::Float(*f),
+ ScalarValue::Float64(Some(f)) => TypedLit::Double(*f),
+ ScalarValue::Int64(Some(i)) => TypedLit::Int(*i),
+ _ => {
+ return Err(DataFusionError::NotImplemented(format!(
+ "Compiling Scalar {} not yet supported in JIT mode",
+ s
+ )))
+ }
+ };
+ Ok(Expr::Literal(Literal::Typed(lit)))
+ }
+ _ => Err(DataFusionError::NotImplemented(format!(
+ "Compiling {} not yet supported",
+ value
+ ))),
+ }
+ }
+}
+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
/// Type to be used in JIT
pub struct JITType {
diff --git a/datafusion/jit/src/lib.rs b/datafusion/jit/src/lib.rs
index 5642b5a9c9..c1db48b45b 100644
--- a/datafusion/jit/src/lib.rs
+++ b/datafusion/jit/src/lib.rs
@@ -24,9 +24,10 @@ pub mod jit;
#[cfg(test)]
mod tests {
use crate::api::{Assembler, GeneratedFunction};
- use crate::ast::I64;
+ use crate::ast::{BinaryExpr, Expr, Literal, TypedLit, I64};
use crate::jit::JIT;
use datafusion_common::Result;
+ use datafusion_expr::lit;
#[test]
fn iterative_fib() -> Result<()> {
@@ -85,6 +86,22 @@ mod tests {
Ok(())
}
+ #[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()?;
+
+ assert_eq!(
+ jit_expr,
+ Expr::Binary(BinaryExpr::Add(
+ Box::new(Expr::Literal(Literal::Typed(TypedLit::Float(1.0)))),
+ Box::new(Expr::Literal(Literal::Typed(TypedLit::Float(2.0))))
+ )),
+ );
+
+ Ok(())
+ }
+
unsafe fn run_code<I, O>(
jit: &mut JIT,
code: GeneratedFunction,