mbutrovich commented on code in PR #334:
URL: https://github.com/apache/datafusion-spark/pull/334#discussion_r4605662710


##########
crates/sqllogictest/test_files/math/ln.slt:
##########
@@ -8,7 +8,94 @@
 # For more information, please see:
 #   https://github.com/apache/datafusion/issues/15914
 
+
 ## Original Query: SELECT ln(1);
 ## PySpark 3.5.5 Result: {'ln(1)': 0.0, 'typeof(ln(1))': 'double', 
'typeof(1)': 'int'}
-#query
-#SELECT ln(1::int);
+query R
+SELECT ln(1::int);
+----
+0
+
+# Result is always DOUBLE
+query T
+SELECT arrow_typeof(ln(1::int));
+----
+Float64
+
+query R
+SELECT ln(2::double);
+----
+0.6931471805599453
+
+query R
+SELECT ln(0.5::double);
+----
+-0.6931471805599453
+
+# Integer, float and decimal inputs are cast to DOUBLE
+query RRRR
+SELECT ln(10::tinyint) AS a, ln(10::bigint) AS b, ln(10::float) AS c, 
ln(10::decimal(5, 2)) AS d;
+----
+2.302585092994046 2.302585092994046 2.302585092994046 2.302585092994046
+
+# Spark returns NULL, not -Infinity or NaN, for an input <= 0
+query RRRR
+SELECT ln(0::int) AS a, ln('-0.0'::double) AS b, ln(-1::int) AS c, 
ln('-Infinity'::double) AS d;
+----
+NULL NULL NULL NULL
+
+query R
+SELECT ln(NULL::double);
+----
+NULL
+
+# NaN is not <= 0, so NaN returns NaN
+query R
+SELECT ln('NaN'::double);
+----
+NaN
+
+query RRR
+SELECT ln('Infinity'::double), ln(1.7976931348623157e308::double), 
ln(4.9e-324::double);
+----
+Infinity 709.782712893384 -744.4400719213812
+
+# Array path, including NULL and non-positive rows
+query R
+SELECT ln(a) FROM (VALUES (1::double), (0::double), (NULL::double), 
(-5::double), (2::double)) AS t(a);
+----
+0
+NULL
+NULL
+NULL
+0.6931471805599453
+
+# Spark uses StrictMath.log (fdlibm), which differs from Rust's f64::ln in the
+# last digit for some inputs (f64::ln gives -0.4000133310645469 here)
+query R
+SELECT ln(0.670311110015402::double);
+----
+-0.40001333106454684
+
+# Spark implicitly casts a string argument to DOUBLE
+query RR
+SELECT ln('10'), ln(a) FROM (VALUES ('1'), (NULL)) AS t(a);
+----
+2.302585092994046 0
+2.302585092994046 NULL
+
+statement ok
+set datafusion.execution.enable_ansi_mode = true;
+
+query error Cannot cast string 'abc' to value of Float64 type
+SELECT ln('abc');
+
+statement ok
+set datafusion.execution.enable_ansi_mode = false;
+
+# Spark 4.2.0 returns NULL for a malformed string when ANSI mode is off; 
DataFusion errors
+# https://github.com/apache/datafusion-spark/issues/88
+# query R
+# SELECT ln('abc');
+# ----
+# NULL

Review Comment:
   Spark parses a string with Java's `Double.valueOf` ([Cast.scala 
L2237-L2240](https://github.com/apache/spark/blob/32f7299601108917fb01920a54e084595b7b3bf8/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala#L2237-L2240)),
 which accepts a trailing `d`, `D`, `f`, or `F`. In PySpark 4.2.0, `ln('10d')` 
and `ln('10F')` return `2.302585092994046` with ANSI mode on and off. Here 
`ln('10d')`, `ln('10D')`, `ln('10F')`, and `ln('1.5f')` fail in both modes with 
`Cannot cast string '10d' to value of Float64 type`. Comet's string-to-float 
cast strips the suffix ([string.rs 
L253-L258](https://github.com/apache/datafusion-comet/blob/0fec45f232eac64aea65971c97956a64811af1dd/native/spark-expr/src/conversion_funcs/string.rs#L253-L258)),
 so the port in #88 should cover it.
   
   Could we record this next to the malformed-string case, and name it in the 
module doc in `log.rs` (lines 19-21) along with the ANSI-off NULL difference?
   
   ```suggestion
   # Spark 4.2.0 returns NULL for a malformed string when ANSI mode is off; 
DataFusion errors
   # https://github.com/apache/datafusion-spark/issues/88
   # query R
   # SELECT ln('abc');
   # ----
   # NULL
   
   # Spark parses strings with Java's Double.valueOf, which accepts a d or f 
suffix in both
   # ANSI modes; DataFusion errors
   # https://github.com/apache/datafusion-spark/issues/88
   # query R
   # SELECT ln('10d');
   # ----
   # 2.302585092994046
   ```



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