brancz commented on code in PR #7649: URL: https://github.com/apache/arrow-rs/pull/7649#discussion_r2144549751
########## arrow-row/src/run.rs: ########## @@ -0,0 +1,164 @@ +// 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. + +use crate::{null_sentinel, RowConverter, Rows, SortField}; +use arrow_array::types::RunEndIndexType; +use arrow_array::{Array, RunArray}; +use arrow_buffer::{ArrowNativeType, Buffer}; +use arrow_schema::{ArrowError, SortOptions}; + +/// Computes the lengths of each row for a RunEndEncodedArray +pub fn compute_lengths<R: RunEndIndexType>( + lengths: &mut [usize], + rows: &Rows, + array: &RunArray<R>, +) { + // We don't need to use run_ends directly, just access through the array + for (idx, length) in lengths.iter_mut().enumerate() { + let physical_idx = array.get_physical_index(idx); + let is_valid = array.values().is_valid(physical_idx); + if is_valid { + let row = rows.row(physical_idx); + *length += row.data.len() + 1; // 1 for the validity byte + } else { + *length += 1; // just the null sentinel + } + } +} + +/// Encodes the provided `RunEndEncodedArray` to `out` with the provided `SortOptions` +/// +/// `rows` should contain the encoded values +pub fn encode<R: RunEndIndexType>( + data: &mut [u8], + offsets: &mut [usize], + rows: &Rows, + opts: SortOptions, + array: &RunArray<R>, +) { + for (idx, offset) in offsets.iter_mut().skip(1).enumerate() { + let physical_idx = array.get_physical_index(idx); + let is_valid = array.values().is_valid(physical_idx); + + let out = &mut data[*offset..]; + if is_valid { + let row = rows.row(physical_idx); + out[0] = 1; // valid + out[1..1 + row.data.len()].copy_from_slice(row.data); + *offset += row.data.len() + 1; + } else { + out[0] = null_sentinel(opts); + *offset += 1; + } + } +} + +/// Decodes a RunEndEncodedArray from `rows` with the provided `options` +/// +/// # Safety +/// +/// `rows` must contain valid data for the provided `converter` +pub unsafe fn decode<R: RunEndIndexType>( + converter: &RowConverter, + rows: &mut [&[u8]], + field: &SortField, + validate_utf8: bool, +) -> Result<RunArray<R>, ArrowError> { + let opts = field.options; + + // Track null values and collect row data to avoid borrow issues + let mut valid_flags = Vec::with_capacity(rows.len()); + let mut row_data = Vec::with_capacity(rows.len()); + + // First pass: collect valid flags and data for each row + for row in rows.iter() { + let is_valid = row[0] != null_sentinel(opts); + valid_flags.push(is_valid); + if is_valid { + row_data.push(&row[1..]); + } else { + row_data.push(&[][..]); + } + } + + // Now build run ends and values + let mut run_ends = Vec::new(); + let mut values_data = Vec::new(); + let mut current_value_idx = 0; + let mut current_run_end = 0; + + for (idx, is_valid) in valid_flags.iter().enumerate() { + current_run_end += 1; + + if idx == 0 { + // Add the first row data + values_data.push(row_data[idx]); + run_ends.push(R::Native::usize_as(current_run_end)); + continue; + } + + // Check if this row is different from the previous one + let value_changed = if !is_valid { + // Null value - check if previous was null + !valid_flags[idx - 1] + } else if !valid_flags[idx - 1] { + // Previous was null, this is not + true + } else { + // Both are valid, compare data + let prev_data = row_data[idx - 1]; + let curr_data = row_data[idx]; + prev_data != curr_data + }; + + if value_changed { + // Start a new run + current_value_idx += 1; + values_data.push(row_data[idx]); + run_ends.push(R::Native::usize_as(current_run_end)); + } else { + // Update the current run end + run_ends[current_value_idx] = R::Native::usize_as(current_run_end); + } + } + + // Convert collected values to arrays + let mut values_rows = values_data.clone(); Review Comment: Walking borrow-checker, you're right! -- 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: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org