Usage
One section per use case. Every code block is copied from an Example function of the package, which runs as a test.
Rows are the easy level: struct tags. Columns are the fast level: typed slices, batch by batch. A batch and its views are valid for one loop iteration, use slices.Clone to keep values.
Read rows
A file goes into structs. A nullable column goes into a sql.Null field or a pointer.
f, err := vortex.OpenFile("testdata/fixtures/0.96.0/extra/default_ints.vortex")
if err != nil {
log.Fatal(err)
}
defer f.Close()
type Row struct {
Sorted sql.Null[int64] `vortex:"sorted"`
}
for r, err := range vortex.Rows[Row](context.Background(), f, vortex.Query{Rows: vortex.Range{End: 3}}) {
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Sorted.V)
}
Read columns
Columns, batch by batch, without structs. Values copies a numeric column into a slice.
f, err := vortex.OpenFile("testdata/fixtures/0.96.0/extra/default_ints.vortex")
if err != nil {
log.Fatal(err)
}
defer f.Close()
for b, err := range f.Scan(context.Background(), vortex.Query{Rows: vortex.Range{End: 4}}, "sorted") {
if err != nil {
log.Fatal(err)
}
v, err := vortex.Values[int64](b.Col(0))
if err != nil {
log.Fatal(err)
}
fmt.Println(b.Len(), v)
}
Project
The names after the query are the projection: only the columns you list are read. In the example above, only sorted is.
With rows, the fields of the struct are the projection.
Filter and take a row range
A Query has a filter and a row range. The statistics of the file skip the zones that cannot hold a row, and no row is returned that does not match.
f, err := vortex.OpenFile("testdata/fixtures/0.96.0/extra/default_ints.vortex")
if err != nil {
log.Fatal(err)
}
defer f.Close()
type Row struct {
Sorted sql.Null[int64] `vortex:"sorted"`
}
q := vortex.Query{Filter: vortex.Col("sorted").Lt(1003), Rows: vortex.Range{End: 10}}
for r, err := range vortex.Rows[Row](context.Background(), f, q) {
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Sorted.V)
}
The same filter by columns:
f, err := vortex.OpenFile("testdata/fixtures/0.96.0/extra/default_ints.vortex")
if err != nil {
log.Fatal(err)
}
defer f.Close()
q := vortex.Query{Filter: vortex.Col("sorted").Lt(1003), Rows: vortex.Range{End: 10}}
for b, err := range f.Scan(context.Background(), q, "sorted") {
if err != nil {
log.Fatal(err)
}
v, err := vortex.Values[int64](b.Col(0))
if err != nil {
log.Fatal(err)
}
fmt.Println(v)
}
Write rows
A slice of structs goes to a file. The columns come from the fields, and the tags are the ones of Rows.
type Trade struct {
ID int64 `vortex:"id"`
Symbol string `vortex:"symbol"`
Price float64 `vortex:"price"`
At time.Time `vortex:"at"`
}
f, err := os.CreateTemp("", "trades-*.vortex")
if err != nil {
log.Fatal(err)
}
defer os.Remove(f.Name())
w := vortex.NewRowWriter[Trade](f, vortex.WriteOptions{})
n, err := w.Write([]Trade{
{1, "ACME", 10.5, time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)},
{2, "ACME", 10.75, time.Date(2026, 1, 2, 3, 4, 6, 0, time.UTC)},
})
if err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
fmt.Println(n, "rows written")
Then read them back. A tag picks the logical type of a decimal.
type Row struct {
Name string `vortex:"name"`
Price vortex.Decimal `vortex:"price,decimal(10,2)"`
Note sql.Null[string] `vortex:"note"`
Tags []string `vortex:"tags"`
}
var buf bytes.Buffer
w := vortex.NewRowWriter[Row](&buf, vortex.WriteOptions{})
if _, err := w.Write([]Row{
{"a", vortex.DecimalFromInt64(1050), sql.Null[string]{V: "first", Valid: true}, []string{"x", "y"}},
{"b", vortex.DecimalFromInt64(299), sql.Null[string]{}, nil},
}); err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
f, err := vortex.Open(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
if err != nil {
log.Fatal(err)
}
for r, err := range vortex.Rows[Row](context.Background(), f, vortex.Query{}) {
if err != nil {
log.Fatal(err)
}
fmt.Println(r.Name, r.Price.Text(2), r.Note.Valid, r.Tags)
}
Write columns
A schema, then one Write per chunk with a Data per column.
s := vortex.NewSchema(
vortex.Field{Name: "id", Type: vortex.Int64Type()},
vortex.Field{Name: "name", Type: vortex.Utf8Type()},
)
var buf bytes.Buffer
w := vortex.NewWriter(&buf, s, vortex.WriteOptions{})
if err := w.Write(vortex.Of([]int64{1, 2, 3}), vortex.OfStrings([]string{"a", "b", "c"})); err != nil {
log.Fatal(err)
}
if err := w.Close(); err != nil {
log.Fatal(err)
}
f, err := vortex.Open(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
if err != nil {
log.Fatal(err)
}
fmt.Println(f.NumRows(), f.Schema())
The three compression modes
Set in WriteOptions, for both writers.
vortex.Default: the compressor picks the lightweight encodings of each chunk (bit-packing, frame of reference, dictionaries, run-end, ALP, FSST), as the reference writer does.vortex.Compact: favors a small file over the time to write it. zstd is a candidate too, for numbers and strings.vortex.None: writes the values as they are, in plain arrays.
w := vortex.NewRowWriter[Trade](f, vortex.WriteOptions{Compression: vortex.Compact})
Errors
Errors name the column, the encoding and the byte offset. Test them with errors.Is:
ErrNotVortex: bad magic at either end, or fewer than 12 bytes.ErrUnsupportedVersion: a format version other than 1.ErrCorrupt: anything out of bounds or malformed. A corrupt or truncated file gives an error, never a panic.ErrWriterClosed: a write after an error or afterClose.ErrDecimalRange: a decimal that does not fit in 256 bits.
f, err := vortex.OpenFile(path)
if errors.Is(err, vortex.ErrNotVortex) {
// not a Vortex file
}
Vortex types and Go types
| Vortex type | Field of a row struct | Column |
|---|---|---|
| Null | none, leave it out | Null(i) is always true |
| Bool | bool | Bool(i) |
u8 to u64, i8 to i64, f32, f64 | the same Go type | Values[T] |
f16 | float32 | Values[float32] |
| Decimal, precision up to 18 | int64, the unscaled value, or vortex.Decimal | Values[int64], or Decimal(i) |
| Decimal, precision 19 to 76 | vortex.Decimal | Decimal(i) |
| Utf8 | string (copies) or []byte (view) | Bytes(i) |
| Binary | []byte | Bytes(i) |
| List | []T | Elems() and List(i) |
| FixedSizeList | [N]T, or []T | Elems() and List(i) |
| Map | map[K]V | Elems() is a struct of key and value, and List(i) |
| Struct | a struct | Field(i) |
| Date | time.Time | the storage, Values[int32] or Values[int64] |
| Time | time.Duration since midnight | the storage, Values[int32] or Values[int64] |
| Timestamp | time.Time | the storage, Values[int64] |
| UUID | [16]byte | Bytes(i), 16 bytes |
| Variant | any | Variant(i), allocates |
The match is exact: an int32 column does not go into an int64 field, and the error names both types.
The reasons for the shape of the API are in docs/interface.md.