From 18a248399be091dcbdc73c79d1c9f46a0f9de05a Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Tue, 18 Jul 2023 19:05:12 +0700 Subject: [PATCH 1/3] Implement cast parser for MySQL --- internal/compiler/compat.go | 28 +++++++++++++++------------- internal/compiler/to_column.go | 2 +- internal/engine/dolphin/convert.go | 5 ++++- internal/sql/astutils/join.go | 6 +++++- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/internal/compiler/compat.go b/internal/compiler/compat.go index d546f992bb..24d41287c0 100644 --- a/internal/compiler/compat.go +++ b/internal/compiler/compat.go @@ -31,14 +31,11 @@ type Relation struct { } func parseRelation(node ast.Node) (*Relation, error) { - switch n := node.(type) { - - case *ast.Boolean: - return &Relation{ - Name: "bool", - }, nil + if n, ok := node.(*ast.Boolean); ok && n != nil { + return &Relation{Name: "bool"}, nil + } - case *ast.List: + if n, ok := node.(*ast.List); ok && n != nil { parts := stringSlice(n) switch len(parts) { case 1: @@ -59,8 +56,9 @@ func parseRelation(node ast.Node) (*Relation, error) { default: return nil, fmt.Errorf("invalid name: %s", astutils.Join(n, ".")) } + } - case *ast.RangeVar: + if n, ok := node.(*ast.RangeVar); ok && n != nil { name := Relation{} if n.Catalogname != nil { name.Catalog = *n.Catalogname @@ -72,13 +70,17 @@ func parseRelation(node ast.Node) (*Relation, error) { name.Name = *n.Relname } return &name, nil + } - case *ast.TypeName: - return parseRelation(n.Names) - - default: - return nil, fmt.Errorf("unexpected node type: %T", n) + if n, ok := node.(*ast.TypeName); ok && n != nil { + if n.Names != nil { + return parseRelation(n.Names) + } else { + return &Relation{Name: n.Name}, nil + } } + + return nil, fmt.Errorf("unexpected node type: %T", node) } func ParseTableName(node ast.Node) (*ast.TableName, error) { diff --git a/internal/compiler/to_column.go b/internal/compiler/to_column.go index 14dee0ac2f..5d3153de4e 100644 --- a/internal/compiler/to_column.go +++ b/internal/compiler/to_column.go @@ -8,7 +8,7 @@ import ( ) func isArray(n *ast.TypeName) bool { - if n == nil { + if n == nil || n.ArrayBounds == nil { return false } return len(n.ArrayBounds.Items) > 0 diff --git a/internal/engine/dolphin/convert.go b/internal/engine/dolphin/convert.go index bd642c55ed..bdacec6eda 100644 --- a/internal/engine/dolphin/convert.go +++ b/internal/engine/dolphin/convert.go @@ -901,7 +901,10 @@ func (c *cc) convertFrameClause(n *pcast.FrameClause) ast.Node { } func (c *cc) convertFuncCastExpr(n *pcast.FuncCastExpr) ast.Node { - return todo(n) + return &ast.TypeCast{ + Arg: c.convert(n.Expr), + TypeName: &ast.TypeName{Name: types.TypeStr(n.Tp.GetType())}, + } } func (c *cc) convertGetFormatSelectorExpr(n *pcast.GetFormatSelectorExpr) ast.Node { diff --git a/internal/sql/astutils/join.go b/internal/sql/astutils/join.go index 7d2f7829b6..2757e08754 100644 --- a/internal/sql/astutils/join.go +++ b/internal/sql/astutils/join.go @@ -7,7 +7,11 @@ import ( ) func Join(list *ast.List, sep string) string { - items := []string{} + if list == nil { + return "" + } + + var items []string for _, item := range list.Items { if n, ok := item.(*ast.String); ok { items = append(items, n.Str) From 21a740d652d3a0c6eb3a1f2c37641f06c64663c8 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Wed, 19 Jul 2023 08:43:33 +0700 Subject: [PATCH 2/3] Add test for MySQL cast function --- .../testdata/func_call_cast/mysql/go/db.go | 31 ++++++++++++++++ .../func_call_cast/mysql/go/models.go | 7 ++++ .../func_call_cast/mysql/go/query.sql.go | 21 +++++++++++ .../testdata/func_call_cast/mysql/query.sql | 2 + .../testdata/func_call_cast/mysql/sqlc.json | 12 ++++++ .../select_column_cast/mysql/go/db.go | 31 ++++++++++++++++ .../select_column_cast/mysql/go/models.go | 11 ++++++ .../select_column_cast/mysql/go/query.sql.go | 37 +++++++++++++++++++ .../select_column_cast/mysql/query.sql | 4 ++ .../select_column_cast/mysql/sqlc.json | 12 ++++++ 10 files changed, 168 insertions(+) create mode 100644 internal/endtoend/testdata/func_call_cast/mysql/go/db.go create mode 100644 internal/endtoend/testdata/func_call_cast/mysql/go/models.go create mode 100644 internal/endtoend/testdata/func_call_cast/mysql/go/query.sql.go create mode 100644 internal/endtoend/testdata/func_call_cast/mysql/query.sql create mode 100644 internal/endtoend/testdata/func_call_cast/mysql/sqlc.json create mode 100644 internal/endtoend/testdata/select_column_cast/mysql/go/db.go create mode 100644 internal/endtoend/testdata/select_column_cast/mysql/go/models.go create mode 100644 internal/endtoend/testdata/select_column_cast/mysql/go/query.sql.go create mode 100644 internal/endtoend/testdata/select_column_cast/mysql/query.sql create mode 100644 internal/endtoend/testdata/select_column_cast/mysql/sqlc.json diff --git a/internal/endtoend/testdata/func_call_cast/mysql/go/db.go b/internal/endtoend/testdata/func_call_cast/mysql/go/db.go new file mode 100644 index 0000000000..fb6ae669f6 --- /dev/null +++ b/internal/endtoend/testdata/func_call_cast/mysql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.1 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/func_call_cast/mysql/go/models.go b/internal/endtoend/testdata/func_call_cast/mysql/go/models.go new file mode 100644 index 0000000000..90d1991962 --- /dev/null +++ b/internal/endtoend/testdata/func_call_cast/mysql/go/models.go @@ -0,0 +1,7 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.1 + +package querytest + +import () diff --git a/internal/endtoend/testdata/func_call_cast/mysql/go/query.sql.go b/internal/endtoend/testdata/func_call_cast/mysql/go/query.sql.go new file mode 100644 index 0000000000..2b2968b305 --- /dev/null +++ b/internal/endtoend/testdata/func_call_cast/mysql/go/query.sql.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.1 +// source: query.sql + +package querytest + +import ( + "context" +) + +const demo = `-- name: Demo :one +SELECT CAST(GREATEST(1,2,3,4,5) AS UNSIGNED) as col1 +` + +func (q *Queries) Demo(ctx context.Context) (int64, error) { + row := q.db.QueryRowContext(ctx, demo) + var col1 int64 + err := row.Scan(&col1) + return col1, err +} diff --git a/internal/endtoend/testdata/func_call_cast/mysql/query.sql b/internal/endtoend/testdata/func_call_cast/mysql/query.sql new file mode 100644 index 0000000000..b653151a82 --- /dev/null +++ b/internal/endtoend/testdata/func_call_cast/mysql/query.sql @@ -0,0 +1,2 @@ +-- name: Demo :one +SELECT CAST(GREATEST(1,2,3,4,5) AS UNSIGNED) as col1 diff --git a/internal/endtoend/testdata/func_call_cast/mysql/sqlc.json b/internal/endtoend/testdata/func_call_cast/mysql/sqlc.json new file mode 100644 index 0000000000..534b7e24e9 --- /dev/null +++ b/internal/endtoend/testdata/func_call_cast/mysql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "mysql", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/select_column_cast/mysql/go/db.go b/internal/endtoend/testdata/select_column_cast/mysql/go/db.go new file mode 100644 index 0000000000..fb6ae669f6 --- /dev/null +++ b/internal/endtoend/testdata/select_column_cast/mysql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.1 + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/select_column_cast/mysql/go/models.go b/internal/endtoend/testdata/select_column_cast/mysql/go/models.go new file mode 100644 index 0000000000..059ed775f7 --- /dev/null +++ b/internal/endtoend/testdata/select_column_cast/mysql/go/models.go @@ -0,0 +1,11 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.1 + +package querytest + +import () + +type Foo struct { + Bar bool +} diff --git a/internal/endtoend/testdata/select_column_cast/mysql/go/query.sql.go b/internal/endtoend/testdata/select_column_cast/mysql/go/query.sql.go new file mode 100644 index 0000000000..f72ddebb6b --- /dev/null +++ b/internal/endtoend/testdata/select_column_cast/mysql/go/query.sql.go @@ -0,0 +1,37 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.19.1 +// source: query.sql + +package querytest + +import ( + "context" +) + +const selectColumnCast = `-- name: SelectColumnCast :many +SELECT CAST(bar AS UNSIGNED) FROM foo +` + +func (q *Queries) SelectColumnCast(ctx context.Context) ([]int64, error) { + rows, err := q.db.QueryContext(ctx, selectColumnCast) + if err != nil { + return nil, err + } + defer rows.Close() + var items []int64 + for rows.Next() { + var bar int64 + if err := rows.Scan(&bar); err != nil { + return nil, err + } + items = append(items, bar) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/select_column_cast/mysql/query.sql b/internal/endtoend/testdata/select_column_cast/mysql/query.sql new file mode 100644 index 0000000000..2173fd00f3 --- /dev/null +++ b/internal/endtoend/testdata/select_column_cast/mysql/query.sql @@ -0,0 +1,4 @@ +CREATE TABLE foo (bar BOOLEAN NOT NULL); + +-- name: SelectColumnCast :many +SELECT CAST(bar AS UNSIGNED) FROM foo; diff --git a/internal/endtoend/testdata/select_column_cast/mysql/sqlc.json b/internal/endtoend/testdata/select_column_cast/mysql/sqlc.json new file mode 100644 index 0000000000..534b7e24e9 --- /dev/null +++ b/internal/endtoend/testdata/select_column_cast/mysql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "mysql", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} From 928e64bedd0b7d13d70937efb2ea312510598038 Mon Sep 17 00:00:00 2001 From: Radhi Fadlillah Date: Sat, 29 Jul 2023 11:26:29 +0700 Subject: [PATCH 3/3] Add nil check in parseRelation --- internal/compiler/compat.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/internal/compiler/compat.go b/internal/compiler/compat.go index 24d41287c0..bb9e0b33b6 100644 --- a/internal/compiler/compat.go +++ b/internal/compiler/compat.go @@ -31,11 +31,17 @@ type Relation struct { } func parseRelation(node ast.Node) (*Relation, error) { - if n, ok := node.(*ast.Boolean); ok && n != nil { + switch n := node.(type) { + case *ast.Boolean: + if n == nil { + return nil, fmt.Errorf("unexpected nil in %T node", n) + } return &Relation{Name: "bool"}, nil - } - if n, ok := node.(*ast.List); ok && n != nil { + case *ast.List: + if n == nil { + return nil, fmt.Errorf("unexpected nil in %T node", n) + } parts := stringSlice(n) switch len(parts) { case 1: @@ -56,9 +62,11 @@ func parseRelation(node ast.Node) (*Relation, error) { default: return nil, fmt.Errorf("invalid name: %s", astutils.Join(n, ".")) } - } - if n, ok := node.(*ast.RangeVar); ok && n != nil { + case *ast.RangeVar: + if n == nil { + return nil, fmt.Errorf("unexpected nil in %T node", n) + } name := Relation{} if n.Catalogname != nil { name.Catalog = *n.Catalogname @@ -70,17 +78,20 @@ func parseRelation(node ast.Node) (*Relation, error) { name.Name = *n.Relname } return &name, nil - } - if n, ok := node.(*ast.TypeName); ok && n != nil { + case *ast.TypeName: + if n == nil { + return nil, fmt.Errorf("unexpected nil in %T node", n) + } if n.Names != nil { return parseRelation(n.Names) } else { return &Relation{Name: n.Name}, nil } - } - return nil, fmt.Errorf("unexpected node type: %T", node) + default: + return nil, fmt.Errorf("unexpected node type: %T", node) + } } func ParseTableName(node ast.Node) (*ast.TableName, error) {