Skip to content

Commit

Permalink
Skip window function for distinct queries
Browse files Browse the repository at this point in the history
* We cannot use the count window function with a distinct select

Co-authored-by: Andrew Paine <andrew.paine@sap.com>
  • Loading branch information
iaftab-alam and andy-paine committed Nov 23, 2021
1 parent 9e12333 commit 15941b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/cloud_controller/paging/sequel_paginator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_page(sequel_dataset, pagination_options)
sequel_dataset = sequel_dataset.order(sequel_order)
sequel_dataset = sequel_dataset.order_append(Sequel.asc(Sequel.qualify(table_name, :guid))) if sequel_dataset.model.columns.include?(:guid)

records, count = if sequel_dataset.supports_window_functions?
records, count = if sequel_dataset.supports_window_functions? && !sequel_dataset.opts[:distinct]
paginate_with_window_function(sequel_dataset, per_page, page, table_name)
else
paginate_with_extension(sequel_dataset, per_page, page)
Expand Down
20 changes: 15 additions & 5 deletions spec/unit/lib/cloud_controller/paging/sequel_paginator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module VCAP::CloudController
end

it 'sorts by the order_by option in the corresponding order_direction' do
options = { page: 1, per_page: 2, order_by: 'name', order_direction: 'asc' }
options = { page: page, per_page: 2, order_by: 'name', order_direction: 'asc' }
app_model2.update(name: 'a')
app_model1.update(name: 'b')
pagination_options = PaginationOptions.new(options)
Expand All @@ -57,7 +57,7 @@ module VCAP::CloudController

it 'works with a multi table result set' do
PackageModel.make(app: app_model1)
options = { page: 1, per_page: per_page }
options = { page: page, per_page: per_page }
pagination_options = PaginationOptions.new(options)
new_dataset = dataset.join(PackageModel.table_name, "#{PackageModel.table_name}__app_guid".to_sym => "#{AppModel.table_name}__guid".to_sym)

Expand All @@ -78,7 +78,7 @@ module VCAP::CloudController
end

it 'orders by GUID as a secondary field when available' do
options = { page: 1, per_page: 2, order_by: 'created_at', order_direction: 'asc' }
options = { page: page, per_page: 2, order_by: 'created_at', order_direction: 'asc' }
app_model1.update(guid: '1', created_at: '2019-12-25T13:00:00Z')
app_model2.update(guid: '2', created_at: '2019-12-25T13:00:00Z')

Expand All @@ -89,7 +89,7 @@ module VCAP::CloudController
end

it 'does not order by GUID when the table has no GUID' do
options = { page: 1, per_page: per_page }
options = { page: page, per_page: per_page }
pagination_options = PaginationOptions.new(options)
request_count_dataset = RequestCount.dataset
RequestCount.make.save
Expand All @@ -102,14 +102,24 @@ module VCAP::CloudController

it 'only calls DB once if DB supports window functions' do
skip 'DB does not support window functions' unless dataset.supports_window_functions?
options = { page: 1, per_page: per_page }
options = { page: page, per_page: per_page }
pagination_options = PaginationOptions.new(options)

expect(dataset.db).to receive(:execute).once.and_call_original

paginated_result = paginator.get_page(dataset, pagination_options)
expect(paginated_result.total).to be > 1
end

it 'returns correct total results for distinct result' do
options = { page: page, per_page: per_page, order_by: :key_name }
pagination_options = PaginationOptions.new(options)
2.times { SpaceLabelModel.create(key_name: 'testLabel') }
dataset = SpaceLabelModel.dataset.distinct(:key_name)
paginated_result = paginator.get_page(dataset, pagination_options)

expect(paginated_result.total).to eq(1)
end
end
end
end

0 comments on commit 15941b0

Please sign in to comment.