/// Generate a relational expression from a SQL expression
Generate a relational expression from a SQL expression
Generate a relational expression from a SQL expression
pub fn sql_to_rex(
        &self,
        sql: &sqlparser::ast::Expr,
        schema: &DataSchema,
    ) -> Result<ExpressionPlan> {
        match sql {
            sqlparser::ast::Expr::Identifier(ref v) => Ok(ExpressionPlan::Column(v.clone().value)),
            sqlparser::ast::Expr::Value(sqlparser::ast::Value::Number(n, _)) => {
                Ok(ExpressionPlan::Literal(DataValue::try_from_literal(n)?))
            }
            sqlparser::ast::Expr::Value(sqlparser::ast::Value::SingleQuotedString(s)) => {
                Ok(ExpressionPlan::Literal(DataValue::String(Some(s.clone()))))
            }
            sqlparser::ast::Expr::BinaryOp { left, op, right } => {
                Ok(ExpressionPlan::BinaryExpression {
                    op: format!("{}", op),
                    left: Box::new(self.sql_to_rex(left, schema)?),
                    right: Box::new(self.sql_to_rex(right, schema)?),
                })
            }
            sqlparser::ast::Expr::Nested(e) => self.sql_to_rex(e, schema),
            sqlparser::ast::Expr::Function(e) => {
                let mut args = Vec::with_capacity(e.args.len());
                for arg in &e.args {
                    match &arg {
                        FunctionArg::Named { arg, .. } => {
                            args.push(self.sql_to_rex(arg, schema)?);
                        }
                        FunctionArg::Unnamed(arg) => {
                            args.push(self.sql_to_rex(arg, schema)?);
                        }
                    }
                }
                Ok(ExpressionPlan::Function {
                    op: e.name.to_string(),
                    args,
                })
            }
            sqlparser::ast::Expr::Wildcard => Ok(ExpressionPlan::Wildcard),
            _ => bail!("Unsupported ExpressionPlan: {}", sql),
        }
    }
pub fn sql_to_rex ( & self , sql : & sqlparser :: ast :: Expr , schema : & DataSchema , ) -> Result < ExpressionPlan > { match sql { sqlparser :: ast :: Expr :: Identifier ( ref v ) => Ok ( ExpressionPlan :: Column ( v . clone ( ) . value ) ) , sqlparser :: ast :: Expr :: Value ( sqlparser :: ast :: Value :: Number ( n , _ ) ) => { Ok ( ExpressionPlan :: Literal ( DataValue :: try_from_literal ( n ) ? ) ) } sqlparser :: ast :: Expr :: Value ( sqlparser :: ast :: Value :: SingleQuotedString ( s ) ) => { Ok ( ExpressionPlan :: Literal ( DataValue :: String ( Some ( s . clone ( ) ) ) ) ) } sqlparser :: ast :: Expr :: BinaryOp { left , op , right } => { Ok ( ExpressionPlan :: BinaryExpression { op : format ! ( " " op ) , left : Box :: new ( self . sql_to_rex ( left , schema ) ? ) , right : Box :: new ( self . sql_to_rex ( right , schema ) ? ) , } ) } sqlparser :: ast :: Expr :: Nested ( e ) => self . sql_to_rex ( e , schema ) , sqlparser :: ast :: Expr :: Function ( e ) => { let mut args = Vec :: with_capacity ( e . args . len ( ) ) ; for arg in & e . args { match & arg { FunctionArg :: Named { arg , .. } => { args . push ( self . sql_to_rex ( arg , schema ) ? ) ; } FunctionArg :: Unnamed ( arg ) => { args . push ( self . sql_to_rex ( arg , schema ) ? ) ; } } } Ok ( ExpressionPlan :: Function { op : e . name . to_string ( ) , args , } ) } sqlparser :: ast :: Expr :: Wildcard => Ok ( ExpressionPlan :: Wildcard ) , _ => bail ! ( " " sql ) , } }
Generate a relational expression from a SQL expression
Generate a relational expression from a SQL expression
&self
sql
&sqlparser::ast::Expr
schema
&DataSchema