Skip to content

Commit 819b8b9

Browse files
authored
dolphin: Add support for coalesce (#802)
For some reason it's a special case in the AST Fixes #725
1 parent ba125cc commit 819b8b9

File tree

11 files changed

+155
-14
lines changed

11 files changed

+155
-14
lines changed

internal/endtoend/testdata/coalesce/sqlc.json renamed to internal/endtoend/testdata/coalesce/mysql/sqlc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"packages": [
44
{
55
"path": "go",
6+
"engine": "mysql",
67
"name": "querytest",
78
"schema": "query.sql",
89
"queries": "query.sql"

internal/endtoend/testdata/coalesce/postgresql/go/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/coalesce/postgresql/go/models.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/coalesce/postgresql/go/query.sql.go

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE foo (bar text, bat text not null);
2+
3+
-- name: Coalesce :many
4+
SELECT coalesce(bar, '') as login
5+
FROM foo;
6+
7+
-- name: CoalesceColumns :many
8+
SELECT bar, bat, coalesce(bar, bat)
9+
FROM foo;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/dolphin/convert.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ func (c *cc) convertFieldList(n *pcast.FieldList) *ast.List {
321321
return &ast.List{Items: fields}
322322
}
323323

324-
func (c *cc) convertFuncCallExpr(n *pcast.FuncCallExpr) *ast.FuncCall {
324+
func (c *cc) convertFuncCallExpr(n *pcast.FuncCallExpr) ast.Node {
325325
schema := n.Schema.String()
326326
name := strings.ToLower(n.FnName.String())
327327

@@ -332,21 +332,28 @@ func (c *cc) convertFuncCallExpr(n *pcast.FuncCallExpr) *ast.FuncCall {
332332
}
333333
items = append(items, &ast.String{Str: name})
334334

335-
fn := &ast.FuncCall{
336-
Args: &ast.List{},
337-
Func: &ast.FuncName{
338-
Schema: schema,
339-
Name: name,
340-
},
341-
Funcname: &ast.List{
342-
Items: items,
343-
},
344-
Location: n.OriginTextPosition(),
345-
}
335+
args := &ast.List{}
346336
for _, arg := range n.Args {
347-
fn.Args.Items = append(fn.Args.Items, c.convert(arg))
337+
args.Items = append(args.Items, c.convert(arg))
338+
}
339+
340+
if schema == "" && name == "coalesce" {
341+
return &ast.CoalesceExpr{
342+
Args: args,
343+
}
344+
} else {
345+
return &ast.FuncCall{
346+
Args: args,
347+
Func: &ast.FuncName{
348+
Schema: schema,
349+
Name: name,
350+
},
351+
Funcname: &ast.List{
352+
Items: items,
353+
},
354+
Location: n.OriginTextPosition(),
355+
}
348356
}
349-
return fn
350357
}
351358

352359
func (c *cc) convertInsertStmt(n *pcast.InsertStmt) *ast.InsertStmt {

0 commit comments

Comments
 (0)