Weiwei Su created SPARK-62269:
---------------------------------

             Summary: IllegalStateException: Couldn't find exists#... when IN 
subquery appears in grouped SELECT projection with HAVING
                 Key: SPARK-62269
                 URL: https://issues.apache.org/jira/browse/SPARK-62269
             Project: Spark
          Issue Type: Bug
          Components: SQL
    Affects Versions: 3.5.5
         Environment: Apache Spark 3.5.5 (SQL / Catalyst, VALUES-only / 
connector-independent)

            Reporter: Weiwei Su


h3. Summary
In Apache Spark 3.5.5, a grouped query with an {{IN (subquery)}} expression in 
the {{SELECT}} projection and a {{HAVING}} clause consistently fails during 
code generation / reference binding with:
{{java.lang.IllegalStateException: Couldn't find exists#... in [...]}}

The failure is connector-independent (pure {{VALUES}}) and reproduces 3/3 
times. Removing the {{IN}} subquery from the projection or moving it into 
{{WHERE}} succeeds 3/3 times.

h3. Minimal Reproducer (fails 3/3)
{code:sql}
WITH sq1(q1_col_1, q1_col_2, q1_col_3) AS (
  SELECT * FROM VALUES
    ('a', 1, 1),
    ('b', 2, 2)
)
SELECT
  37,
  q1_col_2 IN (SELECT v FROM VALUES (1), (2) AS t(v))
FROM sq1
GROUP BY q1_col_2, q1_col_1
HAVING COUNT(q1_col_3) <> 48;
{code}

(Also reproduces 3/3 when combined with an aggregate expression in the same 
projection, e.g. {{COUNT(DISTINCT q1_col_2) <= NULLIF(q1_col_2, q1_col_2) AND 
q1_col_2 IN (SELECT v FROM VALUES (1), (2) AS t(v))}}.)

h3. Observed Error
{code:text}
java.lang.IllegalStateException: Couldn't find exists#83682 in 
[q1_col_2#83673,q1_col_1#83672,count(1)#83678L]
  at 
org.apache.spark.sql.catalyst.expressions.BindReferences$$anonfun$bindReference$1.applyOrElse(BoundAttribute.scala:80)
  at 
org.apache.spark.sql.catalyst.expressions.BindReferences$.bindReference(BoundAttribute.scala:73)
  at 
org.apache.spark.sql.catalyst.expressions.BindReferences$.bindReferences(BoundAttribute.scala:94)
  at 
org.apache.spark.sql.execution.aggregate.HashAggregateExec.generateResultFunction(HashAggregateExec.scala:314)
  at 
org.apache.spark.sql.execution.aggregate.HashAggregateExec.doProduceWithKeys(HashAggregateExec.scala:538)
  at 
org.apache.spark.sql.execution.WholeStageCodegenExec.doCodeGen(WholeStageCodegenExec.scala:660)
{code}

h3. Controls (both succeed 3/3)
1. Removing the {{IN}} subquery from the projection:
{code:sql}
WITH sq1(q1_col_1, q1_col_2, q1_col_3) AS (
  SELECT * FROM VALUES
    ('a', 1, 1),
    ('b', 2, 2)
)
SELECT
  37,
  COUNT(DISTINCT q1_col_2) <= NULLIF(q1_col_2, q1_col_2)
FROM sq1
GROUP BY q1_col_2, q1_col_1
HAVING COUNT(q1_col_3) <> 48;
{code}

2. Moving the {{IN}} subquery into {{WHERE}}:
{code:sql}
WITH sq1(q1_col_1, q1_col_2, q1_col_3) AS (
  SELECT * FROM VALUES
    ('a', 1, 1),
    ('b', 2, 2)
)
SELECT 37, COUNT(DISTINCT q1_col_2)
FROM sq1
WHERE q1_col_2 IN (SELECT v FROM VALUES (1), (2) AS t(v))
GROUP BY q1_col_2, q1_col_1
HAVING COUNT(q1_col_3) <> 48;
{code}

h3. Expected Behavior
The query should plan and execute without an internal {{IllegalStateException}} 
in {{BindReferences}} / {{HashAggregateExec}}, returning two rows: {{(37, 
true)}} and {{(37, true)}}.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to