-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathtpcds.go
More file actions
264 lines (234 loc) · 8.63 KB
/
Copy pathtpcds.go
File metadata and controls
264 lines (234 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* Warp (C) 2019-2026 MinIO, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package iceberg
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync/atomic"
"cloud.google.com/go/storage"
"github.com/apache/iceberg-go"
"golang.org/x/sync/errgroup"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
// TPC-DS data source constants for GCS.
const (
TPCDSGCSBucket = "beam-tpcds"
TPCDSGCSPrefix = "datasets/parquet/nonpartitioned"
)
// scaleFactorMap maps user-friendly names to actual GCS folder names.
var scaleFactorMap = map[string]string{
"sf1": "1GB",
"sf10": "10GB",
"sf100": "100GB",
"sf1000": "1000GB",
"1GB": "1GB",
"10GB": "10GB",
"100GB": "100GB",
"1000GB": "1000GB",
"1TB": "1TB",
"3TB": "3TB",
"10TB": "10TB",
"30TB": "30TB",
"100TB": "100TB",
"300TB": "300TB",
"1PB": "1PB",
}
// TPCDSConfig holds configuration for TPC-DS data operations.
type TPCDSConfig struct {
ScaleFactor string // e.g., "sf1", "sf10", "sf100", "sf1000"
Table string // e.g., "store_sales", "customer", etc.
CacheDir string // local directory to cache downloaded files
Concurrency int // download concurrency
}
// DefaultTPCDSConfig returns sensible defaults.
func DefaultTPCDSConfig() TPCDSConfig {
return TPCDSConfig{
ScaleFactor: "sf100",
Table: "store_sales",
CacheDir: "/tmp/warp-tpcds-cache",
Concurrency: 16,
}
}
// TPCDSDownloadResult holds the result of a download operation.
type TPCDSDownloadResult struct {
Files []string
TotalBytes int64
DownloadedBytes int64
SkippedFiles int
}
// DownloadTPCDS downloads TPC-DS parquet files from GCS to local cache.
// Files that already exist locally are skipped.
func DownloadTPCDS(ctx context.Context, cfg TPCDSConfig, progress func(completed, total, bytes int64)) (*TPCDSDownloadResult, error) {
if cfg.Concurrency <= 0 {
cfg.Concurrency = 16
}
// Map scale factor to folder name
scaleFactor := cfg.ScaleFactor
if mapped, ok := scaleFactorMap[cfg.ScaleFactor]; ok {
scaleFactor = mapped
}
localDir := filepath.Join(cfg.CacheDir, scaleFactor, cfg.Table)
existingFiles, _ := filepath.Glob(filepath.Join(localDir, "*.parquet"))
if len(existingFiles) > 0 {
var totalSize int64
for _, f := range existingFiles {
if info, err := os.Stat(f); err == nil {
totalSize += info.Size()
}
}
return &TPCDSDownloadResult{
Files: existingFiles,
TotalBytes: totalSize,
SkippedFiles: len(existingFiles),
}, nil
}
client, err := storage.NewClient(ctx, option.WithoutAuthentication())
if err != nil {
return nil, fmt.Errorf("failed to create GCS client: %w", err)
}
defer client.Close()
bucket := client.Bucket(TPCDSGCSBucket)
prefix := fmt.Sprintf("%s/%s/%s/", TPCDSGCSPrefix, scaleFactor, cfg.Table)
var gcsFiles []string
it := bucket.Objects(ctx, &storage.Query{Prefix: prefix})
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, fmt.Errorf("failed to list GCS objects: %w", err)
}
if strings.HasSuffix(attrs.Name, ".parquet") {
gcsFiles = append(gcsFiles, attrs.Name)
}
}
if len(gcsFiles) == 0 {
return nil, fmt.Errorf("no parquet files found at gs://%s/%s", TPCDSGCSBucket, prefix)
}
if err := os.MkdirAll(localDir, 0o755); err != nil {
return nil, fmt.Errorf("failed to create cache directory: %w", err)
}
g, ctx := errgroup.WithContext(ctx)
g.SetLimit(cfg.Concurrency)
var completed atomic.Int64
var downloadedBytes atomic.Int64
var skipped atomic.Int64
total := int64(len(gcsFiles))
localFiles := make([]string, len(gcsFiles))
for i, gcsFile := range gcsFiles {
i, gcsFile := i, gcsFile
g.Go(func() error {
localPath := filepath.Join(localDir, filepath.Base(gcsFile))
localFiles[i] = localPath
if info, err := os.Stat(localPath); err == nil && info.Size() > 0 {
skipped.Add(1)
n := completed.Add(1)
if progress != nil {
progress(n, total, downloadedBytes.Load())
}
return nil
}
obj := bucket.Object(gcsFile)
reader, err := obj.NewReader(ctx)
if err != nil {
return fmt.Errorf("failed to read %s: %w", gcsFile, err)
}
defer reader.Close()
f, err := os.Create(localPath)
if err != nil {
return fmt.Errorf("failed to create %s: %w", localPath, err)
}
defer f.Close()
written, err := io.Copy(f, reader)
if err != nil {
os.Remove(localPath)
return fmt.Errorf("failed to download %s: %w", gcsFile, err)
}
downloadedBytes.Add(written)
n := completed.Add(1)
if progress != nil {
progress(n, total, downloadedBytes.Load())
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
var totalSize int64
for _, f := range localFiles {
if info, err := os.Stat(f); err == nil {
totalSize += info.Size()
}
}
return &TPCDSDownloadResult{
Files: localFiles,
TotalBytes: totalSize,
DownloadedBytes: downloadedBytes.Load(),
SkippedFiles: int(skipped.Load()),
}, nil
}
// GetCachedTPCDSFiles returns locally cached TPC-DS files if they exist.
func GetCachedTPCDSFiles(cfg TPCDSConfig) ([]string, error) {
scaleFactor := cfg.ScaleFactor
if mapped, ok := scaleFactorMap[cfg.ScaleFactor]; ok {
scaleFactor = mapped
}
localDir := filepath.Join(cfg.CacheDir, scaleFactor, cfg.Table)
files, err := filepath.Glob(filepath.Join(localDir, "*.parquet"))
if err != nil {
return nil, err
}
if len(files) == 0 {
return nil, fmt.Errorf("no cached TPC-DS files found in %s", localDir)
}
return files, nil
}
// StoreSalesSchema returns the Iceberg schema for TPC-DS store_sales table.
func StoreSalesSchema() *iceberg.Schema {
return iceberg.NewSchema(0,
iceberg.NestedField{ID: 1, Name: "ss_sold_date_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 2, Name: "ss_sold_time_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 3, Name: "ss_item_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 4, Name: "ss_customer_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 5, Name: "ss_cdemo_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 6, Name: "ss_hdemo_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 7, Name: "ss_addr_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 8, Name: "ss_store_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 9, Name: "ss_promo_sk", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 10, Name: "ss_ticket_number", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 11, Name: "ss_quantity", Type: iceberg.PrimitiveTypes.Int64, Required: false},
iceberg.NestedField{ID: 12, Name: "ss_wholesale_cost", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 13, Name: "ss_list_price", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 14, Name: "ss_sales_price", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 15, Name: "ss_ext_discount_amt", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 16, Name: "ss_ext_sales_price", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 17, Name: "ss_ext_wholesale_cost", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 18, Name: "ss_ext_list_price", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 19, Name: "ss_ext_tax", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 20, Name: "ss_coupon_amt", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 21, Name: "ss_net_paid", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 22, Name: "ss_net_paid_inc_tax", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
iceberg.NestedField{ID: 23, Name: "ss_net_profit", Type: iceberg.DecimalTypeOf(7, 2), Required: false},
)
}