Skip to content

Commit

Permalink
Merge pull request #50 from ontoportal-lirmm/fix/filters-with-paginat…
Browse files Browse the repository at this point in the history
…ion-empty

Fix: optimize the pagination query to not re-do the filters and order patterns in the include query
  • Loading branch information
syphax-bouazzouni committed Oct 20, 2023
2 parents 4c46c8a + 5247e8d commit 5979402
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions lib/goo/base/where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ def process_query_intl(count=false)

options_load[:ids] = ids if ids
models_by_id = {}
if @page_i && (options_load[:models].length > 0)
options_load.delete(:filters)
options_load.delete(:order_by)
end

if (@page_i && options_load[:models].length > 0) ||
(!@page_i && (@count.nil? || @count > 0))
models_by_id = Goo::SPARQL::Queries.model_load(options_load)
Expand Down
13 changes: 6 additions & 7 deletions lib/goo/sparql/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ def build_select_query(ids, variables, graphs, patterns,
aggregate_projections, aggregate_vars, variables, optional_patterns = get_aggregate_vars(@aggregate, @collection, graphs, @klass, @unions, variables)
query_filter_str, patterns, optional_patterns, filter_variables =
filter_query_strings(@collection, graphs, @klass, optional_patterns, patterns, @query_filters)


@order_by, variables, optional_patterns = init_order_by(@count, @klass, @order_by, optional_patterns, variables,patterns, query_options, graphs)
order_by_str, order_variables = order_by_string
variables, patterns = add_some_type_to_id(patterns, query_options, variables)

query_filter_str, patterns, optional_patterns, filter_variables =
filter_query_strings(@collection, graphs, @klass, optional_patterns, patterns, @query_filters)

variables = [] if @count
variables.delete :some_type

Expand Down Expand Up @@ -106,6 +102,9 @@ def where(patterns)

def paginate
offset = (@page[:page_i] - 1) * @page[:page_size]
# fix for 4store pagination
offset = offset - 1 if offset.positive? && offset.eql?(@page[:page_size])

@query.slice(offset, @page[:page_size])
self
end
Expand Down Expand Up @@ -150,7 +149,7 @@ def select_distinct(variables, aggregate_variables, filter_variables, order_vari
select_vars = variables.dup
reject_aggregations_from_vars(select_vars, aggregate_variables) if aggregate_variables
# Fix for 4store pagination with a filter https://github.com/ontoportal-lirmm/ontologies_api/issues/25
select_vars = (select_vars + filter_variables + order_variables).uniq if @page
# select_vars = (select_vars + filter_variables + order_variables).uniq if @page
@query = @query.select(*select_vars).distinct(true)
self
end
Expand Down Expand Up @@ -407,7 +406,7 @@ def filter_query_strings(collection, graphs, klass,
patterns.concat(filter_patterns)
end
end
#filter_variables << inspected_patterns.values.last
filter_variables << inspected_patterns.values.last
end
[query_filter_str, patterns, optional_patterns, filter_variables]
end
Expand Down
8 changes: 5 additions & 3 deletions test/test_where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,13 @@ def test_embed_two_levels
end

def test_paging_with_filter_order
total_count = Student.where.count
page_1 = Student.where.page(1, total_count - 1).order_by(name: :asc).to_a

f = Goo::Filter.new(:birth_date) > DateTime.parse('1978-01-03')
total_count = Student.where.filter(f).count
page_1 = Student.where.include(:name, :birth_date).page(1, total_count - 1).filter(f).order_by(name: :asc).to_a
refute_empty page_1
assert page_1.next?
page_2 = Student.where.page(page_1.next_page, total_count - 1).order_by(name: :asc).to_a
page_2 = Student.where.include(:name, :birth_date).page(page_1.next_page, total_count - 1).filter(f).order_by(name: :asc).to_a


refute_empty page_2
Expand Down

0 comments on commit 5979402

Please sign in to comment.