Skip to content

Review and plan solution for GitHub issue#2756

Merged
robfrank merged 2 commits intomainfrom
claude/analyze-issue-2695-011CUpnuipmXLV9EcHvVuk3F
Nov 5, 2025
Merged

Review and plan solution for GitHub issue#2756
robfrank merged 2 commits intomainfrom
claude/analyze-issue-2695-011CUpnuipmXLV9EcHvVuk3F

Conversation

@robfrank
Copy link
Collaborator

@robfrank robfrank commented Nov 5, 2025

…st match

Problem:
When executing a DELETE statement with multiple OR conditions (e.g., (A AND B) OR (C AND D) OR (E AND F)), only records matching the FIRST OR branch were deleted. The remaining OR branches were silently ignored.

Root Cause:
In SelectExecutionPlanner.handleTypeAsTargetWithIndex(), when processing flattened WHERE clauses (one AndBlock per OR branch), the code attempted to optimize each branch using indexes via findBestIndexFor(). However, it used .filter(Objects::nonNull) to remove branches that couldn't be optimized, causing those branches to be silently dropped from execution.

This meant:

  • If branch 1 could use an index → executed
  • If branches 2-6 couldn't use indexes → silently dropped
  • Result: only 1 record deleted instead of 6

Fix:
Changed the logic to check if ANY OR branch cannot be optimized with an index. If so, fall back to a full table scan with WHERE clause filtering instead of silently dropping branches. This ensures ALL OR conditions are evaluated.

The fix changes:
.filter(Objects::nonNull) // REMOVES null entries
.collect(Collectors.toList());
to:
.collect(Collectors.toList());
if (indexSearchDescriptors.contains(null)) // CHECK for nulls
return null; // Fall back to full scan

Testing:
Added two comprehensive test cases:

  1. testDeleteWithMultipleOrConditions - tests without indexes
  2. testDeleteWithMultipleOrConditionsAndIndex - tests with indexes

Both verify that all 6 records matching different OR branches are deleted, not just the first one.

What does this PR do?

A brief description of the change being made with this pull request.

Motivation

What inspired you to submit this pull request?

Related issues

A list of issues either fixed, containing architectural discussions, otherwise relevant
for this Pull Request.

Additional Notes

Anything else we should know when reviewing?

Checklist

  • I have run the build using mvn clean package command
  • My unit tests cover both failure and success scenarios

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @robfrank, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical bug in DELETE statement execution where queries containing multiple OR conditions could lead to incomplete data deletion. Previously, the query planner would silently drop OR branches that couldn't be optimized by an index, resulting in only a subset of matching records being deleted. The fix ensures that if any OR condition cannot leverage an index, the system correctly falls back to a full table scan, guaranteeing that all specified conditions are evaluated and all intended records are deleted. Comprehensive test cases have been added to verify the correctness of this behavior.

Highlights

  • Problem Solved: Addressed a bug where DELETE statements with multiple OR conditions would only process the first optimizable branch, silently ignoring others, leading to incomplete data deletion.
  • Root Cause Identified: The SelectExecutionPlanner's handleTypeAsTargetWithIndex method used filter(Objects::nonNull) which inadvertently removed OR branches that couldn't be optimized with an index, causing them to be silently dropped from execution.
  • Solution Implemented: Modified the query planner to remove the filter(Objects::nonNull) and instead explicitly check if any OR branch resulted in a null (unoptimizable) IndexSearchDescriptor. If so, it now correctly falls back to a full table scan to ensure all OR conditions are evaluated.
  • Enhanced Test Coverage: Two new comprehensive test cases (testDeleteWithMultipleOrConditions and testDeleteWithMultipleOrConditionsAndIndex) were added to DeleteStatementTest.java to validate the fix under various conditions, including with and without indexes, ensuring all matching records are deleted.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This is an excellent pull request that addresses a critical bug where parts of a DELETE statement with OR conditions were silently ignored. The root cause analysis is spot-on, and the fix is correct and clean. I particularly appreciate the detailed problem description and the inclusion of two comprehensive test cases that cover scenarios both with and without indexes, ensuring the fix is robust. I have one minor suggestion for a code cleanup.

@mergify
Copy link
Contributor

mergify bot commented Nov 5, 2025

🧪 CI Insights

Here's what we observed from your CI run for 61fb84a.

🟢 All jobs passed!

But CI Insights is watching 👀

@robfrank robfrank force-pushed the claude/analyze-issue-2695-011CUpnuipmXLV9EcHvVuk3F branch from 623f366 to b5fe173 Compare November 5, 2025 13:47
…st match

Problem:
When executing a DELETE statement with multiple OR conditions (e.g.,
(A AND B) OR (C AND D) OR (E AND F)), only records matching the FIRST
OR branch were deleted. The remaining OR branches were silently ignored.

Root Cause:
In SelectExecutionPlanner.handleTypeAsTargetWithIndex(), when processing
flattened WHERE clauses (one AndBlock per OR branch), the code attempted
to optimize each branch using indexes via findBestIndexFor(). However,
it used .filter(Objects::nonNull) to remove branches that couldn't be
optimized, causing those branches to be silently dropped from execution.

This meant:
- If branch 1 could use an index → executed
- If branches 2-6 couldn't use indexes → silently dropped
- Result: only 1 record deleted instead of 6

Fix:
Changed the logic to check if ANY OR branch cannot be optimized with
an index. If so, fall back to a full table scan with WHERE clause
filtering instead of silently dropping branches. This ensures ALL
OR conditions are evaluated.

The fix changes:
  .filter(Objects::nonNull)  // REMOVES null entries
  .collect(Collectors.toList());
to:
  .collect(Collectors.toList());
  if (indexSearchDescriptors.contains(null))  // CHECK for nulls
    return null;  // Fall back to full scan

Testing:
Added two comprehensive test cases:
1. testDeleteWithMultipleOrConditions - tests without indexes
2. testDeleteWithMultipleOrConditionsAndIndex - tests with indexes

Both verify that all 6 records matching different OR branches are
deleted, not just the first one.
@robfrank robfrank force-pushed the claude/analyze-issue-2695-011CUpnuipmXLV9EcHvVuk3F branch from b5fe173 to 6ddc42d Compare November 5, 2025 13:53
@codacy-production
Copy link

codacy-production bot commented Nov 5, 2025

Coverage summary from Codacy

See diff coverage on Codacy

Coverage variation Diff coverage
-1.04% 100.00%
Coverage variation details
Coverable lines Covered lines Coverage
Common ancestor commit (72d73f2) 73167 46503 63.56%
Head commit (61fb84a) 73166 (-1) 45739 (-764) 62.51% (-1.04%)

Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch: <coverage of head commit> - <coverage of common ancestor commit>

Diff coverage details
Coverable lines Covered lines Diff coverage
Pull request (#2756) 2 2 100.00%

Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified: <covered lines added or modified>/<coverable lines added or modified> * 100%

See your quality gate settings    Change summary preferences

@robfrank robfrank added this to the 25.10.1 milestone Nov 5, 2025
@robfrank robfrank merged commit 2053e74 into main Nov 5, 2025
20 of 22 checks passed
@robfrank robfrank deleted the claude/analyze-issue-2695-011CUpnuipmXLV9EcHvVuk3F branch November 5, 2025 16:16
robfrank added a commit that referenced this pull request Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL: Only records that match first part of big OR expression are deleted

2 participants