fn try_extract_subquery(&mut self, scalar: &Scalar) -> Result<(Scalar, Vec<SubqueryExpr>)> {
        match scalar {
            Scalar::BoundColumnRef(_) => Ok((scalar.clone(), vec![])),

            Scalar::ConstantExpr(_) => Ok((scalar.clone(), vec![])),

            Scalar::AndExpr(expr) => {
                let (left, result_left) = self.try_extract_subquery(&expr.left)?;
                let (right, result_right) = self.try_extract_subquery(&expr.right)?;
                Ok((
                    AndExpr {
                        left: Box::new(left),
                        right: Box::new(right),
                    }
                    .into(),
                    vec![result_left, result_right].concat(),
                ))
            }

            Scalar::OrExpr(expr) => {
                let (left, result_left) = self.try_extract_subquery(&expr.left)?;
                let (right, result_right) = self.try_extract_subquery(&expr.right)?;
                Ok((
                    AndExpr {
                        left: Box::new(left),
                        right: Box::new(right),
                    }
                    .into(),
                    vec![result_left, result_right].concat(),
                ))
            }

            Scalar::ComparisonExpr(expr) => {
                let (left, result_left) = self.try_extract_subquery(&expr.left)?;
                let (right, result_right) = self.try_extract_subquery(&expr.right)?;
                Ok((
                    AndExpr {
                        left: Box::new(left),
                        right: Box::new(right),
                    }
                    .into(),
                    vec![result_left, result_right].concat(),
                ))
            }

            Scalar::AggregateFunction(_) => Ok((scalar.clone(), vec![])),

            Scalar::FunctionCall(func) => {
                let mut args = vec![];
                let mut s_exprs = vec![];
                for arg in func.arguments.iter() {
                    let (scalar, mut subquery) = self.try_extract_subquery(arg)?;
                    args.push(scalar);
                    s_exprs.append(&mut subquery);
                }

                let expr: Scalar = FunctionCall {
                    arguments: args,
                    func_name: func.func_name.clone(),
                    arg_types: func.arg_types.clone(),
                    return_type: func.return_type.clone(),
                }
                .into();

                Ok((expr, s_exprs))
            }

            Scalar::Cast(cast) => {
                let (scalar, s_exprs) = self.try_extract_subquery(&cast.argument)?;
                Ok((
                    CastExpr {
                        argument: Box::new(scalar),
                        from_type: cast.from_type.clone(),
                        target_type: cast.target_type.clone(),
                    }
                    .into(),
                    s_exprs,
                ))
            }

            Scalar::SubqueryExpr(subquery) => {
                // Extract the subquery and replace it with the ColumnBinding from it.
                let column = subquery
                    .output_context
                    .columns
                    .get(0)
                    .ok_or_else(|| ErrorCode::LogicalError("Invalid subquery"))?;
                let name = format!("subquery_{}", column.index);
                let column_ref = ColumnBinding {
                    table_name: None,
                    column_name: name,
                    index: column.index,
                    data_type: subquery.data_type.clone(),
                    visible_in_unqualified_wildcard: false,
                };

                Ok((BoundColumnRef { column: column_ref }.into(), vec![
                    subquery.clone()
                ]))
            }
        }
    }
/// Try to extract subquery from a scalar expression. Returns replaced scalar expression
/// and the subqueries.
Try to extract subquery from a scalar expression. Returns replaced scalar expression
and the subqueries.
Try to extract subquery from a scalar expression . Returns replaced scalar expression and the subqueries .
fn try_extract_subquery(&mut self, scalar: &Scalar) -> Result<(Scalar, Vec<SubqueryExpr>)> {
        match scalar {
            Scalar::BoundColumnRef(_) => Ok((scalar.clone(), vec![])),
            Scalar::ConstantExpr(_) => Ok((scalar.clone(), vec![])),
            Scalar::AndExpr(expr) => {
                let (left, result_left) = self.try_extract_subquery(&expr.left)?;
                let (right, result_right) = self.try_extract_subquery(&expr.right)?;
                Ok((
                    AndExpr {
                        left: Box::new(left),
                        right: Box::new(right),
                    }
                    .into(),
                    vec![result_left, result_right].concat(),
                ))
            }
            Scalar::OrExpr(expr) => {
                let (left, result_left) = self.try_extract_subquery(&expr.left)?;
                let (right, result_right) = self.try_extract_subquery(&expr.right)?;
                Ok((
                    AndExpr {
                        left: Box::new(left),
                        right: Box::new(right),
                    }
                    .into(),
                    vec![result_left, result_right].concat(),
                ))
            }
            Scalar::ComparisonExpr(expr) => {
                let (left, result_left) = self.try_extract_subquery(&expr.left)?;
                let (right, result_right) = self.try_extract_subquery(&expr.right)?;
                Ok((
                    AndExpr {
                        left: Box::new(left),
                        right: Box::new(right),
                    }
                    .into(),
                    vec![result_left, result_right].concat(),
                ))
            }
            Scalar::AggregateFunction(_) => Ok((scalar.clone(), vec![])),
            Scalar::FunctionCall(func) => {
                let mut args = vec![];
                let mut s_exprs = vec![];
                for arg in func.arguments.iter() {
                    let (scalar, mut subquery) = self.try_extract_subquery(arg)?;
                    args.push(scalar);
                    s_exprs.append(&mut subquery);
                }
                let expr: Scalar = FunctionCall {
                    arguments: args,
                    func_name: func.func_name.clone(),
                    arg_types: func.arg_types.clone(),
                    return_type: func.return_type.clone(),
                }
                .into();
                Ok((expr, s_exprs))
            }
            Scalar::Cast(cast) => {
                let (scalar, s_exprs) = self.try_extract_subquery(&cast.argument)?;
                Ok((
                    CastExpr {
                        argument: Box::new(scalar),
                        from_type: cast.from_type.clone(),
                        target_type: cast.target_type.clone(),
                    }
                    .into(),
                    s_exprs,
                ))
            }
            Scalar::SubqueryExpr(subquery) => {
                // Extract the subquery and replace it with the ColumnBinding from it.
                let column = subquery
                    .output_context
                    .columns
                    .get(0)
                    .ok_or_else(|| ErrorCode::LogicalError("Invalid subquery"))?;
                let name = format!("subquery_{}", column.index);
                let column_ref = ColumnBinding {
                    table_name: None,
                    column_name: name,
                    index: column.index,
                    data_type: subquery.data_type.clone(),
                    visible_in_unqualified_wildcard: false,
                };
                Ok((BoundColumnRef { column: column_ref }.into(), vec![
                    subquery.clone()
                ]))
            }
        }
    }
