zeroshade commented on code in PR #1396: URL: https://github.com/apache/arrow-go/pull/1396#discussion_r4339819668
########## parquet/internal/encoding/delta_bit_packing_fast_path_test.go: ########## @@ -0,0 +1,140 @@ +// 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 encoding + +import ( + "fmt" + "math" + "math/rand" + "testing" + + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/parquet" + "github.com/stretchr/testify/require" +) + +func TestDeltaBitPackConstantBlockWireFormat(t *testing.T) { + t.Run("full positive block", func(t *testing.T) { + values := make([]int32, 129) + for i := range values { + values[i] = 10 + int32(i)*2 + } + + enc := NewEncoder(parquet.Types.Int32, parquet.Encodings.DeltaBinaryPacked, false, nil, memory.DefaultAllocator).(*deltaBitPackEncoder[int32]) + defer enc.Release() + enc.Put(values) + + buf, err := enc.FlushValues() + require.NoError(t, err) + defer buf.Release() + + // Block size 128, 4 miniblocks, 129 values, first value 10, + // minimum delta 2, then four zero bit widths and no payload. + require.Equal(t, []byte{0x80, 0x01, 0x04, 0x81, 0x01, 0x14, 0x04, 0, 0, 0, 0}, buf.Bytes()) + }) + + t.Run("partial negative block", func(t *testing.T) { + values := []int64{5, 4, 3, 2, 1, 0, -1} + enc := NewEncoder(parquet.Types.Int64, parquet.Encodings.DeltaBinaryPacked, false, nil, memory.DefaultAllocator).(*deltaBitPackEncoder[int64]) + defer enc.Release() + enc.Put(values) + + buf, err := enc.FlushValues() + require.NoError(t, err) + defer buf.Release() + + // The trailing miniblocks are also written as zero-width. + require.Equal(t, []byte{0x80, 0x01, 0x04, 0x07, 0x0a, 0x01, 0, 0, 0, 0}, buf.Bytes()) + }) +} + +func testDeltaBitPackBlockRoundTrip[T int32 | int64](t *testing.T, typ parquet.Type) { + t.Helper() + + encode := func(parts ...[]T) []byte { + t.Helper() + enc := NewEncoder(typ, parquet.Encodings.DeltaBinaryPacked, false, nil, memory.DefaultAllocator).(*deltaBitPackEncoder[T]) + defer enc.Release() + for _, part := range parts { + enc.Put(part) + } + buf, err := enc.FlushValues() + require.NoError(t, err) + defer buf.Release() + return append([]byte(nil), buf.Bytes()...) + } + + for _, n := range []int{1, 2, 31, 32, 33, 127, 128, 129, 130, 255, 257, 1001} { + for _, pattern := range []string{"constant", "zero", "sparse", "alternating", "random", "overflow"} { Review Comment: Could you add a nearly all-equal block whose only different difference is the last one, for a short block (e.g. `[0, 3, 10]`) and a full 129-value block, for both int32 and int64, with the outlier both higher and lower? Leaving the last difference out of the new all-equal check still passes all of these tests. ########## parquet/internal/encoding/delta_bit_packing_block_bench_test.go: ########## @@ -0,0 +1,74 @@ +// 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 encoding + +import ( + "math/rand" + "testing" + + "github.com/apache/arrow-go/v18/arrow/memory" + "github.com/apache/arrow-go/v18/parquet" +) + +func benchmarkDeltaBitPackBlockFastPath[T int32 | int64](b *testing.B, typ parquet.Type, bytesPerValue int) { + const length = 64 * 1024 + + for _, pattern := range []string{"constant", "sparse", "alternating", "random"} { + b.Run(pattern, func(b *testing.B) { + values := make([]T, length) + rng := rand.New(rand.NewSource(42)) + for i := 1; i < length; i++ { + switch pattern { + case "constant": + values[i] = values[i-1] + T(3) + case "sparse": Review Comment: Please also benchmark a smaller difference at the end of each block, e.g. step `3` except step `-7` at each block's last position. The new check scans the whole block before finding it, and then every miniblock still needs a payload. Pinned int32 runs went from 441,562–457,788 to 460,766–512,463 ns/op (about 10% slower by median). Can the extra scan be avoided, or the tradeoff documented? -- 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]
