JSON columns: ClickHouse and SQLite
Every file of the ClickHouse and SQLite versions side by side, with the lines that differ marked. 8 parameter or column types differ in Go.
Configuration
sgl.yamldiffers
ClickHouse
SQLite
1
version: 11
version: 12
2
3
databases:3
databases:4
- name: orders4
- name: orders5
dialect: clickhouse5
dialect: sqlite6
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_orders.up.sqldiffers
ClickHouse
SQLite
1
CREATE TABLE orders (1
CREATE TABLE orders (2
id UInt64,2
id INTEGER PRIMARY KEY,3
customer String,3
customer TEXT NOT NULL,4
basket String,4
basket JSON NOT NULL,5
delivery Nullable(String)5
delivery JSON6
) ENGINE = MergeTree() ORDER BY id;6
);db/migrations/0001_orders.down.sqlidentical
ClickHouse
SQLite
1
DROP TABLE orders;1
DROP TABLE orders;Queries
PlaceOrderdiffers
ClickHouse · db/queries/orders.sql
SQLite · db/queries/orders.sql
1
-- sgl.query(PlaceOrder, exec)1
-- sgl.query(PlaceOrder, exec)2
INSERT INTO orders (id, customer, basket, delivery)2
INSERT INTO orders (customer, basket, delivery) VALUES (?, ?, ?);3
VALUES ({id:UInt64}, {customer:String}, {basket:String}, {delivery:Nullable(String)});GetOrderdiffers
ClickHouse · db/queries/orders.sql
SQLite · db/queries/orders.sql
1
-- sgl.query(GetOrder, one)1
-- sgl.query(GetOrder, one)2
SELECT id, customer, basket, delivery FROM orders WHERE id = {id:UInt64};2
SELECT id, customer, basket, delivery FROM orders WHERE id = ?;OrdersWithFishdiffers
ClickHouse · db/queries/orders.sql
SQLite · db/queries/orders.sql
1
-- sgl.query(OrdersWithFish, many)1
-- sgl.query(OrdersWithFish, many)2
-- ?1 as sgl.param(fish, type: text)2
SELECT3
SELECT3
id,4
id,4
customer,5
customer,5
JSONExtractRaw(basket, 'extras') AS extras,6
basket -> '$.extras' AS extras,6
JSONExtractString(delivery, 'address') AS address7
CAST(delivery ->> '$.address' AS TEXT) AS address7
FROM orders8
FROM orders8
WHERE JSONExtractString(basket, 'fish') = {fish:String}9
WHERE basket ->> '$.fish' = ?19
ORDER BY id;10
ORDER BY id;What sgl generates
PlaceOrderdiffers
ClickHouse
SQLite
1
func (queries *Queries) PlaceOrder(ctx context.Context, params PlaceOrderParams) error1
func (queries *Queries) PlaceOrder(ctx context.Context, params PlaceOrderParams) error2
2
3
type PlaceOrderParams struct {3
type PlaceOrderParams struct {4
ID uint645
Customer string4
Customer string6
Basket string5
Basket JSON7
Delivery *string6
Delivery JSON8
}7
}8
9
type JSON []byteGetOrderdiffers
ClickHouse
SQLite
1
func (queries *Queries) GetOrder(ctx context.Context, id uint64) (GetOrderRow, error)1
func (queries *Queries) GetOrder(ctx context.Context, id int64) (GetOrderRow, error)2
2
3
type GetOrderRow struct {3
type GetOrderRow struct {4
ID uint64 `json:"id"`4
ID int64 `json:"id"`5
Customer string `json:"customer"`5
Customer string `json:"customer"`6
Basket string `json:"basket"`6
Basket JSON `json:"basket"`7
Delivery *string `json:"delivery"`7
Delivery JSON `json:"delivery"`8
}8
}9
10
type JSON []byteOrdersWithFishdiffers
ClickHouse
SQLite
1
func (queries *Queries) OrdersWithFish(ctx context.Context, fish string) ([]OrdersWithFishRow, error)1
func (queries *Queries) OrdersWithFish(ctx context.Context, fish string) ([]OrdersWithFishRow, error)2
2
3
type OrdersWithFishRow struct {3
type OrdersWithFishRow struct {4
ID uint64 `json:"id"`4
ID int64 `json:"id"`5
Customer string `json:"customer"`5
Customer string `json:"customer"`6
Extras string `json:"extras"`6
Extras JSON `json:"extras"`7
Address *string `json:"address"`7
Address *string `json:"address"`8
}8
}9
10
type JSON []byteThe program
example.godiffers
ClickHouse
SQLite
1
package main1
package main2
2
3
import (3
import (4
"context"4
"context"5
"database/sql"5
"database/sql"6
"encoding/json"6
"encoding/json"7
"fmt"7
"fmt"8
"io"8
"io"9
9
10
"seagull.sh/seagull/examples/clickhouse/006_json/internal/store"10
"seagull.sh/seagull/examples/sqlite/006_json/internal/store"11
)11
)12
12
13
// basket is the document an order's basket column holds.13
// basket is the document an order's basket column holds.14
type basket struct {14
type basket struct {15
// Fish is the fish the order is for.15
// Fish is the fish the order is for.16
Fish string `json:"fish"`16
Fish string `json:"fish"`17
17
18
// Portions is how many portions of fish and chips.18
// Portions is how many portions of fish and chips.19
Portions int `json:"portions"`19
Portions int `json:"portions"`20
20
21
// Extras lists the sides.21
// Extras lists the sides.22
Extras []string `json:"extras"`22
Extras []string `json:"extras"`23
}23
}24
24
25
// delivery is the document an order's delivery column holds, when the order is delivered.25
// delivery is the document an order's delivery column holds, when the order is delivered.26
type delivery struct {26
type delivery struct {27
// Address is where the order goes.27
// Address is where the order goes.28
Address string `json:"address"`28
Address string `json:"address"`29
}29
}30
30
31
// run places three chip shop orders and reads them back. The documents live in String31
// run places three chip shop orders and reads them back. A JSON column becomes the32
// columns, the usual home for JSON text in ClickHouse, so Basket is a string and32
// generated store.JSON type, which holds the raw document as bytes: encode a value into33
// Delivery, a Nullable(String), is *string: encode a value into it with json.Marshal and33
// it with json.Marshal and decode it with json.Unmarshal. A nil store.JSON is SQL NULL.34
// decode it with json.Unmarshal. A nil Delivery is SQL NULL.35
//34
//36
// OrdersWithFish reads inside the documents with ClickHouse's JSON functions instead of35
// OrdersWithFish selects with ->, which extracts a JSON fragment, so Extras is store.JSON37
// operators: JSONExtractString(basket, 'fish') extracts a value as text, and36
// again. It filters with ->>, which extracts the SQL value the document holds: text, a38
// JSONExtractRaw(basket, 'extras') extracts the fragment's JSON text. Both return a37
// number or NULL, depending on the document, so Seagull types it as any. The query casts39
// String, which is empty for a missing key rather than NULL, so Extras is a string.38
// the address to TEXT, which makes Address a *string, nil when the delivery column or its40
// Address is *string because the delivery column is Nullable(String), and a JSON function39
// address is missing.41
// given NULL returns NULL.42
//40
//43
// Takes database (*sql.DB) which is a migrated database.41
// Takes database (*sql.DB) which is a migrated database.44
// Takes output (io.Writer) which receives the decoded orders and the search.42
// Takes output (io.Writer) which receives the decoded orders and the search.45
//43
//46
// Returns error when a query fails or a document does not encode or decode.44
// Returns error when a query fails or a document does not encode or decode.47
func run(ctx context.Context, database *sql.DB, output io.Writer) error {45
func run(ctx context.Context, database *sql.DB, output io.Writer) error {48
queries := store.New(database)46
queries := store.New(database)49
47
50
orders := []struct {48
orders := []struct {51
customer string49
customer string52
basket basket50
basket basket53
delivery *delivery51
delivery *delivery54
}{52
}{55
{customer: "Steven", basket: basket{Fish: "cod", Portions: 2, Extras: []string{"mushy peas", "curry sauce"}}},53
{customer: "Steven", basket: basket{Fish: "cod", Portions: 2, Extras: []string{"mushy peas", "curry sauce"}}},56
{customer: "Gwen", basket: basket{Fish: "haddock", Portions: 1, Extras: []string{}}},54
{customer: "Gwen", basket: basket{Fish: "haddock", Portions: 1, Extras: []string{}}},57
{customer: "Morwenna", basket: basket{Fish: "cod", Portions: 1, Extras: []string{"pickled egg"}}, delivery: &delivery{Address: "4 Harbour Row"}},55
{customer: "Morwenna", basket: basket{Fish: "cod", Portions: 1, Extras: []string{"pickled egg"}}, delivery: &delivery{Address: "4 Harbour Row"}},58
}56
}59
for index, order := range orders {57
for _, order := range orders {60
encodedBasket, err := json.Marshal(order.basket)58
encodedBasket, err := json.Marshal(order.basket)61
if err != nil {59
if err != nil {62
return fmt.Errorf("encoding %s's basket: %w", order.customer, err)60
return fmt.Errorf("encoding %s's basket: %w", order.customer, err)63
}61
}64
parameters := store.PlaceOrderParams{ID: uint64(index + 1), Customer: order.customer, Basket: string(encodedBasket), Delivery: nil}62
parameters := store.PlaceOrderParams{Customer: order.customer, Basket: encodedBasket, Delivery: nil}65
if order.delivery != nil {63
if order.delivery != nil {66
encodedDelivery, err := json.Marshal(order.delivery)64
encodedDelivery, err := json.Marshal(order.delivery)67
if err != nil {65
if err != nil {68
return fmt.Errorf("encoding %s's delivery: %w", order.customer, err)66
return fmt.Errorf("encoding %s's delivery: %w", order.customer, err)69
}67
}70
parameters.Delivery = new(string(encodedDelivery))68
parameters.Delivery = encodedDelivery71
}69
}72
if err := queries.PlaceOrder(ctx, parameters); err != nil {70
if err := queries.PlaceOrder(ctx, parameters); err != nil {73
return fmt.Errorf("placing %s's order: %w", order.customer, err)71
return fmt.Errorf("placing %s's order: %w", order.customer, err)74
}72
}75
}73
}76
74
77
first, err := queries.GetOrder(ctx, 1)75
first, err := queries.GetOrder(ctx, 1)78
if err != nil {76
if err != nil {79
return fmt.Errorf("getting order 1: %w", err)77
return fmt.Errorf("getting order 1: %w", err)80
}78
}81
var decoded basket79
var decoded basket82
if err := json.Unmarshal([]byte(first.Basket), &decoded); err != nil {80
if err := json.Unmarshal(first.Basket, &decoded); err != nil {83
return fmt.Errorf("decoding order 1's basket: %w", err)81
return fmt.Errorf("decoding order 1's basket: %w", err)84
}82
}85
fmt.Fprintf(output, "order 1 stored %s\n", first.Basket)83
fmt.Fprintf(output, "order 1 stored %s\n", first.Basket)86
fmt.Fprintf(output, "decoded: %d x %s with %v, delivery is NULL: %t\n", decoded.Portions, decoded.Fish, decoded.Extras, first.Delivery == nil)84
fmt.Fprintf(output, "decoded: %d x %s with %v, delivery is NULL: %t\n", decoded.Portions, decoded.Fish, decoded.Extras, first.Delivery == nil)87
85
88
cod, err := queries.OrdersWithFish(ctx, "cod")86
cod, err := queries.OrdersWithFish(ctx, "cod")89
if err != nil {87
if err != nil {90
return fmt.Errorf("finding cod orders: %w", err)88
return fmt.Errorf("finding cod orders: %w", err)91
}89
}92
fmt.Fprintln(output, "\ncod orders:")90
fmt.Fprintln(output, "\ncod orders:")93
for _, order := range cod {91
for _, order := range cod {94
address := "collecting"92
address := "collecting"95
if order.Address != nil {93
if order.Address != nil {96
address = "to " + *order.Address94
address = "to " + *order.Address97
}95
}98
fmt.Fprintf(output, " order %d %-9s extras %-30s %s\n", order.ID, order.Customer, order.Extras, address)96
fmt.Fprintf(output, " order %d %-9s extras %-30s %s\n", order.ID, order.Customer, order.Extras, address)99
}97
}100
return nil98
return nil101
}99
}Go type differences
The same SQL types map to different Go types, because each database reports them differently.
| Query | Field | ClickHouse | SQLite |
|---|---|---|---|
| PlaceOrder | parameter basket | string | JSON |
| PlaceOrder | parameter delivery | *string | JSON |
| GetOrder | parameter id | uint64 | int64 |
| GetOrder | column id | uint64 | int64 |
| GetOrder | column basket | string | JSON |
| GetOrder | column delivery | *string | JSON |
| OrdersWithFish | column id | uint64 | int64 |
| OrdersWithFish | column extras | string | JSON |