laskoviymishka commented on code in PR #1914:
URL: https://github.com/apache/iceberg-go/pull/1914#discussion_r3873466796
##########
table/inspect_internal_test.go:
##########
@@ -2617,6 +2617,43 @@ func
TestInspectPartitionAggregateTreeHandlesBinaryAndNaNValues(t *testing.T) {
require.Nil(t, tree.lookup(partitionRecord{[]byte{1, 2, 4},
math.NaN()}))
}
+func TestInspectPartitionAggregateTreeLookupPartition(t *testing.T) {
+ partitionType := &iceberg.StructType{FieldList: []iceberg.NestedField{
+ {ID: 1000, Name: "id", Type: iceberg.PrimitiveTypes.Int32},
+ {ID: 1001, Name: "payload", Type:
iceberg.PrimitiveTypes.Binary},
+ {ID: 1002, Name: "score", Type: iceberg.PrimitiveTypes.Float64},
+ }}
+ partition := map[int]any{
+ 1000: int32(7),
+ 1001: []byte{1, 2, 3},
+ 1002: math.NaN(),
+ }
+ record := newPartitionRecord(inspectCoercePartition(partition,
partitionType), partitionType)
+ aggregate := &inspectPartitionAggregate{specID: 1}
+ tree := newInspectPartitionAggregateTree()
+ tree.insert(record, aggregate)
+
+ require.Same(t, aggregate, tree.lookupPartition(partition,
partitionType))
Review Comment:
This is the equivalence the PR is really leaning on, that `lookupPartition`
matches the old coerce+record+lookup path, and the partition-evolution case is
the one I'd most want pinned down but it isn't covered here.
Could we add a sub-case where a file's partition map is missing one of the
fields in `partitionType` (older spec, field added later)?
`partition[field.ID]` returns nil and `inspectCoercePartition` skips it too, so
both paths key on nil at that level and agree. That's exactly the kind of quiet
invariant a future change to `inspectCoercePartition` could break without
anything failing.
While we're here, the `partitionType == nil` early-return branch is also
untested. wdyt?
##########
table/inspect_partitions.go:
##########
@@ -152,10 +173,11 @@ func (i InspectTable) partitionAggregates(ctx
context.Context, partitionType *ic
return nil, err
}
file := entry.DataFile()
- partition := inspectCoercePartition(file.Partition(),
partitionType)
- record := newPartitionRecord(partition, partitionType)
- aggregate := aggregateTree.lookup(record)
+ partitionValues := dataFilePartition(file)
+ aggregate :=
aggregateTree.lookupPartition(partitionValues, partitionType)
if aggregate == nil {
+ partition :=
inspectCoercePartition(partitionValues, partitionType)
+ record := newPartitionRecord(partition,
partitionType)
aggregate = &inspectPartitionAggregate{
partition:
cloneInspectPartition(partition),
partitionRecord: record,
Review Comment:
`partitionRecord` here still holds the borrowed `[]byte` slice headers from
`dataFilePartition(file)`. `inspectCoercePartition` only shallow-copies the
map, so those byte backing arrays stay aliased to the DataFile's Avro decode
buffer and now escape across the whole aggregation.
The saving grace is that nothing reads `partitionRecord` anymore.
`appendPartitionAggregate` goes through `aggregate.partition`, which
`cloneInspectPartition` deep-copies (bytes included, now), so this field is
dead weight that also pins memory and quietly re-opens the borrow-contract
violation the rest of the PR is careful about.
I'd just drop the field from `inspectPartitionAggregate` and this struct
literal. The trie key already owns its copy via `comparablePartitionKey`, and
`lookupPartition` never touches it.
##########
table/inspect_partitions.go:
##########
@@ -70,6 +71,26 @@ func (t *inspectPartitionAggregateTree) lookup(record
partitionRecord) *inspectP
return node.aggregate
}
+func (t *inspectPartitionAggregateTree) lookupPartition(
+ partition map[int]any,
+ partitionType *iceberg.StructType,
+) *inspectPartitionAggregate {
+ node := t
+ if partitionType == nil {
Review Comment:
This guard makes `lookupPartition` look like it handles a nil
`partitionType`, but the aggregate-creation path after a nil lookup still calls
`newPartitionRecord(nil, nil)` and panics dereferencing the nil `FieldList`.
Same as before this PR, so not a regression, just newly misleading.
I'd either mirror the nil check where the aggregate gets built, or drop a
one-line comment that nil is unreachable here and the guard is purely
defensive. wdyt?
##########
table/inspect_partitions_bench_test.go:
##########
@@ -0,0 +1,160 @@
+// 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 table
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/apache/iceberg-go"
+)
+
+var inspectPartitionAggregationBenchmarkSink int
+
+func BenchmarkInspectPartitionAggregation(b *testing.B) {
+ for _, benchmark := range []struct {
Review Comment:
This struct is spelled out again in `benchmarkInspectPartitionFiles`'s
signature, so adding a field later means matching the shape and order in both
places. Could we lift it to a named type at package scope and use it in both
spots?
##########
table/inspect_partitions.go:
##########
@@ -70,6 +71,26 @@ func (t *inspectPartitionAggregateTree) lookup(record
partitionRecord) *inspectP
return node.aggregate
}
+func (t *inspectPartitionAggregateTree) lookupPartition(
+ partition map[int]any,
+ partitionType *iceberg.StructType,
+) *inspectPartitionAggregate {
+ node := t
+ if partitionType == nil {
+ return node.aggregate
+ }
+
+ for _, field := range partitionType.FieldList {
+ child, ok :=
node.children[comparablePartitionKey(partition[field.ID])]
Review Comment:
`insert` keys the trie positionally off the `partitionRecord` slice, while
`lookupPartition` keys off `partition[field.ID]` walked in `FieldList` order.
They only line up because `record` is always built from this same
`partitionType.FieldList`, and that coupling is load-bearing now that
`lookupPartition` is the only production reader.
I'd add a short comment on `insert` (or here) noting the two have to walk
fields in the same order. `lookup` also only has test callers now, so it's
worth deciding whether to keep it or move those tests onto `lookupPartition`.
##########
table/inspect_partitions_bench_test.go:
##########
@@ -0,0 +1,160 @@
+// 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 table
+
+import (
+ "fmt"
+ "testing"
+
+ "github.com/apache/iceberg-go"
+)
+
+var inspectPartitionAggregationBenchmarkSink int
+
+func BenchmarkInspectPartitionAggregation(b *testing.B) {
+ for _, benchmark := range []struct {
+ name string
+ fileCount int
+ partitionCount int
+ fieldCount int
+ binary bool
+ }{
+ {name: "int32", fileCount: 10_000, partitionCount: 100,
fieldCount: 1},
+ {name: "int32", fileCount: 100_000, partitionCount: 100,
fieldCount: 8},
+ {name: "binary", fileCount: 100_000, partitionCount: 100,
fieldCount: 32, binary: true},
+ } {
+ b.Run(fmt.Sprintf("%s/files=%d/partitions=%d/fields=%d",
+ benchmark.name, benchmark.fileCount,
benchmark.partitionCount, benchmark.fieldCount), func(b *testing.B) {
+ partitionType, files :=
benchmarkInspectPartitionFiles(b, benchmark)
+ b.Run("before", func(b *testing.B) {
+ benchmarkInspectPartitionAggregation(b,
partitionType, files, true)
+ })
+ b.Run("after", func(b *testing.B) {
+ benchmarkInspectPartitionAggregation(b,
partitionType, files, false)
+ })
+ })
+ }
+}
+
+func benchmarkInspectPartitionAggregation(
+ b *testing.B,
+ partitionType *iceberg.StructType,
+ files []iceberg.DataFile,
+ materialize bool,
+) {
+ b.Helper()
+ b.ReportAllocs()
+ b.ReportMetric(float64(len(files)), "files/op")
+ b.ResetTimer()
Review Comment:
`b.Loop()` manages the timer itself (it resets and starts on the first
call), and there's nothing expensive between `ReportMetric` and the loop, so
this `ResetTimer` is redundant. I'd drop it, or add a comment if it's there for
older Go.
--
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]