Skip to content

fix(sqlite) fix EXISTS operator #2334

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

Merged
merged 1 commit into from
Jun 20, 2023
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
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/go/models.go

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

29 changes: 29 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/go/query.sql.go

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

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE bar (id int not null primary key autoincrement);

-- name: BarExists :one
SELECT
EXISTS (
SELECT
1
FROM
bar
where
id = ?
);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_exists/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"engine": "sqlite",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/go/models.go

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

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

13 changes: 13 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CREATE TABLE bar (id integer not null primary key autoincrement);

-- name: BarNotExists :one
SELECT
NOT EXISTS (
SELECT
1
FROM
bar
WHERE
id = ?
);

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_not_exists/sqlite/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"engine": "sqlite",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
54 changes: 12 additions & 42 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ type node interface {
GetParser() antlr.Parser
}

func todo(n node) *ast.TODO {
func todo(funcname string, n node) *ast.TODO {
if debug.Active {
log.Printf("sqlite.convert: Unknown node type %T\n", n)
log.Printf("sqlite.%s: Unknown node type %T\n", funcname, n)
}
return &ast.TODO{}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *cc) convertAlter_table_stmtContext(n *parser.Alter_table_stmtContext) a
return stmt
}

return todo(n)
return todo("convertAlter_table_stmtContext", n)
}

func (c *cc) convertAttach_stmtContext(n *parser.Attach_stmtContext) ast.Node {
Expand Down Expand Up @@ -179,7 +179,7 @@ func (c *cc) convertDelete_stmtContext(n *parser.Delete_stmtContext) ast.Node {
return delete
}

return todo(n)
return todo("convertDelete_stmtContext", n)
}

func (c *cc) convertDrop_stmtContext(n *parser.Drop_stmtContext) ast.Node {
Expand All @@ -196,7 +196,7 @@ func (c *cc) convertDrop_stmtContext(n *parser.Drop_stmtContext) ast.Node {
Tables: []*ast.TableName{&name},
}
}
return todo(n)
return todo("convertDrop_stmtContext", n)
}

func (c *cc) convertFuncContext(n *parser.Expr_functionContext) ast.Node {
Expand Down Expand Up @@ -239,7 +239,7 @@ func (c *cc) convertFuncContext(n *parser.Expr_functionContext) ast.Node {
}
}

return todo(n)
return todo("convertFuncContext", n)
}

func (c *cc) convertExprContext(n *parser.ExprContext) ast.Node {
Expand Down Expand Up @@ -445,7 +445,7 @@ func (c *cc) convertOrderby_stmtContext(n parser.IOrder_by_stmtContext) ast.Node
}
return list
}
return todo(n)
return todo("convertOrderby_stmtContext", n)
}

func (c *cc) convertLimit_stmtContext(n parser.ILimit_stmtContext) (ast.Node, ast.Node) {
Expand Down Expand Up @@ -572,7 +572,7 @@ func (c *cc) convertLiteral(n *parser.Expr_literalContext) ast.Node {
}
}
}
return todo(n)
return todo("convertLiteral", n)
}

func (c *cc) convertMathOperationNode(n *parser.Expr_math_opContext) ast.Node {
Expand Down Expand Up @@ -624,41 +624,11 @@ func (c *cc) convertParam(n *parser.Expr_bindContext) ast.Node {
}
}

return todo(n)
return todo("convertParam", n)
}

func (c *cc) convertInSelectNode(n *parser.Expr_in_selectContext) ast.Node {
if n.IN_() == nil && n.EXISTS_() == nil {
return c.convert(n.Select_stmt())
}

lexpr := c.convert(n.Expr(0))
rexprs := []ast.Node{}
for i, expr := range n.AllExpr()[1:] {
if i == 0 {
continue
}
e := c.convert(expr)
switch t := e.(type) {
case *ast.List:
rexprs = append(rexprs, t.Items...)
default:
rexprs = append(rexprs, t)
}
}

var subquery ast.Node = nil
if n.Select_stmt() != nil {
subquery = c.convert(n.Select_stmt())
}

return &ast.In{
Expr: lexpr,
List: rexprs,
Not: n.NOT_() != nil,
Sel: subquery,
Location: n.GetStart().GetStart(),
}
return c.convert(n.Select_stmt())
}

func (c *cc) convertReturning_caluseContext(n parser.IReturning_clauseContext) *ast.List {
Expand Down Expand Up @@ -930,7 +900,7 @@ func (c *cc) convert(node node) ast.Node {

case *parser.Factored_select_stmtContext:
// TODO: need to handle this
return todo(n)
return todo("convert(case=parser.Factored_select_stmtContext)", n)

case *parser.Insert_stmtContext:
return c.convertInsert_stmtContext(n)
Expand All @@ -948,6 +918,6 @@ func (c *cc) convert(node node) ast.Node {
return c.convertUpdate_stmtContext(n)

default:
return todo(n)
return todo("convert(case=default)", n)
}
}