andygrove commented on code in PR #19610: URL: https://github.com/apache/datafusion/pull/19610#discussion_r2657906973
########## datafusion/spark/src/function/string/space.rs: ########## @@ -0,0 +1,178 @@ +// 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::{ArrayRef, DictionaryArray, StringArray}; +use arrow::datatypes::{DataType, Int32Type}; +use datafusion_common::cast::{as_dictionary_array, as_int32_array}; +use datafusion_common::{Result, exec_err, plan_err}; +use datafusion_expr::{ + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, +}; +use datafusion_functions::utils::make_scalar_function; +use std::any::Any; +use std::sync::Arc; + +/// Spark-compatible `space` expression +/// <https://spark.apache.org/docs/latest/api/sql/index.html#space> +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct SparkSpace { + signature: Signature, +} + +impl Default for SparkSpace { + fn default() -> Self { + Self::new() + } +} + +impl SparkSpace { + pub fn new() -> Self { + Self { + signature: Signature::uniform( + 1, + vec![ + DataType::Int32, + DataType::Dictionary( + Box::new(DataType::Int32), + Box::new(DataType::Int32), + ), + ], + Volatility::Immutable, + ), + } + } +} + +impl ScalarUDFImpl for SparkSpace { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "space" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, args: &[DataType]) -> Result<DataType> { + let return_type = match &args[0] { + DataType::Dictionary(key_type, _) => { + DataType::Dictionary(key_type.clone(), Box::new(DataType::Utf8)) + } + _ => DataType::Utf8, + }; + Ok(return_type) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { + if args.args.len() != 1 { + return plan_err!("space expects exactly 1 argument"); + } + make_scalar_function(spark_space, vec![])(&args.args) Review Comment: I think that `space` is quite often invoked with a literl argument e.g. `space(2)`. The current implementation converts the scalar value into an array for each batch and then calls `" ".repeat(m as usize)` for each row, resulting in the exact same string being allocated each time. This could be optimized in the scalar case to build the string once rather than per-row and then append the same string to the builder repeatedly. -- 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]
