andygrove commented on code in PR #3536: URL: https://github.com/apache/datafusion-comet/pull/3536#discussion_r2822869169
########## native/core/src/parquet/cast_column.rs: ########## @@ -0,0 +1,639 @@ +// 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 arrow::{ + array::{ + make_array, Array, ArrayRef, LargeListArray, ListArray, MapArray, StructArray, + TimestampMicrosecondArray, TimestampMillisecondArray, + }, + compute::CastOptions, + datatypes::{DataType, FieldRef, Schema, TimeUnit}, + record_batch::RecordBatch, +}; + +use crate::parquet::parquet_support::{spark_parquet_convert, SparkParquetOptions}; +use datafusion::common::format::DEFAULT_CAST_OPTIONS; +use datafusion::common::Result as DataFusionResult; +use datafusion::common::ScalarValue; +use datafusion::logical_expr::ColumnarValue; +use datafusion::physical_expr::PhysicalExpr; +use std::{ + any::Any, + fmt::{self, Display}, + hash::Hash, + sync::Arc, +}; + +/// Returns true if two DataTypes are structurally equivalent (same data layout) +/// but may differ in field names within nested types. +fn types_differ_only_in_field_names(physical: &DataType, logical: &DataType) -> bool { + match (physical, logical) { + (DataType::List(pf), DataType::List(lf)) => { + pf.is_nullable() == lf.is_nullable() + && (pf.data_type() == lf.data_type() + || types_differ_only_in_field_names(pf.data_type(), lf.data_type())) + } + (DataType::LargeList(pf), DataType::LargeList(lf)) => { + pf.is_nullable() == lf.is_nullable() + && (pf.data_type() == lf.data_type() + || types_differ_only_in_field_names(pf.data_type(), lf.data_type())) + } + (DataType::Map(pf, p_sorted), DataType::Map(lf, l_sorted)) => { + p_sorted == l_sorted + && pf.is_nullable() == lf.is_nullable() + && (pf.data_type() == lf.data_type() + || types_differ_only_in_field_names(pf.data_type(), lf.data_type())) + } + (DataType::Struct(pfields), DataType::Struct(lfields)) => { + // For Struct types, field names are semantically meaningful (they + // identify different columns), so we require name equality here. + // This distinguishes from List/Map wrapper field names ("item" vs + // "element") which are purely cosmetic. + pfields.len() == lfields.len() + && pfields.iter().zip(lfields.iter()).all(|(pf, lf)| { + pf.name() == lf.name() + && pf.is_nullable() == lf.is_nullable() + && (pf.data_type() == lf.data_type() + || types_differ_only_in_field_names(pf.data_type(), lf.data_type())) + }) + } + _ => false, + } +} + +/// Recursively relabel an array so its DataType matches `target_type`. +/// This only changes metadata (field names, nullability flags in nested fields); +/// it does NOT change the underlying buffer data. +fn relabel_array(array: ArrayRef, target_type: &DataType) -> ArrayRef { + if array.data_type() == target_type { + return array; + } + match target_type { + DataType::List(target_field) => { + let list = array.as_any().downcast_ref::<ListArray>().unwrap(); + let values = relabel_array(Arc::clone(list.values()), target_field.data_type()); + Arc::new(ListArray::new( + Arc::clone(target_field), + list.offsets().clone(), + values, + list.nulls().cloned(), + )) + } + DataType::LargeList(target_field) => { + let list = array.as_any().downcast_ref::<LargeListArray>().unwrap(); + let values = relabel_array(Arc::clone(list.values()), target_field.data_type()); + Arc::new(LargeListArray::new( + Arc::clone(target_field), + list.offsets().clone(), + values, + list.nulls().cloned(), + )) + } + DataType::Map(target_entries_field, sorted) => { + let map = array.as_any().downcast_ref::<MapArray>().unwrap(); + let entries = relabel_array( + Arc::new(map.entries().clone()), + target_entries_field.data_type(), + ); + let entries_struct = entries.as_any().downcast_ref::<StructArray>().unwrap(); + Arc::new(MapArray::new( + Arc::clone(target_entries_field), + map.offsets().clone(), + entries_struct.clone(), + map.nulls().cloned(), + *sorted, + )) + } + DataType::Struct(target_fields) => { + let struct_arr = array.as_any().downcast_ref::<StructArray>().unwrap(); + let columns: Vec<ArrayRef> = target_fields + .iter() + .zip(struct_arr.columns()) + .map(|(tf, col)| relabel_array(Arc::clone(col), tf.data_type())) + .collect(); + Arc::new(StructArray::new( + target_fields.clone(), + columns, + struct_arr.nulls().cloned(), + )) + } + // Primitive types - shallow swap is safe + _ => { + let data = array.to_data(); + let new_data = data + .into_builder() + .data_type(target_type.clone()) + .build() + .expect("relabel_array: data layout must be compatible"); + make_array(new_data) + } + } +} + +/// Casts a Timestamp(Microsecond) array to Timestamp(Millisecond) by dividing values by 1000. +/// Preserves the timezone from the target type. +fn cast_timestamp_micros_to_millis_array( + array: &ArrayRef, + target_tz: Option<Arc<str>>, +) -> ArrayRef { + let micros_array = array + .as_any() + .downcast_ref::<TimestampMicrosecondArray>() + .expect("Expected TimestampMicrosecondArray"); + + let millis_values: TimestampMillisecondArray = micros_array + .iter() + .map(|opt| opt.map(|v| v / 1000)) + .collect(); Review Comment: ```suggestion let millis_values: TimestampMillisecondArray = arrow::compute::kernels::arity::unary(micros_array, |v| v / 1000); ``` -- 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]
