Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for partial compound index queries (closes #272) #278

Merged
merged 4 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions lib/no_brainer/criteria/where.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,45 @@ def find_strategy_compound
get_usable_indexes(:kind => :compound, :geo => false, :multi => false).each do |index|
indexed_clauses = index.what.map { |field| clauses[[field]] }
next unless indexed_clauses.all? { |c| c.try(:compatible_with_index?, index) }

return IndexStrategy.new(self, ast, indexed_clauses, index, :get_all, [indexed_clauses.map(&:value)])
end
return nil
end

def find_strategy_compound_partial
clauses = get_candidate_clauses(:eq, :between).map { |c| [c.key_path, c] }.to_h
return nil unless clauses.present?

get_usable_indexes(:kind => :compound, :geo => false, :multi => false).each do |index|
indexed_clauses = index.what.map { |field| clauses[[field]] }
partial_clauses = indexed_clauses.compact
pad = indexed_clauses.length - partial_clauses.length
if partial_clauses.any? && partial_clauses.all? { |c| c.try(:compatible_with_index?, index) }
# can only use partial compound index if:
# * index contains all clause fields
next unless (clauses.values & partial_clauses) == clauses.values
# * all clause fields come first in the indexed clauses (unused indexed fields are at the end)
next unless indexed_clauses.last(pad).all?(&:nil?)
# * all clause fields are :eq, except the last (which may be :between)
next unless partial_clauses[0..-2].all? { |c| c.op == :eq }

# use range query to cover unused index fields
left_bound = partial_clauses.map(&:value)
right_bound = partial_clauses.map(&:value)
if (clause = partial_clauses[-1]).op == :between
left_bound[-1] = clause.value.min
right_bound[-1] = clause.value.max
end
if pad > 0
left_bound.append *Array.new(pad, RethinkDB::RQL.new.minval)
right_bound.append *Array.new(pad, RethinkDB::RQL.new.maxval)
end
return IndexStrategy.new(self, ast, partial_clauses, index, :between, [left_bound, right_bound], :left_bound => :closed, :right_bound => :closed)
end
end
nil
end

def find_strategy_hidden_between
clauses = get_candidate_clauses(:gt, :ge, :lt, :le).group_by(&:key_path)
return nil unless clauses.present?
Expand Down Expand Up @@ -454,7 +487,7 @@ def find_strategy_union
def find_strategy
return nil unless ast.try(:clauses).present? && !criteria.without_index?
case ast.op
when :and then find_strategy_compound || find_strategy_canonical || find_strategy_hidden_between
when :and then find_strategy_compound || find_strategy_compound_partial || find_strategy_canonical || find_strategy_hidden_between
when :or then find_strategy_union
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/integration/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ def migration_plan

let!(:doc1) { SimpleDocument.create(:field1 => 'hello', :field2 => 'world') }
let!(:doc2) { SimpleDocument.create(:field1 => 'ohai', :field2 => 'yay') }
let!(:doc3) { SimpleDocument.create(:field1 => 'hello', :field2 => 'springfield') }

it 'uses the index' do
SimpleDocument.where(:field12 => ['hello', 'world']).where_indexed?.should == true
Expand All @@ -348,6 +349,19 @@ def migration_plan
SimpleDocument.where(:field1 => 'ohai', :field2 => 'yay').count.should == 1
end

it 'uses a partial index when possible' do
SimpleDocument.where(:field1 => 'hello').used_index.should == :field12
SimpleDocument.where(:field1 => 'hello').count.should == 2
end

it 'uses a partial index with range' do
SimpleDocument.where(:field1 => 'hello', :field2 => ('t'..'z')).used_index.should == :field12
SimpleDocument.where(:field1 => 'hello', :field2 => ('t'..'z')).count.should == 1

SimpleDocument.where(:field1 => 'hello', :field2 => ('s'..'x')).used_index.should == :field12
SimpleDocument.where(:field1 => 'hello', :field2 => ('s'..'x')).count.should == 2
end

it 'does not allow to use a field with the same name as an index' do
SimpleDocument.index :index_name, [:field1, :field2]
expect { SimpleDocument.field :index_name }
Expand Down