vortex-go

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.

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:

f, err := vortex.OpenFile(path)
if errors.Is(err, vortex.ErrNotVortex) {
    // not a Vortex file
}

Vortex types and Go types

Vortex typeField of a row structColumn
Nullnone, leave it outNull(i) is always true
BoolboolBool(i)
u8 to u64, i8 to i64, f32, f64the same Go typeValues[T]
f16float32Values[float32]
Decimal, precision up to 18int64, the unscaled value, or vortex.DecimalValues[int64], or Decimal(i)
Decimal, precision 19 to 76vortex.DecimalDecimal(i)
Utf8string (copies) or []byte (view)Bytes(i)
Binary[]byteBytes(i)
List[]TElems() and List(i)
FixedSizeList[N]T, or []TElems() and List(i)
Mapmap[K]VElems() is a struct of key and value, and List(i)
Structa structField(i)
Datetime.Timethe storage, Values[int32] or Values[int64]
Timetime.Duration since midnightthe storage, Values[int32] or Values[int64]
Timestamptime.Timethe storage, Values[int64]
UUID[16]byteBytes(i), 16 bytes
VariantanyVariant(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.