Skip to content

PushDownFilter moves volatile conjuncts ahead of the comparisons it simplifies #27683

In review

Description

@mbutrovich

Describe the bug

When simplify_predicates removes or rewrites a conjunct of a filter, PushDownFilter rebuilds the filter with every conjunct that is not a column-literal comparison first, volatile ones included. AND skips the rows that earlier conjuncts already rejected, so a volatile conjunct moved to the front runs on more rows. That breaks G2 of #25459 ("A rule never adds an evaluation of a volatile expression"). It also contradicts the 55.0.0 upgrade guide, which says volatile expressions keep "their position in the conjunct list ... so the number of times they evaluate per query does not change."

To Reproduce

The two queries select the same rows. The second has a redundant a > 1. In an .slt file, with counted_volatile and evaluations() from evaluation_counter.rs:

statement ok
CREATE TABLE t (a INT) AS VALUES (1), (2), (3), (4), (5), (6), (7), (8), (9), (10);

statement ok
SELECT evaluations();

query I
SELECT a FROM t WHERE a > 9 AND counted_volatile(a) > 0;
----
10

query I
SELECT evaluations();
----
1

query I
SELECT a FROM t WHERE a > 1 AND a > 9 AND counted_volatile(a) > 0;
----
10

query I
SELECT evaluations();
----
10

The plans show why:

FilterExec: a@0 > 9 AND counted_volatile(a@0) > 0
FilterExec: counted_volatile(a@0) > 0 AND a@0 > 9

The same happens with random() in datafusion-cli:

CREATE TABLE t (a INT) AS VALUES (1), (2), (3);
EXPLAIN SELECT * FROM t WHERE a > 1 AND a > 2 AND random() < 2;

01)Filter: random() < Float64(2) AND t.a > Int32(2)

More shapes:

WHERE Filter
a > 9 AND v > 0 AND a > 1 v > 0 AND a > 9
a >= 9 AND (a < 3 OR a < 10) AND v > 0 a < 10 AND v > 0 AND a >= 9
a > 9 AND v > 0 AND v < 100 AND a > 1 v > 0 AND v < 100 AND a > 9
a > 1 AND v > 0 (nothing simplified) a > 1 AND v > 0

Here v stands for counted_volatile(t.a). Exact duplicates such as a > 9 AND a > 9 are removed earlier by the expression simplifier and keep the order.

Expected behavior

A volatile conjunct keeps its position relative to the conjuncts before it. In the example, counted_volatile(a) > 0 stays after a > 9, and both queries evaluate it once.

Additional context

simplify_conjunction collects the conjuncts it can't group by column into other_predicates, starts its result with them, and appends each column's simplified comparisons (simplify_predicates.rs L219-L224). This dates from #16362. Since #26132, push_down_filter ignores that regrouping when it only permutes the conjuncts, but keeps the regrouped order once a conjunct is removed or rewritten (push_down_filter.rs L876-L903). reorder_predicates keeps the order when any conjunct is volatile (reorder_predicates.rs L34-L39), but it runs after simplify_predicates and receives the order that was already changed.

One fix is for simplify_conjunction to emit each column's simplified comparisons at the position of that column's first conjunct and leave every other conjunct where it was. A simplified comparison is at least as restrictive as each comparison it replaces, so no later conjunct sees more rows than before.

The same regrouping also moves fallible conjuncts ahead of the comparisons that guard them. For example, x <> 0 AND 10 / x > 1 AND y > 1 AND y > 2 becomes 10 / CAST(x AS Int64) > 1 AND x != 0 AND y > 2 and fails with divide by zero. The upgrade guide says evaluation order is not guaranteed for those, but the fix above would keep them in place too.


I ran every query and plan in the draft on `upstream/main` (`fb20da453`) in a scratch `.slt`, now deleted. The `1` in the first count comes from `AND` filtering before an expensive conjunct when at most `PRE_SELECTION_THRESHOLD` (0.2) of the rows remain ([binary.rs L1320](https://github.com/apache/datafusion/blob/fb20da453cf2779dae7f438ec1f508921c4acb19/datafusion/physical-expr/src/expressions/binary.rs#L1320)). Here 1 of 10 rows remains. With #27302 merged, the first query evaluates once at any selectivity. Then the two queries differ whenever `a > 9` rejects any row, not only when at most 20% remain.

The divide-by-zero plan in the last paragraph is from the PR head run (same optimizer code, see "What I ran").

Two things I left out of the issue:

- The `TableScan` pushdown path also moves volatile filters, but to the end: `with_filters` gets the remaining non-volatile filters, then `volatile_filters` ([push_down_filter.rs L1387-L1392](https://github.com/apache/datafusion/blob/fb20da453cf2779dae7f438ec1f508921c4acb19/datafusion/optimizer/src/push_down_filter.rs#L1387-L1392)). That can only reduce evaluations, so it doesn't break G2. It does make the guide's "position is preserved" sentence inexact. If the fixer wants the guide to state the real rule, it is "a volatile conjunct never moves ahead of a conjunct that preceded it".
- With the fix, the permutation check that #26132 added in `push_down_filter` (L878-L895) might become unnecessary, because regrouping would no longer permute. I haven't checked whether it covers other cases, such as OR branches.

I didn't prototype the fix, so I don't know how many plan expectations in `simplify_predicates.slt` and other files would change.

Activity

  1. added theissue type on Nov 23, 2026
  2. added
    bug
    on Nov 23, 2026
  3. vishalkotak commented on Nov 25, 2026

    @vishalkotak

    take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions