RyanMarcus commented on code in PR #6752:
URL: https://github.com/apache/arrow-rs/pull/6752#discussion_r1850601711


##########
arrow-cast/src/cast/mod.rs:
##########
@@ -759,6 +763,12 @@ pub fn cast_with_options(
                 "Casting from type {from_type:?} to dictionary type 
{to_type:?} not supported",
             ))),
         },
+        (RunEndEncoded(re_t, _dt), _) => match re_t.data_type() {

Review Comment:
   I imagine copying Int16 data up to Int32 would have a pretty large 
performance penalty in this case. For `DictionaryArray`, each access to the 
values is potentially random and therefore a cache miss, but for REE we will 
always read the run ends and the values sequentially. So my guess is that an 
extra data copy like that would 2x the runtime. Does that make sense or should 
I write a bench?



##########
arrow-cast/src/cast/runend.rs:
##########
@@ -0,0 +1,330 @@
+// 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 std::sync::Arc;
+
+use arrow_array::{
+    builder::ArrayBuilder, make_array, types::RunEndIndexType, Array, 
ArrayRef, ArrowPrimitiveType,
+    Float16Array, Float32Array, Float64Array, Int16Array, Int32Array, 
Int64Array, Int8Array,
+    PrimitiveArray, RunArray, TypedRunArray, UInt16Array, UInt32Array, 
UInt64Array, UInt8Array,
+};
+use arrow_buffer::{ArrowNativeType, NullBufferBuilder};
+use arrow_data::transform::MutableArrayData;
+use arrow_schema::{ArrowError, DataType};
+
+use crate::cast_with_options;
+
+use super::CastOptions;
+
+/// Attempt to cast a run-encoded array into a new type.
+///
+/// `K` is the *current* run end index type
+pub(crate) fn run_end_cast<K: RunEndIndexType>(
+    array: &dyn Array,
+    to_type: &DataType,
+    cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let ree_array = array
+        .as_any()

Review Comment:
   You mean [this 
trait?](https://docs.rs/arrow/latest/arrow/array/trait.AsArray.html) I don't 
think there's an `as_run_array` or similar. I can add it, but the overall line 
count will go up.



##########
arrow-cast/src/cast/runend.rs:
##########
@@ -0,0 +1,330 @@
+// 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 std::sync::Arc;
+
+use arrow_array::{
+    builder::ArrayBuilder, make_array, types::RunEndIndexType, Array, 
ArrayRef, ArrowPrimitiveType,
+    Float16Array, Float32Array, Float64Array, Int16Array, Int32Array, 
Int64Array, Int8Array,
+    PrimitiveArray, RunArray, TypedRunArray, UInt16Array, UInt32Array, 
UInt64Array, UInt8Array,
+};
+use arrow_buffer::{ArrowNativeType, NullBufferBuilder};
+use arrow_data::transform::MutableArrayData;
+use arrow_schema::{ArrowError, DataType};
+
+use crate::cast_with_options;
+
+use super::CastOptions;
+
+/// Attempt to cast a run-encoded array into a new type.
+///
+/// `K` is the *current* run end index type
+pub(crate) fn run_end_cast<K: RunEndIndexType>(
+    array: &dyn Array,
+    to_type: &DataType,
+    cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let ree_array = array
+        .as_any()
+        .downcast_ref::<RunArray<K>>()
+        .ok_or_else(|| {
+            ArrowError::ComputeError(
+                "Internal Error: Cannot cast run end array to RunArray of the 
expected type"
+                    .to_string(),
+            )
+        })?;
+
+    let curr_value_type = ree_array.values().data_type();
+
+    match (curr_value_type, to_type) {
+        // Potentially convert to a new value or run end type
+        (_, DataType::RunEndEncoded(re_t, dt)) => {
+            let values = cast_with_options(ree_array.values(), dt.data_type(), 
cast_options)?;
+            let re = 
PrimitiveArray::<K>::new(ree_array.run_ends().inner().clone(), None);

Review Comment:
   This enables converting REE arrays of one type to another, for example from 
Int32 runs and Float64 data to Int16 runs and Float32 data. See 
`test_run_end_to_run_end` for an example.



##########
arrow-cast/src/cast/runend.rs:
##########
@@ -0,0 +1,330 @@
+// 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 std::sync::Arc;
+
+use arrow_array::{
+    builder::ArrayBuilder, make_array, types::RunEndIndexType, Array, 
ArrayRef, ArrowPrimitiveType,
+    Float16Array, Float32Array, Float64Array, Int16Array, Int32Array, 
Int64Array, Int8Array,
+    PrimitiveArray, RunArray, TypedRunArray, UInt16Array, UInt32Array, 
UInt64Array, UInt8Array,
+};
+use arrow_buffer::{ArrowNativeType, NullBufferBuilder};
+use arrow_data::transform::MutableArrayData;
+use arrow_schema::{ArrowError, DataType};
+
+use crate::cast_with_options;
+
+use super::CastOptions;
+
+/// Attempt to cast a run-encoded array into a new type.
+///
+/// `K` is the *current* run end index type
+pub(crate) fn run_end_cast<K: RunEndIndexType>(
+    array: &dyn Array,
+    to_type: &DataType,
+    cast_options: &CastOptions,
+) -> Result<ArrayRef, ArrowError> {
+    let ree_array = array
+        .as_any()
+        .downcast_ref::<RunArray<K>>()
+        .ok_or_else(|| {
+            ArrowError::ComputeError(
+                "Internal Error: Cannot cast run end array to RunArray of the 
expected type"
+                    .to_string(),
+            )
+        })?;
+
+    let curr_value_type = ree_array.values().data_type();
+
+    match (curr_value_type, to_type) {
+        // Potentially convert to a new value or run end type
+        (_, DataType::RunEndEncoded(re_t, dt)) => {
+            let values = cast_with_options(ree_array.values(), dt.data_type(), 
cast_options)?;
+            let re = 
PrimitiveArray::<K>::new(ree_array.run_ends().inner().clone(), None);
+            let re = cast_with_options(&re, re_t.data_type(), cast_options)?;
+
+            // TODO: we shouldn't need to validate the new run length array
+            // since we can assume we are converting from a valid one, but
+            // there's no "unchecked" variant yet
+            let result: Arc<dyn Array> = match re.data_type() {
+                DataType::Int16 => Arc::new(RunArray::try_new(
+                    re.as_any().downcast_ref::<Int16Array>().unwrap(),
+                    &values,
+                )?),
+                DataType::Int32 => Arc::new(RunArray::try_new(
+                    re.as_any().downcast_ref::<Int32Array>().unwrap(),
+                    &values,
+                )?),
+                DataType::Int64 => Arc::new(RunArray::try_new(
+                    re.as_any().downcast_ref::<Int64Array>().unwrap(),
+                    &values,
+                )?),
+                _ => Err(ArrowError::ComputeError(format!(
+                    "Invalid run end type requested during cast: {:?}",
+                    re.data_type()
+                )))?,
+            };
+
+            Ok(result.slice(ree_array.run_ends().offset(), 
ree_array.run_ends().len()))

Review Comment:
   When we construct a `PrimitiveArray` from the run ends buffer, we have to 
construct the `PrimitiveArray` from the *inner* buffer, which doesn't have the 
offset information. So we have to re-slice it here. Added some code to 
`test_run_end_to_run_end` to confirm this.



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