Skip to content

Commit

Permalink
feat(chsql): add ORDER BY expression
Browse files Browse the repository at this point in the history
  • Loading branch information
tdakkota committed Jun 5, 2024
1 parent 0dbd8f3 commit 24a5d8d
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/chstorage/chsql/_golden/Test2.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SELECT (span_id) FROM (SELECT (span_id),(timestamp) FROM spans WHERE (true) AND (duration > 3.14) AND (duration < 3.14))
SELECT (span_id) FROM (SELECT (span_id),(timestamp) FROM spans WHERE (true) AND (duration > 3.14) AND (duration < 3.14)) LIMIT 1
1 change: 1 addition & 0 deletions internal/chstorage/chsql/_golden/Test3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT (timestamp) FROM spans ORDER BY timestamp DESC
1 change: 1 addition & 0 deletions internal/chstorage/chsql/_golden/Test4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT (timestamp) FROM spans ORDER BY timestamp ASC,duration DESC
41 changes: 41 additions & 0 deletions internal/chstorage/chsql/chsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@
package chsql

import (
"fmt"
"strings"

"github.com/go-faster/errors"
)

// Order defines sorting order.
type Order uint8

// String implements [fmt.Stringer].
func (o Order) String() string {
switch o {
case Desc:
return "DESC"
case Asc:
return "ASC"
default:
return fmt.Sprintf("unknown order %d", o)
}
}

const (
Desc Order = iota + 1
Asc
)

// Printer prints SQL query.
type Printer struct {
sb strings.Builder
Expand Down Expand Up @@ -164,6 +185,26 @@ func (p *Printer) Where() {
p.Ident("WHERE")
}

// Order writes `ORDER` ident.
func (p *Printer) Order() {
p.Ident("ORDER")
}

// By writes `BY` ident.
func (p *Printer) By() {
p.Ident("BY")
}

// Asc writes `ASC` ident.
func (p *Printer) Asc() {
p.Ident("ASC")
}

// Desc writes `DESC` ident.
func (p *Printer) Desc() {
p.Ident("DESC")
}

// Limit writes `LIMIT` ident.
func (p *Printer) Limit() {
p.Ident("LIMIT")
Expand Down
33 changes: 33 additions & 0 deletions internal/chstorage/chsql/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ type SelectQuery struct {

// where is a set of expression joined by AND
where []expr
order []orderExpr

limit int
}

type orderExpr struct {
expr expr
order Order
}

// Select creates a new [SelectQuery].
func Select(table string, columns ...ResultColumn) *SelectQuery {
return &SelectQuery{
Expand Down Expand Up @@ -50,6 +56,12 @@ func (q *SelectQuery) Where(filters ...expr) *SelectQuery {
return q
}

// Order adds order to query.
func (q *SelectQuery) Order(e expr, order Order) *SelectQuery {
q.order = append(q.order, orderExpr{expr: e, order: order})
return q
}

// Limit sets query limit.
//
// If n is equal to or less than zero, limit is ignored.
Expand Down Expand Up @@ -109,6 +121,27 @@ func (q *SelectQuery) WriteSQL(p *Printer) error {
p.CloseParen()
}
}
if len(q.order) > 0 {
p.Order()
p.By()

for i, e := range q.order {
if i != 0 {
p.Comma()
}
if err := p.WriteExpr(e.expr); err != nil {
return errors.Wrapf(err, "order by %d", i)
}
switch e.order {
case Asc:
p.Asc()
case Desc:
p.Desc()
default:
return errors.Errorf("unexpected order %v", e.order)
}
}
}

if q.limit > 0 {
p.Limit()
Expand Down
28 changes: 28 additions & 0 deletions internal/chstorage/chsql/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,38 @@ func TestSelect(t *testing.T) {
Lt(Ident("duration"), Value[float32](3.14)),
),
Column("span_id", nil),
).
Limit(1)
},
false,
},
{
func() *SelectQuery {
return Select("spans",
Column("timestamp", nil),
).Order(
Ident("timestamp"),
Desc,
)
},
false,
},
{
func() *SelectQuery {
return Select("spans",
Column("timestamp", nil),
).Order(
Ident("timestamp"),
Asc,
).Order(
Ident("duration"),
Desc,
)
},
false,
},

// No columns.
{
func() *SelectQuery { return Select("logs") },
true,
Expand Down

0 comments on commit 24a5d8d

Please sign in to comment.