Your first query: ClickHouse and CockroachDB
Every file of the ClickHouse and CockroachDB versions side by side, with the lines that differ marked. 3 parameter or column types differ in Go.
Configuration
sgl.yamldiffers
ClickHouse
CockroachDB
1
version: 11
version: 12
2
3
databases:3
databases:4
- name: tasks4
- name: tasks5
dialect: clickhouse5
dialect: cockroachdb6
migrations: db/migrations6
migrations: db/migrations7
queries: db/queries7
queries: db/queries8
go:8
go:9
package: store9
package: store10
out: internal/store10
out: internal/storeMigrations
db/migrations/0001_init.up.sqldiffers
ClickHouse
CockroachDB
1
CREATE TABLE tasks (1
CREATE TABLE tasks (2
id UInt64,2
id INT8 NOT NULL DEFAULT unique_rowid() PRIMARY KEY,3
title String,3
title STRING NOT NULL,4
completed UInt8 DEFAULT 0,4
completed BOOL NOT NULL DEFAULT false,5
created_at DateTime DEFAULT now()5
created_at TIMESTAMPTZ NOT NULL DEFAULT now()6
) ENGINE = MergeTree() ORDER BY id;6
);db/migrations/0001_init.down.sqlidentical
ClickHouse
CockroachDB
1
DROP TABLE tasks;1
DROP TABLE tasks;Queries
CreateTaskdiffers
ClickHouse · db/queries/tasks.sql
CockroachDB · db/queries/tasks.sql
1
-- sgl.query(CreateTask, exec)1
-- sgl.query(CreateTask, one)2
INSERT INTO tasks (id, title) VALUES ({id:UInt64}, {title:String});2
INSERT INTO tasks (title) VALUES ($1) RETURNING id, title, completed, created_at;GetTaskonly ClickHouse
ClickHouse · db/queries/tasks.sql
CockroachDB · not in this version
1
-- sgl.query(GetTask, one)2
SELECT id, title, completed, created_at FROM tasks WHERE id = {id:UInt64};ListTasksdiffers
ClickHouse · db/queries/tasks.sql
CockroachDB · db/queries/tasks.sql
1
-- sgl.query(ListTasks, many)1
-- sgl.query(ListTasks, many)2
SELECT id, title, completed, created_at FROM tasks ORDER BY id;2
SELECT id, title, completed, created_at FROM tasks ORDER BY created_at, id;CompleteTaskdiffers
ClickHouse · db/queries/tasks.sql
CockroachDB · db/queries/tasks.sql
1
-- sgl.query(CompleteTask, asyncexec)1
-- sgl.query(CompleteTask, execrows)2
ALTER TABLE tasks UPDATE completed = 1 WHERE id = {id:UInt64};2
UPDATE tasks SET completed = true WHERE id = $1;What sgl generates
CreateTaskdiffers
ClickHouse
CockroachDB
1
func (queries *Queries) CreateTask(ctx context.Context, params CreateTaskParams) error1
func (queries *Queries) CreateTask(ctx context.Context, title string) (CreateTaskRow, error)2
2
3
type CreateTaskParams struct {3
type CreateTaskRow struct {4
ID uint644
ID int64 `json:"id"`5
Title string5
Title string `json:"title"`6
Completed bool `json:"completed"`7
CreatedAt time.Time `json:"created_at"`6
}8
}GetTaskonly ClickHouse
ClickHouse
CockroachDB · not in this version
1
func (queries *Queries) GetTask(ctx context.Context, id uint64) (GetTaskRow, error)2
3
type GetTaskRow struct {4
ID uint64 `json:"id"`5
Title string `json:"title"`6
Completed uint8 `json:"completed"`7
CreatedAt time.Time `json:"created_at"`8
}ListTasksdiffers
ClickHouse
CockroachDB
1
func (queries *Queries) ListTasks(ctx context.Context) ([]ListTasksRow, error)1
func (queries *Queries) ListTasks(ctx context.Context) ([]ListTasksRow, error)2
2
3
type ListTasksRow struct {3
type ListTasksRow struct {4
ID uint64 `json:"id"`4
ID int64 `json:"id"`5
Title string `json:"title"`5
Title string `json:"title"`6
Completed uint8 `json:"completed"`6
Completed bool `json:"completed"`7
CreatedAt time.Time `json:"created_at"`7
CreatedAt time.Time `json:"created_at"`8
}8
}CompleteTaskdiffers
ClickHouse
CockroachDB
1
func (queries *Queries) CompleteTask(ctx context.Context, id uint64) error1
func (queries *Queries) CompleteTask(ctx context.Context, id int64) (int64, error)The program
example.godiffers
ClickHouse
CockroachDB
1
package main1
package main2
2
3
import (3
import (4
"context"4
"context"5
"database/sql"5
"database/sql"6
"fmt"6
"fmt"7
"io"7
"io"8
8
9
"seagull.sh/seagull/examples/clickhouse/001_first_query/internal/store"9
"seagull.sh/seagull/examples/cockroachdb/001_first_query/internal/store"10
)10
)11
11
12
// run adds two tasks, completes the first and lists them all, through the methods sgl12
// run adds two tasks, completes the first and lists them all, through the methods sgl13
// generated from db/queries/tasks.sql.13
// generated from db/queries/tasks.sql.14
//14
//15
// ClickHouse has no auto-increment and no RETURNING, so the caller chooses each task's id15
// The code is what it would be on PostgreSQL; the differences are in the database.16
// and reads a task back with GetTask. CompleteTask uses ALTER TABLE ... UPDATE, which16
// CockroachDB fills id with unique_rowid(), a large int64 built from the time and the17
// queues a mutation that the server applies in the background, so the query is declared17
// node, so IDs are neither 1 and 2 nor the same from one run to the next, and the list18
// asyncexec and returns only an error (concept 36 explains why). The connection sets18
// prints titles instead. CockroachDB uses SERIALIZABLE isolation by default and may abort19
// mutations_sync=2, which makes the server finish the mutation before it replies, so the19
// one with SQLSTATE 40001 when it conflicts with another. The generated RunInTx does not20
// list below already shows the task completed.20
// retry, so code that uses it on CockroachDB retries the whole function itself.21
//21
//22
// Takes database (*sql.DB) which is a migrated database.22
// Takes database (*sql.DB) which is a migrated database.23
// Takes output (io.Writer) which receives the task list.23
// Takes output (io.Writer) which receives the task list.24
//24
//25
// Returns error when a query fails.25
// Returns error when a query fails.26
func run(ctx context.Context, database *sql.DB, output io.Writer) error {26
func run(ctx context.Context, database *sql.DB, output io.Writer) error {27
queries := store.New(database)27
queries := store.New(database)28
28
29
tasks := []store.CreateTaskParams{29
feed, err := queries.CreateTask(ctx, "Feed the seagulls")30
{ID: 1, Title: "Feed the seagulls"},30
if err != nil {31
{ID: 2, Title: "Count the chips"},31
return fmt.Errorf("creating a task: %w", err)32
}32
}33
for _, task := range tasks {33
if _, err := queries.CreateTask(ctx, "Count the chips"); err != nil {34
if err := queries.CreateTask(ctx, task); err != nil {34
return fmt.Errorf("creating a task: %w", err)35
return fmt.Errorf("creating task %d: %w", task.ID, err)36
}37
}35
}38
36
39
feed, err := queries.GetTask(ctx, 1)37
completed, err := queries.CompleteTask(ctx, feed.ID)40
if err != nil {38
if err != nil {41
return fmt.Errorf("getting task 1: %w", err)42
}43
fmt.Fprintf(output, "created task %d, %s\n", feed.ID, feed.Title)44
45
if err := queries.CompleteTask(ctx, feed.ID); err != nil {46
return fmt.Errorf("completing task %d: %w", feed.ID, err)39
return fmt.Errorf("completing task %d: %w", feed.ID, err)47
}40
}48
fmt.Fprintf(output, "completed task %d\n", feed.ID)41
fmt.Fprintf(output, "completed %d task\n", completed)49
42
50
listed, err := queries.ListTasks(ctx)43
tasks, err := queries.ListTasks(ctx)51
if err != nil {44
if err != nil {52
return fmt.Errorf("listing tasks: %w", err)45
return fmt.Errorf("listing tasks: %w", err)53
}46
}54
for _, task := range listed {47
for _, task := range tasks {55
fmt.Fprintf(output, "%d\t%-18s completed=%d\n", task.ID, task.Title, task.Completed)48
fmt.Fprintf(output, "%-18s completed=%t\n", task.Title, task.Completed)56
}49
}57
return nil50
return nil58
}51
}Go type differences
The same SQL types map to different Go types, because each database reports them differently.
| Query | Field | ClickHouse | CockroachDB |
|---|---|---|---|
| ListTasks | column id | uint64 | int64 |
| ListTasks | column completed | uint8 | bool |
| CompleteTask | parameter id | uint64 | int64 |