andygrove commented on code in PR #437:
URL: https://github.com/apache/datafusion-spark/pull/437#discussion_r4747717501
##########
crates/sqllogictest/test_files/aggregate/product.slt:
##########
@@ -0,0 +1,193 @@
+# 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
+
+# Spark registers `product` as an internal function, reachable only through
the DataFrame API.
+# The PySpark runner exposes it to SQL so that these records run on Spark too.
+
+query R
+SELECT product(x) FROM VALUES (CAST(2 AS INT)), (CAST(3 AS INT)), (CAST(NULL
AS INT)), (CAST(-1 AS INT)) AS tab(x);
+----
+-6
+
+query R
+SELECT product(x) FROM VALUES (CAST(NULL AS INT)), (CAST(NULL AS INT)) AS
tab(x);
+----
+NULL
+
+query R
+SELECT product(x) FROM VALUES (CAST(1 AS INT)) AS tab(x) WHERE x > 5;
+----
+NULL
+
+query R
+SELECT product(CAST(3 AS INT));
+----
+3
+
+# Every numeric type is cast to DOUBLE
+
+query R
+SELECT product(x) FROM VALUES (CAST(-2 AS TINYINT)), (CAST(3 AS TINYINT)) AS
tab(x);
+----
+-6
+
+query R
+SELECT product(x) FROM VALUES (CAST(-2 AS SMALLINT)), (CAST(3 AS SMALLINT)) AS
tab(x);
+----
+-6
+
+query R
+SELECT product(x) FROM VALUES (CAST(1.5 AS FLOAT)), (CAST(4 AS FLOAT)) AS
tab(x);
+----
+6
+
+query R
+SELECT product(x) FROM VALUES (CAST('1.50' AS DECIMAL(10, 2))), (CAST('-2.00'
AS DECIMAL(10, 2))), (CAST(NULL AS DECIMAL(10, 2))) AS tab(x);
+----
+-3
+
+# Integer products past the BIGINT range are exact DOUBLEs, not wrapped longs
+
+query R
+SELECT product(x) FROM VALUES (CAST(4611686018427387904 AS BIGINT)), (CAST(4
AS BIGINT)) AS tab(x);
+----
+18446744073709552000
+
+query R
+SELECT product(x) FROM VALUES (CAST(-4611686018427387904 AS BIGINT)), (CAST(4
AS BIGINT)) AS tab(x);
+----
+-18446744073709552000
+
+query R
+SELECT product(x) FROM VALUES (CAST(9223372036854775807 AS BIGINT)),
(CAST(9223372036854775807 AS BIGINT)) AS tab(x);
+----
+85070591730234620000000000000000000000
+
+# Overflow and underflow follow IEEE 754 with the sign of the product, in both
ANSI modes
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e308 AS DOUBLE)), (CAST(10 AS DOUBLE)) AS
tab(x);
+----
+Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e308 AS DOUBLE)), (CAST(-10 AS DOUBLE))
AS tab(x);
+----
+-Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(-1e308 AS DOUBLE)), (CAST(-10 AS DOUBLE))
AS tab(x);
+----
+Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e-300 AS DOUBLE)), (CAST(1e-300 AS
DOUBLE)) AS tab(x);
+----
+0
+
+query R
+SELECT product(x) FROM VALUES (CAST('Infinity' AS DOUBLE)), (CAST(-1 AS
DOUBLE)) AS tab(x);
+----
+-Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST('Infinity' AS DOUBLE)), (CAST(0 AS
DOUBLE)) AS tab(x);
+----
+NaN
+
+query R
+SELECT product(x) FROM VALUES (CAST('NaN' AS DOUBLE)), (CAST(2 AS DOUBLE)) AS
tab(x);
+----
+NaN
+
+statement ok
+set datafusion.execution.enable_ansi_mode = true;
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e308 AS DOUBLE)), (CAST(-10 AS DOUBLE))
AS tab(x);
+----
+-Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(4611686018427387904 AS BIGINT)), (CAST(4
AS BIGINT)) AS tab(x);
+----
+18446744073709552000
+
+statement ok
+set datafusion.execution.enable_ansi_mode = false;
+
+# Strings are implicitly cast to DOUBLE
+
+query R
+SELECT product(x) FROM VALUES (CAST('2' AS STRING)), (CAST('3' AS STRING)) AS
tab(x);
+----
+6
+
+statement ok
+set datafusion.execution.enable_ansi_mode = true;
+
+query error
+SELECT product(x) FROM VALUES (CAST('2' AS STRING)), (CAST('abc' AS STRING))
AS tab(x);
+
+statement ok
+set datafusion.execution.enable_ansi_mode = false;
+
+# xfail spark: Spark's legacy cast turns 'abc' into NULL and returns 2.0, but
the implicit cast is DataFusion's strict one
(https://github.com/apache/datafusion-spark/issues/88)
+query error Cannot cast string 'abc' to value of Float64 type
+SELECT product(x) FROM VALUES (CAST('2' AS STRING)), (CAST('abc' AS STRING))
AS tab(x);
+
+query error
+SELECT product(x) FROM VALUES (true), (false) AS tab(x);
+
+# Groups
Review Comment:
Nice coverage of overflow, underflow and the window frames. Could we also
add a few records that go through the two-phase and multi-batch path? Something
like `SET datafusion.execution.target_partitions = 4; SET
datafusion.execution.batch_size = 2;` followed by a grouped `product(CAST(x AS
DOUBLE)) FROM generate_series(1, 10) ... GROUP BY x % 3`. The current records
all fit in one batch, so `merge_batch` on the groups accumulator isn't
exercised from SQL. I tried this locally and the results are correct (`3628800`
overall, and `162 / 280 / 80` per group). It would be good to keep it as a
regression test.
##########
crates/sqllogictest/test_files/aggregate/product.slt:
##########
@@ -0,0 +1,193 @@
+# 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
+
+# Spark registers `product` as an internal function, reachable only through
the DataFrame API.
+# The PySpark runner exposes it to SQL so that these records run on Spark too.
+
+query R
+SELECT product(x) FROM VALUES (CAST(2 AS INT)), (CAST(3 AS INT)), (CAST(NULL
AS INT)), (CAST(-1 AS INT)) AS tab(x);
+----
+-6
+
+query R
+SELECT product(x) FROM VALUES (CAST(NULL AS INT)), (CAST(NULL AS INT)) AS
tab(x);
+----
+NULL
+
+query R
+SELECT product(x) FROM VALUES (CAST(1 AS INT)) AS tab(x) WHERE x > 5;
+----
+NULL
+
+query R
+SELECT product(CAST(3 AS INT));
+----
+3
+
+# Every numeric type is cast to DOUBLE
+
+query R
+SELECT product(x) FROM VALUES (CAST(-2 AS TINYINT)), (CAST(3 AS TINYINT)) AS
tab(x);
+----
+-6
+
+query R
+SELECT product(x) FROM VALUES (CAST(-2 AS SMALLINT)), (CAST(3 AS SMALLINT)) AS
tab(x);
+----
+-6
+
+query R
+SELECT product(x) FROM VALUES (CAST(1.5 AS FLOAT)), (CAST(4 AS FLOAT)) AS
tab(x);
+----
+6
+
+query R
+SELECT product(x) FROM VALUES (CAST('1.50' AS DECIMAL(10, 2))), (CAST('-2.00'
AS DECIMAL(10, 2))), (CAST(NULL AS DECIMAL(10, 2))) AS tab(x);
+----
+-3
+
+# Integer products past the BIGINT range are exact DOUBLEs, not wrapped longs
+
+query R
+SELECT product(x) FROM VALUES (CAST(4611686018427387904 AS BIGINT)), (CAST(4
AS BIGINT)) AS tab(x);
+----
+18446744073709552000
+
+query R
+SELECT product(x) FROM VALUES (CAST(-4611686018427387904 AS BIGINT)), (CAST(4
AS BIGINT)) AS tab(x);
+----
+-18446744073709552000
+
+query R
+SELECT product(x) FROM VALUES (CAST(9223372036854775807 AS BIGINT)),
(CAST(9223372036854775807 AS BIGINT)) AS tab(x);
+----
+85070591730234620000000000000000000000
+
+# Overflow and underflow follow IEEE 754 with the sign of the product, in both
ANSI modes
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e308 AS DOUBLE)), (CAST(10 AS DOUBLE)) AS
tab(x);
+----
+Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e308 AS DOUBLE)), (CAST(-10 AS DOUBLE))
AS tab(x);
+----
+-Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(-1e308 AS DOUBLE)), (CAST(-10 AS DOUBLE))
AS tab(x);
+----
+Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e-300 AS DOUBLE)), (CAST(1e-300 AS
DOUBLE)) AS tab(x);
+----
+0
+
+query R
+SELECT product(x) FROM VALUES (CAST('Infinity' AS DOUBLE)), (CAST(-1 AS
DOUBLE)) AS tab(x);
+----
+-Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST('Infinity' AS DOUBLE)), (CAST(0 AS
DOUBLE)) AS tab(x);
+----
+NaN
+
+query R
+SELECT product(x) FROM VALUES (CAST('NaN' AS DOUBLE)), (CAST(2 AS DOUBLE)) AS
tab(x);
+----
+NaN
+
+statement ok
+set datafusion.execution.enable_ansi_mode = true;
+
+query R
+SELECT product(x) FROM VALUES (CAST(1e308 AS DOUBLE)), (CAST(-10 AS DOUBLE))
AS tab(x);
+----
+-Infinity
+
+query R
+SELECT product(x) FROM VALUES (CAST(4611686018427387904 AS BIGINT)), (CAST(4
AS BIGINT)) AS tab(x);
+----
+18446744073709552000
+
+statement ok
+set datafusion.execution.enable_ansi_mode = false;
+
+# Strings are implicitly cast to DOUBLE
+
+query R
+SELECT product(x) FROM VALUES (CAST('2' AS STRING)), (CAST('3' AS STRING)) AS
tab(x);
+----
+6
+
+statement ok
+set datafusion.execution.enable_ansi_mode = true;
+
+query error
+SELECT product(x) FROM VALUES (CAST('2' AS STRING)), (CAST('abc' AS STRING))
AS tab(x);
+
+statement ok
+set datafusion.execution.enable_ansi_mode = false;
+
+# xfail spark: Spark's legacy cast turns 'abc' into NULL and returns 2.0, but
the implicit cast is DataFusion's strict one
(https://github.com/apache/datafusion-spark/issues/88)
+query error Cannot cast string 'abc' to value of Float64 type
+SELECT product(x) FROM VALUES (CAST('2' AS STRING)), (CAST('abc' AS STRING))
AS tab(x);
+
+query error
+SELECT product(x) FROM VALUES (true), (false) AS tab(x);
+
+# Groups
+
+query TR
+SELECT k, product(v) FROM VALUES ('a', CAST(2 AS INT)), ('a', CAST(3 AS INT)),
('b', CAST(NULL AS INT)), ('c', CAST(-4 AS INT)) AS tab(k, v) GROUP BY k ORDER
BY k;
+----
+a 6
+b NULL
+c -4
+
+# Windows
Review Comment:
Would you add `product(DISTINCT x)`, a `FILTER (WHERE ...)` case that
filters out every row (expecting NULL), and an input of `-0.0` (expecting
`-0`)? I checked all three locally and they behave as expected. The `-0.0` one
matters because Arrow's float comparison kernels and Java disagree about it, so
it is a cheap guard. Spark supports `DISTINCT` and `FILTER` on this aggregate
through the DataFrame API.
##########
crates/sqllogictest/scripts/pyspark_slt_runner.py:
##########
@@ -1267,11 +1272,23 @@ def _start(self) -> None:
builder = builder.config(key, value)
self.spark = builder.getOrCreate()
self.spark.sparkContext.setLogLevel("FATAL")
+ self._register_internal_functions()
self.version = self.spark.version
self.time_zone = SPARK_DEFAULTS["spark.sql.session.timeZone"]
# Set when a query runs the JVM out of memory, which leaves it
unusable.
self.broken = False
+ def _register_internal_functions(self) -> None:
Review Comment:
The `INTERNAL_FUNCTIONS` approach looks reasonable to me as the pattern for
#436. It keeps the SLT files as the single source of expectations. If that is
the plan, should the PR description say `Closes #436`? Right now it says "None".
--
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]