Skip to content

ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing - #107

Merged
AdamMusa merged 2 commits into
mainfrom
fix/record-open-ended-ranges
Nov 20, 2026
Merged

AdamMusa merged 2 commits into
mainfrom
fix/record-open-ended-ranges

Conversation

@AdamMusa

@AdamMusa AdamMusa commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Fixes #104

Problem

where with an open-ended range matched no rows. User.where(age: 18..), User.where(age: ..17), User.where(age: ...18) and where(created_at: since..) all returned an empty result, with no error. SQL.predicate always emitted BETWEEN ? AND ? (or >= ? AND < ? for ...) and bound range.begin and range.end as they were. For an open range, one of those is nil. The open side was bound as NULL, so the condition was never true. find_by, exists?, count, update_all and delete_all use the same fragment, so they matched nothing too.

What changed

packages/ruflet_record/lib/ruflet_record/sql.rb, the Range branch of SQL.predicate. An open end now leaves that side of the range unbounded, as in Active Record:

Range Before After
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 rows 1 = 1, no binds, every row
20..30, 20...30 unchanged unchanged, byte for byte (SQL and binds)

The code uses only Range#begin, #end and #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 where with an open-ended range, which matched no rows before, so no app can rely on the old result.

  1. where(col: a..), where(col: ..b) and where(col: ...b) now match rows. They compile to col >= a, col <= b and col < b, as in Active Record (checked against 8.1.3.1). Rows where col is NULL are still excluded. Before, these conditions matched no rows. This also applies to find_by, exists?, count, update_all and delete_all on such a relation, and to find_or_create_by and find_or_initialize_by, which call find_by: find_or_create_by(age: 18..) with a matching row now returns that row, where before it raised NoMethodError (undefined method to_i for a Range). With no matching row, find_or_initialize_by(age: ..17) raises that same NoMethodError on main and on this branch.
  2. where(col: nil..nil) now matches every row, including rows where col is NULL. It compiles to 1 = 1, as Active Record does. Before, it matched no rows. Reviewers should note one case: update_all or delete_all on a relation filtered only by from..to, with both ends nil (for example, two empty optional filters), now affects every row instead of none. Active Record behaves the same way.

These stay the same:

  • Bounded ranges. The SQL and binds are identical.
  • where.not with 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, and where.not(age: nil..nil) still gives none. The new tests pin these results.
  • Creating through a scope. where(age: 18..).create! still does not take the bound as an attribute.

How verified

  • New regression test. packages/ruflet_record/test/open_range_test.rb has 10 tests. They cover:
    • endless, beginless and fully open ranges, on rows aged 9, 17, 18 and 40, plus a row whose age is NULL
    • the exact SQL and binds for each open range
    • bounded ranges compiling byte for byte as before
    • bind order when an open range is mixed with other conditions
    • where.not results
    • open ranges on a datetime column
    • count, exists?, find_by, update_all and delete_all
    • creating through an open-range scope
  • Results of the new test:
    • With the original sql.rb planted back: 7 failures. All of them are the open-range cases, which returned [] and a count of 0.
    • With the fix: 10 runs, 43 assertions, 0 failures. Seeds 1, 7, 42, 999 and 31337 all pass.
    • Three tests pass on both versions, because they guard behavior that must not change: bounded ranges, where.not, and create-through-scope.
  • Full ruflet_record suite. Each test file ran in its own process (with -w, no warnings), plus rake test: 34 runs, 125 assertions, 0 failures (CRuby 4.0.5).
  • Merged with open PRs 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. Both edit 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 test gives 95 runs, 594 assertions, 0 failures.
  • Embedded mruby VM (host_vm 3.4, with this sql.rb loaded over the preloaded copy):
    • With the fix, 18.., 18..., ..17, ..18, ...18 and nil..nil return the expected rows and compile to the SQL in the table above. The bounded ranges and where.not results are unchanged.
    • With the original sql.rb, the six open-range cases return [].
  • test/mruby_smoke.rb gains an endless-range check and a beginless-range check. It passes with this sql.rb and 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.
  • H1 goldens: cd packages/ruflet && ruby test/compat/compat.rb --jobs 4 checked 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, and ruby -Ilib -Itest test/compat_goldens_test.rb gave 298 runs, 0 failures.
  • Final head 6a3eda9 adds only a merge of main 43f7582, which touches no ruflet_record file. On it, every ruflet_record test file passes, each in its own process with -w, on CRuby 4.0.5 and on CI Ruby 3.4.5.
  • Not touched: engine, template, CLI, server and rails.

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
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.

ruflet_record: where with an endless or beginless range (age: 18.., created_at: since..) matches no rows

1 participant