pub fn to_substrait_rex(expr: &Expr, schema: &DFSchemaRef) -> Result<Expression> {
    match expr {
        Expr::Column(col) => Ok(Expression {
            rex_type: Some(RexType::Selection(Box::new(FieldReference {
                reference_type: Some(ReferenceType::MaskedReference(MaskExpression {
                    select: Some(StructSelect {
                        struct_items: vec![StructItem {
                            field: schema.index_of(&col.name)? as i32,
                            child: None,
                        }],
                    }),
                    maintain_singular_struct: false,
                })),
                root_type: None,
            }))),
        }),
        Expr::BinaryExpr { left, op, right } => {
            let l = to_substrait_rex(left, schema)?;
            let r = to_substrait_rex(right, schema)?;
            let function_reference: u32 = operator_to_reference(*op);
            Ok(Expression {
                rex_type: Some(RexType::ScalarFunction(ScalarFunction {
                    function_reference,
                    args: vec![l, r],
                    output_type: None,
                })),
            })
        }
        Expr::Literal(value) => {
            let literal_type = match value {
                ScalarValue::Int8(Some(n)) => Some(LiteralType::I8(*n as i32)),
                ScalarValue::Int16(Some(n)) => Some(LiteralType::I16(*n as i32)),
                ScalarValue::Int32(Some(n)) => Some(LiteralType::I32(*n)),
                ScalarValue::Int64(Some(n)) => Some(LiteralType::I64(*n)),
                ScalarValue::Boolean(Some(b)) => Some(LiteralType::Boolean(*b)),
                ScalarValue::Float32(Some(f)) => Some(LiteralType::Fp32(*f)),
                ScalarValue::Float64(Some(f)) => Some(LiteralType::Fp64(*f)),
                ScalarValue::Utf8(Some(s)) => Some(LiteralType::String(s.clone())),
                ScalarValue::LargeUtf8(Some(s)) => Some(LiteralType::String(s.clone())),
                ScalarValue::Binary(Some(b)) => Some(LiteralType::Binary(b.clone())),
                ScalarValue::LargeBinary(Some(b)) => Some(LiteralType::Binary(b.clone())),
                ScalarValue::Date32(Some(d)) => Some(LiteralType::Date(*d)),
                _ => {
                    return Err(DataFusionError::NotImplemented(format!(
                        "Unsupported literal: {:?}",
                        value
                    )))
                }
            };
            Ok(Expression {
                rex_type: Some(RexType::Literal(Literal {
                    nullable: true,
                    literal_type,
                })),
            })
        }
        _ => Err(DataFusionError::NotImplemented(format!(
            "Unsupported expression: {:?}",
            expr
        ))),
    }
}
/// Convert DataFusion Expr to Substrait Rex
Convert DataFusion Expr to Substrait Rex
Convert DataFusion Expr to Substrait Rex
pub fn to_substrait_rex(expr: &Expr, schema: &DFSchemaRef) -> Result<Expression> {
    match expr {
        Expr::Column(col) => Ok(Expression {
            rex_type: Some(RexType::Selection(Box::new(FieldReference {
                reference_type: Some(ReferenceType::MaskedReference(MaskExpression {
                    select: Some(StructSelect {
                        struct_items: vec![StructItem {
                            field: schema.index_of(&col.name)? as i32,
                            child: None,
                        }],
                    }),
                    maintain_singular_struct: false,
                })),
                root_type: None,
            }))),
        }),
        Expr::BinaryExpr { left, op, right } => {
            let l = to_substrait_rex(left, schema)?;
            let r = to_substrait_rex(right, schema)?;
            let function_reference: u32 = operator_to_reference(*op);
            Ok(Expression {
                rex_type: Some(RexType::ScalarFunction(ScalarFunction {
                    function_reference,
                    args: vec![l, r],
                    output_type: None,
                })),
            })
        }
        Expr::Literal(value) => {
            let literal_type = match value {
                ScalarValue::Int8(Some(n)) => Some(LiteralType::I8(*n as i32)),
                ScalarValue::Int16(Some(n)) => Some(LiteralType::I16(*n as i32)),
                ScalarValue::Int32(Some(n)) => Some(LiteralType::I32(*n)),
                ScalarValue::Int64(Some(n)) => Some(LiteralType::I64(*n)),
                ScalarValue::Boolean(Some(b)) => Some(LiteralType::Boolean(*b)),
                ScalarValue::Float32(Some(f)) => Some(LiteralType::Fp32(*f)),
                ScalarValue::Float64(Some(f)) => Some(LiteralType::Fp64(*f)),
                ScalarValue::Utf8(Some(s)) => Some(LiteralType::String(s.clone())),
                ScalarValue::LargeUtf8(Some(s)) => Some(LiteralType::String(s.clone())),
                ScalarValue::Binary(Some(b)) => Some(LiteralType::Binary(b.clone())),
                ScalarValue::LargeBinary(Some(b)) => Some(LiteralType::Binary(b.clone())),
                ScalarValue::Date32(Some(d)) => Some(LiteralType::Date(*d)),
                _ => {
                    return Err(DataFusionError::NotImplemented(format!(
                        "Unsupported literal: {:?}",
                        value
                    )))
                }
            };
            Ok(Expression {
                rex_type: Some(RexType::Literal(Literal {
                    nullable: true,
                    literal_type,
                })),
            })
        }
        _ => Err(DataFusionError::NotImplemented(format!(
            "Unsupported expression: {:?}",
            expr
        ))),
    }
}
pub fn to_substrait_rex ( expr : & Expr , schema : & DFSchemaRef ) -> Result < Expression > { match expr { Expr :: Column ( col ) => Ok ( Expression { rex_type : Some ( RexType :: Selection ( Box :: new ( FieldReference { reference_type : Some ( ReferenceType :: MaskedReference ( MaskExpression { select : Some ( StructSelect { struct_items : vec ! [ StructItem { field schema index_of ( col name ) as i32 child None } ] , } ) , maintain_singular_struct : false , } ) ) , root_type : None , } ) ) ) , } ) , Expr :: BinaryExpr { left , op , right } => { let l = to_substrait_rex ( left , schema ) ? ; let r = to_substrait_rex ( right , schema ) ? ; let function_reference : u32 = operator_to_reference ( * op ) ; Ok ( Expression { rex_type : Some ( RexType :: ScalarFunction ( ScalarFunction { function_reference , args : vec ! [ l r ] , output_type : None , } ) ) , } ) } Expr :: Literal ( value ) => { let literal_type = match value { ScalarValue :: Int8 ( Some ( n ) ) => Some ( LiteralType :: I8 ( * n as i32 ) ) , ScalarValue :: Int16 ( Some ( n ) ) => Some ( LiteralType :: I16 ( * n as i32 ) ) , ScalarValue :: Int32 ( Some ( n ) ) => Some ( LiteralType :: I32 ( * n ) ) , ScalarValue :: Int64 ( Some ( n ) ) => Some ( LiteralType :: I64 ( * n ) ) , ScalarValue :: Boolean ( Some ( b ) ) => Some ( LiteralType :: Boolean ( * b ) ) , ScalarValue :: Float32 ( Some ( f ) ) => Some ( LiteralType :: Fp32 ( * f ) ) , ScalarValue :: Float64 ( Some ( f ) ) => Some ( LiteralType :: Fp64 ( * f ) ) , ScalarValue :: Utf8 ( Some ( s ) ) => Some ( LiteralType :: String ( s . clone ( ) ) ) , ScalarValue :: LargeUtf8 ( Some ( s ) ) => Some ( LiteralType :: String ( s . clone ( ) ) ) , ScalarValue :: Binary ( Some ( b ) ) => Some ( LiteralType :: Binary ( b . clone ( ) ) ) , ScalarValue :: LargeBinary ( Some ( b ) ) => Some ( LiteralType :: Binary ( b . clone ( ) ) ) , ScalarValue :: Date32 ( Some ( d ) ) => Some ( LiteralType :: Date ( * d ) ) , _ => { return Err ( DataFusionError :: NotImplemented ( format ! ( " " value ) ) ) } } ; Ok ( Expression { rex_type : Some ( RexType :: Literal ( Literal { nullable : true , literal_type , } ) ) , } ) } _ => Err ( DataFusionError :: NotImplemented ( format ! ( " " expr ) ) ) , } }
Convert DataFusion Expr to Substrait Rex
Convert DataFusion Expr to Substrait Rex
expr
&Expr
schema
&DFSchemaRef
expr
&Expr
schema
&DFSchemaRef