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) } }