/// Extract sort column from sort expression. It checks the monotonicity information
/// of the sort expression (like f(x) = x+2 is a monotonic function), and extract the
/// column information, returns as Expression::Column.
Extract sort column from sort expression. It checks the monotonicity information
of the sort expression (like f(x) = x+2 is a monotonic function), and extract the
column information, returns as Expression::Column.
Extract sort column from sort expression . It checks the monotonicity information of the sort expression ( like f ( x ) = x + 2 is a monotonic function ) and extract the column information returns as Expression :: Column .
pub fn extract_sort_column(
        sort_expr: &Expression,
        left: Option<DataColumnWithField>,
        right: Option<DataColumnWithField>,
    ) -> Result<Expression> {
        if let Expression::Sort {
            expr: _,
            asc,
            nulls_first,
            origin_expr,
        } = sort_expr
        {
            let visitor = MonotonicityCheckVisitor {
                mono: None,
                variable_left: left,
                variable_right: right,
                columns: HashSet::new(),
            };
            let visitor = origin_expr.accept(visitor)?;
            // The expression has more than one column, we don't know the monotonicity.
            // Skip the optimization, just return the input
            if visitor.columns.len() != 1 {
                return Ok(sort_expr.clone());
            }
            // not a monotonicity expression
            if visitor.mono.is_none() {
                return Ok(sort_expr.clone());
            }
            let mono = visitor.mono.unwrap();
            if !mono.is_monotonic {
                return Ok(sort_expr.clone());
            }
            let column_name = visitor.columns.iter().next().unwrap();
            // need to flip the asc when is_positive is false
            let new_asc = if mono.is_positive { *asc } else { !*asc };
            return Ok(Expression::Sort {
                expr: Box::new(col(column_name)),
                asc: new_asc,
                nulls_first: *nulls_first,
                origin_expr: origin_expr.clone(),
            });
        }
        Err(ErrorCode::BadArguments(format!(
            "expect sort expression, get {:?}",
            sort_expr
        )))
    }
pub fn extract_sort_column ( sort_expr : & Expression , left : Option < DataColumnWithField > , right : Option < DataColumnWithField > , ) -> Result < Expression > { if let Expression :: Sort { expr : _ , asc , nulls_first , origin_expr , } = sort_expr { let visitor = MonotonicityCheckVisitor { mono : None , variable_left : left , variable_right : right , columns : HashSet :: new ( ) , } ; let visitor = origin_expr . accept ( visitor ) ? ; // The expression has more than one column, we don't know the monotonicity. // Skip the optimization, just return the input if visitor . columns . len ( ) != 1 { return Ok ( sort_expr . clone ( ) ) ; } // not a monotonicity expression if visitor . mono . is_none ( ) { return Ok ( sort_expr . clone ( ) ) ; } let mono = visitor . mono . unwrap ( ) ; if ! mono . is_monotonic { return Ok ( sort_expr . clone ( ) ) ; } let column_name = visitor . columns . iter ( ) . next ( ) . unwrap ( ) ; // need to flip the asc when is_positive is false let new_asc = if mono . is_positive { * asc } else { ! * asc } ; return Ok ( Expression :: Sort { expr : Box :: new ( col ( column_name ) ) , asc : new_asc , nulls_first : * nulls_first , origin_expr : origin_expr . clone ( ) , } ) ; } Err ( ErrorCode :: BadArguments ( format ! ( " " sort_expr ) ) ) }
Extract sort column from sort expression.
Extract sort column from sort expression .
sort_expr
&Expression
left
Option<DataColumnWithField>
right
Option<DataColumnWithField>