@@ -143,7 +143,8 @@ struct Mem_compare_queue_key {
143
143
144
144
static ha_rows read_all_rows (
145
145
THD *thd, Sort_param *param, const Prealloced_array<TABLE *, 4 > &tables,
146
- Filesort_info *fs_info, IO_CACHE *chunk_file, IO_CACHE *tempfile,
146
+ table_map tables_to_get_rowid_for, Filesort_info *fs_info,
147
+ IO_CACHE *chunk_file, IO_CACHE *tempfile,
147
148
Bounded_queue<uchar *, uchar *, Sort_param, Mem_compare_queue_key> *pq,
148
149
RowIterator *source_iterator, ha_rows *found_rows, size_t *longest_key,
149
150
size_t *longest_addon);
@@ -355,6 +356,10 @@ static void trace_filesort_information(Opt_trace_context *trace,
355
356
@param thd Current thread
356
357
@param filesort How to sort the table
357
358
@param source_iterator Where to read the rows to be sorted from.
359
+ @param tables_to_get_rowid_for
360
+ Which tables we are responsible for getting row IDs
361
+ for. Tables in this set that are not also in
362
+ "tables" are ignored.
358
363
@param num_rows_estimate How many rows source_iterator is expected
359
364
to produce. Only used for whether we intend
360
365
to use the priority queue optimization or not;
@@ -374,8 +379,9 @@ static void trace_filesort_information(Opt_trace_context *trace,
374
379
*/
375
380
376
381
bool filesort (THD *thd, Filesort *filesort, RowIterator *source_iterator,
377
- ha_rows num_rows_estimate, Filesort_info *fs_info,
378
- Sort_result *sort_result, ha_rows *found_rows) {
382
+ table_map tables_to_get_rowid_for, ha_rows num_rows_estimate,
383
+ Filesort_info *fs_info, Sort_result *sort_result,
384
+ ha_rows *found_rows) {
379
385
int error;
380
386
ulong memory_available = thd->variables .sortbuff_size ;
381
387
ha_rows num_rows_found = HA_POS_ERROR;
@@ -411,6 +417,15 @@ bool filesort(THD *thd, Filesort *filesort, RowIterator *source_iterator,
411
417
my_b_clear (&chunk_file);
412
418
error = 1 ;
413
419
420
+ if (!param->using_addon_fields ()) {
421
+ for (TABLE *table : filesort->tables ) {
422
+ if (table->pos_in_table_list == nullptr ||
423
+ (tables_to_get_rowid_for & table->pos_in_table_list ->map ())) {
424
+ table->prepare_for_position ();
425
+ }
426
+ }
427
+ }
428
+
414
429
// Make sure the source iterator is initialized before init_for_filesort(),
415
430
// since table->file (and in particular, ref_length) may not be initialized
416
431
// before that.
@@ -512,9 +527,9 @@ bool filesort(THD *thd, Filesort *filesort, RowIterator *source_iterator,
512
527
{
513
528
Opt_trace_array ota (trace, " filesort_execution" );
514
529
num_rows_found = read_all_rows (
515
- thd, param, filesort->tables , fs_info, &chunk_file, &tempfile ,
516
- param->using_pq ? &pq : nullptr , source_iterator, found_rows ,
517
- &longest_key, &longest_addons);
530
+ thd, param, filesort->tables , tables_to_get_rowid_for, fs_info ,
531
+ &chunk_file, &tempfile, param->using_pq ? &pq : nullptr ,
532
+ source_iterator, found_rows, &longest_key, &longest_addons);
518
533
if (num_rows_found == HA_POS_ERROR) goto err;
519
534
}
520
535
@@ -875,6 +890,10 @@ static bool alloc_and_make_sortkey(Sort_param *param, Filesort_info *fs_info,
875
890
@param thd Thread handle
876
891
@param param Sorting parameter
877
892
@param tables List of all tables being sorted.
893
+ @param tables_to_get_rowid_for
894
+ Which tables we are responsible for getting row IDs
895
+ for. Tables in this set that are not also in "tables"
896
+ are ignored.
878
897
@param fs_info Struct containing sort buffer etc.
879
898
@param chunk_file File to write Merge_chunks describing sorted segments
880
899
in tempfile.
@@ -930,7 +949,8 @@ static bool alloc_and_make_sortkey(Sort_param *param, Filesort_info *fs_info,
930
949
931
950
static ha_rows read_all_rows (
932
951
THD *thd, Sort_param *param, const Prealloced_array<TABLE *, 4 > &tables,
933
- Filesort_info *fs_info, IO_CACHE *chunk_file, IO_CACHE *tempfile,
952
+ table_map tables_to_get_rowid_for, Filesort_info *fs_info,
953
+ IO_CACHE *chunk_file, IO_CACHE *tempfile,
934
954
Bounded_queue<uchar *, uchar *, Sort_param, Mem_compare_queue_key> *pq,
935
955
RowIterator *source_iterator, ha_rows *found_rows, size_t *longest_key,
936
956
size_t *longest_addons) {
@@ -985,7 +1005,10 @@ static ha_rows read_all_rows(
985
1005
// Note where we are, for the case where we are not using addon fields.
986
1006
if (!param->using_addon_fields ()) {
987
1007
for (TABLE *table : tables) {
988
- table->file ->position (table->record [0 ]);
1008
+ if (table->pos_in_table_list == nullptr ||
1009
+ (table->pos_in_table_list ->map () & tables_to_get_rowid_for)) {
1010
+ table->file ->position (table->record [0 ]);
1011
+ }
989
1012
}
990
1013
}
991
1014
DBUG_EXECUTE_IF (" debug_filesort" , {
0 commit comments