vustef commented on code in PR #575:
URL: https://github.com/apache/arrow-julia/pull/575#discussion_r2534318883


##########
src/arraytypes/runendencoded.jl:
##########
@@ -0,0 +1,245 @@
+# 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.
+
+import ..ArrowTypes: RunEndEncodedKind
+
+"""
+    Arrow.RunEndEncoded
+
+An `ArrowVector` that uses run-end encoding (REE) to efficiently represent
+arrays with sequences of repeated values. This is a variation of run-length
+encoding where each run is represented by a value and an integer giving the
+logical index where the run ends.
+
+The array contains two child arrays:
+- `run_ends`: A vector of Int16, Int32, or Int64 values representing the
+  accumulated length where each run ends (strictly ascending, 1-indexed)
+- `values`: The actual values for each run
+
+For example, the array `[1, 1, 1, 2, 2]` would be encoded as:
+- `run_ends = [3, 5]`
+- `values = [1, 2]`
+
+Note: The parent array has no validity bitmap (null_count = 0). Nulls are
+represented as null values in the `values` child array.
+"""
+struct RunEndEncoded{T,R<:Union{Int16,Int32,Int64},A} <: ArrowVector{T}
+    arrow::Vector{UInt8}  # reference to arrow memory blob
+    validity::ValidityBitmap  # always empty for REE (null_count = 0)
+    run_ends::Vector{R}  # strictly ascending indices where runs end
+    values::A  # child array with actual values
+    ℓ::Int64  # logical length of the decoded array
+    metadata::Union{Nothing,Base.ImmutableDict{String,String}}
+end
+
+RunEndEncoded(
+    ::Type{T},
+    b::Vector{UInt8},
+    v::ValidityBitmap,
+    run_ends::Vector{R},
+    values::A,
+    len,
+    meta,
+) where {T,R,A} = RunEndEncoded{T,R,A}(b, v, run_ends, values, len, meta)
+
+Base.size(r::RunEndEncoded) = (r.ℓ,)
+
+"""
+    _find_physical_index(run_ends, logical_index)
+
+Find the physical index (into the values array) for a given logical index.
+Uses binary search to achieve O(log n) lookup time.
+"""
+@inline function _find_physical_index(run_ends::Vector{R}, i::Integer) where 
{R}
+    # Binary search to find which run contains index i
+    # run_ends[j-1] < i <= run_ends[j]
+    lo = 1
+    hi = length(run_ends)
+
+    @inbounds while lo < hi
+        mid = (lo + hi) >>> 1  # unsigned right shift for safe midpoint
+        if run_ends[mid] < i
+            lo = mid + 1
+        else
+            hi = mid
+        end
+    end
+
+    return lo
+end
+
+@propagate_inbounds function Base.getindex(r::RunEndEncoded{T}, i::Integer) 
where {T}
+    @boundscheck checkbounds(r, i)
+    # Find which run contains this index
+    @inbounds physical_idx = _find_physical_index(r.run_ends, i)
+    # Return the value for that run
+    return @inbounds ArrowTypes.fromarrow(T, r.values[physical_idx])
+end
+
+# Iteration - implement efficiently by iterating over runs
+function Base.iterate(r::RunEndEncoded{T}) where {T}
+    isempty(r) && return nothing
+    # State: (current_physical_index, current_logical_index, run_end)
+    run_idx = 1

Review Comment:
   This seems unnecessary



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

Reply via email to