Skip to content

Commit

Permalink
chore(embedded/sql): Allow expressions in ORDER BY clauses
Browse files Browse the repository at this point in the history
Signed-off-by: Stefano Scafiti <[email protected]>
  • Loading branch information
ostafen committed Nov 29, 2024
1 parent fef9d8d commit 0ce2146
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 304 deletions.
6 changes: 3 additions & 3 deletions embedded/document/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,9 +1408,9 @@ func (e *Engine) CopyCatalogToTx(ctx context.Context, tx *store.OngoingTx) error
return e.sqlEngine.CopyCatalogToTx(ctx, tx)
}

func generateSQLOrderByClauses(table *sql.Table, orderBy []*protomodel.OrderByClause) (ordCols []*sql.OrdCol) {
func generateSQLOrderByClauses(table *sql.Table, orderBy []*protomodel.OrderByClause) (ordExps []*sql.OrdExp) {
for _, col := range orderBy {
ordCols = append(ordCols, sql.NewOrdCol(table.Name(), col.Field, col.Desc))
ordExps = append(ordExps, sql.NewOrdCol(table.Name(), col.Field, col.Desc))
}
return ordCols
return ordExps
}
39 changes: 24 additions & 15 deletions embedded/sql/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,34 +266,39 @@ func (i *Index) enginePrefix() []byte {
return i.table.catalog.enginePrefix
}

func (i *Index) coversOrdCols(ordCols []*OrdCol, rangesByColID map[uint32]*typedValueRange) bool {
if !ordColumnsHaveSameDirection(ordCols) {
func (i *Index) coversOrdCols(ordExps []*OrdExp, rangesByColID map[uint32]*typedValueRange) bool {
if !ordExpsHaveSameDirection(ordExps) {
return false
}
return i.hasPrefix(i.cols, ordCols) || i.sortableUsing(ordCols, rangesByColID)
return i.hasPrefix(i.cols, ordExps) || i.sortableUsing(ordExps, rangesByColID)
}

func ordColumnsHaveSameDirection(cols []*OrdCol) bool {
if len(cols) == 0 {
func ordExpsHaveSameDirection(exps []*OrdExp) bool {
if len(exps) == 0 {
return true
}

desc := cols[0].descOrder
for _, ordCol := range cols[1:] {
if ordCol.descOrder != desc {
desc := exps[0].descOrder
for _, e := range exps[1:] {
if e.descOrder != desc {
return false
}
}
return true
}

func (i *Index) hasPrefix(columns []*Column, ordCols []*OrdCol) bool {
if len(ordCols) > len(columns) {
func (i *Index) hasPrefix(columns []*Column, ordExps []*OrdExp) bool {
if len(ordExps) > len(columns) {
return false
}

for j, ordCol := range ordCols {
aggFn, _, colName := ordCol.sel.resolve(i.table.Name())
for j, ordCol := range ordExps {
sel := ordCol.AsSelector()
if sel == nil {
return false
}

aggFn, _, colName := sel.resolve(i.table.Name())
if len(aggFn) > 0 {
return false
}
Expand All @@ -306,9 +311,14 @@ func (i *Index) hasPrefix(columns []*Column, ordCols []*OrdCol) bool {
return true
}

func (i *Index) sortableUsing(columns []*OrdCol, rangesByColID map[uint32]*typedValueRange) bool {
func (i *Index) sortableUsing(columns []*OrdExp, rangesByColID map[uint32]*typedValueRange) bool {
// all columns before colID must be fixedValues otherwise the index can not be used
aggFn, _, colName := columns[0].sel.resolve(i.table.Name())
sel := columns[0].AsSelector()
if sel == nil {
return false
}

aggFn, _, colName := sel.resolve(i.table.Name())
if len(aggFn) > 0 {
return false
}
Expand All @@ -327,7 +337,6 @@ func (i *Index) sortableUsing(columns []*OrdCol, rangesByColID map[uint32]*typed
if ok && colRange.unitary() {
continue
}

return false
}
return false
Expand Down
Loading

0 comments on commit 0ce2146

Please sign in to comment.