andygrove commented on code in PR #4365: URL: https://github.com/apache/datafusion-comet/pull/4365#discussion_r3303886623
########## native/spark-expr/src/json_funcs/json_array_length.rs: ########## @@ -0,0 +1,137 @@ +// 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::{Array, ArrayRef, Int32Builder}; +use arrow::datatypes::DataType; +use datafusion::common::cast::as_string_array; +use datafusion::common::{exec_err, Result, ScalarValue}; +use datafusion::logical_expr::{ + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, +}; + +use std::any::Any; +use std::sync::Arc; + +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct JsonArrayLength { + signature: Signature, +} + +impl Default for JsonArrayLength { + fn default() -> Self { + Self::new() + } +} + +impl JsonArrayLength { + pub fn new() -> Self { + Self { + signature: Signature::variadic(vec![DataType::Utf8], Volatility::Immutable), + } + } +} + +impl ScalarUDFImpl for JsonArrayLength { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "json_array_length" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { + Ok(DataType::Int32) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { + spark_json_array_length(&args.args) + } +} + +fn spark_json_array_length(args: &[ColumnarValue]) -> Result<ColumnarValue> { + if args.len() != 1 { + return exec_err!("json_array_length function takes exactly one argument"); + } + match &args[0] { + ColumnarValue::Array(array) => { + let result = spark_json_array_length_array(array)?; + Ok(ColumnarValue::Array(result)) + } + ColumnarValue::Scalar(scalar) => { + let result = spark_json_array_length_scalar(scalar)?; + Ok(ColumnarValue::Scalar(result)) + } + } +} + +fn spark_json_array_length_array(array: &ArrayRef) -> Result<ArrayRef> { + match array.data_type() { + DataType::Utf8 => { + let array = as_string_array(array)?; + let mut builder = Int32Builder::with_capacity(array.len()); + + for row_idx in 0..array.len() { + if array.is_null(row_idx) { + builder.append_null(); + } else { + let json_str = array.value(row_idx); + if let Some(json_array_length) = get_json_array_length(json_str) { + builder.append_value(json_array_length); + } else { + builder.append_null() + } + } + } + Ok(Arc::new(builder.finish())) + } + other => { + exec_err!("Unsupported data type {other:?} for function `json_array_length`") + } + } +} + +fn spark_json_array_length_scalar(scalar: &ScalarValue) -> Result<ScalarValue> { + match scalar { + ScalarValue::Utf8(value) => { + let length = value + .clone() + .and_then(|json_str| get_json_array_length(&json_str)); + Ok(ScalarValue::Int32(length)) + } + other => { + exec_err!("Unsupported data type {other:?} for function `json_array_length`") + } + } +} + +fn get_json_array_length(json_str: &str) -> Option<i32> { + match serde_json::from_str::<serde_json::Value>(json_str) { Review Comment: This is parsing and materializing the whole array just to get the length. Spark avoids this with a streaming parser, so our performance here may not be great, and we will likely use more memory than Spark. Maybe you could look into streaming options with `serde_json`? -- 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]
