Skip to content

Skip LOWER() on MySQL columns with a case-insensitive collation - #1812

Merged
scarroll32 merged 1 commit into
mainfrom
mysql-ci-collation-skip-lower
Nov 22, 2026
Merged

scarroll32 merged 1 commit into
mainfrom
mysql-ci-collation-skip-lower

Conversation

@scarroll32

Copy link
Copy Markdown
Member

Fixes #1811.

On MySQL, the case-insensitive predicates (i_cont, i_start, i_end and their variants) wrapped the column in LOWER(). MySQL's default collations already make LIKE case-insensitive, so the LOWER() changed no results. It did stop a prefix LIKE from using the column's index:

EXPLAIN SELECT * FROM t WHERE number LIKE 'r1%';
-> Index range scan on t using number ...

EXPLAIN SELECT * FROM t WHERE LOWER(number) LIKE 'r1%';
-> Table scan on t

This came up in solidusio/solidus#6745. Solidus moved its order-number and email filters from _start to _i_start, which turned them into full scans on MySQL.

Change

  • Dialect#case_insensitive_collation? is true for a MySQL collation whose name ends in _ci.
  • Context#collation_for reads the column's collation from the schema. It returns nil for ransackers and non-string columns.
  • Condition#attr_value_for_attribute leaves the column bare when the dialect has ILIKE or the column's collation ignores case. Otherwise it lowers the column, as before.

_bin / _cs columns, columns with no collation, and ransackers are still lowered, so i_ predicates stay case-insensitive on them. PostgreSQL and SQLite are unchanged.

Specs

  • New unit specs for the dialect and context methods.
  • New collation specs in predicate_spec.rb. They force the MySQL dialect on SQLite and stub the collation.
  • A MySQL-only spec creates a table with one _ci and one utf8mb4_bin column. i_start matches either case on both, and only the _bin column is lowered.
  • The per-dialect case/if blocks in predicate_spec.rb now use a shared i_like_name helper.

Passes on SQLite, MySQL (mysql2 and Trilogy) and PostgreSQL. With the lib change reverted, 18 of the MySQL specs fail.

🤖 Generated with Claude Code

On MySQL the i_ predicates wrapped the column in LOWER(). The default
collations (utf8mb4_0900_ai_ci, utf8mb4_general_ci, ...) already make
LIKE case-insensitive, so the LOWER() changed no results but kept a
prefix LIKE off the column's index: `i_start` did a full table scan
where `start` used an index range scan.

Ransack now reads the column's collation from the schema and leaves
the column bare when the collation name ends in `_ci`. It still lowers
`_bin` and `_cs` columns, columns with no collation, and ransackers.
PostgreSQL (ILIKE) and SQLite are unchanged.

Fixes #1811

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@scarroll32
scarroll32 merged commit a09ec4a into main Nov 22, 2026
10 checks passed
@scarroll32
scarroll32 deleted the mysql-ci-collation-skip-lower branch November 22, 2026 09:50
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.

Case-insensitive predicates lower _ci columns on MySQL, defeating the index

1 participant