// Final Projections project final select columns for result-writing
514793
func NewProjectionFinal(ctx *Context, p *Select) (*Projection, error) {
	s := &Projection{
		P:        p,
		Stmt:     p.Stmt,
		PlanBase: NewPlanBase(false),
		Final:    true,
	}
	var err error
	if len(p.Stmt.From) == 0 {
		err = s.loadLiteralProjection(ctx)
	} else if len(p.From) == 1 && p.From[0].Proj != nil {
		s.Proj = p.From[0].Proj
	} else {
		err = s.loadFinal(ctx, true)
	}
	if err != nil {
		return nil, err
	}
	return s, nil
}
1105005
func (s *Schema) ColumnsByIndices(offsets []int) []*Column {
	cols := make([]*Column, 0, len(offsets))
	for _, offset := range offsets {
		cols = append(cols, s.Columns[offset])
	}
	return cols
}
760413
func (t *Table) Select(columns ...string) (stmt string, names []string) {
	if len(columns) == 0 {
		return t.sel.stmt, t.sel.names
	}

	return qb.Select(t.metadata.Name).
		Columns(columns...).
		Where(t.primaryKeyCmp[0:len(t.metadata.PartKey)]...).
		ToCql()
}
828703
func (conn *FakeVTGateConn) StreamExecuteShards(ctx context.Context, query string, keyspace string, shards []string, bindVars map[string]*querypb.BindVariable, tabletType topodatapb.TabletType, options *querypb.ExecuteOptions) (sqltypes.ResultStream, error) {
	panic("not implemented")
}
1305144
func (s *rowSegment) Columns() []uint64 {
	a := make([]uint64, 0, s.Count())
	itr := s.data.Iterator()
	for v, eof := itr.Next(); !eof; v, eof = itr.Next() {
		a = append(a, v)
	}
	return a
}
358376
func eliminatePhysicalProjection(p PhysicalPlan) PhysicalPlan {
	oldSchema := p.Schema()
	newRoot := doPhysicalProjectionElimination(p)
	newCols := newRoot.Schema().Columns
	for i, oldCol := range oldSchema.Columns {
		oldCol.Index = newCols[i].Index
		newRoot.Schema().Columns[i] = oldCol
	}
	return newRoot
}
1172851
func (b *SelectBuilder) Columns(columns ...string) *SelectBuilder {
	b.columns = append(b.columns, columns...)
	return b
}
541329
func NewProjection(ctx *plan.Context, p *plan.Projection) *Projection {
	if p.Final {
		return NewProjectionFinal(ctx, p)
	}
	return NewProjectionInProcess(ctx, p)
}
531330
func (b *Builder) Select(cols ...string) *Builder {
	b.selects = cols
	b.optype = selectType
	return b
}
1092197
func (pb *primitiveBuilder) processDMLTable(tableExprs sqlparser.TableExprs) (*routeOption, error) {
	if err := pb.processTableExprs(tableExprs); err != nil {
		return nil, err
	}
	rb, ok := pb.bldr.(*route)
	if !ok {
		return nil, errors.New("unsupported: multi-shard or vindex write statement")
	}
	ro := rb.routeOptions[0]
	for _, sub := range ro.substitutions {
		*sub.oldExpr = *sub.newExpr
	}
	return ro, nil
}
499629
func (p *LogicalWindow) GetWindowResultColumn() *expression.Column {
	return p.schema.Columns[p.schema.Len()-1]
}
131035
func (s *Client) Final() []byte {
	return sasl.MakeMessage(s.clientReplyNotProof(), makeKeyValue('p', sasl.Base64ToBytes(s.proof())))
}
1183118
func (b *PlanBuilder) buildProjection(p LogicalPlan, fields []*ast.SelectField, mapper map[*ast.AggregateFuncExpr]int, considerWindow bool) (LogicalPlan, int, error) {
	b.optFlag |= flagEliminateProjection
	b.curClause = fieldList
	proj := LogicalProjection{Exprs: make([]expression.Expression, 0, len(fields))}.Init(b.ctx)
	schema := expression.NewSchema(make([]*expression.Column, 0, len(fields))...)
	oldLen := 0
	for i, field := range fields {
		if !field.Auxiliary {
			oldLen++
		}

		isWindowFuncField := ast.HasWindowFlag(field.Expr)
		// Although window functions occurs in the select fields, but it has to be processed after having clause.
		// So when we build the projection for select fields, we need to skip the window function.
		// When `considerWindow` is false, we will only build fields for non-window functions, so we add fake placeholders.
		// for window functions. These fake placeholders will be erased in column pruning.
		// When `considerWindow` is true, all the non-window fields have been built, so we just use the schema columns.
		if considerWindow && !isWindowFuncField {
			col := p.Schema().Columns[i]
			proj.Exprs = append(proj.Exprs, col)
			schema.Append(col)
			continue
		} else if !considerWindow && isWindowFuncField {
			expr := expression.Zero
			proj.Exprs = append(proj.Exprs, expr)
			col, err := b.buildProjectionField(proj.id, schema.Len()+1, field, expr)
			if err != nil {
				return nil, 0, err
			}
			schema.Append(col)
			continue
		}
		newExpr, np, err := b.rewrite(field.Expr, p, mapper, true)
		if err != nil {
			return nil, 0, err
		}

		p = np
		proj.Exprs = append(proj.Exprs, newExpr)

		col, err := b.buildProjectionField(proj.id, schema.Len()+1, field, newExpr)
		if err != nil {
			return nil, 0, err
		}
		schema.Append(col)
	}
	proj.SetSchema(schema)
	proj.SetChildren(p)
	return proj, oldLen, nil
}
584222
func (e *ProjectionExec) Exec() *flow.Dataset {
	d := e.Src.Exec()

	// fmt.Printf("=================\n")
	// fmt.Printf("context: %+v\n", e.ctx)

	var inputs []string
	for _, col := range e.Src.Schema().Columns {
		// fmt.Printf("input: %s TblName:%s DBName:%s FromID:%s %d\n", col, col.TblName, col.DBName, col.FromID, col.Position)
		inputs = append(inputs, col.ColName.String())
	}

	/*
		for _, col := range e.Schema().Columns {
			fmt.Printf("output:%s TblName:%s DBName:%s FromID:%s %d\n", col, col.TblName, col.DBName, col.FromID, col.Position)
		}
	*/

	var outputs []string
	for _, expr := range e.exprs {
		// fmt.Printf("expression: %s %+v\n", expr.String(), reflect.TypeOf(expr))
		outputs = append(outputs, expr.String())
	}

	inputParams := removeTableName(strings.Join(inputs, ","))
	outputParams := removeTableName(strings.Join(outputs, ","))

	if inputParams == outputParams {
		return d
	}

	/*
			sqlText := fmt.Sprintf(`
		        function(%s)
		          return %s
		        end
		    `, inputParams, outputParams)
	*/

	// println(sqlText)

	// ret := d.Map("map", sqlText)
	ret := d

	return ret
}
1256316
func (f *BatchFuture) Results() (uint64, error) {
	<-f.waitCh
	return f.index, f.err
}
9246
func (t *Table) C(cols ...string) ValExprBuilder {
	if len(cols) == 0 {
		return ValExprBuilder{makeErrVal("no columns specified")}
	}

	if len(cols) == 1 {
		name := cols[0]
		if _, ok := t.ColumnMap[name]; !ok {
			return ValExprBuilder{makeErrVal("unknown column: %s", name)}
		}
		return ValExprBuilder{&ColName{Name: name, Qualifier: t.aliasOrName()}}
	}
	list := make([]interface{}, len(cols))
	for i, name := range cols {
		if _, ok := t.ColumnMap[name]; !ok {
			list[i] = makeErrVal("unknown column: %s", name)
		} else {
			list[i] = &ColName{Name: name, Qualifier: t.aliasOrName()}
		}
	}
	return ValExprBuilder{makeValTuple(list)}
}
1361803
func (ta *Table) Done() {
	if !ta.HasPrimary() {
		return
	}
	pkIndex := ta.Indexes[0]
	ta.PKColumns = make([]int, len(pkIndex.Columns))
	for i, pkCol := range pkIndex.Columns {
		ta.PKColumns[i] = ta.FindColumn(pkCol)
	}
}