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

Don't push filters beneath aggregates with empty grouping expressions

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Critical
    • Resolution: Fixed
    • Affects Version/s: 2.1.0, 2.2.0, 2.3.0
    • Fix Version/s: 2.2.2, 2.3.0
    • Component/s: SQL
    • Labels:
    • Target Version/s:

      Description

      The following SQL query should return zero rows, but in Spark it actually returns one row:

      SELECT 1 from (
        SELECT 1 AS z,
        MIN(a.x)
        FROM (select 1 as x) a
        WHERE false
      ) b
      where b.z != b.z
      

      The problem stems from the `PushDownPredicate` rule: when this rule encounters a filter on top of an Aggregate operator, e.g. `Filter(Agg(...))`, it removes the original filter and adds a new filter onto Aggregate's child, e.g. `Agg(Filter(...))`. This is often okay, but the case above is a counterexample: because there is no explicit `GROUP BY`, we are implicitly computing a global aggregate over the entire table so the original filter was not acting like a `HAVING` clause filtering the number of groups: if we push this filter then it fails to actually reduce the cardinality of the Aggregate output, leading to the wrong answer.

      A simple fix is to never push down filters beneath aggregates when there are no grouping expressions.

        Attachments

          Activity

            People

            • Assignee:
              joshrosen Josh Rosen
              Reporter:
              joshrosen Josh Rosen
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: