|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//go:build ignore |
| 19 | + |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "bytes" |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + "path/filepath" |
| 27 | + "strconv" |
| 28 | + |
| 29 | + "github.com/apache/iceberg-go/puffin" |
| 30 | + "github.com/apache/iceberg-go/table/dv" |
| 31 | +) |
| 32 | + |
| 33 | +type fixtureBlob struct { |
| 34 | + referencedDataFile string |
| 35 | + positions []uint64 |
| 36 | + ranges []positionRange |
| 37 | +} |
| 38 | + |
| 39 | +type positionRange struct { |
| 40 | + start uint64 |
| 41 | + end uint64 |
| 42 | +} |
| 43 | + |
| 44 | +func writeFixture(outputDir, fileName, createdBy string, blobs []fixtureBlob) error { |
| 45 | + var output bytes.Buffer |
| 46 | + writer, err := puffin.NewWriter(&output) |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + if err := writer.SetCreatedBy(createdBy); err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + for _, blob := range blobs { |
| 55 | + bitmap := dv.NewRoaringPositionBitmap() |
| 56 | + for _, position := range blob.positions { |
| 57 | + bitmap.Set(position) |
| 58 | + } |
| 59 | + for _, positionRange := range blob.ranges { |
| 60 | + bitmap.SetRange(positionRange.start, positionRange.end) |
| 61 | + } |
| 62 | + payload, err := dv.SerializeDV(bitmap) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + _, err = writer.AddBlob(puffin.BlobMetadataInput{ |
| 67 | + Type: puffin.BlobTypeDeletionVector, |
| 68 | + SnapshotID: -1, |
| 69 | + SequenceNumber: -1, |
| 70 | + Fields: []int32{}, |
| 71 | + Properties: map[string]string{ |
| 72 | + "referenced-data-file": blob.referencedDataFile, |
| 73 | + "cardinality": strconv.FormatInt(bitmap.Cardinality(), 10), |
| 74 | + }, |
| 75 | + }, payload) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if err := writer.Finish(); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + return os.WriteFile(filepath.Join(outputDir, fileName), output.Bytes(), 0o644) |
| 85 | +} |
| 86 | + |
| 87 | +func main() { |
| 88 | + if len(os.Args) != 2 { |
| 89 | + fmt.Fprintln(os.Stderr, "usage: go run generate_go_fixtures.go OUTPUT_DIR") |
| 90 | + os.Exit(2) |
| 91 | + } |
| 92 | + outputDir := os.Args[1] |
| 93 | + if err := os.MkdirAll(outputDir, 0o755); err != nil { |
| 94 | + panic(err) |
| 95 | + } |
| 96 | + |
| 97 | + err := writeFixture(outputDir, "single-blob-dv.puffin", |
| 98 | + "iceberg-go test fixture", []fixtureBlob{{ |
| 99 | + referencedDataFile: "data/test.parquet", |
| 100 | + positions: []uint64{1, 3, 5, 7, 9}, |
| 101 | + }}) |
| 102 | + if err != nil { |
| 103 | + panic(err) |
| 104 | + } |
| 105 | + |
| 106 | + err = writeFixture(outputDir, "multi-blob-dv.puffin", |
| 107 | + "iceberg-go cross-language fixture", []fixtureBlob{ |
| 108 | + { |
| 109 | + referencedDataFile: "s3://warehouse/db/table/data/go-file-001.parquet", |
| 110 | + positions: []uint64{ |
| 111 | + 0, 100, 200, (uint64(1) << 32) + 7, |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + referencedDataFile: "s3://warehouse/db/table/data/go-file-002.parquet", |
| 116 | + positions: []uint64{ |
| 117 | + 50, 150, (uint64(2) << 32) + 9, |
| 118 | + }, |
| 119 | + }, |
| 120 | + }) |
| 121 | + if err != nil { |
| 122 | + panic(err) |
| 123 | + } |
| 124 | + |
| 125 | + position := func(bucket, container, value uint64) uint64 { |
| 126 | + return (bucket << 32) + (container << 16) + value |
| 127 | + } |
| 128 | + allContainerPositions := []uint64{ |
| 129 | + position(0, 0, 5), |
| 130 | + position(0, 0, 7), |
| 131 | + position(1, 0, 10), |
| 132 | + position(1, 0, 20), |
| 133 | + } |
| 134 | + for bucket := uint64(0); bucket < 2; bucket++ { |
| 135 | + for value := uint64(0); value < 10000; value += 2 { |
| 136 | + allContainerPositions = |
| 137 | + append(allContainerPositions, position(bucket, 2, value)) |
| 138 | + } |
| 139 | + } |
| 140 | + err = writeFixture(outputDir, "all-container-types-dv.puffin", |
| 141 | + "iceberg-go cross-language fixture", []fixtureBlob{{ |
| 142 | + referencedDataFile: "s3://warehouse/db/table/data/all-containers.parquet", |
| 143 | + positions: allContainerPositions, |
| 144 | + ranges: []positionRange{ |
| 145 | + {start: position(0, 1, 1), end: position(0, 1, 1000)}, |
| 146 | + {start: position(1, 1, 10), end: position(1, 1, 500)}, |
| 147 | + }, |
| 148 | + }}) |
| 149 | + if err != nil { |
| 150 | + panic(err) |
| 151 | + } |
| 152 | +} |
0 commit comments