Yibo Dong created SPARK-62283:
---------------------------------
Summary: Correlated scalar subquery in WHERE over another
scalar-subquery result fails with an internal assertion
Key: SPARK-62283
URL: https://issues.apache.org/jira/browse/SPARK-62283
Project: Spark
Issue Type: Bug
Components: SQL
Affects Versions: 5.0.0
Environment: Stock Apache Spark 5.0.0-SNAPSHOT at commit
082cf160890a0a0a748f70ffb5a993144b0de079, Java 25.0.1, macOS 15.7.4 aarch64,
local[2], in-memory session catalog, built-in Parquet data source (V1), default
SQL configuration.
Reporter: Yibo Dong
A derived table computes a column using a correlated scalar subquery. A second
correlated scalar subquery referencing that computed column works in the SELECT
list, but fails with an internal assertion when used in WHERE.
Both queries are valid and should produce consistent results. Instead, the
WHERE query fails with:
{noformat}
AssertionError: Expects 1 field, but got 2; something went wrong in analysis
{noformat}
h2. How to reproduce
Run the following SQL in a fresh Spark SQL session:
{code:sql}
CREATE OR REPLACE TEMP VIEW o(k) AS VALUES (1), (2);
CREATE OR REPLACE TEMP VIEW a(v) AS VALUES (1), (1);
CREATE OR REPLACE TEMP VIEW b(w) AS VALUES (2);
-- Control: the second correlated subquery in SELECT.
SELECT k, s,
(SELECT count(*) FROM b WHERE b.w = d.s) AS c
FROM (
SELECT k,
(SELECT sum(v) FROM a WHERE a.v = o.k) AS s
FROM o
) d;
-- The same subquery used in WHERE.
SELECT k, s
FROM (
SELECT k,
(SELECT sum(v) FROM a WHERE a.v = o.k) AS s
FROM o
) d
WHERE (SELECT count(*) FROM b WHERE b.w = d.s) > 0;
{code}
Save as tmp.sql and execute:
{code:bash}
spark-sql --master 'local[2]' --conf spark.sql.catalogImplementation=in-memory
-f tmp.sql
{code}
h2. Expected result
The control query returns:
{noformat}
k | s | c
--+------+---
1 | 2 | 1
2 | NULL | 0
{noformat}
The WHERE query should retain only the row for which the second scalar subquery
is positive:
{noformat}
k | s
--+--
1 | 2
{noformat}
h2. Actual result
The control query returns the expected results, but the WHERE query fails:
{noformat}
java.util.concurrent.ExecutionException: Boxed Exception
Caused by: java.lang.AssertionError:
assertion failed: Expects 1 field, but got 2;
something went wrong in analysis
at scala.Predef$.assert(Predef.scala:279)
at org.apache.spark.sql.execution.ScalarSubquery.updateResult(subquery.scala:94)
at
org.apache.spark.sql.execution.SparkPlan.$anonfun$waitForSubqueries$1(SparkPlan.scala:302)
{noformat}
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]