// parseOrderBy parses the "ORDER BY" clause of a query, if it exists.
559316
func (p *Parser) parseOrderBy() (SortFields, error) {
	// Return nil result and nil error if no ORDER token at this position.
	if tok, _, _ := p.ScanIgnoreWhitespace(); tok != ORDER {
		p.Unscan()
		return nil, nil
	}

	// Parse the required BY token.
	if tok, pos, lit := p.ScanIgnoreWhitespace(); tok != BY {
		return nil, newParseError(tokstr(tok, lit), []string{"BY"}, pos)
	}

	// Parse the ORDER BY fields.
	fields, err := p.parseSortFields()
	if err != nil {
		return nil, err
	}

	return fields, nil
}
240083
func (a *CompositeAggregationDateHistogramValuesSource) Order(order string) *CompositeAggregationDateHistogramValuesSource {
	a.order = order
	return a
}
1074493
func (session *Session) OrderBy(order string) *Session {
	session.Statement.OrderBy(order)
	return session
}
1227904
func (api *DatabaseAPI) FilterBy(key string, value interface{}) *DatabaseAPI {
	api.filterBy(key, value, false)
	return api
}
9161
func (a *DataTable) GenOrderBy(args ...func(string, string) string) string {
	return a.orders.Sql(args...)
}
274445
func orderByBindings() ElementHook {
	var f func(st *Statement, ce ConsumedElement) (ElementHook, error)
	f = func(st *Statement, ce ConsumedElement) (ElementHook, error) {
		if ce.IsSymbol() {
			return f, nil
		}
		tkn := ce.Token()
		switch tkn.Type {
		case lexer.ItemBinding:
			st.orderBy = append(st.orderBy, table.SortConfig{{Binding: tkn.Text}}...)
		case lexer.ItemAsc:
			st.orderBy[len(st.orderBy)-1].Desc = false
		case lexer.ItemDesc:
			st.orderBy[len(st.orderBy)-1].Desc = true
		}
		return f, nil
	}
	return f
}
991312
func (b *SelectStmt) OrderBy(col string) *SelectStmt {
	b.Order = append(b.Order, Expr(col))
	return b
}
9451
func (api *DiskAPI) SortBy(key string, reverse bool) *DiskAPI {
	api.sortBy(key, reverse)
	return api
}
390894
func (api *InterfaceAPI) FilterBy(key string, value interface{}) *InterfaceAPI {
	api.filterBy(key, value, false)
	return api
}
927516
func (api *RegionAPI) FilterBy(key string, value interface{}) *RegionAPI {
	api.filterBy(key, value, false)
	return api
}
246312
func (api *NoteAPI) SortBy(key string, reverse bool) *NoteAPI {
	api.sortBy(key, reverse)
	return api
}
583966
func (rb *route) PushOrderBy(order *sqlparser.Order) error {
	// By this time, if any single shard options were already present,
	// multi-sharded options would have already been removed. So, this
	// call is only for checking if the route has single shard options.
	if rb.removeMultishardOptions() {
		rb.Select.AddOrder(order)
		return nil
	}

	// If it's a scatter, we have to populate the OrderBy field.
	colnum := -1
	switch expr := order.Expr.(type) {
	case *sqlparser.SQLVal:
		var err error
		if colnum, err = ResultFromNumber(rb.resultColumns, expr); err != nil {
			return fmt.Errorf("invalid order by: %v", err)
		}
	case *sqlparser.ColName:
		c := expr.Metadata.(*column)
		for i, rc := range rb.resultColumns {
			if rc.column == c {
				colnum = i
				break
			}
		}
	default:
		return fmt.Errorf("unsupported: in scatter query: complex order by expression: %s", sqlparser.String(expr))
	}
	// If column is not found, then the order by is referencing
	// a column that's not on the select list.
	if colnum == -1 {
		return fmt.Errorf("unsupported: in scatter query: order by must reference a column in the select list: %s", sqlparser.String(order))
	}
	ob := engine.OrderbyParams{
		Col:  colnum,
		Desc: order.Direction == sqlparser.DescScr,
	}
	for _, ro := range rb.routeOptions {
		ro.eroute.OrderBy = append(ro.eroute.OrderBy, ob)
	}

	rb.Select.AddOrder(order)
	return nil
}
1089700
func (p *queryPlan) orderBy() {
	order := p.stm.OrderByConfig()
	if len(order) <= 0 {
		return
	}
	tracer.Trace(p.tracer, func() []string {
		return []string{"Ordering by " + order.String()}
	})
	p.tbl.Sort(order)
}
874315
func (api *WebAccelAPI) FilterBy(key string, value interface{}) *WebAccelAPI {
	api.filterBy(key, value, false)
	return api
}
1292514
func (q *MssqlQueryBuilder) BuildOrderByAndLimit(sql string, cols []string, limit int64, offset int64) string {
	orderBy := q.BuildOrderBy(cols)
	if limit < 0 && offset < 0 {
		if orderBy == "" {
			return sql
		}
		return sql + "\n" + orderBy
	}

	// only SQL SERVER 2012 or newer are supported by this method

	if orderBy == "" {
		// ORDER BY clause is required when FETCH and OFFSET are in the SQL
		orderBy = "ORDER BY (SELECT NULL)"
	}
	sql += "\n" + orderBy

	// http://technet.microsoft.com/en-us/library/gg699618.aspx
	if offset < 0 {
		offset = 0
	}
	sql += "\n" + fmt.Sprintf("OFFSET %v ROWS", offset)
	if limit >= 0 {
		sql += "\n" + fmt.Sprintf("FETCH NEXT %v ROWS ONLY", limit)
	}
	return sql
}
1057862
func (q *BaseQueryBuilder) BuildOrderByAndLimit(sql string, cols []string, limit int64, offset int64) string {
	if orderBy := q.BuildOrderBy(cols); orderBy != "" {
		sql += " " + orderBy
	}
	if limit := q.BuildLimit(limit, offset); limit != "" {
		return sql + " " + limit
	}
	return sql
}
832807
func (api *CDROMAPI) SortBy(key string, reverse bool) *CDROMAPI {
	api.sortBy(key, reverse)
	return api
}