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