Oracle EXISTS AND NOT EXISTS Returns a Row: Bug 35915968

A self-contradicting predicate returns a row in Oracle 19c and 21c. The cause is an optimizer rewrite bug, and there are three ways around it.

Binary Buddies · 5 min read · 4 views

A view that gets called inside a NOT EXISTS misbehaves, and the smallest reproduction is a query that contradicts itself. Select from dual where EXISTS (SELECT * FROM view_abcd) AND NOT EXISTS (SELECT * FROM view_abcd), and Oracle returns one row. Both subqueries read the same view, so at most one of them can be true. This was reported on Oracle 19c Enterprise (19.11) and 21c (21.3), and the original question on Stack Overflow has the full details.

The accepted answer calls it a bug, and it is not a logic error in the query. The view uses ANSI syntax (OUTER APPLY, a JOIN, and FETCH FIRST inside a correlated subquery). Oracle rewrites that syntax internally, and the rewrites interact badly when the view is merged into an outer query that has EXISTS predicates.

If you have this kind of view in production, the cost is silent wrong results rather than an error. A NOT EXISTS check that is quietly always true or always false is the kind of defect that passes review and only shows up in the data.

The reproduction

The reporter reduced the view to the parts that still triggered the behaviour:

CREATE OR REPLACE FORCE EDITIONABLE VIEW view_abcd (DUMMY) AS
SELECT 1 AS DUMMY
FROM dual a
OUTER APPLY (
  SELECT * FROM dual b
  JOIN dual c ON c.DUMMY = (
    SELECT d.DUMMY FROM dual d
    WHERE d.DUMMY = a.DUMMY
    FETCH FIRST 1 ROW ONLY
  )
);

The contradictory query against it returns ONE_ROW = 1. Wrapping the same view in a CTE (WITH with_abcd AS (SELECT * FROM view_abcd)) and running the same EXISTS AND NOT EXISTS pattern returns no rows, which is the correct result. That difference is a useful diagnostic. The data and the view definition are identical, and only the query shape the optimizer sees has changed.

What the plan shows

The answer's reading of the execution plan is that Oracle ends up with an unconditional FAST DUAL. The existence predicates are gone from the executable plan, so the outer query on dual just returns its row. Neither the EXISTS nor the NOT EXISTS is evaluated.

The explanation given is that the analytic-based FETCH FIRST transformation introduces query-block boundaries inside a correlated, outer-joined lateral-view structure. After join translation and view merging, the old code path fails to keep those predicates. This is why the CTE form behaves differently: it changes how the blocks are merged.

Why FETCH FIRST is the trigger

Per the answer, ANSI constructs such as OUTER JOIN and FETCH FIRST are not native to the engine. They are supported through query-planner transformations, and those transformations often conflict with other transformations. The answer describes the ANSI transformations as unfortunately famous for their bugs.

In this case FETCH FIRST 1 ROW ONLY is rewritten to ROW_NUMBER() OVER ... with a filter of <= 1. That form merges with the other transformations and triggers the bug. The fix, tracked as Bug 35915968, replaces it with ROWNUM <= 1. A ROWNUM predicate makes the query block non-mergeable, so the problematic merge never happens.

Confirming it on your own system

The answer's author found the cause by testing parameters and fix controls and looking for any that, besides optimizer_features_enable='21.1.0', reproduce the bug. You can use the same levers for diagnosis:

  • optimizer_features_enable='21.1.0' reproduces the wrong result, which points at optimizer behaviour rather than data.
  • _fix_control = '35915968:0' disables the fix and brings the bug back on 23ai Free, where it is otherwise fixed. This is an underscore parameter, so treat it as a test-only switch.
  • dbms_utility.expand_sql_text shows the query after the ANSI rewrites, so you can see the ROW_NUMBER() form that Oracle generated.

Run the contradictory query and the CTE variant side by side. If the first returns a row and the second does not, you are probably looking at this problem.

Workarounds

The answer lists three routes:

  1. Move to a version where Bug 35915968 is fixed. The answer confirms 23ai Free, but the thread does not say which other releases or patches carry the fix.
  2. Replace FETCH FIRST with the legacy ROWNUM <= 1 syntax. This matches what the fix itself does internally.
  3. Use expand_sql_text to see the expanded query, then rewrite it in native syntax instead of relying on bolt-on ANSI features.

The answer also notes the age of the features involved. FETCH FIRST arrived in 12c and OUTER JOIN in 8i, but both are implemented on top of the legacy syntax. The more of that layering a view contains, the more transformation interactions you are depending on.

Where this does not fit

This is one reported bug with one trigger shape: an outer-joined lateral view containing a correlated FETCH FIRST subquery. Nothing in the thread establishes that every use of FETCH FIRST or OUTER APPLY is affected, so do not rewrite a whole codebase on this evidence. The reproduction also uses dual throughout, and the thread does not show how it behaves with real tables.

Treat the wrong-result check as the thing to automate. Where a view uses these constructs, compare an EXISTS AND NOT EXISTS probe against the CTE form after each upgrade or patch. The thread does not say whether a patch exists for 19c, so confirm that with Oracle Support rather than assuming it.

This article draws on Why does Oracle return rows with EXISTS AND NOT EXISTS contradiction and an answer to it, on the Stack Exchange network, licensed under CC BY-SA 4.0.

  • #fetch first
  • #oracle optimizer bug
  • #oracle-database
  • #oracle19c
  • #oracle21c
  • #sql

Comments

Add a comment
Sign in with Google to join the conversation