Skip to content

Commit 220adef

Browse files
committed
First working tests!!! Simple version of Create Table is working. Looking forward to test Select and implement insert
1 parent 37f9809 commit 220adef

File tree

6 files changed

+187
-7
lines changed

6 files changed

+187
-7
lines changed

internal/engine/yql/catalog.go renamed to internal/engine/ydb/catalog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package yql
1+
package ydb
22

33
import "github.com/sqlc-dev/sqlc/internal/sql/catalog"
44

internal/engine/ydb/catalog_test.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package ydb
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
"github.com/google/go-cmp/cmp/cmpopts"
10+
"github.com/sqlc-dev/sqlc/internal/sql/ast"
11+
"github.com/sqlc-dev/sqlc/internal/sql/catalog"
12+
)
13+
14+
func TestCreateTable(t *testing.T) {
15+
tests := []struct {
16+
stmt string
17+
s *catalog.Schema
18+
}{
19+
{
20+
stmt: `CREATE TABLE users (
21+
id Uint64,
22+
age Int32,
23+
score Float,
24+
PRIMARY KEY (id)
25+
)`,
26+
s: &catalog.Schema{
27+
Name: "main",
28+
Tables: []*catalog.Table{
29+
{
30+
Rel: &ast.TableName{Name: "users"},
31+
Columns: []*catalog.Column{
32+
{
33+
Name: "id",
34+
Type: ast.TypeName{Name: "Uint64"},
35+
IsNotNull: true,
36+
},
37+
{
38+
Name: "age",
39+
Type: ast.TypeName{Name: "Int32"},
40+
},
41+
{
42+
Name: "score",
43+
Type: ast.TypeName{Name: "Float"},
44+
},
45+
},
46+
},
47+
},
48+
},
49+
},
50+
{
51+
stmt: `CREATE TABLE posts (
52+
id Uint64,
53+
title Utf8 NOT NULL,
54+
content String,
55+
metadata Json,
56+
PRIMARY KEY (id)
57+
)`,
58+
s: &catalog.Schema{
59+
Name: "main",
60+
Tables: []*catalog.Table{
61+
{
62+
Rel: &ast.TableName{Name: "posts"},
63+
Columns: []*catalog.Column{
64+
{
65+
Name: "id",
66+
Type: ast.TypeName{Name: "Uint64"},
67+
IsNotNull: true,
68+
},
69+
{
70+
Name: "title",
71+
Type: ast.TypeName{Name: "Utf8"},
72+
IsNotNull: true,
73+
},
74+
{
75+
Name: "content",
76+
Type: ast.TypeName{Name: "String"},
77+
},
78+
{
79+
Name: "metadata",
80+
Type: ast.TypeName{Name: "Json"},
81+
},
82+
},
83+
},
84+
},
85+
},
86+
},
87+
{
88+
stmt: `CREATE TABLE orders (
89+
id Uuid,
90+
amount Decimal(22,9),
91+
created_at Uint64,
92+
PRIMARY KEY (id)
93+
)`,
94+
s: &catalog.Schema{
95+
Name: "main",
96+
Tables: []*catalog.Table{
97+
{
98+
Rel: &ast.TableName{Name: "orders"},
99+
Columns: []*catalog.Column{
100+
{
101+
Name: "id",
102+
Type: ast.TypeName{Name: "Uuid"},
103+
IsNotNull: true,
104+
},
105+
{
106+
Name: "amount",
107+
Type: ast.TypeName{
108+
Name: "Decimal",
109+
Names: &ast.List{
110+
Items: []ast.Node{
111+
&ast.Integer{Ival: 22},
112+
&ast.Integer{Ival: 9},
113+
},
114+
},
115+
},
116+
},
117+
{
118+
Name: "created_at",
119+
Type: ast.TypeName{Name: "Uint64"},
120+
},
121+
},
122+
},
123+
},
124+
},
125+
},
126+
}
127+
128+
p := NewParser()
129+
for i, tc := range tests {
130+
test := tc
131+
t.Run(strconv.Itoa(i), func(t *testing.T) {
132+
stmts, err := p.Parse(strings.NewReader(test.stmt))
133+
if err != nil {
134+
t.Log(test.stmt)
135+
t.Fatal(err)
136+
}
137+
138+
c := newTestCatalog()
139+
if err := c.Build(stmts); err != nil {
140+
t.Log(test.stmt)
141+
t.Fatal(err)
142+
}
143+
144+
e := newTestCatalog()
145+
if test.s != nil {
146+
var replaced bool
147+
for i := range e.Schemas {
148+
if e.Schemas[i].Name == test.s.Name {
149+
e.Schemas[i] = test.s
150+
replaced = true
151+
break
152+
}
153+
}
154+
if !replaced {
155+
e.Schemas = append(e.Schemas, test.s)
156+
}
157+
}
158+
159+
if diff := cmp.Diff(e, c, cmpopts.EquateEmpty(), cmpopts.IgnoreUnexported(catalog.Column{})); diff != "" {
160+
t.Log(test.stmt)
161+
t.Errorf("catalog mismatch:\n%s", diff)
162+
}
163+
})
164+
}
165+
}

internal/engine/yql/convert.go renamed to internal/engine/ydb/convert.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package yql
1+
package ydb
22

33
import (
44
"log"
@@ -403,7 +403,7 @@ func (c *cc) convertCreate_table_stmtContext(n *parser.Create_table_stmtContext)
403403
for _, cname := range conCtx.AllAn_id() {
404404
for _, col := range stmt.Cols {
405405
if col.Colname == parseAnId(cname) {
406-
col.PrimaryKey = true
406+
col.IsNotNull = true
407407
}
408408
}
409409
}
@@ -483,7 +483,7 @@ func (c *cc) convertTypeName(n parser.IType_nameContext) *ast.TypeName {
483483
if decimal := n.Type_name_decimal(); decimal != nil {
484484
if integerOrBinds := decimal.AllInteger_or_bind(); len(integerOrBinds) >= 2 {
485485
return &ast.TypeName{
486-
Name: "DECIMAL",
486+
Name: "Decimal",
487487
TypeOid: 0,
488488
Names: &ast.List{
489489
Items: []ast.Node{
@@ -1443,8 +1443,23 @@ func (c *cc) convertLiteralValue(n *parser.Literal_valueContext) ast.Node {
14431443
}
14441444
}
14451445

1446+
func (c *cc) convertSqlStmt(n *parser.Sql_stmtContext) ast.Node {
1447+
if n == nil {
1448+
return nil
1449+
}
1450+
// todo: handle explain
1451+
if core := n.Sql_stmt_core(); core != nil {
1452+
return c.convert(core)
1453+
}
1454+
1455+
return nil
1456+
}
1457+
14461458
func (c *cc) convert(node node) ast.Node {
14471459
switch n := node.(type) {
1460+
case *parser.Sql_stmtContext:
1461+
return c.convertSqlStmt(n)
1462+
14481463
case *parser.Sql_stmt_coreContext:
14491464
return c.convertSqlStmtCore(n)
14501465

internal/engine/yql/parse.go renamed to internal/engine/ydb/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package yql
1+
package ydb
22

33
import (
44
"errors"

internal/engine/yql/reserved.go renamed to internal/engine/ydb/reserved.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package yql
1+
package ydb
22

33
import "strings"
44

internal/engine/yql/utils.go renamed to internal/engine/ydb/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package yql
1+
package ydb
22

33
import (
44
"strconv"

0 commit comments

Comments
 (0)