laskoviymishka commented on code in PR #2373:
URL: https://github.com/apache/iceberg-go/pull/2373#discussion_r4431216667


##########
table/sort_compaction_ordered_fastpath_test.go:
##########
@@ -0,0 +1,426 @@
+// 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 (
+       "context"
+       "errors"
+       "slices"
+       "strconv"
+       "testing"
+
+       "github.com/apache/arrow-go/v18/arrow"
+       "github.com/apache/arrow-go/v18/arrow/array"
+       "github.com/apache/arrow-go/v18/arrow/compute"
+       "github.com/apache/arrow-go/v18/arrow/memory"
+       "github.com/apache/iceberg-go"
+)
+
+type orderedPartitionTestRow struct {
+       key   int64
+       valid bool
+       label string
+}
+
+func orderedPartitionTestSetup(t testing.TB) (*arrow.Schema, rowComparator) {
+       t.Helper()
+       sc := arrow.NewSchema([]arrow.Field{
+               {Name: "key", Type: arrow.PrimitiveTypes.Int64, Nullable: true},
+               {Name: "label", Type: arrow.BinaryTypes.String},
+       }, nil)
+       schema := iceberg.NewSchema(0,
+               iceberg.NestedField{ID: 1, Name: "key", Type: 
iceberg.PrimitiveTypes.Int64},
+               iceberg.NestedField{ID: 2, Name: "label", Type: 
iceberg.PrimitiveTypes.String},
+       )
+       order, err := NewSortOrder(1, []SortField{{
+               SourceIDs: []int{1}, Transform: iceberg.IdentityTransform{},
+               Direction: SortASC, NullOrder: NullsFirst,
+       }})
+       if err != nil {
+               t.Fatal(err)
+       }
+       cmp, err := newRowComparator(order, schema, sc)
+       if err != nil {
+               t.Fatal(err)
+       }
+
+       return sc, cmp
+}
+
+func makeOrderedPartitionTestBatch(mem memory.Allocator, sc *arrow.Schema, 
rows []orderedPartitionTestRow) arrow.RecordBatch {
+       builder := array.NewRecordBuilder(mem, sc)
+       defer builder.Release()
+       keys := builder.Field(0).(*array.Int64Builder)
+       labels := builder.Field(1).(*array.StringBuilder)
+       for _, row := range rows {
+               if row.valid {
+                       keys.Append(row.key)
+               } else {
+                       keys.AppendNull()
+               }
+               labels.Append(row.label)
+       }
+
+       return builder.NewRecordBatch()
+}
+
+func TestYieldSortedPartitionFastPathPreservesTiesAndNulls(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       t.Cleanup(func() { mem.AssertSize(t, 0) })
+       sc, cmp := orderedPartitionTestSetup(t)
+       batches := []arrow.RecordBatch{
+               makeOrderedPartitionTestBatch(mem, sc, 
[]orderedPartitionTestRow{
+                       {label: "null"}, {key: 1, valid: true, label: "a"}, 
{key: 1, valid: true, label: "b"},
+               }),
+               makeOrderedPartitionTestBatch(mem, sc, 
[]orderedPartitionTestRow{
+                       {key: 1, valid: true, label: "c"},
+                       {key: 2, valid: true, label: "d"},
+                       {key: 3, valid: true, label: "e"},
+                       {key: 3, valid: true, label: "f"},
+                       {key: 4, valid: true, label: "g"},
+               }),
+       }
+       defer func() {
+               for _, batch := range batches {
+                       batch.Release()
+               }
+       }()
+       var got []string
+       var sizes []int64
+       calls := 0
+       ok := yieldSortedPartition(t.Context(), batches, cmp, 3, func(rec 
arrow.RecordBatch, err error) bool {
+               if err != nil {
+                       t.Fatalf("yield: %v", err)
+               }
+               defer rec.Release()
+               if calls == 0 && rec != batches[0] {
+                       t.Fatal("full input batch was copied")
+               }
+               calls++
+               sizes = append(sizes, rec.NumRows())
+               col := rec.Column(1).(*array.String)
+               for i := range col.Len() {
+                       got = append(got, col.Value(i))
+               }
+
+               return true
+       })
+       if !ok {
+               t.Fatal("iteration stopped early")
+       }
+       if want := []int64{3, 3, 2}; !slices.Equal(sizes, want) {
+               t.Fatalf("batch sizes %v; want %v", sizes, want)
+       }
+       if want := []string{"null", "a", "b", "c", "d", "e", "f", "g"}; 
!slices.Equal(got, want) {
+               t.Fatalf("row order %v; want %v", got, want)
+       }
+}
+
+func TestYieldSortedPartitionFastPathFallsBack(t *testing.T) {
+       tests := []struct {
+               name  string
+               input [][]orderedPartitionTestRow
+               want  []string
+       }{
+               {name: "overlapping batches", input: 
[][]orderedPartitionTestRow{
+                       {{key: 1, valid: true, label: "a"}, {key: 4, valid: 
true, label: "b"}, {key: 4, valid: true, label: "c"}},
+                       {{key: 0, valid: true, label: "d"}, {key: 2, valid: 
true, label: "e"}, {key: 4, valid: true, label: "f"}},
+               }, want: []string{"d", "a", "e", "b", "c", "f"}},
+               {name: "unordered within batch", input: 
[][]orderedPartitionTestRow{
+                       {{key: 5, valid: true, label: "a"}, {key: 1, valid: 
true, label: "b"}, {key: 5, valid: true, label: "c"}},
+                       {{key: 2, valid: true, label: "d"}, {key: 3, valid: 
true, label: "e"}, {key: 4, valid: true, label: "f"}},
+               }, want: []string{"b", "d", "e", "f", "a", "c"}},
+       }
+       for _, tc := range tests {
+               t.Run(tc.name, func(t *testing.T) {
+                       mem := 
memory.NewCheckedAllocator(memory.DefaultAllocator)
+                       t.Cleanup(func() { mem.AssertSize(t, 0) })
+                       sc, cmp := orderedPartitionTestSetup(t)
+                       batches := make([]arrow.RecordBatch, len(tc.input))
+                       for i, rows := range tc.input {
+                               batches[i] = makeOrderedPartitionTestBatch(mem, 
sc, rows)
+                       }
+                       defer func() {
+                               for _, batch := range batches {
+                                       batch.Release()
+                               }
+                       }()
+                       var got []string
+                       ok := yieldSortedPartition(t.Context(), batches, cmp, 
2, func(rec arrow.RecordBatch, err error) bool {
+                               if err != nil {
+                                       t.Fatalf("yield: %v", err)
+                               }
+                               defer rec.Release()
+                               col := rec.Column(1).(*array.String)
+                               for i := range col.Len() {
+                                       got = append(got, col.Value(i))
+                               }
+
+                               return true
+                       })
+                       if !ok || !slices.Equal(got, tc.want) {
+                               t.Fatalf("got %v (ok=%v), want %v", got, ok, 
tc.want)
+                       }
+               })
+       }
+}
+
+func TestYieldSortedPartitionFastPathCancellation(t *testing.T) {
+       mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
+       t.Cleanup(func() { mem.AssertSize(t, 0) })
+       sc, cmp := orderedPartitionTestSetup(t)
+       batch := makeOrderedPartitionTestBatch(mem, sc, 
[]orderedPartitionTestRow{{key: 1, valid: true, label: "a"}})
+       defer batch.Release()
+       ctx, cancel := context.WithCancel(t.Context())
+       cancel()
+       calls := 0
+       ok := yieldSortedPartition(ctx, []arrow.RecordBatch{batch}, cmp, 1, 
func(rec arrow.RecordBatch, err error) bool {
+               calls++
+               if rec != nil {
+                       rec.Release()
+                       t.Fatal("expected cancellation, got record")
+               }
+               if err != context.Canceled {
+                       t.Fatalf("got %v, want context.Canceled", err)
+               }
+
+               return true
+       })
+       if ok || calls != 1 {
+               t.Fatalf("ok=%v calls=%d", ok, calls)
+       }
+}
+
+// legacyYieldSortedPartition retains the pre-fast-path implementation to
+// measure before and after on the same Go toolchain and Arrow version.
+func legacyYieldSortedPartition(

Review Comment:
   Could we add a `Test` that runs this next to `yieldSortedPartition` on 
ordered input? Right now it's only called from the 
[benchmark](https://github.com/apache/iceberg-go/blob/f4d9626be2e909773e1508190fab41f190f35217/table/sort_compaction_ordered_fastpath_test.go#L336),
 and the ties test uses aligned batch sizes and reads only `label`. Source 
batches of 3 and 5 rows at `batchRows=4`, comparing every column including the 
key nulls, would cover it. Boundaries follow the source batches by design, so 
compare rows end to end and pin the fast path's own sizes.



-- 
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]

Reply via email to