Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add semantic actions for ORDER BY. #2157

Merged
merged 1 commit into from
Aug 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions sql/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ func TestParse(t *testing.T) {
{`SELECT FROM t WHERE a = COUNT(*)`},
{`SELECT (a.b) FROM t WHERE (b.c) = 2`},

{`SELECT FROM t ORDER BY a`},
{`SELECT FROM t ORDER BY a ASC`},
{`SELECT FROM t ORDER BY a DESC`},

{`SELECT FROM t HAVING a = b`},

{`SELECT FROM t UNION SELECT 1 FROM t`},
Expand Down Expand Up @@ -395,9 +399,6 @@ func TestParseSyntax(t *testing.T) {
{`SELECT e'\'\"\b\n\r\t\\' FROM t`},
{`SELECT '\x' FROM t`},
{`SELECT 1 FROM t GROUP BY a`},
{`SELECT 1 FROM t ORDER BY a`},
{`SELECT 1 FROM t ORDER BY a ASC`},
{`SELECT 1 FROM t ORDER BY a DESC`},
{`CREATE INDEX a ON b (c)`},
{`CREATE INDEX a ON b.c (d)`},
{`CREATE INDEX ON a (b)`},
Expand Down
36 changes: 28 additions & 8 deletions sql/parser/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,20 +284,40 @@ func (node OrderBy) String() string {
return buf.String()
}

// Direction for ordering results.
type Direction int

// Direction values.
const (
DefaultDirection Direction = iota
Ascending
Descending
)

var directionName = [...]string{
DefaultDirection: "",
Ascending: "ASC",
Descending: "DESC",
}

func (d Direction) String() string {
if d < 0 || d > Direction(len(directionName)-1) {
return fmt.Sprintf("Direction(%d)", d)
}
return directionName[d]
}

// Order represents an ordering expression.
type Order struct {
Expr Expr
Direction string
Direction Direction
}

// Order.Direction
const (
astAsc = " ASC"
astDesc = " DESC"
)

func (node *Order) String() string {
return fmt.Sprintf("%s%s", node.Expr, node.Direction)
if node.Direction == DefaultDirection {
return node.Expr.String()
}
return fmt.Sprintf("%s %s", node.Expr, node.Direction)
}

// Limit represents a LIMIT clause.
Expand Down
Loading