Skip to content

Commit cc8779a

Browse files
committed
Initial YQL support: SELECT logic, some convert tests, and YDB engine integration
- Added reserved keywords and parsing logic - CREATE TABLE support and tests - SELECT initial support and tests - Initial YDB engine integration and examples
1 parent da09201 commit cc8779a

File tree

23 files changed

+3396
-45
lines changed

23 files changed

+3396
-45
lines changed

docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ services:
1919
POSTGRES_DB: postgres
2020
POSTGRES_PASSWORD: mysecretpassword
2121
POSTGRES_USER: postgres
22+
23+
ydb:
24+
image: ydbplatform/local-ydb:latest
25+
ports:
26+
- "2135:2135"
27+
- "2136:2136"
28+
- "8765:8765"
29+
restart: always
30+
environment:
31+
- YDB_USE_IN_MEMORY_PDISKS=true
32+
- GRPC_TLS_PORT=2135
33+
- GRPC_PORT=2136
34+
- MON_PORT=8765

examples/authors/sqlc.yaml

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,65 @@ version: '2'
22
cloud:
33
project: "01HAQMMECEYQYKFJN8MP16QC41"
44
sql:
5-
- name: postgresql
6-
schema: postgresql/schema.sql
7-
queries: postgresql/query.sql
8-
engine: postgresql
9-
database:
10-
uri: "${VET_TEST_EXAMPLES_POSTGRES_AUTHORS}"
11-
analyzer:
12-
database: false
13-
rules:
14-
- sqlc/db-prepare
15-
- postgresql-query-too-costly
5+
# - name: postgresql
6+
# schema: postgresql/schema.sql
7+
# queries: postgresql/query.sql
8+
# engine: postgresql
9+
# database:
10+
# uri: "${VET_TEST_EXAMPLES_POSTGRES_AUTHORS}"
11+
# analyzer:
12+
# database: false
13+
# rules:
14+
# - sqlc/db-prepare
15+
# - postgresql-query-too-costly
16+
# gen:
17+
# go:
18+
# package: authors
19+
# sql_package: pgx/v5
20+
# out: postgresql
21+
# - name: mysql
22+
# schema: mysql/schema.sql
23+
# queries: mysql/query.sql
24+
# engine: mysql
25+
# database:
26+
# uri: "${VET_TEST_EXAMPLES_MYSQL_AUTHORS}"
27+
# rules:
28+
# - sqlc/db-prepare
29+
# # - mysql-query-too-costly
30+
# gen:
31+
# go:
32+
# package: authors
33+
# out: mysql
34+
# - name: sqlite
35+
# schema: sqlite/schema.sql
36+
# queries: sqlite/query.sql
37+
# engine: sqlite
38+
# database:
39+
# uri: file:authors?mode=memory&cache=shared
40+
# rules:
41+
# - sqlc/db-prepare
42+
# gen:
43+
# go:
44+
# package: authors
45+
# out: sqlite
46+
- name: ydb
47+
schema: ydb/schema.sql
48+
queries: ydb/query.sql
49+
engine: ydb
50+
# database:
51+
# uri: file:authors?mode=memory&cache=shared
52+
# rules:
53+
# - sqlc/db-prepare
1654
gen:
1755
go:
1856
package: authors
19-
sql_package: pgx/v5
20-
out: postgresql
21-
- name: mysql
22-
schema: mysql/schema.sql
23-
queries: mysql/query.sql
24-
engine: mysql
25-
database:
26-
uri: "${VET_TEST_EXAMPLES_MYSQL_AUTHORS}"
27-
rules:
28-
- sqlc/db-prepare
29-
# - mysql-query-too-costly
30-
gen:
31-
go:
32-
package: authors
33-
out: mysql
34-
- name: sqlite
35-
schema: sqlite/schema.sql
36-
queries: sqlite/query.sql
37-
engine: sqlite
38-
database:
39-
uri: file:authors?mode=memory&cache=shared
40-
rules:
41-
- sqlc/db-prepare
42-
gen:
43-
go:
44-
package: authors
45-
out: sqlite
46-
rules:
47-
- name: postgresql-query-too-costly
48-
message: "Too costly"
49-
rule: "postgresql.explain.plan.total_cost > 300.0"
50-
- name: mysql-query-too-costly
51-
message: "Too costly"
52-
rule: "has(mysql.explain.query_block.cost_info) && double(mysql.explain.query_block.cost_info.query_cost) > 2.0"
57+
out: ydb
58+
59+
60+
# rules:
61+
# - name: postgresql-query-too-costly
62+
# message: "Too costly"
63+
# rule: "postgresql.explain.plan.total_cost > 300.0"
64+
# - name: mysql-query-too-costly
65+
# message: "Too costly"
66+
# rule: "has(mysql.explain.query_block.cost_info) && double(mysql.explain.query_block.cost_info.query_cost) > 2.0"

