Skip to content

Storage Join(ALL, RIGHT, ...) + QUALIFY NULL: rewriting WHERE p to WHERE p AND 1 flips 0 rows → all right-side rows with left columns defaulted (no-op rewrite changes result + breaks join matching) #106949

Closed

Description

@qoega

Wrong result: WHERE p AND 1 + QUALIFY NULL over a Join-engine RIGHT JOIN drops both filters (and join matching)

Summary

Over an ALL RIGHT JOIN against a Join(ALL, RIGHT, ...) engine table,
wrapping the WHERE predicate in a no-op conjunction (p → p AND 1)
flips the result from correct to wrong when a QUALIFY clause is present:

  • WHERE p QUALIFY NULL → 0 rows (correct — QUALIFY NULL filters every row);
  • WHERE (p AND 1) QUALIFY NULL → all right-side rows, with the
    left-side columns defaulted even for keys that do match.

Both the WHERE and the QUALIFY filters are effectively dropped, and the
join stops matching. The predicate must reference left-table columns through
a null-safe comparison (t.key <=> tj.key) composed inside another
expression; a plain t.val > 0 predicate or a Memory right table behaves
correctly, so the bug needs the storage-Join right side + the composed
<=> predicate + QUALIFY + the extra AND.

Found by the AST-fuzzer correctness-oracle suite from PR
#99980 (Identity WHERE
oracle, p AND 1 variant).

Minimal repro

CREATE TABLE tmin (key2 UInt64, key1 Int64) ENGINE = Memory;
INSERT INTO tmin VALUES (2,-2),(3,-3);
CREATE TABLE tjmin (key2 UInt64, key1 Int64) ENGINE = Join(ALL, RIGHT, key1, key2);
INSERT INTO tjmin VALUES (2,-2),(6,-6);

SELECT * FROM tmin ALL RIGHT JOIN tjmin ON tjmin.key2 = tmin.key2 AND tjmin.key1 = tmin.key1
WHERE (tmin.key2 <=> tjmin.key2) = tmin.key2
QUALIFY NULL;
-- 0 rows (correct)

SELECT * FROM tmin ALL RIGHT JOIN tjmin ON tjmin.key2 = tmin.key2 AND tjmin.key1 = tmin.key1
WHERE and((tmin.key2 <=> tjmin.key2) = tmin.key2, 1)
QUALIFY NULL;
-- 2 rows (WRONG):
-- 0  0  2  -2     <- left side defaulted although (2,-2) matches
-- 0  0  6  -6

Reproduced on 26.6.1.1 (master 76c2edca2fd, release build), default
settings (no compatibility needed). Without QUALIFY the two forms agree;
with a Memory right table they agree; with the bare predicate
tmin.key2 <=> tjmin.key2 they agree.

Fix area

Interaction of QUALIFY-clause planning with filter pushdown into
StorageJoin reads: with the extra conjunction the combined
WHERE+QUALIFY filter appears to be pushed into (or eliminated around)
the storage-join read incorrectly. Likely in the analyzer/planner QUALIFY
handling (src/Planner/) combined with
StorageJoin's ALL RIGHT non-matched-row generation.

Original fuzzer hit (for reference)

2026-06-10 10:34:50, fuzz-lab cycle 9i instance 04 (release, batch with
--compatibility=25.3), run dir
tmp/fuzz_lab/runs/20260610-1020-58419a326d5/inst/04/
(mismatch_artifacts/run_1/). Identity WHERE (p AND 1) oracle on a
fuzzed query from the storage-join name-clash stateless test.

