comphead commented on code in PR #17729: URL: https://github.com/apache/datafusion/pull/17729#discussion_r2377158640
########## datafusion/spark/src/function/string/elt.rs: ########## @@ -0,0 +1,257 @@ +// 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::any::Any; +use std::sync::Arc; + +use arrow::array::{ + Array, ArrayRef, AsArray, PrimitiveArray, StringArray, StringBuilder, +}; +use arrow::compute::cast; +use arrow::datatypes::DataType::Utf8; +use arrow::datatypes::{DataType, Int32Type, Int64Type}; +use datafusion_common::{plan_datafusion_err, DataFusionError, Result}; +use datafusion_expr::Volatility::Immutable; +use datafusion_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature}; +use datafusion_functions::utils::make_scalar_function; + +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct SparkElt { + signature: Signature, +} + +impl Default for SparkElt { + fn default() -> Self { + SparkElt::new() + } +} + +impl SparkElt { + pub fn new() -> Self { + Self { + signature: Signature::variadic_any(Immutable), + } + } +} + +impl ScalarUDFImpl for SparkElt { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "elt" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { + Ok(Utf8) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { + make_scalar_function(elt, vec![])(&args.args) + } +} + +fn elt(args: &[ArrayRef]) -> Result<ArrayRef, DataFusionError> { + if args.len() < 2 { + plan_datafusion_err!("elt expects at least 2 arguments: index, value1"); + } + + let n_rows = args[0].len(); + + let idx_i32: Option<&PrimitiveArray<Int32Type>> = + args[0].as_primitive_opt::<Int32Type>(); + let idx_i64: Option<&PrimitiveArray<Int64Type>> = + args[0].as_primitive_opt::<Int64Type>(); + + if idx_i32.is_none() && idx_i64.is_none() { + plan_datafusion_err!( + "elt: first argument must be Int32 or Int64 (got {:?})", + args[0].data_type() + ); + } + + let k: usize = args.len() - 1; Review Comment: lets have a good name for `k` -- 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]
