Repository navigation
Skip LOWER() on MySQL columns with a case-insensitive collation - #1812
Merged
Merged
Conversation
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>
6 of 10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1811.
On MySQL, the case-insensitive predicates (
i_cont,i_start,i_endand their variants) wrapped the column inLOWER(). MySQL's default collations already makeLIKEcase-insensitive, so theLOWER()changed no results. It did stop a prefixLIKEfrom using the column's index:This came up in solidusio/solidus#6745. Solidus moved its order-number and email filters from
_startto_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_forreads the column's collation from the schema. It returns nil for ransackers and non-string columns.Condition#attr_value_for_attributeleaves the column bare when the dialect has ILIKE or the column's collation ignores case. Otherwise it lowers the column, as before._bin/_cscolumns, columns with no collation, and ransackers are still lowered, soi_predicates stay case-insensitive on them. PostgreSQL and SQLite are unchanged.Specs
predicate_spec.rb. They force the MySQL dialect on SQLite and stub the collation._ciand oneutf8mb4_bincolumn.i_startmatches either case on both, and only the_bincolumn is lowered.case/ifblocks inpredicate_spec.rbnow use a sharedi_like_namehelper.Passes on SQLite, MySQL (mysql2 and Trilogy) and PostgreSQL. With the lib change reverted, 18 of the MySQL specs fail.
🤖 Generated with Claude Code