func (b *planBuilder) buildProjection(p LogicalPlan, fields []*ast.SelectField, mapper map[*ast.AggregateFuncExpr]int) (LogicalPlan, int) {
	proj := &Projection{
		Exprs:           make([]expression.Expression, 0, len(fields)),
		baseLogicalPlan: newBaseLogicalPlan(Proj, b.allocator),
	}
	proj.self = proj
	proj.initIDAndContext(b.ctx)
	proj.correlated = p.IsCorrelated()
	schema := make(expression.Schema, 0, len(fields))
	oldLen := 0
	for _, field := range fields {
		newExpr, np, err := b.rewrite(field.Expr, p, mapper, true)
		if err != nil {
			b.err = errors.Trace(err)
			return nil, oldLen
		}
		p = np
		proj.correlated = proj.correlated || newExpr.IsCorrelated()
		proj.Exprs = append(proj.Exprs, newExpr)
		var tblName, colName model.CIStr
		if field.AsName.L != "" {
			colName = field.AsName
		} else if c, ok := newExpr.(*expression.Column); ok && !c.IsAggOrSubq {
			if astCol, ok := getInnerFromParentheses(field.Expr).(*ast.ColumnNameExpr); ok {
				colName = astCol.Name.Name
				tblName = astCol.Name.Table
			} else {
				colName = c.ColName
				tblName = c.TblName
			}
		} else {
			// When the query is select t.a from t group by a; The Column Name should be a but not t.a;
			if agg, ok := field.Expr.(*ast.AggregateFuncExpr); ok && agg.F == ast.AggFuncFirstRow {
				if col, ok := agg.Args[0].(*ast.ColumnNameExpr); ok {
					colName = col.Name.Name
				}
			} else {
				innerExpr := getInnerFromParentheses(field.Expr)
				if _, ok := innerExpr.(*ast.ValueExpr); ok && innerExpr.Text() != "" {
					colName = model.NewCIStr(innerExpr.Text())
				} else {
					colName = model.NewCIStr(field.Text())
				}
			}
		}
		schemaCol := &expression.Column{
			FromID:  proj.id,
			TblName: tblName,
			ColName: colName,
			RetType: newExpr.GetType(),
		}
		if !field.Auxiliary {
			oldLen++
		}
		schema = append(schema, schemaCol)
		schemaCol.Position = len(schema)
	}
	proj.SetSchema(schema)
	addChild(proj, p)
	return proj, oldLen
}
// buildProjection returns a Projection plan and non-aux columns length.
buildProjection returns a Projection plan and non-aux columns length.
// buildProjection returns a Projection plan and non - aux columns length .
func (b *planBuilder) buildProjection(p LogicalPlan, fields []*ast.SelectField, mapper map[*ast.AggregateFuncExpr]int) (LogicalPlan, int) {
	proj := &Projection{
		Exprs:           make([]expression.Expression, 0, len(fields)),
		baseLogicalPlan: newBaseLogicalPlan(Proj, b.allocator),
	}
	proj.self = proj
	proj.initIDAndContext(b.ctx)
	proj.correlated = p.IsCorrelated()
	schema := make(expression.Schema, 0, len(fields))
	oldLen := 0
	for _, field := range fields {
		newExpr, np, err := b.rewrite(field.Expr, p, mapper, true)
		if err != nil {
			b.err = errors.Trace(err)
			return nil, oldLen
		}
		p = np
		proj.correlated = proj.correlated || newExpr.IsCorrelated()
		proj.Exprs = append(proj.Exprs, newExpr)
		var tblName, colName model.CIStr
		if field.AsName.L != "" {
			colName = field.AsName
		} else if c, ok := newExpr.(*expression.Column); ok && !c.IsAggOrSubq {
			if astCol, ok := getInnerFromParentheses(field.Expr).(*ast.ColumnNameExpr); ok {
				colName = astCol.Name.Name
				tblName = astCol.Name.Table
			} else {
				colName = c.ColName
				tblName = c.TblName
			}
		} else {
			if agg, ok := field.Expr.(*ast.AggregateFuncExpr); ok && agg.F == ast.AggFuncFirstRow {
				if col, ok := agg.Args[0].(*ast.ColumnNameExpr); ok {
					colName = col.Name.Name
				}
			} else {
				innerExpr := getInnerFromParentheses(field.Expr)
				if _, ok := innerExpr.(*ast.ValueExpr); ok && innerExpr.Text() != "" {
					colName = model.NewCIStr(innerExpr.Text())
				} else {
					colName = model.NewCIStr(field.Text())
				}
			}
		}
		schemaCol := &expression.Column{
			FromID:  proj.id,
			TblName: tblName,
			ColName: colName,
			RetType: newExpr.GetType(),
		}
		if !field.Auxiliary {
			oldLen++
		}
		schema = append(schema, schemaCol)
		schemaCol.Position = len(schema)
	}
	proj.SetSchema(schema)
	addChild(proj, p)
	return proj, oldLen
}
func ( b * planBuilder ) buildProjection ( p LogicalPlan , fields [ ] * ast . SelectField , mapper map [ * ast . AggregateFuncExpr ] int ) ( LogicalPlan , int ) { proj := & Projection { Exprs : make ( [ ] expression . Expression , 0 , len ( fields ) ) , baseLogicalPlan : newBaseLogicalPlan ( Proj , b . allocator ) , } proj . self = proj proj . initIDAndContext ( b . ctx ) proj . correlated = p . IsCorrelated ( ) schema := make ( expression . Schema , 0 , len ( fields ) ) oldLen := 0 for _ , field := range fields { newExpr , np , err := b . rewrite ( field . Expr , p , mapper , true ) if err != nil { b . err = errors . Trace ( err ) return nil , oldLen } p = np proj . correlated = proj . correlated || newExpr . IsCorrelated ( ) proj . Exprs = append ( proj . Exprs , newExpr ) var tblName , colName model . CIStr if field . AsName . L != " " { colName = field . AsName } else if c , ok := newExpr . ( * expression . Column ) ; ok && ! c . IsAggOrSubq { if astCol , ok := getInnerFromParentheses ( field . Expr ) . ( * ast . ColumnNameExpr ) ; ok { colName = astCol . Name . Name tblName = astCol . Name . Table } else { colName = c . ColName tblName = c . TblName } } else { if agg , ok := field . Expr . ( * ast . AggregateFuncExpr ) ; ok && agg . F == ast . AggFuncFirstRow { if col , ok := agg . Args [ 0 ] . ( * ast . ColumnNameExpr ) ; ok { colName = col . Name . Name } } else { innerExpr := getInnerFromParentheses ( field . Expr ) if _ , ok := innerExpr . ( * ast . ValueExpr ) ; ok && innerExpr . Text ( ) != " " { colName = model . NewCIStr ( innerExpr . Text ( ) ) } else { colName = model . NewCIStr ( field . Text ( ) ) } } } schemaCol := & expression . Column { FromID : proj . id , TblName : tblName , ColName : colName , RetType : newExpr . GetType ( ) , } if ! field . Auxiliary { oldLen ++ } schema = append ( schema , schemaCol ) schemaCol . Position = len ( schema ) } proj . SetSchema ( schema ) addChild ( proj , p ) return proj , oldLen }
buildProjection returns a Projection plan and non-aux columns length.
buildProjection returns a Projection plan and non - aux columns length .
// When the query is select t.a from t group by a; The Column Name should be a but not t.a;