Skip to content
Snippets Groups Projects
Unverified Commit 5c0ac48e authored by ou yuanning's avatar ou yuanning Committed by GitHub
Browse files

Change args in case function (#2763)

parent efa7e1d1
No related branches found
No related tags found
No related merge requests found
......@@ -229,62 +229,57 @@ func buildCastExpr(astExpr *tree.CastExpr, ctx CompilerContext, query *Query, no
func buildCaseExpr(astExpr *tree.CaseExpr, ctx CompilerContext, query *Query, node *Node, binderCtx *BinderContext, needAgg bool) (expr *Expr, isAgg bool, err error) {
var args []*Expr
var caseExpr *Expr
if astExpr.Expr != nil {
caseExpr, _, err := buildExpr(astExpr.Expr, ctx, query, node, binderCtx, needAgg)
caseExpr, _, err = buildExpr(astExpr.Expr, ctx, query, node, binderCtx, needAgg)
if err != nil {
return nil, false, err
}
args = append(args, caseExpr)
}
isAgg = true
whenList := make([]*Expr, len(astExpr.Whens))
for idx, whenExpr := range astExpr.Whens {
exprs := make([]*Expr, 2)
expr, paramIsAgg, err := buildExpr(whenExpr.Cond, ctx, query, node, binderCtx, needAgg)
for _, whenExpr := range astExpr.Whens {
condExpr, paramIsAgg, err := buildExpr(whenExpr.Cond, ctx, query, node, binderCtx, needAgg)
if err != nil {
return nil, false, err
}
isAgg = isAgg && paramIsAgg
exprs[0] = expr
expr, paramIsAgg, err = buildExpr(whenExpr.Val, ctx, query, node, binderCtx, needAgg)
if caseExpr != nil {
// rewrite "case col when 1 then '1' else '2'" to "case when col=1 then '1' else '2'"
condExpr, _, err = getFunctionExprByNameAndPlanExprs("=", []*Expr{caseExpr, condExpr})
if err != nil {
return nil, false, err
}
}
args = append(args, condExpr)
valExpr, paramIsAgg, err := buildExpr(whenExpr.Val, ctx, query, node, binderCtx, needAgg)
if err != nil {
return nil, false, err
}
isAgg = isAgg && paramIsAgg
exprs[1] = expr
whenList[idx] = &Expr{
Expr: &plan.Expr_List{
List: &plan.ExprList{
List: exprs,
},
},
Typ: &plan.Type{
Id: plan.Type_TUPLE,
},
}
}
whenExpr := &Expr{
Expr: &plan.Expr_List{
List: &plan.ExprList{
List: whenList,
},
},
Typ: &plan.Type{
Id: plan.Type_TUPLE,
},
args = append(args, valExpr)
}
args = append(args, whenExpr)
if astExpr.Else != nil {
elseExpr, paramIsAgg, err := buildExpr(astExpr.Else, ctx, query, node, binderCtx, needAgg)
elseExpr, _, err := buildExpr(astExpr.Else, ctx, query, node, binderCtx, needAgg)
if err != nil {
return nil, false, err
}
isAgg = isAgg && paramIsAgg
args = append(args, elseExpr)
} else {
args = append(args, &Expr{
Expr: &plan.Expr_C{
C: &Const{
Isnull: true,
},
},
Typ: &plan.Type{
Id: plan.Type_ANY,
Nullable: true,
},
})
}
return getFunctionExprByNameAndPlanExprs("case", args)
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment