Copilot commented on code in PR #50386: URL: https://github.com/apache/arrow/pull/50386#discussion_r3527582316
########## ruby/red-arrow-format/lib/arrow-format/bitmap-builder.rb: ########## @@ -0,0 +1,89 @@ +# 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. + +require_relative "buffer-alignable" + +module ArrowFormat + class DenseBitmapBuilder + include BufferAlignable + + def initialize + @buffer = +"".b + @n_bits = 0 + @byte = 0 + end + + def append(value) + @byte |= 1 << @n_bits if value + @n_bits += 1 + flush if @n_bits == 8 + self + end + + def finish + flush if @n_bits > 0 + padding_size = buffer_padding_size(@buffer) + @buffer.append_as_bytes(padding(padding_size)) if padding_size > 0 + @buffer.freeze + IO::Buffer.for(@buffer) + end + + private + def flush + @buffer.append_as_bytes([@byte].pack("C")) + @n_bits = 0 + @byte = 0 + end + end + + class SparseBitmapBuilder + include BufferAlignable + + def initialize(default) + @default = default + @indexes = {} + end + + def set(index) + @indexes[index] = true + end + + def finish(size) + builder = DenseBitmapBuilder.new + not_set_value = @default + set_value = !@default + if @indexes.empty? + size.times do + builder.append(not_set_value) + end + else + previous_index = 0 + @indexes.keys.sort.each do |index| + previous_index.upto(index - 1) do + builder.append(not_set_value) + end + builder.append(set_value) + previous_index = index + 1 + end Review Comment: `SparseBitmapBuilder#finish` can generate a bitmap larger than the requested `size` if `set` was called with an index < 0 or >= `size`. This silently produces extra bits/bytes and can lead to invalid Arrow buffers (buffer length no longer matches the logical bitmap size). Consider validating indexes during `finish` (or in `set`) and raising an ArgumentError when out of range. ########## ruby/red-arrow-format/lib/arrow-format/bitmap-builder.rb: ########## @@ -0,0 +1,89 @@ +# 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. + +require_relative "buffer-alignable" + +module ArrowFormat + class DenseBitmapBuilder + include BufferAlignable + + def initialize + @buffer = +"".b + @n_bits = 0 + @byte = 0 + end + + def append(value) + @byte |= 1 << @n_bits if value + @n_bits += 1 + flush if @n_bits == 8 + self + end + + def finish + flush if @n_bits > 0 + padding_size = buffer_padding_size(@buffer) + @buffer.append_as_bytes(padding(padding_size)) if padding_size > 0 + @buffer.freeze + IO::Buffer.for(@buffer) + end + + private + def flush + @buffer.append_as_bytes([@byte].pack("C")) + @n_bits = 0 + @byte = 0 + end + end + + class SparseBitmapBuilder + include BufferAlignable + + def initialize(default) + @default = default + @indexes = {} + end + + def set(index) + @indexes[index] = true + end Review Comment: `SparseBitmapBuilder#set` always writes the *non-default* value (it flips `default`) and doesn't allow setting an explicit bit value. With a `default` of `true`, calling `set(index)` actually clears the bit, which is surprising for a public API and easy to misuse. Consider renaming this method (e.g., `mark`, `flip`, `set_not_default`) or changing the API to accept an explicit value (e.g., `set(index, value)`). ########## ruby/red-arrow-format/lib/arrow-format/array.rb: ########## @@ -210,6 +221,26 @@ def clear_cache super @values_bitmap = nil end + + def build_data(data) + n = 0 + validity_buffer_builder = nil + values_buffer_builder = DenseBitmapBuilder.new + data.each_with_index do |value, i| + if value.nil? + validity_buffer_builder ||= SparseBitmapBuilder.new(true) + validity_buffer_builder.set(i) + values_buffer_builder.append(false) + elsif value + values_buffer_builder.append(true) + else + values_buffer_builder.append(false) + end + n += 1 + end Review Comment: `BooleanArray.new(values)` currently treats any truthy Ruby object as `true` (e.g., `0`, `"false"`, `Object.new`), which can silently encode unexpected values into a BooleanArray. Since this is a new user-facing constructor, it’s safer to require elements to be `true`, `false`, or `nil` and raise for anything else. -- 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]
