andygrove commented on code in PR #302:
URL: https://github.com/apache/datafusion-spark/pull/302#discussion_r4585375380
##########
crates/spark/src/function/math/positive.rs:
##########
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF)
under one or more contributor license agreements
+
+use arrow::compute::CastOptions;
+use arrow::datatypes::DataType;
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, plan_err};
+use datafusion_expr::{
+ ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+ Volatility,
+};
+use datafusion_macros::user_doc;
+
+/// Spark-compatible `positive` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#positive>
+#[user_doc(
+ doc_section(label = "Math Functions"),
+ description = "Returns `expr` unchanged. Accepts integer, floating-point,
decimal and interval values. \
+ A string is cast to DOUBLE: a malformed string returns NULL with
`datafusion.execution.enable_ansi_mode` off \
+ and fails with it on. Returns NULL if the input is NULL.",
+ syntax_example = "positive(expr)",
+ sql_example = r#"```sql
+> SELECT positive(1);
+1
+> SELECT positive('-1.11');
+-1.11
+```"#,
+ argument(
+ name = "expr",
+ description = "Numeric, interval or string expression. NULL returns
NULL."
+ )
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkPositive {
+ signature: Signature,
+}
+
+impl Default for SparkPositive {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl SparkPositive {
+ pub fn new() -> Self {
+ Self {
+ signature: Signature::user_defined(Volatility::Immutable),
+ }
+ }
+}
+
+fn is_string(data_type: &DataType) -> bool {
+ matches!(
+ data_type,
+ DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View
+ )
+}
+
+impl ScalarUDFImpl for SparkPositive {
+ fn name(&self) -> &str {
+ "positive"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+ if is_string(&arg_types[0]) {
Review Comment:
`return_type` indexes `arg_types[0]` with no length check. Only the
`coerce_types` path enforces arity through `take_function_args`, so a direct
`return_type(&[])` call panics instead of returning an error. Use
`take_function_args(self.name(), arg_types)?` here as well.
--
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]