Relation#each_batch doesn't respect limited datasets

Open
#329 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
ruby, sql
Domain
database

Research direction

Start at the Relation#each_batch implementation and reproduce the issue with a limited relation such as users.limit(10) and a batch size of 10. Trace how the relation is used while batching, then ensure the existing limit remains effective; done means only the limited rows are processed.

Written by the indexing model from the issue text.

Description

bug

Describe the bug

When using Relation#each_batch, if you pass in a relation that already has a limit applied, Relation#each_batch will not respect it and process the batches as if the limit did not exist.

To Reproduce

users = ROM.env.relations[:users]
1000.times { create_user }
top_100 = users.limit(10)

i = 0
top_100.each_batch(size: 10) do |batch|
   i += 1
  raise 'This is unexpected...' if i > 10
end

Expected behavior

Relation#each_batch should respect the passed in limit, ideally by wrapping the input dataset in a subquery such that it will not be manipulated.

Your environment

  • Affects my production application: YES
  • Ruby version: 2.6.2
  • OS: Linux
Dominant language
Ruby
Stars
220
Forks
97
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rom-rb/rom-sql

All issues in rom-rb/rom-sql

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.