Files
seaweedfs/weed/worker/tasks/iceberg/geometry_fixture_test.go
T
81f9231e2f iceberg compact: optional spatial clustering of geometry tables (#13135)
* iceberg compact: optional spatial clustering of geometry tables

Compaction of an Iceberg v3 table with a geometry column merges files in
manifest order (bin-pack) or by a declared sort order. Neither keeps nearby
geometries together: after a bin-pack every output file and row group spans
the whole extent of the table, so a reader can skip nothing by the geometry
bounds and row-group geospatial statistics. In one measurement a point lookup
on a table of places read 4.59 MB after bin-pack compaction and 0.55 MB after
a spatial one. A declared sort order cannot express this either: Iceberg v3
has no spatial sort transform, a geometry cannot be a sort source, and DuckDB
refuses INSERTs into a table that declares a sort order.

This adds an opt-in spatial order to the compaction job.

Spatial clustering (`spatial_clustering`, off by default). When on, a v3
table with no declared sort order and exactly one top-level geometry column
is rewritten in the Hilbert order of each row's bounding-box centre, whatever
`rewrite_strategy` says (a declared sort order is still honoured, since such a
table does not qualify):

- The 62-bit key (31 bits per axis) is computed from the WKB while the rows
  stream through the merge, carried as an extra last column through
  parquet-go's sorting writer into a temporary file in `sort_spill_dir`, and
  dropped when a plain writer copies the rows to the output. Row lineage is
  mapped before the key is added. The grid is the globe for a lon/lat CRS and
  the union of the bin's file bounds for a projected one. Null, empty and
  unreadable geometries sort last. The key only orders rows; pruning stays on
  the geometry bounds, so a poor key costs speed, never results.
- Files are listed by the key of their bound's centre before binning, so the
  bins of one partition cover compact areas.
- Row groups target `spatial_row_group_size_mb` (2 MB, capped by the table's
  `write.parquet.row-group-size-bytes`, at least 16 rows; an explicit
  `write.parquet.row-group-limit` still wins). A row group is what a spatial
  filter skips, and a row of a large polygon can be tens of kilobytes.
- A bin the order cannot be laid over (a projected column whose files carry
  no bounds, a geometry column the merge cannot find, e.g. after a rename in
  files without field ids) is bin-packed in bin-pack row groups.
- The table property `seaweedfs.compaction.spatial-column` names the column
  when a table has several, or opts a table out with `none`.
- The snapshot summary records `rewrite-strategy=spatial` and
  `spatial-column`, or `binpack` when every bin fell back, and
  `spatial-fallback-bins` when some did. The planning
  index's config hash covers the new settings.

Open time partitions (`spatial_hold_open_partitions`, default true, and
`spatial_open_partition_grace_minutes`, default 120; both only matter with
spatial clustering on). A spatially ordered partition has every row group
spanning its whole time range. On a day of vehicle positions, "where is every
vehicle now" read 2.51 MB of the spatially compacted day against 0.04 MB of
the files as written, while an area query on a closed day read 2.74 MB
against 19.35 MB. So a day or hour partition is left in write order until it
has ended plus the grace (late batches still land in it), and is ordered in
space after that. The job and detection hold the same bins; the planning
index caches its answer only until the first held partition is released, so
a table nobody writes to after midnight still gets its last day ordered.
Coarser time partitions and the other strategies are unchanged.

`github.com/twpayne/go-geom` (already in the module graph) becomes a direct
requirement for its WKB parser.

Tests:

- spatial_order_test.go: the Hilbert curve, plan resolution (version, sort
  order, one or several geometry columns, the property, opt-out), 2,400
  scattered points compacted end to end in spatial order with row ids kept
  and no key column written, the job setting off as control, nulls last, a
  projected CRS with and without file bounds, file ordering, and the
  row-group sizing.
- spatial_fallback_test.go: the bin-pack fallback uses bin-pack row groups;
  a geometry column the merge cannot find falls back, with the summary
  saying so.
- spatial_open_test.go: partition ends for day and hour across the value
  types a manifest reader returns and far from 1970; the hold, the grace and
  their controls; the planning index release time and config hash; on the
  fake filer, a day-partitioned geometry table where the job compacts the
  past day in spatial order and leaves today's files, a table of only today
  commits nothing, and the index records midnight UTC plus the grace (each
  with the hold off as control); spatial clustering is off by default.
- Mutation checks: a constant key fails the spatial order tests; disabling
  the hold fails four open-partition tests.
- go vet and go test (also with -race) of weed/worker/tasks/iceberg pass;
  go build ./weed passes.

* iceberg compact: give the spatial sort key a name no table column has

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* iceberg compact: report spatial order only for bins it wrote

A bin skipped after a read or merge error no longer counts as
spatially ordered in the snapshot summary or the job result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* iceberg compact: do not hold an open partition the spatial order cannot cover

A projected geometry column whose files carry no bounds is bin-packed,
so holding its open day or hour partition only left small files behind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* iceberg compact: do not hold a drop-only bin in an open partition

A bin whose rows are all deleted removes its files and writes nothing, so
there is no spatial rewrite for an open day or hour partition to wait for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* iceberg compact: pack delete-heavy files in the spatial file order

Delete-heavy bins took the table's order while the other bins of a spatial
plan list their files by the Hilbert key of their geometry bounds, so a
partition split into several delete-heavy bins merged overlapping areas.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 10:51:16 -08:00

393 lines
14 KiB
Go

package iceberg
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"path"
"strings"
"testing"
"time"
"github.com/apache/iceberg-go"
"github.com/apache/iceberg-go/table"
"github.com/parquet-go/parquet-go"
"github.com/parquet-go/parquet-go/compress/zstd"
"github.com/parquet-go/parquet-go/format"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
)
// Fixtures for v3 tables with a geometry column: Parquet files with field
// ids, ZSTD and a native GEOMETRY column, and manifests that carry the v3
// geometry bounds of each file, as DuckDB writes them.
const (
geoTestIDField = 1
geoTestGeomField = 2
geoTestDayField = 3 // geoTableOptions.dayPartitioned only
geoTestDayPartitionField = 1000 // its day partition field
)
func wkbPoint(x, y float64) []byte {
b := make([]byte, 21)
b[0] = 1 // little endian
binary.LittleEndian.PutUint32(b[1:], 1)
binary.LittleEndian.PutUint64(b[5:], math.Float64bits(x))
binary.LittleEndian.PutUint64(b[13:], math.Float64bits(y))
return b
}
// wkbBox is the polygon of one closed ring around the box.
func wkbBox(xmin, ymin, xmax, ymax float64) []byte {
ring := [][2]float64{{xmin, ymin}, {xmax, ymin}, {xmax, ymax}, {xmin, ymax}, {xmin, ymin}}
b := make([]byte, 0, 13+16*len(ring))
b = append(b, 1)
b = binary.LittleEndian.AppendUint32(b, 3)
b = binary.LittleEndian.AppendUint32(b, 1)
b = binary.LittleEndian.AppendUint32(b, uint32(len(ring)))
for _, p := range ring {
b = binary.LittleEndian.AppendUint64(b, math.Float64bits(p[0]))
b = binary.LittleEndian.AppendUint64(b, math.Float64bits(p[1]))
}
return b
}
func geoLE64(v int64) []byte {
b := make([]byte, 8)
binary.LittleEndian.PutUint64(b, uint64(v))
return b
}
func xyBound(x, y float64) []byte {
b := make([]byte, 16)
binary.LittleEndian.PutUint64(b, math.Float64bits(x))
binary.LittleEndian.PutUint64(b[8:], math.Float64bits(y))
return b
}
func decodeXYBound(t *testing.T, b []byte) (float64, float64) {
t.Helper()
if len(b) < 16 {
t.Fatalf("geometry bound of %d bytes, want 16", len(b))
}
return math.Float64frombits(binary.LittleEndian.Uint64(b)), math.Float64frombits(binary.LittleEndian.Uint64(b[8:]))
}
type geoRow struct {
ID int64
Geom []byte // nil: a null geometry
}
type geoFile struct {
Name string
Rows []geoRow
ParquetCRSSet bool // declare the default CRS in the Parquet logical type
NoBounds bool // the manifest entry records no geometry bounds
GeomName string // the Parquet name of the geometry column ("" = geom)
NoFieldIDs bool // write no field ids (readers then match columns by name)
Day int32 // the file's day partition (days since the epoch) when the table is dayPartitioned
}
type geoTableOptions struct {
icebergType iceberg.Type // the geometry column's Iceberg type (default: geometry with the default CRS)
parquetCRS string // the CRS of the Parquet GEOMETRY type
sortOrder *table.SortOrder // nil: unsorted
properties iceberg.Properties
// fixup, when set, rewrites each input file after it is written
fixup func(t *testing.T, data []byte) []byte
// dayPartitioned adds an optional date column the files do not carry and partitions the table by day of it, as a
// streaming table is often partitioned by day(ts); each file's partition is its Day
dayPartitioned bool
}
func geoTestSchema(geomType iceberg.Type) *iceberg.Schema {
return iceberg.NewSchema(0,
iceberg.NestedField{ID: geoTestIDField, Type: iceberg.PrimitiveTypes.Int64, Name: "id", Required: true},
iceberg.NestedField{ID: geoTestGeomField, Type: geomType, Name: "geom", Required: false},
)
}
func writeGeoParquet(t *testing.T, rows []geoRow, crs string) []byte {
t.Helper()
return writeGeoParquetWith(t, rows, crs, "geom", true)
}
// writeGeoParquetWith is writeGeoParquet with the geometry column's Parquet
// name and, optionally, no field ids.
func writeGeoParquetWith(t *testing.T, rows []geoRow, crs, geomName string, fieldIDs bool) []byte {
t.Helper()
id := parquet.Compressed(parquet.Int(64), &zstd.Codec{})
geom := parquet.Compressed(parquet.Optional(parquet.Geometry(crs)), &zstd.Codec{})
if fieldIDs {
id, geom = parquet.FieldID(id, geoTestIDField), parquet.FieldID(geom, geoTestGeomField)
}
schema := parquet.NewSchema("table", parquet.Group{"id": id, geomName: geom})
geomCol, idCol := -1, -1
for i, p := range schema.Columns() {
switch strings.Join(p, ".") {
case geomName:
geomCol = i
case "id":
idCol = i
}
}
var buf bytes.Buffer
w := parquet.NewGenericWriter[any](&buf, schema)
for _, r := range rows {
row := make(parquet.Row, 2)
if r.Geom == nil {
row[geomCol] = parquet.NullValue().Level(0, 0, geomCol)
} else {
row[geomCol] = parquet.ByteArrayValue(r.Geom).Level(0, 1, geomCol)
}
row[idCol] = parquet.Int64Value(r.ID).Level(0, 0, idCol)
if _, err := w.WriteRows([]parquet.Row{row}); err != nil {
t.Fatalf("write row: %v", err)
}
}
if err := w.Close(); err != nil {
t.Fatalf("close writer: %v", err)
}
return buf.Bytes()
}
// footerGeoBox is the union of the file's row-group geometry boxes.
func footerGeoBox(t *testing.T, data []byte) (xmin, ymin, xmax, ymax float64, ok bool) {
t.Helper()
f, err := parquet.OpenFile(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatalf("open: %v", err)
}
xmin, ymin, xmax, ymax = math.Inf(1), math.Inf(1), math.Inf(-1), math.Inf(-1)
for _, rg := range f.Metadata().RowGroups {
for _, c := range rg.Columns {
if strings.Join(c.MetaData.PathInSchema, ".") != "geom" || len(c.MetaData.GeospatialStatistics.GeoSpatialTypes) == 0 {
continue
}
bb := c.MetaData.GeospatialStatistics.BBox
xmin, ymin = math.Min(xmin, bb.XMin), math.Min(ymin, bb.YMin)
xmax, ymax = math.Max(xmax, bb.XMax), math.Max(ymax, bb.YMax)
ok = true
}
}
return
}
// footerGeometryCRS is the CRS the file's GEOMETRY logical type declares, and
// whether the column is GEOMETRY at all.
func footerGeometryCRS(t *testing.T, data []byte, column string) (string, bool) {
t.Helper()
f, err := parquet.OpenFile(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatalf("open: %v", err)
}
for _, el := range f.Metadata().Schema {
if el.Name != column {
continue
}
if g, ok := el.LogicalType.Value.(*format.GeometryType); ok {
return g.CRS, true
}
return "", false
}
t.Fatalf("no column %s in the footer", column)
return "", false
}
// populateGeoTable writes a v3 geometry table with one append snapshot and
// one manifest per file.
func populateGeoTable(t *testing.T, fs *fakeFilerServer, setup tableSetup, files []geoFile, opt geoTableOptions) table.Metadata {
t.Helper()
geomType := opt.icebergType
if geomType == nil {
geomType = iceberg.GeometryType{}
}
schema := geoTestSchema(geomType)
spec := *iceberg.UnpartitionedSpec
if opt.dayPartitioned {
schema = iceberg.NewSchema(0, append(schema.Fields(),
iceberg.NestedField{ID: geoTestDayField, Type: iceberg.PrimitiveTypes.Date, Name: "day", Required: false})...)
spec = iceberg.NewPartitionSpecID(0, iceberg.PartitionField{SourceIDs: []int{geoTestDayField}, FieldID: geoTestDayPartitionField,
Name: "day_day", Transform: iceberg.DayTransform{}})
}
location := "s3://" + setup.BucketName + "/" + setup.dataPath()
props := iceberg.Properties{"format-version": "3"}
for k, v := range opt.properties {
props[k] = v
}
sortOrder := table.UnsortedSortOrder
if opt.sortOrder != nil {
sortOrder = *opt.sortOrder
}
meta, err := table.NewMetadata(schema, &spec, sortOrder, location, props)
if err != nil {
t.Fatalf("create metadata: %v", err)
}
bucketPath := path.Join(s3tables.TablesPath, setup.BucketName)
tableFilerPath := path.Join(bucketPath, setup.dataPath())
metaDir := path.Join(tableFilerPath, "metadata")
dataDir := path.Join(tableFilerPath, "data")
putFile := func(dir, name string, data []byte) {
fs.putEntry(dir, name, &filer_pb.Entry{Name: name, Content: data,
Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix(), FileSize: uint64(len(data))}})
}
snapID := int64(1)
var manifests []iceberg.ManifestFile
for i, f := range files {
crs := opt.parquetCRS
if f.ParquetCRSSet && crs == "" {
crs = iceberg.DefaultGeoCRS
}
geomName := f.GeomName
if geomName == "" {
geomName = "geom"
}
data := writeGeoParquetWith(t, f.Rows, crs, geomName, !f.NoFieldIDs)
if opt.fixup != nil && !f.ParquetCRSSet {
data = opt.fixup(t, data)
}
putFile(dataDir, f.Name, data)
partition := map[int]any{}
if opt.dayPartitioned {
partition[geoTestDayPartitionField] = iceberg.Date(f.Day)
}
dfb, err := iceberg.NewDataFileBuilder(spec, iceberg.EntryContentData, setup.fileRef("data", f.Name), iceberg.ParquetFile, partition, nil, nil, int64(len(f.Rows)), int64(len(data)))
if err != nil {
t.Fatalf("data file %s: %v", f.Name, err)
}
lo, hi := int64(math.MaxInt64), int64(math.MinInt64)
for _, r := range f.Rows {
lo, hi = min(lo, r.ID), max(hi, r.ID)
}
lower := map[int][]byte{geoTestIDField: geoLE64(lo)}
upper := map[int][]byte{geoTestIDField: geoLE64(hi)}
if xmin, ymin, xmax, ymax, ok := footerGeoBox(t, data); ok && !f.NoBounds {
lower[geoTestGeomField], upper[geoTestGeomField] = xyBound(xmin, ymin), xyBound(xmax, ymax)
}
dfb.LowerBoundValues(lower)
dfb.UpperBoundValues(upper)
entry := iceberg.NewManifestEntry(iceberg.EntryStatusADDED, &snapID, nil, nil, dfb.Build())
name := fmt.Sprintf("data-manifest-%d.avro", i)
var buf bytes.Buffer
mf, err := iceberg.WriteManifest(setup.fileRef("metadata", name), &buf, 3, spec, schema, snapID, []iceberg.ManifestEntry{entry})
if err != nil {
t.Fatalf("write manifest: %v", err)
}
putFile(metaDir, name, buf.Bytes())
manifests = append(manifests, mf)
}
var mlBuf bytes.Buffer
mlw, err := iceberg.NewManifestListWriterV3(&mlBuf, snapID, 1, 0, nil)
if err != nil {
t.Fatalf("manifest list writer: %v", err)
}
if err := mlw.AddManifests(manifests); err != nil {
t.Fatalf("add manifests: %v", err)
}
if err := mlw.Close(); err != nil {
t.Fatalf("close manifest list: %v", err)
}
putFile(metaDir, "snap-1.avro", mlBuf.Bytes())
firstRowID := int64(0)
addedRows := *mlw.NextRowID()
snap := table.Snapshot{SnapshotID: snapID, SequenceNumber: 1, TimestampMs: time.Now().UnixMilli(),
ManifestList: setup.fileRef("metadata", "snap-1.avro"),
Summary: &table.Summary{Operation: table.OpAppend, Properties: map[string]string{}},
FirstRowID: &firstRowID, AddedRows: &addedRows}
builder, err := table.MetadataBuilderFromBase(meta, location)
if err != nil {
t.Fatalf("metadata builder: %v", err)
}
if err := builder.AddSnapshot(&snap); err != nil {
t.Fatalf("add snapshot: %v", err)
}
if err := builder.SetSnapshotRef(table.MainBranch, snapID, table.BranchRef); err != nil {
t.Fatalf("set ref: %v", err)
}
if meta, err = builder.Build(); err != nil {
t.Fatalf("build metadata: %v", err)
}
full, _ := json.Marshal(meta)
xattr, _ := json.Marshal(map[string]interface{}{
"metadataVersion": 1,
"metadataLocation": setup.fileRef("metadata", "v1.metadata.json"),
"metadata": map[string]interface{}{"fullMetadata": json.RawMessage(full)},
})
fs.putEntry(s3tables.TablesPath, setup.BucketName, &filer_pb.Entry{
Name: setup.BucketName, IsDirectory: true, Extended: map[string][]byte{s3tables.ExtendedKeyTableBucket: []byte("true")},
})
fs.putEntry(bucketPath, setup.Namespace, &filer_pb.Entry{Name: setup.Namespace, IsDirectory: true})
fs.putEntry(path.Join(bucketPath, setup.Namespace), setup.TableName, &filer_pb.Entry{
Name: setup.TableName, IsDirectory: true, Extended: map[string][]byte{s3tables.ExtendedKeyMetadata: xattr},
})
return meta
}
func compactGeo(t *testing.T, client filer_pb.SeaweedFilerClient, setup tableSetup, config Config) string {
t.Helper()
if config.TargetFileSizeBytes == 0 {
config.TargetFileSizeBytes = 256 * 1024 * 1024
}
if config.MinInputFiles == 0 {
config.MinInputFiles = 2
}
if config.MaxCommitRetries == 0 {
config.MaxCommitRetries = 3
}
config.ApplyDeletes = true
result, _, err := NewHandler(nil).compactDataFiles(context.Background(), client, setup.BucketName, setup.tablePath(), config, nil)
if err != nil {
t.Fatalf("compactDataFiles: %v", err)
}
return result
}
// compactedFiles is every compact-*.parquet in the table's data directory.
func compactedFiles(t *testing.T, fs *fakeFilerServer, setup tableSetup) map[string][]byte {
t.Helper()
out := map[string][]byte{}
for _, e := range fs.listDir(path.Join(s3tables.TablesPath, setup.BucketName, setup.tablePath(), "data")) {
if strings.HasPrefix(e.Name, "compact-") {
out[e.Name] = e.Content
}
}
if len(out) == 0 {
t.Fatal("no compacted file")
}
return out
}
// currentDataEntries is every entry of the current snapshot's data manifests.
func currentDataEntries(t *testing.T, client filer_pb.SeaweedFilerClient, setup tableSetup) []iceberg.ManifestEntry {
t.Helper()
ctx := context.Background()
state, err := loadCurrentMetadata(ctx, client, setup.BucketName, setup.tablePath())
if err != nil {
t.Fatalf("loadCurrentMetadata: %v", err)
}
manifests, err := loadCurrentManifests(ctx, client, setup.BucketName, state.DataPath, state.Metadata)
if err != nil {
t.Fatalf("loadCurrentManifests: %v", err)
}
var out []iceberg.ManifestEntry
for _, mf := range manifests {
if mf.ManifestContent() != iceberg.ManifestContentData {
continue
}
data, err := loadFileByIcebergPath(ctx, client, setup.BucketName, state.DataPath, mf.FilePath())
if err != nil {
t.Fatalf("load manifest: %v", err)
}
entries, err := iceberg.ReadManifest(mf, bytes.NewReader(data), true)
if err != nil {
t.Fatalf("read manifest: %v", err)
}
out = append(out, entries...)
}
return out
}