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

Closed
r-stu31 opened this Issue Nov 16, 2012 · 1 comment

Projects

None yet

2 participants

@r-stu31
Contributor
r-stu31 commented Nov 16, 2012
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 added a commit that closed this issue Nov 17, 2012
@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