mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
* 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>
107 lines
4.3 KiB
Go
107 lines
4.3 KiB
Go
package iceberg
|
|
|
|
import (
|
|
"bytes"
|
|
"path"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/apache/iceberg-go"
|
|
"github.com/parquet-go/parquet-go"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
|
|
)
|
|
|
|
// A bin the spatial order cannot be laid over is bin-packed.
|
|
|
|
// A projected column whose files carry no bounds is bin-packed in bin-pack row groups, not the spatial ones.
|
|
func TestCompactSpatialFallbackUsesBinpackRowGroups(t *testing.T) {
|
|
feet, err := iceberg.GeometryTypeOf("EPSG:2913")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var files []geoFile
|
|
for f := 0; f < 3; f++ {
|
|
var rows []geoRow
|
|
for i := 0; i < 300; i++ {
|
|
rows = append(rows, geoRow{ID: int64(f*300 + i), Geom: wkbPoint(7.6e6+float64(i), 6.8e5+float64(f))})
|
|
}
|
|
files = append(files, geoFile{Name: "p" + string(rune('a'+f)) + ".parquet", Rows: rows, NoBounds: true})
|
|
}
|
|
fs, client := startFakeFiler(t)
|
|
setup := tableSetup{BucketName: "tb", Namespace: "ns", TableName: "geo"}
|
|
populateGeoTable(t, fs, setup, files, geoTableOptions{icebergType: feet, parquetCRS: "EPSG:2913"})
|
|
// spatial row groups of 1 KB would cut these 900 rows into dozens; bin-pack's default holds them in one
|
|
compactGeo(t, client, setup, Config{SpatialClustering: true, SpatialRowGroupSizeBytes: 1024})
|
|
for name, data := range compactedFiles(t, fs, setup) {
|
|
f, err := parquet.OpenFile(bytes.NewReader(data), int64(len(data)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n := len(f.Metadata().RowGroups); n != 1 {
|
|
t.Errorf("%s: %d row groups; the bin-packed fallback should use bin-pack sizing (1)", name, n)
|
|
}
|
|
if rows := readSpatialOutput(t, data); len(rows) != 900 {
|
|
t.Errorf("%s: %d rows, want 900", name, len(rows))
|
|
}
|
|
}
|
|
}
|
|
|
|
// Files without field ids whose geometry column has another name than the table's: the merge cannot find the column
|
|
// to order by and bin-packs the bin rather than failing it.
|
|
func TestCompactSpatialFallsBackWhenTheGeometryColumnIsNotFound(t *testing.T) {
|
|
fs, client := startFakeFiler(t)
|
|
setup := tableSetup{BucketName: "tb", Namespace: "ns", TableName: "geo"}
|
|
files := fiveGeoFiles()
|
|
for i := range files {
|
|
files[i].GeomName, files[i].NoFieldIDs = "shape", true
|
|
}
|
|
populateGeoTable(t, fs, setup, files, geoTableOptions{})
|
|
result := compactGeo(t, client, setup, Config{SpatialClustering: true})
|
|
if !strings.Contains(result, "compacted 5 files into 1") {
|
|
t.Fatalf("result %q: the bin was not compacted", result)
|
|
}
|
|
// the summary says what was written: every bin fell back, so this was a bin-pack
|
|
props := currentTableMetadata(t, client, setup).CurrentSnapshot().Summary.Properties
|
|
if props["rewrite-strategy"] != "binpack" || props["spatial-column"] != "" || props["spatial-fallback-bins"] != "1" {
|
|
t.Errorf("snapshot summary %v", props)
|
|
}
|
|
for name, data := range compactedFiles(t, fs, setup) {
|
|
f, err := parquet.OpenFile(bytes.NewReader(data), int64(len(data)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f.NumRows() != 200 {
|
|
t.Errorf("%s: %d rows, want 200", name, f.NumRows())
|
|
}
|
|
if _, ok := f.Schema().Lookup(spatialKeyColumn); ok {
|
|
t.Errorf("%s: a spatial key column was written", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
// One bin falls back and the other cannot be read: nothing was written in spatial order, so the summary says bin-pack.
|
|
func TestCompactSpatialSummaryCountsOnlyWrittenBins(t *testing.T) {
|
|
fs, client := startFakeFiler(t)
|
|
setup := tableSetup{BucketName: "tb", Namespace: "ns", TableName: "geo"}
|
|
files := scatteredGeoFiles(6, 50, 11)
|
|
for i := range files {
|
|
files[i].Day = 20000
|
|
if i < 3 {
|
|
files[i].Day = 20001
|
|
files[i].GeomName, files[i].NoFieldIDs = "shape", true
|
|
}
|
|
}
|
|
populateGeoTable(t, fs, setup, files, geoTableOptions{dayPartitioned: true})
|
|
dataDir := path.Join(s3tables.TablesPath, setup.BucketName, setup.tablePath(), "data")
|
|
fs.putEntry(dataDir, files[3].Name, &filer_pb.Entry{Name: files[3].Name, Content: []byte("not parquet")})
|
|
result := compactGeo(t, client, setup, Config{SpatialClustering: true})
|
|
if !strings.Contains(result, "compacted 3 files into 1 (across 2 bins)") || strings.Contains(result, "spatial") {
|
|
t.Fatalf("result %q", result)
|
|
}
|
|
props := currentTableMetadata(t, client, setup).CurrentSnapshot().Summary.Properties
|
|
if props["rewrite-strategy"] != "binpack" || props["spatial-column"] != "" || props["spatial-fallback-bins"] != "1" {
|
|
t.Errorf("snapshot summary %v", props)
|
|
}
|
|
}
|