examples/authors/ydb/db.go

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

examples/authors/ydb/db_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package authors
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/sqlc-dev/sqlc/internal/sqltest/local"
8+
_ "github.com/ydb-platform/ydb-go-sdk/v3"
9+
)
10+
11+
func TestAuthors(t *testing.T) {
12+
ctx := context.Background()
13+
14+
test := local.YDB(t, []string{"schema.sql"})
15+
defer test.DB.Close()
16+
17+
q := New(test.DB)
18+
19+
t.Run("ListAuthors", func(t *testing.T) {
20+
authors, err := q.ListAuthors(ctx)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
if len(authors) == 0 {
25+
t.Fatal("expected at least one author, got none")
26+
}
27+
t.Log("Authors:")
28+
for _, a := range authors {
29+
bio := "NULL"
30+
if a.Bio.Valid {
31+
bio = a.Bio.String
32+
}
33+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
34+
}
35+
})
36+
37+
t.Run("GetAuthor", func(t *testing.T) {
38+
singleAuthor, err := q.GetAuthor(ctx, 10)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
bio := "NULL"
43+
if singleAuthor.Bio.Valid {
44+
bio = singleAuthor.Bio.String
45+
}
46+
t.Logf("- ID: %d | Name: %s | Bio: %s", singleAuthor.ID, singleAuthor.Name, bio)
47+
})
48+
49+
t.Run("GetAuthorByName", func(t *testing.T) {
50+
authors, err := q.GetAuthorsByName(ctx, "Александр Пушкин")
51+
if err != nil {
52+
t.Fatal(err)
53+
}
54+
if len(authors) == 0 {
55+
t.Fatal("expected at least one author with this name, got none")
56+
}
57+
t.Log("Authors with this name:")
58+
for _, a := range authors {
59+
bio := "NULL"
60+
if a.Bio.Valid {
61+
bio = a.Bio.String
62+
}
63+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
64+
}
65+
})
66+
67+
t.Run("ListAuthorsWithIdModulo", func(t *testing.T) {
68+
authors, err := q.ListAuthorsWithIdModulo(ctx)
69+
if err != nil {
70+
t.Fatal(err)
71+
}
72+
if len(authors) == 0 {
73+
t.Fatal("expected at least one author with even ID, got none")
74+
}
75+
t.Log("Authors with even IDs:")
76+
for _, a := range authors {
77+
bio := "NULL"
78+
if a.Bio.Valid {
79+
bio = a.Bio.String
80+
}
81+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
82+
}
83+
})
84+
85+
t.Run("ListAuthorsWithNullBio", func(t *testing.T) {
86+
authors, err := q.ListAuthorsWithNullBio(ctx)
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
if len(authors) == 0 {
91+
t.Fatal("expected at least one author with NULL bio, got none")
92+
}
93+
t.Log("Authors with NULL bio:")
94+
for _, a := range authors {
95+
bio := "NULL"
96+
if a.Bio.Valid {
97+
bio = a.Bio.String
98+
}
99+
t.Logf("- ID: %d | Name: %s | Bio: %s", a.ID, a.Name, bio)
100+
}
101+
})
102+
}

examples/authors/ydb/models.go

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

examples/authors/ydb/query.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- name: ListAuthors :many
2+
SELECT * FROM authors;
3+
4+
-- name: GetAuthor :one
5+
SELECT * FROM authors
6+
WHERE id = $p0;
7+
8+
-- name: ListAuthorsWithIdModulo :many
9+
SELECT * FROM authors
10+
WHERE id % 2 = 0;
11+
12+
-- name: GetAuthorsByName :many
13+
SELECT * FROM authors
14+
WHERE name = $p0;
15+
16+
-- name: ListAuthorsWithNullBio :many
17+
SELECT * FROM authors
18+
WHERE bio IS NULL;
19+

0 commit comments

Comments
 (0)