Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-35080

Correlated subqueries with equality predicates can return wrong results

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 3.2.0
    • 2.4.8, 3.0.3, 3.1.2, 3.2.0
    • SQL
    • None

    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

          Activity

            People

              allisonwang-db Allison Wang
              allisonwang-db Allison Wang
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: