Skip to content

Fix ORDER BY arg parsing in sqlite #3962

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/order_by_binds/sqlite/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/endtoend/testdata/order_by_binds/sqlite/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions internal/endtoend/testdata/order_by_binds/sqlite/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions internal/endtoend/testdata/order_by_binds/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- name: ListAuthorsColumnSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY CASE WHEN sqlc.arg(sort_column) = 'name' THEN name END;

-- name: ListAuthorsColumnSortDirection :many
SELECT * FROM authors
WHERE id > ?
ORDER BY
CASE
WHEN @order_by = 'asc' THEN name
END ASC,
CASE
WHEN @order_by = 'desc' OR @order_by IS NULL THEN name
END DESC;

-- name: ListAuthorsColumnSortFnWtihArg :many
SELECT * FROM authors
ORDER BY id % ?;

-- name: ListAuthorsNameSort :many
SELECT * FROM authors
WHERE id > sqlc.arg(min_id)
ORDER BY name ASC;
5 changes: 5 additions & 0 deletions internal/endtoend/testdata/order_by_binds/sqlite/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE authors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
bio TEXT
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/order_by_binds/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "sqlite",
"name": "order_by_binds",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
61 changes: 61 additions & 0 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,11 @@ func (c *cc) convertMultiSelect_stmtContext(n *parser.Select_stmtContext) ast.No
}

limitCount, limitOffset := c.convertLimit_stmtContext(n.Limit_stmt())
sortClause := c.buildSortClause(n.Order_by_stmt())

selectStmt.LimitCount = limitCount
selectStmt.LimitOffset = limitOffset
selectStmt.SortClause = sortClause
selectStmt.WithClause = &ast.WithClause{Ctes: &ctes}
return selectStmt
}
Expand Down Expand Up @@ -1212,3 +1215,61 @@ func (c *cc) convert(node node) ast.Node {
return todo("convert(case=default)", n)
}
}

// buildSortClause converts an IOrder_by_stmtContext into an *ast.List of *ast.SortBy nodes.
func (c *cc) buildSortClause(orderByNode parser.IOrder_by_stmtContext) *ast.List {
if orderByNode == nil {
return nil
}

orderByCtx, ok := orderByNode.(*parser.Order_by_stmtContext)
if !ok {
if debug.Active {
log.Printf("sqlite.buildSortClause: unexpected type %T for IOrder_by_stmtContext", orderByNode)
}
return nil
}

if len(orderByCtx.AllOrdering_term()) == 0 {
return nil
}

sortItems := &ast.List{Items: []ast.Node{}}
for _, otermIP := range orderByCtx.AllOrdering_term() {
oterm, ok := otermIP.(*parser.Ordering_termContext)
if !ok {
if debug.Active {
log.Printf("sqlite.buildSortClause: unexpected type %T for IOrdering_termContext", otermIP)
}
continue
}

sortByDir := ast.SortByDirDefault
if adNode := oterm.Asc_desc(); adNode != nil {
// Asc_descContext has ASC_() and DESC_() methods which return TerminalNode
if adNode.ASC_() != nil {
sortByDir = ast.SortByDirAsc
} else if adNode.DESC_() != nil {
sortByDir = ast.SortByDirDesc
}
}

sortByNulls := ast.SortByNullsDefault
if oterm.NULLS_() != nil { // NULLS_() is a TerminalNode
if oterm.FIRST_() != nil { // FIRST_() is a TerminalNode
sortByNulls = ast.SortByNullsFirst
} else if oterm.LAST_() != nil { // LAST_() is a TerminalNode
sortByNulls = ast.SortByNullsLast
}
}

sortItems.Items = append(sortItems.Items, &ast.SortBy{
Node: c.convert(oterm.Expr()),
SortbyDir: sortByDir,
SortbyNulls: sortByNulls,
UseOp: &ast.List{}, // Typically empty for standard SQLite ORDER BY
Location: oterm.GetStart().GetStart(),
})
}
return sortItems
}
Loading