fn try_extract_subquery ( & mut self , scalar : & Scalar ) -> Result < ( Scalar , Vec < SubqueryExpr > ) > { match scalar { Scalar :: BoundColumnRef ( _ ) => Ok ( ( scalar . clone ( ) , vec ! [ ] ) ) , Scalar :: ConstantExpr ( _ ) => Ok ( ( scalar . clone ( ) , vec ! [ ] ) ) , Scalar :: AndExpr ( expr ) => { let ( left , result_left ) = self . try_extract_subquery ( & expr . left ) ? ; let ( right , result_right ) = self . try_extract_subquery ( & expr . right ) ? ; Ok ( ( AndExpr { left : Box :: new ( left ) , right : Box :: new ( right ) , } . into ( ) , vec ! [ result_left result_right ] . concat ( ) , ) ) } Scalar :: OrExpr ( expr ) => { let ( left , result_left ) = self . try_extract_subquery ( & expr . left ) ? ; let ( right , result_right ) = self . try_extract_subquery ( & expr . right ) ? ; Ok ( ( AndExpr { left : Box :: new ( left ) , right : Box :: new ( right ) , } . into ( ) , vec ! [ result_left result_right ] . concat ( ) , ) ) } Scalar :: ComparisonExpr ( expr ) => { let ( left , result_left ) = self . try_extract_subquery ( & expr . left ) ? ; let ( right , result_right ) = self . try_extract_subquery ( & expr . right ) ? ; Ok ( ( AndExpr { left : Box :: new ( left ) , right : Box :: new ( right ) , } . into ( ) , vec ! [ result_left result_right ] . concat ( ) , ) ) } Scalar :: AggregateFunction ( _ ) => Ok ( ( scalar . clone ( ) , vec ! [ ] ) ) , Scalar :: FunctionCall ( func ) => { let mut args = vec ! [ ] ; let mut s_exprs = vec ! [ ] ; for arg in func . arguments . iter ( ) { let ( scalar , mut subquery ) = self . try_extract_subquery ( arg ) ? ; args . push ( scalar ) ; s_exprs . append ( & mut subquery ) ; } let expr : Scalar = FunctionCall { arguments : args , func_name : func . func_name . clone ( ) , arg_types : func . arg_types . clone ( ) , return_type : func . return_type . clone ( ) , } . into ( ) ; Ok ( ( expr , s_exprs ) ) } Scalar :: Cast ( cast ) => { let ( scalar , s_exprs ) = self . try_extract_subquery ( & cast . argument ) ? ; Ok ( ( CastExpr { argument : Box :: new ( scalar ) , from_type : cast . from_type . clone ( ) , target_type : cast . target_type . clone ( ) , } . into ( ) , s_exprs , ) ) } Scalar :: SubqueryExpr ( subquery ) => { // Extract the subquery and replace it with the ColumnBinding from it. let column = subquery . output_context . columns . get ( 0 ) . ok_or_else ( | | ErrorCode :: LogicalError ( " " ) ) ? ; let name = format ! ( " " column index ) ; let column_ref = ColumnBinding { table_name : None , column_name : name , index : column . index , data_type : subquery . data_type . clone ( ) , visible_in_unqualified_wildcard : false , } ; Ok ( ( BoundColumnRef { column : column_ref } . into ( ) , vec ! [ subquery clone ( ) ] ) ) } } }
Try to extract subquery from a scalar expression.
Try to extract subquery from a scalar expression .
&mut self
scalar
&Scalar
&mut self
scalar
&Scalar