Files
seaweedfs/weed/worker/tasks/iceberg/operations_test.go
T
dependabot[bot]andChris Lu 2d4b730a2f build(deps): bump github.com/twmb/avro from 1.7.2 to 1.8.0 (#11210)
* build(deps): bump github.com/twmb/avro from 1.7.2 to 1.8.0

Bumps [github.com/twmb/avro](https://github.com/twmb/avro) from 1.7.2 to 1.8.0.
- [Commits](https://github.com/twmb/avro/compare/v1.7.2...v1.8.0)

---
updated-dependencies:
- dependency-name: github.com/twmb/avro
  dependency-version: 1.8.0
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* iceberg: adapt to twmb/avro v1.8.0 and iceberg-go defensive copies

avro v1.8.0 changes Schema.Root() to return *SchemaNode, which breaks
iceberg-go v0.6.0's internal avro_schemas.go. The fix (apache/iceberg-go#1843)
is only on iceberg-go's main branch, unreleased, so bump iceberg-go to
that commit (c210509) alongside the avro bump.

That iceberg-go revision also changes two behaviors seaweedfs worked
around:

- It now infers a manifest list's format version from the embedded
  writer schema, so a list missing the "format-version" header entry
  (DuckDB's shape is read as v2, not v1. ReadManifestList's header
  patching is now a redundant safety net; tests updated to expect v2.

- It returns defensive copies from DataFile.Partition(), so the
  ReadManifest shim's in-place partition normalization was silently
  discarded. Rebuild the entry through NewDataFileBuilder when any
  partition value is normalized, copying every other DataFile field so
  manifest round-trips are preserved.

- It converts day-transform partitions to iceberg.Date on read
  (applyDayTransformDates), so the day-partition cases the shim and
  tests guarded now convert without help; tests updated to expect
  iceberg.Date from the raw read.
EOF
)

* iceberg: accept assert-ref-snapshot-id without snapshot-id

iceberg-go's new nullableInt64 parser rejects an assert-ref-snapshot-id
requirement whose "snapshot-id" field is absent from the JSON, even
though the Iceberg REST spec makes it optional (null means the ref must
not already exist). v0.6.0 used a plain *int64, so absent was nil and
accepted. ClickHouse sends the requirement without snapshot-id when
asserting a branch does not yet exist, so its writes fail with
"missing required field \"snapshot-id\"".

normalizeRequirements splices an explicit null into any
assert-ref-snapshot-id requirement missing the field before handing
the JSON to iceberg-go's parser, restoring the v0.6.0 behavior across
both iceberg-go versions.

* iceberg: fix v1 block_size_in_bytes default in rebuilt manifest entries

rebuildManifestEntry set block_size_in_bytes to 0, but the v1 manifest
schema requires the default of 64 MiB ("Always write default in v1").
The original value is not exposed on the DataFile interface, so use the
spec default. Also clarify the fallback comment to note that empty
(zero-record / zero-byte) files also trigger it, not just a nil spec.

Add a round-trip test that writes a rebuilt entry as v1 and verifies
block_size_in_bytes is 64 MiB via Avro decoding.

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Chris Lu <chris.lu@gmail.com>
2026-09-07 15:58:24 -07:00

153 lines
5.6 KiB
Go

package iceberg
import (
"bytes"
"context"
"fmt"
"path"
"testing"
"time"
"github.com/apache/iceberg-go"
"github.com/apache/iceberg-go/table"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables"
"github.com/seaweedfs/seaweedfs/weed/s3api/s3tables/s3tablestest"
)
// Merging manifests a foreign producer wrote used to fail for good on a
// day-partitioned table, whichever way round the producer spelled the
// partition union: nothing between reading an entry and writing it looks at
// its partition, so the time.Time the Avro decoder produced is still there
// when the manifest writer, which has no date logical type for a day
// partition, tries to encode it.
func TestRewriteManifestsNormalizesForeignDayPartitions(t *testing.T) {
for _, valueFirst := range []bool{true, false} {
name := "null-first partition union"
if valueFirst {
name = "value-first partition union"
}
t.Run(name, func(t *testing.T) {
rewriteForeignDayPartitions(t, valueFirst)
})
}
}
func rewriteForeignDayPartitions(t *testing.T, valueFirst bool) {
t.Helper()
fs, client := startFakeFiler(t)
schema := iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "event_time", Type: iceberg.PrimitiveTypes.Timestamp, Required: true},
)
spec := iceberg.NewPartitionSpec(iceberg.PartitionField{
SourceIDs: []int{1}, FieldID: 1000, Name: "event_time_day", Transform: iceberg.DayTransform{},
})
setup := tableSetup{
BucketName: "test-bucket",
Namespace: "analytics",
TableName: "events",
Schema: schema,
Spec: &spec,
Snapshots: []table.Snapshot{{
SnapshotID: 1,
TimestampMs: time.Now().UnixMilli(),
ManifestList: "metadata/snap-1.avro",
}},
}
meta := populateTable(t, fs, setup)
metaDir := path.Join(s3tables.TablesPath, setup.BucketName, setup.tablePath(), "metadata")
wantDays := make(map[string]iceberg.Date)
var manifests []iceberg.ManifestFile
for i := 0; i < 3; i++ {
day := iceberg.Date(20737 + i)
filePath := setup.fileRef("data", fmt.Sprintf("foreign-%d.parquet", i))
dfBuilder, err := iceberg.NewDataFileBuilder(spec, iceberg.EntryContentData, filePath, iceberg.ParquetFile,
map[int]any{1000: day}, nil, nil, 1, 1)
if err != nil {
t.Fatalf("build data file %d: %v", i, err)
}
snapshotID := int64(1)
entry := iceberg.NewManifestEntry(iceberg.EntryStatusADDED, &snapshotID, nil, nil, dfBuilder.Build())
manifestName := fmt.Sprintf("foreign-manifest-%d.avro", i)
foreignBytes, manifest := s3tablestest.ForeignPartitionManifest(t, schema, spec, entry,
setup.fileRef("metadata", manifestName), "date", time.Unix(int64(day)*24*60*60, 0).UTC(), valueFirst)
fs.putEntry(metaDir, manifestName, &filer_pb.Entry{
Name: manifestName, Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix()}, Content: foreignBytes,
})
manifests = append(manifests, manifest)
wantDays[filePath] = day
}
var manifestList bytes.Buffer
seqNum := int64(1)
if err := iceberg.WriteManifestList(meta.Version(), &manifestList, 1, nil, &seqNum, 0, manifests); err != nil {
t.Fatalf("write manifest list: %v", err)
}
fs.putEntry(metaDir, "snap-1.avro", &filer_pb.Entry{
Name: "snap-1.avro", Attributes: &filer_pb.FuseAttributes{Mtime: time.Now().Unix()}, Content: manifestList.Bytes(),
})
result, _, err := NewHandler(nil).rewriteManifests(context.Background(), client, setup.BucketName, setup.tablePath(), Config{
MinManifestsToRewrite: 3,
MaxCommitRetries: 3,
})
if err != nil {
t.Fatalf("rewriteManifests failed for foreign day partitions: %v", err)
}
if result != "rewrote 3 manifests into 1 (3 entries)" {
t.Fatalf("rewriteManifests result = %q, want 3 manifests merged", result)
}
// The merged manifest has to carry the days the foreign manifests held.
// iceberg-go writes a day partition as a bare Avro int, without the date
// logical type, but now converts day-transform partitions to iceberg.Date
// on read (applyDayTransformDates), so they read back as iceberg.Date.
got := make(map[string]any)
for _, mf := range currentManifests(t, client, setup) {
manifestData, err := loadFileByIcebergPath(context.Background(), client, setup.BucketName, setup.tablePath(), mf.FilePath())
if err != nil {
t.Fatalf("load merged manifest %s: %v", mf.FilePath(), err)
}
entries, err := iceberg.ReadManifest(mf, bytes.NewReader(manifestData), true)
if err != nil {
t.Fatalf("parse merged manifest %s: %v", mf.FilePath(), err)
}
for _, entry := range entries {
got[entry.DataFile().FilePath()] = entry.DataFile().Partition()[1000]
}
}
if len(got) != len(wantDays) {
t.Fatalf("merged manifests hold %d entries, want %d", len(got), len(wantDays))
}
for filePath, day := range wantDays {
if got[filePath] != day {
t.Errorf("%s partition = %#v (%T), want %d", filePath, got[filePath], got[filePath], day)
}
}
}
// currentManifests reads the manifests of the table's current snapshot.
func currentManifests(t *testing.T, client filer_pb.SeaweedFilerClient, setup tableSetup) []iceberg.ManifestFile {
t.Helper()
state, err := loadCurrentMetadata(context.Background(), client, setup.BucketName, setup.tablePath())
if err != nil {
t.Fatalf("reload metadata: %v", err)
}
snapshot := state.Metadata.CurrentSnapshot()
if snapshot == nil {
t.Fatal("table has no current snapshot")
}
manifestListData, err := loadFileByIcebergPath(context.Background(), client, setup.BucketName, setup.tablePath(), snapshot.ManifestList)
if err != nil {
t.Fatalf("load manifest list: %v", err)
}
manifests, err := s3tables.ReadManifestList(manifestListData)
if err != nil {
t.Fatalf("parse manifest list: %v", err)
}
return manifests
}