Types through CTEs, window functions and views: TimescaleDB and DuckDB
Every file of the TimescaleDB and DuckDB versions side by side, with the lines that differ marked. 4 parameter or column types differ in Go.
Configuration
sgl.yamldiffers
TimescaleDB
DuckDB
1
version: 11
version: 12
2
3
databases:3
databases:4
- name: colony4
- name: colony5
dialect: postgres5
dialect: duckdb6
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/store11
driver: database/sql12
migrate:13
dsn: ${env:DATABASE_URL}Migrations
db/migrations/0001_gulls.up.sqldiffers
TimescaleDB
DuckDB
1
CREATE SEQUENCE gull_ids;2
3
CREATE SEQUENCE theft_ids;4
1
CREATE TABLE gulls (5
CREATE TABLE gulls (2
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,6
id BIGINT PRIMARY KEY DEFAULT nextval('gull_ids'),3
name TEXT NOT NULL,7
name VARCHAR NOT NULL,4
parent_id BIGINT REFERENCES gulls (id),8
parent_id BIGINT REFERENCES gulls (id),5
beach TEXT NOT NULL9
beach VARCHAR NOT NULL6
);10
);7
11
8
CREATE TABLE thefts (12
CREATE TABLE thefts (9
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,13
id BIGINT PRIMARY KEY DEFAULT nextval('theft_ids'),10
gull_id BIGINT NOT NULL REFERENCES gulls (id),14
gull_id BIGINT NOT NULL REFERENCES gulls (id),11
snack TEXT NOT NULL,15
snack VARCHAR NOT NULL,12
stolen_on DATE NOT NULL16
stolen_on VARCHAR NOT NULL13
);17
);14
18
15
CREATE VIEW beach_records AS19
CREATE VIEW beach_records AS16
SELECT20
SELECT17
gulls.beach,21
gulls.beach,18
COUNT(DISTINCT gulls.id) AS gulls,22
COUNT(DISTINCT gulls.id) AS gulls,19
COUNT(thefts.id) AS thefts,23
COUNT(thefts.id) AS thefts,20
MAX(thefts.stolen_on) AS last_theft24
MAX(thefts.stolen_on) AS last_theft21
FROM gulls25
FROM gulls22
LEFT JOIN thefts ON thefts.gull_id = gulls.id26
LEFT JOIN thefts ON thefts.gull_id = gulls.id23
GROUP BY gulls.beach;27
GROUP BY gulls.beach;db/migrations/0001_gulls.down.sqldiffers
TimescaleDB
DuckDB
1
DROP VIEW beach_records;1
DROP VIEW beach_records;2
DROP TABLE thefts;2
DROP TABLE thefts;3
DROP TABLE gulls;3
DROP TABLE gulls;4
DROP SEQUENCE theft_ids;5
DROP SEQUENCE gull_ids;Queries
HatchGullidentical
TimescaleDB · db/queries/gulls.sql
DuckDB · db/queries/gulls.sql
1
-- sgl.query(HatchGull, exec)1
-- sgl.query(HatchGull, exec)2
INSERT INTO gulls (name, parent_id, beach) VALUES ($1, $2, $3);2
INSERT INTO gulls (name, parent_id, beach) VALUES ($1, $2, $3);RecordTheftidentical
TimescaleDB · db/queries/gulls.sql
DuckDB · db/queries/gulls.sql
1
-- sgl.query(RecordTheft, exec)1
-- sgl.query(RecordTheft, exec)2
INSERT INTO thefts (gull_id, snack, stolen_on) VALUES ($1, $2, $3);2
INSERT INTO thefts (gull_id, snack, stolen_on) VALUES ($1, $2, $3);FamilyTreediffers
TimescaleDB · db/queries/gulls.sql
DuckDB · db/queries/gulls.sql
1
-- sgl.query(FamilyTree, many)1
-- sgl.query(FamilyTree, many)2
WITH RECURSIVE descendants AS (2
WITH RECURSIVE family AS (3
SELECT id, name, parent_id, 0::BIGINT AS generation3
SELECT id, name, parent_id, 0 AS generation4
FROM gulls4
FROM gulls5
WHERE id = $15
WHERE id = $16
UNION ALL6
UNION ALL7
SELECT gulls.id, gulls.name, gulls.parent_id, descendants.generation + 17
SELECT gulls.id, gulls.name, gulls.parent_id, family.generation + 18
FROM gulls8
FROM gulls9
JOIN descendants ON gulls.parent_id = descendants.id9
JOIN family ON gulls.parent_id = family.id10
)10
)11
SELECT descendants.name, descendants.generation, parents.name AS parent11
SELECT family.name, family.generation, parents.name AS parent12
FROM descendants12
FROM family13
LEFT JOIN gulls AS parents ON parents.id = descendants.parent_id13
LEFT JOIN gulls AS parents ON parents.id = family.parent_id14
ORDER BY descendants.generation, descendants.id;14
ORDER BY family.generation, family.id;TheftLeaguediffers
TimescaleDB · db/queries/gulls.sql
DuckDB · db/queries/gulls.sql
1
-- sgl.query(TheftLeague, many)1
-- sgl.query(TheftLeague, many)2
WITH totals AS (2
WITH totals AS (3
SELECT gulls.name, gulls.beach, COUNT(thefts.id) AS thefts3
SELECT gulls.name, gulls.beach, COUNT(thefts.id) AS thefts4
FROM gulls4
FROM gulls5
LEFT JOIN thefts ON thefts.gull_id = gulls.id5
LEFT JOIN thefts ON thefts.gull_id = gulls.id6
GROUP BY gulls.id6
GROUP BY gulls.id, gulls.name, gulls.beach7
)7
)8
SELECT8
SELECT9
name,9
name,10
beach,10
beach,11
thefts,11
thefts,12
RANK() OVER (PARTITION BY beach ORDER BY thefts DESC) AS beach_rank,12
RANK() OVER (PARTITION BY beach ORDER BY thefts DESC) AS beach_rank,13
SUM(thefts) OVER (PARTITION BY beach) AS beach_thefts13
SUM(thefts) OVER (PARTITION BY beach) AS beach_thefts14
FROM totals14
FROM totals15
ORDER BY beach, beach_rank, name;15
ORDER BY beach, beach_rank, name;BeachRecordsdiffers
TimescaleDB · db/queries/gulls.sql
DuckDB · db/queries/gulls.sql
1
-- sgl.query(BeachRecords, many)1
-- sgl.query(BeachRecords, many)2
SELECT * FROM beach_records ORDER BY beach;2
SELECT beach, gulls, thefts, last_theft FROM beach_records ORDER BY beach;What sgl generates
HatchGullidentical
TimescaleDB
DuckDB
1
func (queries *Queries) HatchGull(ctx context.Context, params HatchGullParams) error1
func (queries *Queries) HatchGull(ctx context.Context, params HatchGullParams) error2
2
3
type HatchGullParams struct {3
type HatchGullParams struct {4
Name string4
Name string5
ParentID *int645
ParentID *int646
Beach string6
Beach string7
}7
}RecordTheftdiffers
TimescaleDB
DuckDB
1
func (queries *Queries) RecordTheft(ctx context.Context, params RecordTheftParams) error1
func (queries *Queries) RecordTheft(ctx context.Context, params RecordTheftParams) error2
2
3
type RecordTheftParams struct {3
type RecordTheftParams struct {4
GullID int644
GullID int645
Snack string5
Snack string6
StolenOn time.Time6
StolenOn string7
}7
}FamilyTreediffers
TimescaleDB
DuckDB
1
func (queries *Queries) FamilyTree(ctx context.Context, id int64) ([]FamilyTreeRow, error)1
func (queries *Queries) FamilyTree(ctx context.Context, id int64) ([]FamilyTreeRow, error)2
2
3
type FamilyTreeRow struct {3
type FamilyTreeRow struct {4
Name string `json:"name"`4
Name string `json:"name"`5
Generation int64 `json:"generation"`5
Generation int32 `json:"generation"`6
Parent *string `json:"parent"`6
Parent *string `json:"parent"`7
}7
}TheftLeaguediffers
TimescaleDB
DuckDB
1
func (queries *Queries) TheftLeague(ctx context.Context) ([]TheftLeagueRow, error)1
func (queries *Queries) TheftLeague(ctx context.Context) ([]TheftLeagueRow, error)2
2
3
type TheftLeagueRow struct {3
type TheftLeagueRow struct {4
Name string `json:"name"`4
Name string `json:"name"`5
Beach string `json:"beach"`5
Beach string `json:"beach"`6
Thefts int64 `json:"thefts"`6
Thefts int64 `json:"thefts"`7
BeachRank int64 `json:"beach_rank"`7
BeachRank int64 `json:"beach_rank"`8
BeachThefts *apd.Decimal `json:"beach_thefts"`8
BeachThefts *big.Int `json:"beach_thefts"`9
}9
}BeachRecordsdiffers
TimescaleDB
DuckDB
1
func (queries *Queries) BeachRecords(ctx context.Context) ([]BeachRecordsRow, error)1
func (queries *Queries) BeachRecords(ctx context.Context) ([]BeachRecordsRow, error)2
2
3
type BeachRecordsRow struct {3
type BeachRecordsRow struct {4
Beach string `json:"beach"`4
Beach string `json:"beach"`5
Gulls int64 `json:"gulls"`5
Gulls int64 `json:"gulls"`6
Thefts int64 `json:"thefts"`6
Thefts int64 `json:"thefts"`7
LastTheft *time.Time `json:"last_theft"`7
LastTheft *string `json:"last_theft"`8
}8
}The program
example.godiffers
TimescaleDB
DuckDB
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
"time"9
8
10
"seagull.sh/seagull/examples/postgres/019_ctes_and_windows/internal/store"9
"seagull.sh/seagull/examples/duckdb/019_ctes_and_windows/internal/store"11
)10
)12
11
13
// run hatches a colony of gulls, records their snack thefts and reads them back three12
// run hatches a colony of gulls, records their snack thefts and reads them back three14
// ways. Seagull follows each column's type and nullability through the SQL:13
// ways. Seagull follows each column's type and nullability through the SQL:15
//14
//16
// - FamilyTree's recursive CTE starts from one gull and joins its chicks generation by15
// - FamilyTree's recursive CTE starts from one gull and joins its chicks generation by17
// generation; generation is an int64 from 0::BIGINT AS generation and generation + 1,16
// generation; generation is an integer from 0 AS generation and generation + 1, and18
// and parent is *string because the LEFT JOIN finds no parent for the first gull;17
// parent is *string because the LEFT JOIN finds no parent for the first gull;19
// - TheftLeague counts thefts in a CTE, then ranks gulls on each beach with RANK() and18
// - TheftLeague counts thefts in a CTE, then ranks gulls on each beach with RANK() and20
// totals the beach with SUM() OVER; COUNT and RANK are bigint, so int64, while SUM of19
// totals the beach with SUM() OVER; RANK is a BIGINT that is never NULL, so int64.21
// a bigint is numeric in PostgreSQL, so BeachThefts is a nullable *apd.Decimal;20
// DuckDB widens the SUM of a BIGINT to a 128-bit HUGEINT, which arrives as *big.Int,21
// and nil stands for NULL, as SQL sums no rows to NULL;22
// - BeachRecords reads the beach_records view from the migration as if it were a table,22
// - BeachRecords reads the beach_records view from the migration as if it were a table,23
// and its columns keep the types the view's own SELECT gives them: MAX over a DATE23
// and its columns keep the types the view's own SELECT gives them.24
// column is a *time.Time, nil for a beach with no thefts. The query is written as25
// SELECT *, and the generated SQL names the view's four columns instead, so a26
// migration that later adds a column to the view does not break the code already27
// deployed, which scans exactly those four. go.keep_stars: true in sgl.yaml keeps the28
// star as written.29
//24
//30
// Takes database (*sql.DB) which is a migrated database.25
// Takes database (*sql.DB) which is a migrated database.31
// Takes output (io.Writer) which receives the three listings.26
// Takes output (io.Writer) which receives the three listings.32
//27
//33
// Returns error when a query fails.28
// Returns error when a query fails.34
func run(ctx context.Context, database *sql.DB, output io.Writer) error {29
func run(ctx context.Context, database *sql.DB, output io.Writer) error {35
queries := store.New(database)30
queries := store.New(database)36
31
37
gulls := []store.HatchGullParams{32
gulls := []store.HatchGullParams{38
{Name: "Steven", ParentID: nil, Beach: "Porthmeor"},33
{Name: "Steven", ParentID: nil, Beach: "Porthmeor"},39
{Name: "Gwen", ParentID: new(int64(1)), Beach: "Porthmeor"},34
{Name: "Gwen", ParentID: new(int64(1)), Beach: "Porthmeor"},40
{Name: "Jago", ParentID: new(int64(1)), Beach: "Porthminster"},35
{Name: "Jago", ParentID: new(int64(1)), Beach: "Porthminster"},41
{Name: "Kerensa", ParentID: new(int64(2)), Beach: "Porthmeor"},36
{Name: "Kerensa", ParentID: new(int64(2)), Beach: "Porthmeor"},42
{Name: "Tristan", ParentID: nil, Beach: "Porthminster"},37
{Name: "Tristan", ParentID: nil, Beach: "Porthminster"},43
{Name: "Morwenna", ParentID: nil, Beach: "Gwithian"},38
{Name: "Morwenna", ParentID: nil, Beach: "Gwithian"},44
}39
}45
for _, gull := range gulls {40
for _, gull := range gulls {46
if err := queries.HatchGull(ctx, gull); err != nil {41
if err := queries.HatchGull(ctx, gull); err != nil {47
return fmt.Errorf("hatching %s: %w", gull.Name, err)42
return fmt.Errorf("hatching %s: %w", gull.Name, err)48
}43
}49
}44
}50
thefts := []store.RecordTheftParams{45
thefts := []store.RecordTheftParams{51
{GullID: 1, Snack: "chips", StolenOn: july(1)},46
{GullID: 1, Snack: "chips", StolenOn: "2026-07-01"},52
{GullID: 1, Snack: "pasty", StolenOn: july(3)},47
{GullID: 1, Snack: "pasty", StolenOn: "2026-07-03"},53
{GullID: 1, Snack: "ice cream", StolenOn: july(4)},48
{GullID: 1, Snack: "ice cream", StolenOn: "2026-07-04"},54
{GullID: 2, Snack: "chips", StolenOn: july(2)},49
{GullID: 2, Snack: "chips", StolenOn: "2026-07-02"},55
{GullID: 3, Snack: "pasty", StolenOn: july(2)},50
{GullID: 3, Snack: "pasty", StolenOn: "2026-07-02"},56
{GullID: 3, Snack: "chips", StolenOn: july(5)},51
{GullID: 3, Snack: "chips", StolenOn: "2026-07-05"},57
{GullID: 5, Snack: "sandwich", StolenOn: july(3)},52
{GullID: 5, Snack: "sandwich", StolenOn: "2026-07-03"},58
}53
}59
for _, theft := range thefts {54
for _, theft := range thefts {60
if err := queries.RecordTheft(ctx, theft); err != nil {55
if err := queries.RecordTheft(ctx, theft); err != nil {61
return fmt.Errorf("recording a theft: %w", err)56
return fmt.Errorf("recording a theft: %w", err)62
}57
}63
}58
}64
59
65
family, err := queries.FamilyTree(ctx, 1)60
family, err := queries.FamilyTree(ctx, 1)66
if err != nil {61
if err != nil {67
return fmt.Errorf("tracing Steven's family: %w", err)62
return fmt.Errorf("tracing Steven's family: %w", err)68
}63
}69
fmt.Fprintln(output, "Steven's family:")64
fmt.Fprintln(output, "Steven's family:")70
for _, member := range family {65
for _, member := range family {71
fmt.Fprintf(output, " generation %d %-8s parent %s\n", member.Generation, member.Name, orNull(member.Parent))66
fmt.Fprintf(output, " generation %d %-8s parent %s\n", member.Generation, member.Name, orNull(member.Parent))72
}67
}73
68
74
league, err := queries.TheftLeague(ctx)69
league, err := queries.TheftLeague(ctx)75
if err != nil {70
if err != nil {76
return fmt.Errorf("ranking thieves: %w", err)71
return fmt.Errorf("ranking thieves: %w", err)77
}72
}78
fmt.Fprintln(output, "theft league:")73
fmt.Fprintln(output, "theft league:")79
for _, entry := range league {74
for _, entry := range league {80
beachThefts := "NULL"75
fmt.Fprintf(output, " %-13s #%d %-8s %d of %s\n", entry.Beach, entry.BeachRank, entry.Name, entry.Thefts, entry.BeachThefts)81
if entry.BeachThefts != nil {82
beachThefts = entry.BeachThefts.String()83
}84
fmt.Fprintf(output, " %-13s #%d %-8s %d of %s\n", entry.Beach, entry.BeachRank, entry.Name, entry.Thefts, beachThefts)85
}76
}86
77
87
records, err := queries.BeachRecords(ctx)78
records, err := queries.BeachRecords(ctx)88
if err != nil {79
if err != nil {89
return fmt.Errorf("reading beach records: %w", err)80
return fmt.Errorf("reading beach records: %w", err)90
}81
}91
fmt.Fprintln(output, "beach records:")82
fmt.Fprintln(output, "beach records:")92
for _, record := range records {83
for _, record := range records {93
lastTheft := "NULL"84
fmt.Fprintf(output, " %-13s gulls %d, thefts %d, last theft %s\n", record.Beach, record.Gulls, record.Thefts, orNull(record.LastTheft))94
if record.LastTheft != nil {95
lastTheft = record.LastTheft.Format(time.DateOnly)96
}97
fmt.Fprintf(output, " %-13s gulls %d, thefts %d, last theft %s\n", record.Beach, record.Gulls, record.Thefts, lastTheft)98
}85
}99
return nil86
return nil100
}87
}101
88
102
// july returns a day in July 2026, the month the thefts happen in.103
//104
// Takes day (int) which is the day of the month.105
//106
// Returns time.Time which is midnight UTC on that day, as a DATE column holds it.107
func july(day int) time.Time {108
return time.Date(2026, time.July, day, 0, 0, 0, 0, time.UTC)109
}110
111
// orNull formats a nullable field, or returns NULL when the field is nil.89
// orNull formats a nullable field, or returns NULL when the field is nil.112
//90
//113
// Takes value (*T) which is a nullable field.91
// Takes value (*T) which is a nullable field.114
//92
//115
// Returns string which is the text to print.93
// Returns string which is the text to print.116
func orNull[T any](value *T) string {94
func orNull[T any](value *T) string {117
if value == nil {95
if value == nil {118
return "NULL"96
return "NULL"119
}97
}120
return fmt.Sprint(*value)98
return fmt.Sprint(*value)121
}99
}Go type differences
The same SQL types map to different Go types, because each database reports them differently.
| Query | Field | TimescaleDB | DuckDB |
|---|---|---|---|
| RecordTheft | parameter stolen_on | time.Time | string |
| FamilyTree | column generation | int64 | int32 |
| TheftLeague | column beach_thefts | *apd.Decimal | *big.Int |
| BeachRecords | column last_theft | *time.Time | *string |