Skip to content

Native select: OR with a non-cursor-buildable operator (NEQ/LIKE/ILIKE) on one side silently drops the whole other branch #6577

Closed

Description

@lvca

Context: spotted during code review on #6571 (fix for #6565, native select index candidate-cap bug) - pre-existing, unrelated to that PR's changes, confirmed independently with a scratch reproduction before filing.

Repro (com.arcadedb.query.select, embedded DatabaseFactory, no server):

final var t = database.getSchema().createDocumentType("T");
t.createProperty("a", Type.STRING);
t.createProperty("n", Type.INTEGER);
t.createTypeIndex(Schema.INDEX_TYPE.LSM_TREE, false, "a");
t.createTypeIndex(Schema.INDEX_TYPE.LSM_TREE, false, "n");
database.transaction(() -> {
  for (int i = 0; i < 1000; i++)
    database.newDocument("T").set("a", "x", "n", i).save();
});

long count = database.select().fromType("T").where()
    .property("a").neq().value("nonexistent")
    .or().property("n").eq().value(-1)
    .count();
// every one of the 1000 rows has a = "x" != "nonexistent", so this should be 1000.
// actual: 0

Root cause

SelectExecutor.isTheNodeFullyIndexed() (used by filterWithIndexesFinalNode()'s "under an OR, both sides must be indexed" gate) only excludes is_null/is_not_null from "this leaf is indexed" - every other operator on an indexed property, including neq/like/ilike, is treated as indexed as long as the property has an index:

if (node.operator == SelectOperator.is_null || node.operator == SelectOperator.is_not_null)
  return false;
if (!(node.right instanceof SelectPropertyValue)) {
  final TypeIndex propertyIndex = select.fromType.getPolymorphicIndexByProperties(...);
  ...
  return propertyIndex != null;
}

But the cursor-building switch a few lines down in filterWithIndexesFinalNode() only actually knows how to build a cursor for eq/in_op/between/gt/ge/lt/le - everything else (including neq/like/ilike) falls through to a bare return;, contributing no cursor for that leaf:

if (node.operator == SelectOperator.eq) cursor = ...
else if (node.operator == SelectOperator.in_op) { ... }
else if (node.operator == SelectOperator.between) { ... }
else if (node.operator == SelectOperator.gt) ...
else if (node.operator == SelectOperator.ge) ...
else if (node.operator == SelectOperator.lt) ...
else if (node.operator == SelectOperator.le) ...
else
  return;   // <-- neq/like/ilike land here: no cursor, no error

So for a != 'nonexistent' OR n = -1: the "both sides indexed" gate passes (both properties have indexes, per the overly-permissive check above), the neq leaf silently contributes zero cursors, and the eq leaf on the other side contributes its own cursor alone. The final candidate stream is only what the n = -1 cursor returns - the a != 'nonexistent' branch is dropped entirely, not "evaluated less efficiently." This isn't a capping/performance issue (unrelated to #6565/#6571) - it's a missing-rows correctness bug: any record that satisfies the neq/like/ilike branch but not the other indexed branch is silently excluded from the result.

Suggested fix

isTheNodeFullyIndexed()'s leaf case should only report "indexed" for operators the cursor-building switch actually handles (eq, in_op, between, gt, ge, lt, le) - i.e. mirror that switch's operator set, or extract it into one shared list/method both use, rather than letting the two independently drift (this is exactly the trap this issue fell into). A leaf under neq/like/ilike should be treated the same as is_null/is_not_null: not indexed, so an OR involving it falls back to a full scan (correct, if less optimal) instead of a partial union (wrong).

Scope note

Not blocking #6571 - that PR is specifically about the candidate cap (how many index results are consumed), and this bug is about which cursors get built in the first place; it predates #6571 and reproduces identically on main before that PR's changes. Filing separately per the reviewer's suggestion during that PR's review.

Activity

  1. added this to the 26.9.1 milestone on Aug 22, 2026
  2. self-assigned this
    on Aug 24, 2026
  3. added
    in progress
    on Aug 24, 2026
  4. added a commit that references this issue on Aug 24, 2026
  5. removed
    in progress
    on Aug 24, 2026
  6. added
    engine
    on Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugengine

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions