Repository navigation
ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing - #107
Merged
Merged
Conversation
where(age: 18..), where(age: ..17), where(age: ...18) and where(created_at: since..) bound the open end as NULL, so the condition was never true and the query matched no rows. An endless range now compiles to col >= ?, a beginless one to col <= ? (col < ? for ...), and nil..nil to 1 = 1, as in Active Record. Bounded ranges keep the same SQL and binds. Fixes #104
This was referenced Nov 20, 2026
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 #104
Problem
wherewith an open-ended range matched no rows.User.where(age: 18..),User.where(age: ..17),User.where(age: ...18)andwhere(created_at: since..)all returned an empty result, with no error.SQL.predicatealways emittedBETWEEN ? AND ?(or>= ? AND < ?for...) and boundrange.beginandrange.endas they were. For an open range, one of those isnil. The open side was bound asNULL, so the condition was never true.find_by,exists?,count,update_allanddelete_alluse the same fragment, so they matched nothing too.What changed
packages/ruflet_record/lib/ruflet_record/sql.rb, theRangebranch ofSQL.predicate. An open end now leaves that side of the range unbounded, as in Active Record:18..,18..."age" BETWEEN ? AND ?(or>= ? AND < ?), binds[18, nil], no rows"age" >= ?, binds[18]..17"age" BETWEEN ? AND ?, binds[nil, 17], no rows"age" <= ?, binds[17]...18("age" >= ? AND "age" < ?), binds[nil, 18], no rows"age" < ?, binds[18]nil..nil,nil...nil"age" BETWEEN ? AND ?, binds[nil, nil], no rows1 = 1, no binds, every row20..30,20...30The code uses only
Range#begin,#endand#exclude_end?, so it runs unchanged in the embedded mruby VM.The package README gains two open-range examples and one sentence that says what they mean.
No app code changes.
Behavior changes
Both are bug fixes. They change results only for
wherewith an open-ended range, which matched no rows before, so no app can rely on the old result.where(col: a..),where(col: ..b)andwhere(col: ...b)now match rows. They compile tocol >= a,col <= bandcol < b, as in Active Record (checked against 8.1.3.1). Rows wherecolisNULLare still excluded. Before, these conditions matched no rows. This also applies tofind_by,exists?,count,update_allanddelete_allon such a relation, and tofind_or_create_byandfind_or_initialize_by, which callfind_by:find_or_create_by(age: 18..)with a matching row now returns that row, where before it raisedNoMethodError(undefined methodto_ifor a Range). With no matching row,find_or_initialize_by(age: ..17)raises that sameNoMethodErroron main and on this branch.where(col: nil..nil)now matches every row, including rows wherecolisNULL. It compiles to1 = 1, as Active Record does. Before, it matched no rows. Reviewers should note one case:update_allordelete_allon a relation filtered only byfrom..to, with both endsnil(for example, two empty optional filters), now affects every row instead of none. Active Record behaves the same way.These stay the same:
where.notwith an open range. It already returned the right rows through SQL's three-valued logic.where.not(age: 18..)still gives the rows under 18, andwhere.not(age: nil..nil)still gives none. The new tests pin these results.where(age: 18..).create!still does not take the bound as an attribute.How verified
packages/ruflet_record/test/open_range_test.rbhas 10 tests. They cover:NULLwhere.notresultsdatetimecolumncount,exists?,find_by,update_allanddelete_allsql.rbplanted back: 7 failures. All of them are the open-range cases, which returned[]and a count of0.where.not, and create-through-scope.ruflet_recordsuite. Each test file ran in its own process (with-w, no warnings), plusrake test: 34 runs, 125 assertions, 0 failures (CRuby 4.0.5).ruflet_record. On a merge of the final head 6a3eda9 with ruflet_record: fix six bugs the coverage tests exposed; test the untested public API #90 (20e4e22) and ruflet_record: end the transaction on return, break and throw #99 (71367b2), the merge is clean and all 14 test files pass, each in its own process, including the new files from ruflet_record: fix six bugs the coverage tests exposed; test the untested public API #90 and ruflet_record: end the transaction on return, break and throw #99.rake testgives 95 runs, 594 assertions, 0 failures.host_vm3.4, with thissql.rbloaded over the preloaded copy):18..,18...,..17,..18,...18andnil..nilreturn the expected rows and compile to the SQL in the table above. The bounded ranges andwhere.notresults are unchanged.sql.rb, the six open-range cases return[].test/mruby_smoke.rbgains an endless-range check and a beginless-range check. It passes with thissql.rband fails on the original. It runs against the copy preloaded in the VM, so it passes there only once the VM is rebuilt from this source.cd packages/ruflet && ruby test/compat/compat.rb --jobs 4checked 234 goldens on 618b37f and exited 0. On the final head 6a3eda9, which is 618b37f merged with main 43f7582, it checked 290 goldens and exited 0, andruby -Ilib -Itest test/compat_goldens_test.rbgave 298 runs, 0 failures.ruflet_recordfile. On it, everyruflet_recordtest file passes, each in its own process with-w, on CRuby 4.0.5 and on CI Ruby 3.4.5.