Description
Correlated subqueries with aggregate that pass CheckAnalysis (with only correlated equality predicates) can still return wrong results. This is because equality predicates do not guarantee one-to-one mappings between inner and outer attributes, and the semantics of the plan will be changed when the inner attributes are pulled up through an Aggregate, which gives us wrong results. Currently, the decorrelation framework does not support these types of correlated subqueries, and they should be blocked in CheckAnalysis.
Example 1:
create or replace view t1(c) as values ('a'), ('b') create or replace view t2(c) as values ('ab'), ('abc'), ('bc') select c, (select count(*) from t2 where t1.c = substring(t2.c, 1, 1)) from t1
Correct results: [(a, 2), (b, 1)]
Spark results:
+---+-----------------+ |c |scalarsubquery(c)| +---+-----------------+ |a |1 | |a |1 | |b |1 | +---+-----------------+
Example 2:
create or replace view t1(a, b) as values (0, 6), (1, 5), (2, 4), (3, 3); create or replace view t2(c) as values (6); select c, (select count(*) from t1 where a + b = c) from t2;
Correct results: [(6, 4)]
Spark results:
+---+-----------------+ |c |scalarsubquery(c)| +---+-----------------+ |6 |1 | |6 |1 | |6 |1 | |6 |1 | +---+-----------------+
Attachments
Issue Links
- relates to
-
SPARK-36114 Support subqueries with correlated non-equality predicates
- Resolved
-
SPARK-38180 Allow safe up-cast expressions in correlated equality predicates
- Resolved
- links to