0lai0 commented on code in PR #5310: URL: https://github.com/apache/datafusion-comet/pull/5310#discussion_r3741142792
########## native/common/src/utf8.rs: ########## @@ -0,0 +1,444 @@ +// 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::decode_utf8_spark_lossy; +use arrow::array::{ + downcast_dictionary_array, Array, ArrayRef, FixedSizeListArray, GenericListArray, + GenericStringArray, GenericStringBuilder, MapArray, OffsetSizeTrait, StructArray, +}; +use arrow::datatypes::DataType; +use arrow::error::ArrowError; +use std::sync::Arc; + +/// Ensure every `Utf8`/`LargeUtf8` array reachable from `array` holds valid UTF-8, decoding invalid +/// bytes the way Spark renders `StringType`. Returns the same `Arc` (zero-copy) when nothing needed +/// decoding. Used at the JVM->native FFI import boundary, where arrow's `from_ffi` builds string +/// arrays via `new_unchecked` and does not validate UTF-8. +pub fn decode_string_arrays(array: &ArrayRef) -> Result<ArrayRef, ArrowError> { + match array.data_type() { + DataType::Utf8 => decode_generic_string::<i32>(array), + DataType::LargeUtf8 => decode_generic_string::<i64>(array), + DataType::Dictionary(_, value_type) + if matches!(value_type.as_ref(), DataType::Utf8 | DataType::LargeUtf8) => + { + // Capture the original Arc before `downcast_dictionary_array!` shadows `array`, so the + // unchanged branch returns it verbatim, preserving the zero-copy contract that the + // Struct/List/Map arms rely on via `Arc::ptr_eq`. + let original = Arc::clone(array); + downcast_dictionary_array!( + array => { + let values = array.values(); + let decoded = decode_string_arrays(values)?; + if Arc::ptr_eq(&decoded, values) { + Ok(original) + } else { + Ok(Arc::new(array.with_values(decoded))) + } + } + t => unreachable!("dictionary type checked by guard: {t}"), + ) + } + DataType::Struct(fields) => { + let s = array + .as_any() + .downcast_ref::<StructArray>() + .expect("data type checked by caller"); + let mut changed = false; + let mut columns = Vec::with_capacity(s.num_columns()); + for col in s.columns() { + let decoded = decode_string_arrays(col)?; + changed |= !Arc::ptr_eq(&decoded, col); + columns.push(decoded); + } + if !changed { + return Ok(Arc::clone(array)); + } + Ok(Arc::new(StructArray::new( + fields.clone(), + columns, + s.nulls().cloned(), + ))) + } + DataType::List(_) => decode_list::<i32>(array), + DataType::LargeList(_) => decode_list::<i64>(array), + DataType::FixedSizeList(field, size) => { + let list = array + .as_any() + .downcast_ref::<FixedSizeListArray>() + .expect("data type checked by caller"); + let values = list.values(); + let decoded = decode_string_arrays(values)?; + if Arc::ptr_eq(&decoded, values) { + return Ok(Arc::clone(array)); + } + Ok(Arc::new(FixedSizeListArray::try_new( + Arc::clone(field), + *size, + decoded, + list.nulls().cloned(), + )?)) + } + DataType::Map(field, ordered) => { + let map = array + .as_any() + .downcast_ref::<MapArray>() + .expect("data type checked by caller"); + let entries: ArrayRef = Arc::new(map.entries().clone()); Review Comment: on the Map arm, `Arc::new(map.entries().clone())` runs even when nothing needs decoding Suggestion : Could we inline the struct walk over `map.entries()` columns (like the Struct arm) so the valid/zero-copy path doesn't allocate that Arc? ########## native/core/src/execution/jni_api.rs: ########## @@ -1315,7 +1316,10 @@ pub unsafe extern "system" fn Java_org_apache_comet_Native_columnarToRowConvert( let array_data = from_ffi(ffi_array, &ffi_schema) .map_err(|e| CometError::Internal(format!("Failed to import array: {}", e)))?; - arrays.push(arrow::array::make_array(array_data)); + let imported = arrow::array::make_array(array_data); + arrays.push(decode_string_arrays(&imported).map_err(|e| { + CometError::Internal(format!("Failed to decode imported string array: {}", e)) Review Comment: nit: Could we map this to `CometError::Arrow` instead of `CometError::Internal`? `decode_string_arrays` is declared as `Result<ArrayRef, ArrowError>`, and the other two call sites (`scan.rs`, `jvm_udf`) already surface that as `CometError::Arrow` (via `?` or an explicit map). Using `Internal` here misclassifies an Arrow/data failure as a Comet internal error. `arrays.push(decode_string_arrays(&imported)?)` should be enough, since `CometError` implements `From<ArrowError>`. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