Activity

  1. qoega commented on Jun 10, 2026

    @qoega
    MemberAuthor

    @groeneai could you please reproduce and fix this one? Repro is in the issue body — deterministic on master 76c2edca2fd, default settings.

  2. added
    fuzz
    sqlancer
    on Jun 10, 2026
  3. groeneai commented on Jun 10, 2026

    @groeneai
    Collaborator

    Reproduced on master and fixed in #107084.

    Root cause is in OUTER JOIN filter push-down. getConjunctionNodes() marks a WHERE conjunct pushable to a join side when all its children are allowed inputs of that side. A pure constant (1, or the NULL literal that QUALIFY NULL folds into WHERE) has no children, so it is trivially pushable to ANY side, including the non-preserved side of a RIGHT JOIN (whose allowed-input set is empty). The constant was pushed into the left input and dropped from the post-join filter, so the NULL filtered out the left rows, the join fabricated non-matched rows with left columns defaulted, and they escaped the now constant-free filter.

    The Join(ALL, RIGHT, ...) engine is needed only because it makes the planner take the FilledJoin direct-read path; the underlying defect also reproduces over a Memory right table with WHERE and(<left predicate>, 1, NULL) (the minimal trigger, no QUALIFY required).

    Fix: push a conjunct to a join side only if it actually depends on an allowed input of that side. Constants stay in the post-join filter. Regression test added; it returns 2 rows on master and 0 with the fix.

  4. clickgapai commented on Jun 10, 2026

    @clickgapai
    Contributor

    Found via ClickGap automated review. Please close or comment if this is incorrect or needs adjustment.

    TL;DR

    Storage Join(ALL, RIGHT, ...) + QUALIFY NULL: rewriting WHERE p to WHERE p AND 1 flips 0 rows → all right-side rows with left columns defaulted (no-op rewrite changes result + breaks join matching)

    ClickGap verified: reproduced it with the script below on master; tested 4 version(s) — 4 affected, 0 clean (see Affects).

    Reproduction

    #!/bin/bash
    # Issue #106949 — Join(ALL,RIGHT) + QUALIFY NULL: WHERE p vs WHERE (p AND 1)
    # Correct result of q2 = 0 rows (QUALIFY NULL filters every row).
    # Bug: q2 emits right-side rows with left columns defaulted (e.g. "0\t0\t2\t-2").
    # exit 1 = bug reproduces ; exit 0 = clean ; exit 125 = inconclusive ; exit 124 = timeout
    set -uo pipefail
    BIN="$1"
    PFX="CREATE TABLE tmin (key2 UInt64, key1 Int64) ENGINE = Memory; INSERT INTO tmin VALUES (2,-2),(3,-3); CREATE TABLE tjmin (key2 UInt64, key1 Int64) ENGINE = Join(ALL, RIGHT, key1, key2); INSERT INTO tjmin VALUES (2,-2),(6,-6);"
    Q2="SELECT * FROM tmin ALL RIGHT JOIN tjmin ON tjmin.key2 = tmin.key2 AND tjmin.key1 = tmin.key1 WHERE and((tmin.key2 <=> tjmin.key2) = tmin.key2, 1) QUALIFY NULL;"
    ERRF="$(mktemp)"
    out=$(timeout 60 "$BIN" local --multiquery ${CLICKGAP_REPRO_SETTINGS:+--$CLICKGAP_REPRO_SETTINGS} --query "$PFX $Q2" </dev/null 2>"$ERRF")
    rc=$?
    err=$(cat "$ERRF"); rm -f "$ERRF"
    [ "$rc" -eq 124 ] && { echo "TIMEOUT"; exit 124; }
    if echo "$err" | grep -qiE "exception|Code: [0-9]+|NOT_IMPLEMENTED|UNKNOWN_|SYNTAX_ERROR|window function"; then
      echo "INCONCLUSIVE (error/unsupported): $err"; exit 125
    fi
    [ "$rc" -ne 0 ] && { echo "INCONCLUSIVE (rc=$rc): $err"; exit 125; }
    if [ -n "$out" ]; then
      echo "BUG: q2 returned rows (expected 0):"; echo "$out"; exit 1
    fi
    echo "CLEAN: q2 returned 0 rows"; exit 0
    

    Bisect

    • Bisected on master in 10 steps (confidence: MEDIUM); a single introducing PR could not be pinned (untested merges with no prebuilt binary in the boundary range).
    • Candidate range: b24615b6e963..a1f135baca60
    • A source-build bisect over the range was attempted but could not produce a verdict; the range above is the most precise information available.

    Affects

    • Reproduces on: master, 26.5, 26.4, 26.3
    • Could not test (binary unavailable): 25.8

    Suggested next step

    Manual triage of the comp-query-optimizer path is needed. The reproducer above should be a fast first check.

    Component: comp-query-optimizer
    Severity: P2


    ClickGap · Finding: h_issue_106949

    Triage metadata

  5. added 3 commits that reference this issue on Jun 11, 2026
    709ad2a
    032515d
    a8177b3
  6. rschu1ze commented on Jul 21, 2026

    @rschu1ze
    Member
  7. added 3 commits that reference this issue on Sep 11, 2026
    0628e25
    1aa8a8b
    b37509a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fuzzsqlancer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions