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

Fix global index detection #216

Merged
merged 1 commit into from
Jan 6, 2018
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
2 changes: 1 addition & 1 deletion lib/dynamoid/criteria/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def key_present?
# But only do so if projects ALL attributes otherwise we won't
# get back full data
source.global_secondary_indexes.each do |_, gsi|
next unless query_keys.include?(gsi.hash_key.to_s) && gsi.projected_attributes == :all
next unless query.keys.map(&:to_s).include?(gsi.hash_key.to_s) && gsi.projected_attributes == :all
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 276 above we have this:

if query_keys.include?(source.range_key.to_s)

Seems like we may need a more complete solution than trying to remember to do this to_s conversion everywhere.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pboling I considered wrapping query hash in OOP stile to incapsulate some logic and hide type conversion but still don't have simple and flexible solution

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bug is too narrow still. The problem is that we are comparing hash keys with strings, and the hash keys may not themselves be strings. Fixing this one instance but leaving the bug in other places where we do comparisons seems like a bad idea to me, as we'll just lose track of the actual problem. I think we should go with a more encompassing solution. Doesn't need to be OOP necessarily, but, why not have new method like query_keys that gives them as string, e.g. query_keys_as_string, and then call that from everywhere we do key comparisons.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pboling Agree

The simplest solution (and it's already used here in this project) is to convert all user strings to the same type, e.g. symbols. And to have proper tests :).

I will check how we handle and store all user strings like field names, key names, conditions etc to ensue we convert them to symbols.

Actually this bug isn't related to symbol/string comparison. We need to check hash_key: 'value' condition but query_keys contains filtered field names without operators so 'hash_key.contains': 'value' triggers involving index too.

@hash_key = gsi.hash_key
@range_key = gsi.range_key
@index_name = gsi.name
Expand Down
9 changes: 9 additions & 0 deletions spec/dynamoid/criteria/chain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,15 @@ def request_params
expect(chain).to receive(:records_via_query).and_call_original
expect(chain.where(city: 'San Francisco').start(@customer2).all).to contain_exactly(@customer3)
end

it "does not use index if a condition for index hash key is other than 'equal'" do
chain = Dynamoid::Criteria::Chain.new(model)
expect(chain).to receive(:records_via_scan).and_call_original
expect(chain.where('city.begins_with' => 'San').count).to eq(3)
expect(chain.hash_key).to be_nil
expect(chain.range_key).to be_nil
expect(chain.index_name).to be_nil
end
end

it 'supports query on global secondary index with correct start key without table range key' do
Expand Down