code: func (b *PlanBuilder) resolveHavingAndOrderBy(sel *ast.SelectStmt, p LogicalPlan) (
  	map[*ast.AggregateFuncExpr]int, map[*ast.AggregateFuncExpr]int, error) {
  	extractor := &havingWindowAndOrderbyExprResolver{
  		p:            p,
  		selectFields: sel.Fields.Fields,
  		aggMapper:    make(map[*ast.AggregateFuncExpr]int),
  		colMapper:    b.colMapper,
  		outerSchemas: b.outerSchemas,
  	}
  	if sel.GroupBy != nil {
  		extractor.gbyItems = sel.GroupBy.Items
  	}
  	// Extract agg funcs from having clause.
  	if sel.Having != nil {
  		extractor.curClause = havingClause
  		n, ok := sel.Having.Expr.Accept(extractor)
  		if !ok {
  			return nil, nil, errors.Trace(extractor.err)
  		}
  		sel.Having.Expr = n.(ast.ExprNode)
  	}
  	havingAggMapper := extractor.aggMapper
  	extractor.aggMapper = make(map[*ast.AggregateFuncExpr]int)
  	extractor.orderBy = true
  	extractor.inExpr = false
  	// Extract agg funcs from order by clause.
  	if sel.OrderBy != nil {
  		extractor.curClause = orderByClause
  		for _, item := range sel.OrderBy.Items {
  			n, ok := item.Expr.Accept(extractor)
  			if !ok {
  				return nil, nil, errors.Trace(extractor.err)
  			}
  			item.Expr = n.(ast.ExprNode)
  		}
  	}
  	sel.Fields.Fields = extractor.selectFields
  	return havingAggMapper, extractor.aggMapper, nil
  }
docs: // resolveHavingAndOrderBy will process aggregate functions and resolve the columns that don't exist in select fields.
  // If we found some columns that are not in select fields, we will append it to select fields and update the colMapper.
  // When we rewrite the order by / having expression, we will find column in map at first.