Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Incorrect SQL for "count" of dataset with OFFSET #587

Closed
r-stu31 opened this Issue · 1 comment

2 participants

@r-stu31
irb(main):014:0> RssItem.order(:id).limit(nil, 4).sql
=> "SELECT * FROM \"rss_items\" ORDER BY \"id\" OFFSET 4"
irb(main):015:0> RssItem.order(:id).limit(nil, 4).all.count
(0.000870s) SELECT * FROM "rss_items" ORDER BY "id" OFFSET 4
=> 1
irb(main):016:0> RssItem.order(:id).limit(nil, 4).count
(0.000828s) SELECT COUNT(*) AS "count" FROM "rss_items" LIMIT 1 OFFSET 4
=> 0

The "count" should probably internally use "from_self":

irb(main):017:0> RssItem.order(:id).limit(nil, 4).from_self.count
(0.000977s) SELECT COUNT(*) AS "count" FROM (SELECT * FROM "rss_items" ORDER BY "id" OFFSET 4) AS "t1" LIMIT 1
=> 1
@jeremyevans
Owner

Dataset#count already uses from_self internally in many cases (distinct, group, limit, union/intersect/except), but not currently for offset without a limit. I'll fix that.

@jeremyevans jeremyevans closed this issue from a commit
@jeremyevans Make Dataset#count use a subselect when the dataset has an offset wit…
…hout a limit (Fixed #587)

Dataset#count already used a subselect if there was a limit on
the dataset, but it didn't previously consider the offset without
limit case.
1be7fcd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.