Revanth14 commented on code in PR #1492: URL: https://github.com/apache/iceberg-go/pull/1492#discussion_r3635487696
########## catalog/rest/scan_task_decoder.go: ########## @@ -0,0 +1,716 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package rest + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "math" + "slices" + + "github.com/apache/iceberg-go" + "github.com/apache/iceberg-go/table" + "github.com/twmb/avro/atype" +) + +// The exported REST wire types below are intentional: the low-level planning +// methods expose ScanTasks directly, while DecodeScanTasks is the boundary for +// callers that want table.FileScanTask domain objects. + +// RESTCountMap is the REST column-id-to-count representation. Keys and values +// are parallel arrays because JSON object keys cannot preserve integer IDs. +type RESTCountMap struct { + Keys []int `json:"keys"` + Values []int64 `json:"values"` +} + +// RESTValueMap is the REST column-id-to-binary representation used for lower +// and upper bounds. Java's ContentFileParser encodes each raw Iceberg binary +// value as a hexadecimal string; these are not typed JSON partition values. +type RESTValueMap struct { + Keys []int `json:"keys"` + Values []string `json:"values"` +} + +// RESTContentFile contains the fields common to REST data and delete files. +// Partition values remain raw until DecodeScanTasks resolves spec-id and the +// result type of each partition transform. +type RESTContentFile struct { + SpecID int `json:"spec-id"` + Partition []json.RawMessage `json:"partition"` + Content string `json:"content"` + FilePath string `json:"file-path"` + FileFormat string `json:"file-format"` + FileSizeInBytes int64 `json:"file-size-in-bytes"` + RecordCount int64 `json:"record-count"` + KeyMetadata *string `json:"key-metadata,omitempty"` + SplitOffsets []int64 `json:"split-offsets,omitempty"` + SortOrderID *int `json:"sort-order-id,omitempty"` +} + +// RESTDataFile is the data-file arm of the REST ContentFile union. +type RESTDataFile struct { + RESTContentFile + + FirstRowID *int64 `json:"first-row-id,omitempty"` + ColumnSizes *RESTCountMap `json:"column-sizes,omitempty"` + ValueCounts *RESTCountMap `json:"value-counts,omitempty"` + NullValueCounts *RESTCountMap `json:"null-value-counts,omitempty"` + NaNValueCounts *RESTCountMap `json:"nan-value-counts,omitempty"` + LowerBounds *RESTValueMap `json:"lower-bounds,omitempty"` + UpperBounds *RESTValueMap `json:"upper-bounds,omitempty"` +} + +// RESTFileScanTask is the REST FileScanTask wire payload. Delete references +// are zero-based indices into the DeleteFiles slice of the same ScanTasks +// envelope; they must be resolved before responses are combined. +type RESTFileScanTask struct { + DataFile *RESTDataFile `json:"data-file"` + DeleteFileReferences []int `json:"delete-file-references,omitempty"` + ResidualFilter json.RawMessage `json:"residual-filter,omitempty"` +} + +// RESTDeleteFile is the position/equality arm of the REST ContentFile union. +// Content discriminates the variant. Puffin position deletes are decoded as +// deletion vectors and associated with the data file that references them. +type RESTDeleteFile struct { + RESTContentFile + + EqualityIDs []int `json:"equality-ids,omitempty"` + // ReferencedDataFile is emitted by Java's ContentFileParser for + // file-scoped position deletes and deletion vectors. It is not currently + // declared by the REST OpenAPI DeleteFile schema, so clients must also + // support deriving a DV's target from its FileScanTask association. + ReferencedDataFile *string `json:"referenced-data-file,omitempty"` + ContentOffset *int64 `json:"content-offset,omitempty"` + ContentSizeInBytes *int64 `json:"content-size-in-bytes,omitempty"` +} + +// DecodeScanTasks converts one REST ScanTasks envelope into domain scan tasks. +// Delete-file references are scoped to one envelope, so callers must invoke +// this before combining inline and fetchScanTasks responses. +// +// schema is the schema selected for the scan and is used to decode residual +// literals. When nil, metadata.CurrentSchema is used. +// fallbackResidual is used when a task omits residual-filter, as required by +// the REST specification; it is commonly the original scan filter. +func DecodeScanTasks( + wire ScanTasks, + metadata table.ScanPlanningMetadata, + schema *iceberg.Schema, + fallbackResidual iceberg.BooleanExpression, +) ([]table.FileScanTask, error) { + if metadata == nil { + return nil, fmt.Errorf("%w: decoding scan tasks: table metadata is required", ErrRESTError) + } + if schema == nil { + schema = metadata.CurrentSchema() + } + if schema == nil { + return nil, fmt.Errorf("%w: decoding scan tasks: scan schema is required", ErrRESTError) + } + if len(wire.FileScanTasks) == 0 { + if len(wire.DeleteFiles) != 0 { + return nil, fmt.Errorf("%w: decoding scan tasks: %d delete files have no file scan tasks", ErrRESTError, len(wire.DeleteFiles)) + } + + return nil, nil + } + + // Decode every delete even if the server did not reference it. This catches + // a malformed response rather than silently ignoring corrupt wire data. + deletes := make([]iceberg.DataFile, len(wire.DeleteFiles)) Review Comment: I opted to treat an unreferenced delete as malformed. To answer the Java question directly: no, Java's `PlanTableScanResponse.validate()` only rejects `delete-files` when there are no file scan tasks at all ("deleteFiles should only be returned with fileScanTasks that reference them"); with tasks present, an unreferenced delete is silently ignored at parse time. So this is intentionally stricter than Java - the REST spec describes `delete-files` as "Delete files referenced by file scan tasks", and this enforces the contract Java's own validation message states, per-entry rather than only in the degenerate case. The decoder now tracks referenced indices and returns a field-specific error for any unused `delete-files` entry, with a test covering it. Happy to relax to Java's ignore-silently behavior if you'd rather match parity here. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
