Skip to content

Commit

Permalink
select t.*, t.customer_id from customer t where email = 'aaa11' order…
Browse files Browse the repository at this point in the history
… by customer_id desc limit 0, 3;

Signed-off-by: yangxuanjia <yangxuanjia@jd.com>
  • Loading branch information
yangxuanjia committed Apr 2, 2021
1 parent cb6747b commit f059aa4
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
19 changes: 13 additions & 6 deletions go/vt/vtgate/engine/comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ import (

// comparer is the struct that has the logic for comparing two rows in the result set
type comparer struct {
orderBy, weightString int
desc bool
orderBy, weightString, starColFixedIndex int
desc bool
}

// compare compares two rows given the comparer and returns which one should be earlier in the result set
// -1 if the first row should be earlier
// 1 is the second row should be earlier
// 0 if both the rows have equal ordering
func (c *comparer) compare(r1, r2 []sqltypes.Value) (int, error) {
cmp, err := evalengine.NullsafeCompare(r1[c.orderBy], r2[c.orderBy])
var colIndex int
if c.starColFixedIndex < len(r1) {
colIndex = c.starColFixedIndex
} else {
colIndex = c.orderBy
}
cmp, err := evalengine.NullsafeCompare(r1[colIndex], r2[colIndex])
if err != nil {
_, isComparisonErr := err.(evalengine.UnsupportedComparisonError)
if !(isComparisonErr && c.weightString != -1) {
Expand All @@ -58,9 +64,10 @@ func extractSlices(input []OrderbyParams) []*comparer {
var result []*comparer
for _, order := range input {
result = append(result, &comparer{
orderBy: order.Col,
weightString: order.WeightStringCol,
desc: order.Desc,
orderBy: order.Col,
weightString: order.WeightStringCol,
desc: order.Desc,
starColFixedIndex: order.StarColFixedIndex,
})
}
return result
Expand Down
5 changes: 3 additions & 2 deletions go/vt/vtgate/engine/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ type OrderbyParams struct {
Col int
// WeightStringCol is the weight_string column that will be used for sorting.
// It is set to -1 if such a column is not added to the query
WeightStringCol int
Desc bool
WeightStringCol int
Desc bool
StarColFixedIndex int
}

func (obp OrderbyParams) String() string {
Expand Down
8 changes: 5 additions & 3 deletions go/vt/vtgate/planbuilder/memory_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ func newMemorySort(plan logicalPlan, orderBy sqlparser.OrderBy) (*memorySort, er
if colNumber == -1 {
return nil, fmt.Errorf("unsupported: memory sort: order by must reference a column in the select list: %s", sqlparser.String(order))
}

ob := engine.OrderbyParams{
Col: colNumber,
WeightStringCol: -1,
Desc: order.Direction == sqlparser.DescOrder,
Col: colNumber,
WeightStringCol: -1,
Desc: order.Direction == sqlparser.DescOrder,
StarColFixedIndex: colNumber,
}
ms.eMemorySort.OrderBy = append(ms.eMemorySort.OrderBy, ob)
}
Expand Down
24 changes: 21 additions & 3 deletions go/vt/vtgate/planbuilder/ordering.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,28 @@ func planRouteOrdering(orderBy sqlparser.OrderBy, node *route) (logicalPlan, err
if colNumber == -1 {
return nil, fmt.Errorf("unsupported: in scatter query: order by must reference a column in the select list: %s", sqlparser.String(order))
}
starColFixedIndex := colNumber
if selectStatement, ok := node.Select.(*sqlparser.Select); ok {
for i, selectExpr := range selectStatement.SelectExprs {
if starExpr, ok := selectExpr.(*sqlparser.StarExpr); ok {
if i < colNumber {
tableName := starExpr.TableName
tableMap := node.resultColumns[i].column.st.tables
tableMeta := tableMap[tableName]
if tableMeta == nil {
break
}
starColFixedIndex += len(tableMeta.columnNames) - 1
}
}
}
}

ob := engine.OrderbyParams{
Col: colNumber,
WeightStringCol: -1,
Desc: order.Direction == sqlparser.DescOrder,
Col: colNumber,
WeightStringCol: -1,
Desc: order.Direction == sqlparser.DescOrder,
StarColFixedIndex: starColFixedIndex,
}
node.eroute.OrderBy = append(node.eroute.OrderBy, ob)

Expand Down

0 comments on commit f059aa4

Please sign in to comment.