@@ -51,6 +51,8 @@ pgroup.before_each(function(g)
51
51
helpers .truncate_space_on_cluster (g .cluster , ' customers_name_key_non_uniq_index' )
52
52
helpers .truncate_space_on_cluster (g .cluster , ' customers_secondary_idx_name_key' )
53
53
helpers .truncate_space_on_cluster (g .cluster , ' customers_age_key' )
54
+ helpers .truncate_space_on_cluster (g .cluster , ' customers_name_age_key_different_indexes' )
55
+ helpers .truncate_space_on_cluster (g .cluster , ' customers_name_age_key_three_fields_index' )
54
56
end )
55
57
56
58
pgroup .test_insert_object = function (g )
@@ -279,13 +281,7 @@ pgroup.test_select = function(g)
279
281
t .assert_equals (result .rows [1 ], tuple )
280
282
end
281
283
282
- -- TODO: After enabling support of sharding keys that are not equal to primary
283
- -- keys, we should handle it differently: it is not enough to look just on scan
284
- -- value, we should traverse all conditions. Now missed cases lead to
285
- -- map-reduce. Will be resolved in #213.
286
- pgroup .test_select_wont_lead_map_reduce = function (g )
287
- local space_name = ' customers_name_key_uniq_index'
288
-
284
+ local prepare_data_name_sharding_key = function (g , space_name )
289
285
local conn_s1 = g .cluster :server (' s1-master' ).net_box
290
286
local conn_s2 = g .cluster :server (' s2-master' ).net_box
291
287
@@ -301,12 +297,85 @@ pgroup.test_select_wont_lead_map_reduce = function(g)
301
297
-- bucket_id is 1161, storage is s-2
302
298
local result = conn_s2 .space [space_name ]:insert ({4 , 1161 , ' James Joyce' , 59 })
303
299
t .assert_not_equals (result , nil )
300
+ end
301
+
302
+ local prepare_data_name_age_sharding_key = function (g , space_name )
303
+ local conn_s1 = g .cluster :server (' s1-master' ).net_box
304
+ local conn_s2 = g .cluster :server (' s2-master' ).net_box
305
+
306
+ -- bucket_id is 2310, storage is s-1
307
+ local result = conn_s1 .space [space_name ]:insert ({1 , 2310 , ' Viktor Pelevin' , 58 })
308
+ t .assert_not_equals (result , nil )
309
+ -- bucket_id is 63, storage is s-2
310
+ local result = conn_s2 .space [space_name ]:insert ({2 , 63 , ' Isaac Asimov' , 72 })
311
+ t .assert_not_equals (result , nil )
312
+ -- bucket_id is 2901, storage is s-1
313
+ local result = conn_s1 .space [space_name ]:insert ({3 , 2901 , ' Aleksandr Solzhenitsyn' , 89 })
314
+ t .assert_not_equals (result , nil )
315
+ -- bucket_id is 1365, storage is s-2
316
+ local result = conn_s2 .space [space_name ]:insert ({4 , 1365 , ' James Joyce' , 59 })
317
+ t .assert_not_equals (result , nil )
318
+ end
319
+
320
+ local cases = {
321
+ select_for_indexed_sharding_key = {
322
+ space_name = ' customers_name_key_uniq_index' ,
323
+ prepare_data = prepare_data_name_sharding_key ,
324
+ conditions = {{' ==' , ' name' , ' Viktor Pelevin' }},
325
+ },
326
+ select_for_sharding_key_as_index_part = {
327
+ space_name = ' customers_name_key' ,
328
+ prepare_data = prepare_data_name_sharding_key ,
329
+ conditions = {{' ==' , ' name' , ' Viktor Pelevin' }},
330
+ },
331
+ select_for_sharding_key_as_several_indexes_parts = {
332
+ space_name = ' customers_name_age_key_different_indexes' ,
333
+ prepare_data = prepare_data_name_age_sharding_key ,
334
+ conditions = {{' ==' , ' name' , ' Viktor Pelevin' }, {' ==' , ' age' , 58 }},
335
+ },
336
+ select_by_index_cond_for_sharding_key_as_several_indexes_parts = {
337
+ space_name = ' customers_name_age_key_different_indexes' ,
338
+ prepare_data = prepare_data_name_age_sharding_key ,
339
+ conditions = {{' ==' , ' id' , {1 , ' Viktor Pelevin' }}, {' ==' , ' age' , 58 }},
340
+ },
341
+ select_by_partial_index_cond_for_sharding_key_included = {
342
+ space_name = ' customers_name_age_key_three_fields_index' ,
343
+ prepare_data = prepare_data_name_age_sharding_key ,
344
+ conditions = {{' ==' , ' three_fields' , {58 , ' Viktor Pelevin' , nil }}},
345
+ },
346
+ }
347
+
348
+ for name , case in pairs (cases ) do
349
+ pgroup [(' test_%s_wont_lead_to_map_reduce' ):format (name )] = function (g )
350
+ case .prepare_data (g , case .space_name )
351
+
352
+ local stat_a = storage_stat .collect (g .cluster )
353
+
354
+ local result , err = g .cluster .main_server .net_box :call (' crud.select' , {
355
+ case .space_name , case .conditions
356
+ })
357
+ t .assert_equals (err , nil )
358
+ t .assert_not_equals (result , nil )
359
+ t .assert_equals (# result .rows , 1 )
360
+
361
+ local stat_b = storage_stat .collect (g .cluster )
362
+
363
+ -- Check a number of select() requests made by CRUD on cluster's storages
364
+ -- after calling select() on a router. Make sure only a single storage has
365
+ -- a single select() request. Otherwise we lead to map-reduce.
366
+ local stats = storage_stat .diff (stat_b , stat_a )
367
+ t .assert_equals (storage_stat .total (stats ), 1 , ' Select request was not a map reduce' )
368
+ end
369
+ end
370
+
371
+ pgroup .test_select_for_part_of_sharding_key_will_lead_to_map_reduce = function (g )
372
+ local space_name = ' customers_name_age_key_different_indexes'
373
+ prepare_data_name_age_sharding_key (g , space_name )
304
374
305
375
local stat_a = storage_stat .collect (g .cluster )
306
376
307
- -- Select a tuple with name 'Viktor Pelevin'.
308
377
local result , err = g .cluster .main_server .net_box :call (' crud.select' , {
309
- space_name , {{' ==' , ' name ' , ' Viktor Pelevin ' }}
378
+ space_name , {{' ==' , ' age ' , 58 }},
310
379
})
311
380
t .assert_equals (err , nil )
312
381
t .assert_not_equals (result , nil )
@@ -315,16 +384,10 @@ pgroup.test_select_wont_lead_map_reduce = function(g)
315
384
local stat_b = storage_stat .collect (g .cluster )
316
385
317
386
-- Check a number of select() requests made by CRUD on cluster's storages
318
- -- after calling select() on a router. Make sure only a single storage has
319
- -- a single select() request. Otherwise we lead map-reduce.
320
- t .assert_equals (storage_stat .diff (stat_b , stat_a ), {
321
- [' s-1' ] = {
322
- select_requests = 0 ,
323
- },
324
- [' s-2' ] = {
325
- select_requests = 1 ,
326
- },
327
- })
387
+ -- after calling select() on a router. Make sure it was a map-reduce
388
+ -- since we do not have sharding key values in conditions.
389
+ local stats = storage_stat .diff (stat_b , stat_a )
390
+ t .assert_equals (storage_stat .total (stats ), 2 , ' Select request was a map reduce' )
328
391
end
329
392
330
393
pgroup .test_select_secondary_idx = function (g )
0 commit comments