Skip to content

BETWEEN on a case-insensitive (COLLATE CI) index doesn't use the index when wrapped in LOWER() #6033

Closed

Description

@lvca

Summary

Follow-up from #5966 (fixed in #6018). #6018 taught the planner to recognize a bare field BETWEEN a AND b as index-range-eligible, including on COLLATE CI indexes:

CREATE INDEX ON Product (name COLLATE CI) NOTUNIQUE;
SELECT * FROM Product WHERE name BETWEEN 'a' AND 'c'   -- uses the index (fixed by #6018)

However, the equivalent query with the common LOWER()-wrapped pattern still falls back to a full bucket scan:

SELECT * FROM Product WHERE LOWER(name) BETWEEN 'a' AND 'c'   -- does NOT use the index

Root cause

BinaryCondition.isIndexAware() has two branches: one for a direct column comparison, and a second CI-index optimization that matches field.toLowerCase() = 'value'-style expressions via isFieldWithLowerCaseMethod() (see engine/src/main/java/com/arcadedb/query/sql/parser/BinaryCondition.java). BetweenCondition.isIndexAware() (added in #6018) only mirrors the first branch - it doesn't have an equivalent check for the LOWER()-wrapped case, so it falls through to the BooleanExpression default (false) and the planner picks a full scan instead.

Suggested fix

Extend BetweenCondition.isIndexAware() with a second branch analogous to BinaryCondition's, reusing isFieldWithLowerCaseMethod() (or a shared helper extracted from it) against first, gated the same way (info.isCaseInsensitive() and both bounds isEarlyCalculated).

Environment

Discovered during code review of PR #6018 (merged, commit d86debf3), which fixed #5966. Not implemented there since it's outside that issue's exact repro (a plain indexed column) - filed separately per the reviewer's own suggestion ("worth a follow-up issue if that pattern shows up in practice").

Activity

  1. justinblethrow-cloud commented on Aug 10, 2026

    @justinblethrow-cloud
    Contributor

    I can take this next, after #6010. I’ll add focused planner coverage for LOWER(field) BETWEEN on a COLLATE CI index, mirror the existing case-insensitive index-aware behavior, and submit it as a separate scoped change.

  2. justinblethrow-cloud commented on Aug 10, 2026

    @justinblethrow-cloud
    Contributor

    Reproduction found one syntax distinction worth recording: the exact SQL spelling LOWER(name) currently raises Unknown function name 'LOWER'; ArcadeDB's supported equivalent is name.toLowerCase(). The supported form reproduces the planner gap exactly (full bucket scan before the fix), and now uses the COLLATE CI range index with correct results.

    I am keeping this change scoped to the planner behavior described in the root cause rather than making an otherwise-unknown SQL function work only when this particular index plan is available. A standard SQL LOWER() alias, if desired, should be implemented consistently as a separate parser/function change.

  3. added 3 commits that reference this issue on Aug 10, 2026
  4. self-assigned this
    on Aug 10, 2026
  5. added theissue type on Aug 10, 2026
  6. added this to the 26.9.1 milestone on Aug 10, 2026
  7. 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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions