Skip to content

Commit 0ce2146

Browse files
committed
chore(embedded/sql): Allow expressions in ORDER BY clauses
Signed-off-by: Stefano Scafiti <[email protected]>
1 parent fef9d8d commit 0ce2146

File tree

10 files changed

+407
-304
lines changed

10 files changed

+407
-304
lines changed

embedded/document/engine.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1408,9 +1408,9 @@ func (e *Engine) CopyCatalogToTx(ctx context.Context, tx *store.OngoingTx) error
14081408
return e.sqlEngine.CopyCatalogToTx(ctx, tx)
14091409
}
14101410

1411-
func generateSQLOrderByClauses(table *sql.Table, orderBy []*protomodel.OrderByClause) (ordCols []*sql.OrdCol) {
1411+
func generateSQLOrderByClauses(table *sql.Table, orderBy []*protomodel.OrderByClause) (ordExps []*sql.OrdExp) {
14121412
for _, col := range orderBy {
1413-
ordCols = append(ordCols, sql.NewOrdCol(table.Name(), col.Field, col.Desc))
1413+
ordExps = append(ordExps, sql.NewOrdCol(table.Name(), col.Field, col.Desc))
14141414
}
1415-
return ordCols
1415+
return ordExps
14161416
}

embedded/sql/catalog.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,34 +266,39 @@ func (i *Index) enginePrefix() []byte {
266266
return i.table.catalog.enginePrefix
267267
}
268268

269-
func (i *Index) coversOrdCols(ordCols []*OrdCol, rangesByColID map[uint32]*typedValueRange) bool {
270-
if !ordColumnsHaveSameDirection(ordCols) {
269+
func (i *Index) coversOrdCols(ordExps []*OrdExp, rangesByColID map[uint32]*typedValueRange) bool {
270+
if !ordExpsHaveSameDirection(ordExps) {
271271
return false
272272
}
273-
return i.hasPrefix(i.cols, ordCols) || i.sortableUsing(ordCols, rangesByColID)
273+
return i.hasPrefix(i.cols, ordExps) || i.sortableUsing(ordExps, rangesByColID)
274274
}
275275

276-
func ordColumnsHaveSameDirection(cols []*OrdCol) bool {
277-
if len(cols) == 0 {
276+
func ordExpsHaveSameDirection(exps []*OrdExp) bool {
277+
if len(exps) == 0 {
278278
return true
279279
}
280280

281-
desc := cols[0].descOrder
282-
for _, ordCol := range cols[1:] {
283-
if ordCol.descOrder != desc {
281+
desc := exps[0].descOrder
282+
for _, e := range exps[1:] {
283+
if e.descOrder != desc {
284284
return false
285285
}
286286
}
287287
return true
288288
}
289289

290-
func (i *Index) hasPrefix(columns []*Column, ordCols []*OrdCol) bool {
291-
if len(ordCols) > len(columns) {
290+
func (i *Index) hasPrefix(columns []*Column, ordExps []*OrdExp) bool {
291+
if len(ordExps) > len(columns) {
292292
return false
293293
}
294294

295-
for j, ordCol := range ordCols {
296-
aggFn, _, colName := ordCol.sel.resolve(i.table.Name())
295+
for j, ordCol := range ordExps {
296+
sel := ordCol.AsSelector()
297+
if sel == nil {
298+
return false
299+
}
300+
301+
aggFn, _, colName := sel.resolve(i.table.Name())
297302
if len(aggFn) > 0 {
298303
return false
299304
}
@@ -306,9 +311,14 @@ func (i *Index) hasPrefix(columns []*Column, ordCols []*OrdCol) bool {
306311
return true
307312
}
308313

309-
func (i *Index) sortableUsing(columns []*OrdCol, rangesByColID map[uint32]*typedValueRange) bool {
314+
func (i *Index) sortableUsing(columns []*OrdExp, rangesByColID map[uint32]*typedValueRange) bool {
310315
// all columns before colID must be fixedValues otherwise the index can not be used
311-
aggFn, _, colName := columns[0].sel.resolve(i.table.Name())
316+
sel := columns[0].AsSelector()
317+
if sel == nil {
318+
return false
319+
}
320+
321+
aggFn, _, colName := sel.resolve(i.table.Name())
312322
if len(aggFn) > 0 {
313323
return false
314324
}
@@ -327,7 +337,6 @@ func (i *Index) sortableUsing(columns []*OrdCol, rangesByColID map[uint32]*typed
327337
if ok && colRange.unitary() {
328338
continue
329339
}
330-
331340
return false
332341
}
333342
return false

0 commit comments

Comments
 (0)