zeroshade commented on code in PR #1857: URL: https://github.com/apache/iceberg-go/pull/1857#discussion_r3855431334
########## catalog/rest/fetch_scan_tasks_validation.go: ########## @@ -0,0 +1,94 @@ +// 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/json" + "fmt" +) + +// validatePlanningTaskEnvelope mirrors the Java response validation for the +// status-discriminated planning responses. Empty arrays are still present on +// the wire, so inspect the raw JSON instead of relying only on decoded slice +// lengths when rejecting task fields before planning completes. +func validatePlanningTaskEnvelope(data []byte, status PlanStatus, tasks ScanTasks, endpoint string) error { + var fields map[string]json.RawMessage + if err := json.Unmarshal(data, &fields); err != nil { + return err + } + + if status != PlanStatusCompleted { + for _, name := range []string{"plan-tasks", "file-scan-tasks"} { + raw, ok := fields[name] + if ok && !isJSONNull(raw) { + return fmt.Errorf("%w: %s response includes %s for status %q", ErrRESTError, endpoint, name, status) + } + } + if len(tasks.DeleteFiles) > 0 { + return fmt.Errorf("%w: %s response includes delete-files for status %q", ErrRESTError, endpoint, status) + } + } + + if len(tasks.DeleteFiles) > 0 && len(tasks.FileScanTasks) == 0 { + return fmt.Errorf( + "%w: %s response has delete-files without file-scan-tasks", + ErrRESTError, + endpoint, + ) + } + + return nil +} + +func (r *FetchScanTasksResponse) UnmarshalJSON(data []byte) error { + var fields map[string]json.RawMessage + if err := json.Unmarshal(data, &fields); err != nil { + return err + } + + planTasks, hasPlanTasks := fields["plan-tasks"] + fileScanTasks, hasFileScanTasks := fields["file-scan-tasks"] + deleteFiles, hasDeleteFiles := fields["delete-files"] + + isNull := func(raw json.RawMessage) bool { + return bytes.Equal(bytes.TrimSpace(raw), []byte("null")) + } + if hasPlanTasks && isNull(planTasks) { + return fmt.Errorf("%w: fetchScanTasks response field plan-tasks must not be null", ErrRESTError) + } + if hasFileScanTasks && isNull(fileScanTasks) { + return fmt.Errorf("%w: fetchScanTasks response field file-scan-tasks must not be null", ErrRESTError) + } + if hasDeleteFiles && isNull(deleteFiles) { + return fmt.Errorf("%w: fetchScanTasks response field delete-files must not be null", ErrRESTError) + } + + type responseAlias FetchScanTasksResponse + var decoded responseAlias + if err := json.Unmarshal(data, &decoded); err != nil { + return err + } + if len(decoded.DeleteFiles) > 0 && len(decoded.FileScanTasks) == 0 { + return fmt.Errorf("%w: fetchScanTasks response has delete-files without file-scan-tasks", ErrRESTError) + } + + *r = FetchScanTasksResponse(decoded) Review Comment: Please reject a top-level JSON `null` before assigning the decoded response. Both `json.Unmarshal(null, &fields)` and the alias decode succeed here, so this returns a zero-valued `FetchScanTasksResponse`; `collectScanTasks` then treats the malformed response as an empty task envelope and the scan silently returns no files. An exact regression through `FetchScanTasks` would protect against incomplete query results. ########## table/scanner.go: ########## @@ -1010,6 +1064,11 @@ func (scan *Scan) planFilesRemote(ctx context.Context) ([]FileScanTask, error) { return result.Tasks, nil } + planIO, err := newPlanIOState(result.IO) + if err != nil { + return nil, err + } + oldPlanIO := scan.planIO scan.planIO = planIO Review Comment: This installs an owning reference that is not deterministically released after a normal scan. `releasePlanIOAfter` drops only the reader lease, so after iterator exhaustion `owners == 1`, `readers == 0`, and `PlanIO.Close` is never called. Since `planIOWithCleanup.Close` sends `DELETE /plan/{id}` and `Scan` has no public `Close`, the server plan remains active until a later successful replan. Please provide deterministic owner release when consumption finishes—or another public lifecycle mechanism—and cover normal exhaustion and early stop. -- 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]
