TechWizard9999 commented on code in PR #302:
URL: https://github.com/apache/datafusion-spark/pull/302#discussion_r4586054091


##########
crates/sqllogictest/test_files/math/positive.slt:
##########
@@ -10,5 +10,115 @@
 
 ## Original Query: SELECT positive(1);
 ## PySpark 3.5.5 Result: {'(+ 1)': 1, 'typeof((+ 1))': 'int', 'typeof(1)': 
'int'}
-#query
-#SELECT positive(1::int);
+query IT
+SELECT positive(1::int), arrow_typeof(positive(1::int));
+----
+1 Int32
+
+# Integers keep their type and value, including the minimum value
+query IIII
+SELECT positive((-128)::tinyint), positive((-32768)::smallint), 
positive((-2147483648)::int), positive(CAST('-9223372036854775808' AS BIGINT));
+----
+-128 -32768 -2147483648 -9223372036854775808
+
+query TTTT
+SELECT arrow_typeof(positive(1::tinyint)) AS a, 
arrow_typeof(positive(1::smallint)) AS b, arrow_typeof(positive(1::int)) AS c, 
arrow_typeof(positive(1::bigint)) AS d;
+----
+Int8 Int16 Int32 Int64
+
+# Floating point values are returned unchanged
+query RRRR
+SELECT positive(1.5::float), positive(CAST('NaN' AS DOUBLE)), 
positive(CAST('Infinity' AS DOUBLE)), positive(CAST('-Infinity' AS DOUBLE));
+----
+1.5 NaN Infinity -Infinity
+
+query T
+SELECT arrow_typeof(positive(1.5::float));
+----
+Float32
+
+# The sign of -0.0 is kept: pow(-0.0, -1.0) is -Infinity
+query R
+SELECT pow(positive(CAST('-0.0' AS DOUBLE)), -1.0);
+----
+-Infinity
+
+# DECIMAL keeps its precision and scale
+query RT
+SELECT positive(CAST(-1.11 AS DECIMAL(3, 2))), 
arrow_typeof(positive(CAST(-1.11 AS DECIMAL(3, 2))));
+----
+-1.11 Decimal128(3, 2)
+
+# A STRING is cast to DOUBLE
+query RT
+SELECT positive('-1.11'), arrow_typeof(positive('-1.11'));

Review Comment:
   Added the column tests.



##########
crates/sqllogictest/test_files/math/positive.slt:
##########
@@ -10,5 +10,115 @@
 
 ## Original Query: SELECT positive(1);
 ## PySpark 3.5.5 Result: {'(+ 1)': 1, 'typeof((+ 1))': 'int', 'typeof(1)': 
'int'}
-#query
-#SELECT positive(1::int);
+query IT
+SELECT positive(1::int), arrow_typeof(positive(1::int));
+----
+1 Int32
+
+# Integers keep their type and value, including the minimum value
+query IIII
+SELECT positive((-128)::tinyint), positive((-32768)::smallint), 
positive((-2147483648)::int), positive(CAST('-9223372036854775808' AS BIGINT));
+----
+-128 -32768 -2147483648 -9223372036854775808
+
+query TTTT
+SELECT arrow_typeof(positive(1::tinyint)) AS a, 
arrow_typeof(positive(1::smallint)) AS b, arrow_typeof(positive(1::int)) AS c, 
arrow_typeof(positive(1::bigint)) AS d;
+----
+Int8 Int16 Int32 Int64
+
+# Floating point values are returned unchanged
+query RRRR
+SELECT positive(1.5::float), positive(CAST('NaN' AS DOUBLE)), 
positive(CAST('Infinity' AS DOUBLE)), positive(CAST('-Infinity' AS DOUBLE));
+----
+1.5 NaN Infinity -Infinity
+
+query T
+SELECT arrow_typeof(positive(1.5::float));
+----
+Float32
+
+# The sign of -0.0 is kept: pow(-0.0, -1.0) is -Infinity
+query R
+SELECT pow(positive(CAST('-0.0' AS DOUBLE)), -1.0);
+----
+-Infinity
+
+# DECIMAL keeps its precision and scale
+query RT
+SELECT positive(CAST(-1.11 AS DECIMAL(3, 2))), 
arrow_typeof(positive(CAST(-1.11 AS DECIMAL(3, 2))));
+----
+-1.11 Decimal128(3, 2)
+
+# A STRING is cast to DOUBLE
+query RT
+SELECT positive('-1.11'), arrow_typeof(positive('-1.11'));
+----
+-1.11 Float64
+
+# A malformed STRING is NULL when ANSI mode is off
+query R
+SELECT positive('abc');
+----
+NULL
+
+# Intervals are returned unchanged
+query BT
+SELECT positive(make_dt_interval(1::int, 2::int)) = make_dt_interval(1::int, 
2::int), arrow_typeof(positive(make_dt_interval(1::int, 2::int)));

Review Comment:
   Added tests for both units.



##########
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]) {
+            Ok(DataType::Float64)
+        } else {
+            Ok(arg_types[0].clone())
+        }
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        let [data_type] = take_function_args(self.name(), arg_types)?;
+        match data_type {
+            // A NULL literal is treated as DOUBLE, like Spark's implicit cast
+            DataType::Null => Ok(vec![DataType::Float64]),
+            t if t.is_numeric()
+                || is_string(t)
+                || matches!(t, DataType::Interval(_) | DataType::Duration(_)) 
=>

Review Comment:
   Narrowed it to the Spark types.



##########
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]) {
+            Ok(DataType::Float64)
+        } else {
+            Ok(arg_types[0].clone())
+        }
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        let [data_type] = take_function_args(self.name(), arg_types)?;
+        match data_type {
+            // A NULL literal is treated as DOUBLE, like Spark's implicit cast
+            DataType::Null => Ok(vec![DataType::Float64]),
+            t if t.is_numeric()
+                || is_string(t)
+                || matches!(t, DataType::Interval(_) | DataType::Duration(_)) 
=>
+            {
+                Ok(vec![t.clone()])
+            }
+            _ => plan_err!(
+                "positive requires a numeric or interval input, got 
{data_type}"

Review Comment:
   Updated the error message.



##########
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:
   Done thanks.



-- 
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]

Reply via email to