~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_base.cc

  • Committer: Brian Aker
  • Date: 2008-07-10 19:37:55 UTC
  • mfrom: (51.1.67 remove-dbug)
  • Revision ID: brian@tangent.org-20080710193755-f5g761uieqa3wxmt
Merge.

Show diffs side-by-side

added added

removed removed

Lines of Context:
62
62
 
63
63
void table_cache_free(void)
64
64
{
65
 
  DBUG_ENTER("table_cache_free");
66
65
  if (table_def_inited)
67
66
  {
68
67
    close_cached_tables(NULL, NULL, false, false, false);
69
68
    if (!open_cache.records)                    // Safety first
70
69
      hash_free(&open_cache);
71
70
  }
72
 
  DBUG_VOID_RETURN;
 
71
  return;
73
72
}
74
73
 
75
74
uint cached_open_tables(void)
77
76
  return open_cache.records;
78
77
}
79
78
 
80
 
 
81
 
#ifdef EXTRA_DEBUG
82
 
static void check_unused(void)
83
 
{
84
 
  uint count= 0, open_files= 0, idx= 0;
85
 
  TABLE *cur_link,*start_link;
86
 
 
87
 
  if ((start_link=cur_link=unused_tables))
88
 
  {
89
 
    do
90
 
    {
91
 
      if (cur_link != cur_link->next->prev || cur_link != cur_link->prev->next)
92
 
      {
93
 
        DBUG_PRINT("error",("Unused_links aren't linked properly")); /* purecov: inspected */
94
 
        return; /* purecov: inspected */
95
 
      }
96
 
    } while (count++ < open_cache.records &&
97
 
             (cur_link=cur_link->next) != start_link);
98
 
    if (cur_link != start_link)
99
 
    {
100
 
      DBUG_PRINT("error",("Unused_links aren't connected")); /* purecov: inspected */
101
 
    }
102
 
  }
103
 
  for (idx=0 ; idx < open_cache.records ; idx++)
104
 
  {
105
 
    TABLE *entry=(TABLE*) hash_element(&open_cache,idx);
106
 
    if (!entry->in_use)
107
 
      count--;
108
 
    if (entry->file)
109
 
      open_files++;
110
 
  }
111
 
  if (count != 0)
112
 
  {
113
 
    DBUG_PRINT("error",("Unused_links doesn't match open_cache: diff: %d", /* purecov: inspected */
114
 
                        count)); /* purecov: inspected */
115
 
  }
116
 
 
117
 
#ifdef NOT_SAFE_FOR_REPAIR
118
 
  /*
119
 
    check that open cache and table definition cache has same number of
120
 
    aktive tables
121
 
  */
122
 
  count= 0;
123
 
  for (idx=0 ; idx < table_def_cache.records ; idx++)
124
 
  {
125
 
    TABLE_SHARE *entry= (TABLE_SHARE*) hash_element(&table_def_cache,idx);
126
 
    count+= entry->ref_count;
127
 
  }
128
 
  if (count != open_files)
129
 
  {
130
 
    DBUG_PRINT("error", ("table_def ref_count: %u  open_cache: %u",
131
 
                         count, open_files));
132
 
    DBUG_ASSERT(count == open_files);
133
 
  }
134
 
#endif
135
 
}
136
 
#else
137
 
#define check_unused()
138
 
#endif
139
 
 
140
 
 
141
79
/*
142
80
  Create a table cache key
143
81
 
194
132
 
195
133
static void table_def_free_entry(TABLE_SHARE *share)
196
134
{
197
 
  DBUG_ENTER("table_def_free_entry");
198
135
  if (share->prev)
199
136
  {
200
137
    /* remove from old_unused_share list */
204
141
    pthread_mutex_unlock(&LOCK_table_share);
205
142
  }
206
143
  free_table_share(share);
207
 
  DBUG_VOID_RETURN;
 
144
  return;
208
145
}
209
146
 
210
147
 
223
160
 
224
161
void table_def_free(void)
225
162
{
226
 
  DBUG_ENTER("table_def_free");
227
163
  if (table_def_inited)
228
164
  {
229
165
    table_def_inited= 0;
230
166
    pthread_mutex_destroy(&LOCK_table_share);
231
167
    hash_free(&table_def_cache);
232
168
  }
233
 
  DBUG_VOID_RETURN;
 
169
  return;
234
170
}
235
171
 
236
172
 
269
205
                             uint key_length, uint db_flags, int *error)
270
206
{
271
207
  TABLE_SHARE *share;
272
 
  DBUG_ENTER("get_table_share");
273
208
 
274
209
  *error= 0;
275
210
 
280
215
 
281
216
  if (!(share= alloc_table_share(table_list, key, key_length)))
282
217
  {
283
 
    DBUG_RETURN(0);
 
218
    return(0);
284
219
  }
285
220
 
286
221
  /*
307
242
  if (my_hash_insert(&table_def_cache, (uchar*) share))
308
243
  {
309
244
    free_table_share(share);
310
 
    DBUG_RETURN(0);                             // return error
 
245
    return(0);                          // return error
311
246
  }
312
247
  if (open_table_def(thd, share, db_flags))
313
248
  {
314
249
    *error= share->error;
315
250
    (void) hash_delete(&table_def_cache, (uchar*) share);
316
 
    DBUG_RETURN(0);
 
251
    return(0);
317
252
  }
318
253
  share->ref_count++;                           // Mark in use
319
 
  DBUG_PRINT("exit", ("share: 0x%lx  ref_count: %u",
320
 
                      (ulong) share, share->ref_count));
321
254
  (void) pthread_mutex_unlock(&share->mutex);
322
 
  DBUG_RETURN(share);
 
255
  return(share);
323
256
 
324
257
found:
325
258
  /* 
334
267
    /* Table definition contained an error */
335
268
    open_table_error(share, share->error, share->open_errno, share->errarg);
336
269
    (void) pthread_mutex_unlock(&share->mutex);
337
 
    DBUG_RETURN(0);
 
270
    return(0);
338
271
  }
339
272
 
340
273
  if (!share->ref_count++ && share->prev)
343
276
      Share was not used before and it was in the old_unused_share list
344
277
      Unlink share from this list
345
278
    */
346
 
    DBUG_PRINT("info", ("Unlinking from not used list"));
347
279
    pthread_mutex_lock(&LOCK_table_share);
348
280
    *share->prev= share->next;
349
281
    share->next->prev= share->prev;
361
293
    VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
362
294
  }
363
295
 
364
 
  DBUG_PRINT("exit", ("share: 0x%lx  ref_count: %u",
365
 
                      (ulong) share, share->ref_count));
366
 
  DBUG_RETURN(share);
 
296
  return(share);
367
297
}
368
298
 
369
299
 
380
310
{
381
311
  TABLE_SHARE *share;
382
312
  int tmp;
383
 
  DBUG_ENTER("get_table_share_with_create");
384
313
 
385
314
  share= get_table_share(thd, table_list, key, key_length, db_flags, error);
386
315
  /*
404
333
  */
405
334
  if (share || (thd->is_error() && (thd->main_da.sql_errno() != ER_NO_SUCH_TABLE)))
406
335
 
407
 
    DBUG_RETURN(share);
 
336
    return(share);
408
337
 
409
338
  /* Table didn't exist. Check if some engine can provide it */
410
339
  if ((tmp= ha_create_table_from_engine(thd, table_list->db,
423
352
      thd->clear_error();
424
353
      my_error(ER_VIEW_INVALID, MYF(0), "", "");
425
354
    }
426
 
    DBUG_RETURN(0);
 
355
    return(0);
427
356
  }
428
357
  if (tmp)
429
358
  {
430
359
    /* Give right error message */
431
360
    thd->clear_error();
432
 
    DBUG_PRINT("error", ("Discovery of %s/%s failed", table_list->db,
433
 
                         table_list->table_name));
434
361
    my_printf_error(ER_UNKNOWN_ERROR,
435
362
                    "Failed to open '%-.64s', error while "
436
363
                    "unpacking from engine",
437
364
                    MYF(0), table_list->table_name);
438
 
    DBUG_RETURN(0);
 
365
    return(0);
439
366
  }
440
367
  /* Table existed in engine. Let's open it */
441
368
  mysql_reset_errors(thd, 1);                   // Clear warnings
442
369
  thd->clear_error();                           // Clear error message
443
 
  DBUG_RETURN(get_table_share(thd, table_list, key, key_length,
 
370
  return(get_table_share(thd, table_list, key, key_length,
444
371
                              db_flags, error));
445
372
}
446
373
 
470
397
                         enum release_type type __attribute__((__unused__)))
471
398
{
472
399
  bool to_be_deleted= 0;
473
 
  DBUG_ENTER("release_table_share");
474
 
  DBUG_PRINT("enter",
475
 
             ("share: 0x%lx  table: %s.%s  ref_count: %u  version: %lu",
476
 
              (ulong) share, share->db.str, share->table_name.str,
477
 
              share->ref_count, share->version));
478
400
 
479
401
  safe_mutex_assert_owner(&LOCK_open);
480
402
 
486
408
    else
487
409
    {
488
410
      /* Link share last in used_table_share list */
489
 
      DBUG_PRINT("info",("moving share to unused list"));
490
 
 
491
 
      DBUG_ASSERT(share->next == 0);
 
411
      assert(share->next == 0);
492
412
      pthread_mutex_lock(&LOCK_table_share);
493
413
      share->prev= end_of_unused_share.prev;
494
414
      *end_of_unused_share.prev= share;
502
422
 
503
423
  if (to_be_deleted)
504
424
  {
505
 
    DBUG_PRINT("info", ("Deleting share"));
506
425
    hash_delete(&table_def_cache, (uchar*) share);
507
 
    DBUG_VOID_RETURN;
 
426
    return;
508
427
  }
509
428
  pthread_mutex_unlock(&share->mutex);
510
 
  DBUG_VOID_RETURN;
 
429
  return;
511
430
}
512
431
 
513
432
 
561
480
  TABLE_SHARE *share, *old_share= table->s;
562
481
  char *key_buff;
563
482
  MEM_ROOT *mem_root= &table->mem_root;
564
 
  DBUG_ENTER("close_handle_and_leave_table_as_lock");
565
483
 
566
 
  DBUG_ASSERT(table->db_stat);
 
484
  assert(table->db_stat);
567
485
 
568
486
  /*
569
487
    Make a local copy of the table share and free the current one.
587
505
  table->s= share;
588
506
  table->file->change_table_ptr(table, table->s);
589
507
 
590
 
  DBUG_VOID_RETURN;
 
508
  return;
591
509
}
592
510
 
593
511
 
616
534
  int result = 0;
617
535
  OPEN_TABLE_LIST **start_list, *open_list;
618
536
  TABLE_LIST table_list;
619
 
  DBUG_ENTER("list_open_tables");
620
537
 
621
538
  VOID(pthread_mutex_lock(&LOCK_open));
622
539
  bzero((char*) &table_list,sizeof(table_list));
669
586
    *start_list=0;
670
587
  }
671
588
  VOID(pthread_mutex_unlock(&LOCK_open));
672
 
  DBUG_RETURN(open_list);
 
589
  return(open_list);
673
590
}
674
591
 
675
592
/*****************************************************************************
679
596
 
680
597
void intern_close_table(TABLE *table)
681
598
{                                               // Free all structures
682
 
  DBUG_ENTER("intern_close_table");
683
 
  DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx",
684
 
                        table->s ? table->s->db.str : "?",
685
 
                        table->s ? table->s->table_name.str : "?",
686
 
                        (long) table));
687
 
 
688
599
  free_io_cache(table);
689
600
  if (table->file)                              // Not true if name lock
690
601
    VOID(closefrm(table, 1));                   // close file
691
 
  DBUG_VOID_RETURN;
 
602
  return;
692
603
}
693
604
 
694
605
/*
704
615
 
705
616
static void free_cache_entry(TABLE *table)
706
617
{
707
 
  DBUG_ENTER("free_cache_entry");
708
 
 
709
618
  intern_close_table(table);
710
619
  if (!table->in_use)
711
620
  {
717
626
      if (table == unused_tables)
718
627
        unused_tables=0;
719
628
    }
720
 
    check_unused();                             // consisty check
721
629
  }
722
630
  my_free((uchar*) table,MYF(0));
723
 
  DBUG_VOID_RETURN;
 
631
  return;
724
632
}
725
633
 
726
634
/* Free resources allocated by filesort() and read_record() */
727
635
 
728
636
void free_io_cache(TABLE *table)
729
637
{
730
 
  DBUG_ENTER("free_io_cache");
731
638
  if (table->sort.io_cache)
732
639
  {
733
640
    close_cached_file(table->sort.io_cache);
734
641
    my_free((uchar*) table->sort.io_cache,MYF(0));
735
642
    table->sort.io_cache=0;
736
643
  }
737
 
  DBUG_VOID_RETURN;
 
644
  return;
738
645
}
739
646
 
740
647
 
757
664
                         bool wait_for_refresh, bool wait_for_placeholders)
758
665
{
759
666
  bool result=0;
760
 
  DBUG_ENTER("close_cached_tables");
761
 
  DBUG_ASSERT(thd || (!wait_for_refresh && !tables));
 
667
  assert(thd || (!wait_for_refresh && !tables));
762
668
 
763
669
  if (!have_lock)
764
670
    VOID(pthread_mutex_lock(&LOCK_open));
780
686
      pthread_mutex_lock(&oldest_unused_share->mutex);
781
687
      VOID(hash_delete(&table_def_cache, (uchar*) oldest_unused_share));
782
688
    }
783
 
    DBUG_PRINT("tcache", ("incremented global refresh_version to: %lu",
784
 
                          refresh_version));
785
689
    if (wait_for_refresh)
786
690
    {
787
691
      /*
857
761
 
858
762
    bool found=1;
859
763
    /* Wait until all threads has closed all the tables we had locked */
860
 
    DBUG_PRINT("info",
861
 
               ("Waiting for other threads to close their open tables"));
862
764
    while (found && ! thd->killed)
863
765
    {
864
766
      found=0;
886
788
            (table->open_placeholder && wait_for_placeholders)))
887
789
        {
888
790
          found=1;
889
 
          DBUG_PRINT("signal", ("Waiting for COND_refresh"));
890
791
          pthread_cond_wait(&COND_refresh,&LOCK_open);
891
792
          break;
892
793
        }
921
822
    thd_proc_info(thd, 0);
922
823
    pthread_mutex_unlock(&thd->mysys_var->mutex);
923
824
  }
924
 
  DBUG_RETURN(result);
 
825
  return(result);
925
826
}
926
827
 
927
828
 
936
837
  uint idx;
937
838
  TABLE_LIST tmp, *tables= NULL;
938
839
  bool result= false;
939
 
  DBUG_ENTER("close_cached_connections");
940
 
  DBUG_ASSERT(thd);
 
840
  assert(thd);
941
841
 
942
842
  bzero(&tmp, sizeof(TABLE_LIST));
943
843
 
986
886
    pthread_mutex_unlock(&thd->mysys_var->mutex);
987
887
  }
988
888
 
989
 
  DBUG_RETURN(result);
 
889
  return(result);
990
890
}
991
891
 
992
892
 
1065
965
 
1066
966
  VOID(pthread_mutex_lock(&LOCK_open));
1067
967
 
1068
 
  DBUG_PRINT("info", ("thd->open_tables: 0x%lx", (long) thd->open_tables));
1069
 
 
1070
968
  while (thd->open_tables)
1071
969
    found_old_table|= close_thread_table(thd, &thd->open_tables);
1072
970
  thd->some_tables_deleted= 0;
1074
972
  /* Free tables to hold down open files */
1075
973
  while (open_cache.records > table_cache_size && unused_tables)
1076
974
    VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
1077
 
  check_unused();
1078
975
  if (found_old_table)
1079
976
  {
1080
977
    /* Tell threads waiting for refresh that something has happened */
1106
1003
void close_thread_tables(THD *thd)
1107
1004
{
1108
1005
  TABLE *table;
1109
 
  DBUG_ENTER("close_thread_tables");
1110
1006
 
1111
1007
  /*
1112
1008
    We are assuming here that thd->derived_tables contains ONLY derived
1163
1059
    /*
1164
1060
      We are under simple LOCK TABLES so should not do anything else.
1165
1061
    */
1166
 
    DBUG_VOID_RETURN;
 
1062
    return;
1167
1063
  }
1168
1064
 
1169
1065
  if (thd->lock)
1191
1087
  if (thd->open_tables)
1192
1088
    close_open_tables(thd);
1193
1089
 
1194
 
  DBUG_VOID_RETURN;
 
1090
  return;
1195
1091
}
1196
1092
 
1197
1093
 
1201
1097
{
1202
1098
  bool found_old_table= 0;
1203
1099
  TABLE *table= *table_ptr;
1204
 
  DBUG_ENTER("close_thread_table");
1205
 
  DBUG_ASSERT(table->key_read == 0);
1206
 
  DBUG_ASSERT(!table->file || table->file->inited == handler::NONE);
1207
 
  DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx", table->s->db.str,
1208
 
                        table->s->table_name.str, (long) table));
 
1100
 
 
1101
  assert(table->key_read == 0);
 
1102
  assert(!table->file || table->file->inited == handler::NONE);
1209
1103
 
1210
1104
  *table_ptr=table->next;
1211
1105
 
1221
1115
      Open placeholders have TABLE::db_stat set to 0, so they should be
1222
1116
      handled by the first alternative.
1223
1117
    */
1224
 
    DBUG_ASSERT(!table->open_placeholder);
 
1118
    assert(!table->open_placeholder);
1225
1119
 
1226
1120
    /* Free memory and reset for next loop */
1227
1121
    table->file->ha_reset();
1236
1130
    else
1237
1131
      unused_tables=table->next=table->prev=table;
1238
1132
  }
1239
 
  DBUG_RETURN(found_old_table);
 
1133
  return(found_old_table);
1240
1134
}
1241
1135
 
1242
1136
 
1467
1361
{
1468
1362
  TABLE_LIST *res;
1469
1363
  const char *d_name, *t_name, *t_alias;
1470
 
  DBUG_ENTER("unique_table");
1471
 
  DBUG_PRINT("enter", ("table alias: %s", table->alias));
1472
1364
 
1473
1365
  /*
1474
1366
    If this function called for query which update table (INSERT/UPDATE/...)
1484
1376
  {
1485
1377
    /* temporary table is always unique */
1486
1378
    if (table->table && table->table->s->tmp_table != NO_TMP_TABLE)
1487
 
      DBUG_RETURN(0);
 
1379
      return(0);
1488
1380
    table= table->find_underlying_table(table->table);
1489
1381
    /*
1490
1382
      as far as we have table->table we have to find real TABLE_LIST of
1491
1383
      it in underlying tables
1492
1384
    */
1493
 
    DBUG_ASSERT(table);
 
1385
    assert(table);
1494
1386
  }
1495
1387
  d_name= table->db;
1496
1388
  t_name= table->table_name;
1497
1389
  t_alias= table->alias;
1498
1390
 
1499
 
  DBUG_PRINT("info", ("real table: %s.%s", d_name, t_name));
1500
1391
  for (;;)
1501
1392
  {
1502
1393
    if (((! (res= find_table_in_global_list(table_list, d_name, t_name))) &&
1513
1404
      (exclude_from_table_unique_test) or prelocking placeholder.
1514
1405
    */
1515
1406
    table_list= res->next_global;
1516
 
    DBUG_PRINT("info",
1517
 
               ("found same copy of table or table which we should skip"));
1518
1407
  }
1519
 
  DBUG_RETURN(res);
 
1408
  return(res);
1520
1409
}
1521
1410
 
1522
1411
 
1557
1446
  char  key[MAX_DBKEY_LENGTH];
1558
1447
  uint  key_length;
1559
1448
  TABLE *table;
1560
 
  DBUG_ENTER("find_temporary_table");
1561
 
  DBUG_PRINT("enter", ("table: '%s'.'%s'",
1562
 
                       table_list->db, table_list->table_name));
1563
1449
 
1564
1450
  key_length= create_table_def_key(thd, key, table_list, 1);
1565
1451
  for (table=thd->temporary_tables ; table ; table= table->next)
1566
1452
  {
1567
1453
    if (table->s->table_cache_key.length == key_length &&
1568
1454
        !memcmp(table->s->table_cache_key.str, key, key_length))
1569
 
    {
1570
 
      DBUG_PRINT("info",
1571
 
                 ("Found table. server_id: %u  pseudo_thread_id: %lu",
1572
 
                  (uint) thd->server_id,
1573
 
                  (ulong) thd->variables.pseudo_thread_id));
1574
 
      DBUG_RETURN(table);
1575
 
    }
 
1455
      return(table);
1576
1456
  }
1577
 
  DBUG_RETURN(0);                               // Not a temporary table
 
1457
  return(0);                               // Not a temporary table
1578
1458
}
1579
1459
 
1580
1460
 
1607
1487
int drop_temporary_table(THD *thd, TABLE_LIST *table_list)
1608
1488
{
1609
1489
  TABLE *table;
1610
 
  DBUG_ENTER("drop_temporary_table");
1611
 
  DBUG_PRINT("tmptable", ("closing table: '%s'.'%s'",
1612
 
                          table_list->db, table_list->table_name));
1613
1490
 
1614
1491
  if (!(table= find_temporary_table(thd, table_list)))
1615
 
    DBUG_RETURN(1);
 
1492
    return(1);
1616
1493
 
1617
1494
  /* Table might be in use by some outer statement. */
1618
1495
  if (table->query_id && table->query_id != thd->query_id)
1619
1496
  {
1620
1497
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
1621
 
    DBUG_RETURN(-1);
 
1498
    return(-1);
1622
1499
  }
1623
1500
 
1624
1501
  /*
1627
1504
  */
1628
1505
  mysql_lock_remove(thd, thd->locked_tables, table, false);
1629
1506
  close_temporary_table(thd, table, 1, 1);
1630
 
  DBUG_RETURN(0);
 
1507
  return(0);
1631
1508
}
1632
1509
 
1633
1510
/*
1637
1514
void close_temporary_table(THD *thd, TABLE *table,
1638
1515
                           bool free_share, bool delete_table)
1639
1516
{
1640
 
  DBUG_ENTER("close_temporary_table");
1641
 
  DBUG_PRINT("tmptable", ("closing table: '%s'.'%s' 0x%lx  alias: '%s'",
1642
 
                          table->s->db.str, table->s->table_name.str,
1643
 
                          (long) table, table->alias));
1644
 
 
1645
1517
  if (table->prev)
1646
1518
  {
1647
1519
    table->prev->next= table->next;
1651
1523
  else
1652
1524
  {
1653
1525
    /* removing the item from the list */
1654
 
    DBUG_ASSERT(table == thd->temporary_tables);
 
1526
    assert(table == thd->temporary_tables);
1655
1527
    /*
1656
1528
      slave must reset its temporary list pointer to zero to exclude
1657
1529
      passing non-zero value to end_slave via rli->save_temporary_tables
1664
1536
  if (thd->slave_thread)
1665
1537
  {
1666
1538
    /* natural invariant of temporary_tables */
1667
 
    DBUG_ASSERT(slave_open_temp_tables || !thd->temporary_tables);
 
1539
    assert(slave_open_temp_tables || !thd->temporary_tables);
1668
1540
    slave_open_temp_tables--;
1669
1541
  }
1670
1542
  close_temporary(table, free_share, delete_table);
1671
 
  DBUG_VOID_RETURN;
 
1543
  return;
1672
1544
}
1673
1545
 
1674
1546
 
1683
1555
void close_temporary(TABLE *table, bool free_share, bool delete_table)
1684
1556
{
1685
1557
  handlerton *table_type= table->s->db_type();
1686
 
  DBUG_ENTER("close_temporary");
1687
 
  DBUG_PRINT("tmptable", ("closing table: '%s'.'%s'",
1688
 
                          table->s->db.str, table->s->table_name.str));
1689
1558
 
1690
1559
  free_io_cache(table);
1691
1560
  closefrm(table, 0);
1701
1570
    free_table_share(table->s);
1702
1571
    my_free((char*) table,MYF(0));
1703
1572
  }
1704
 
  DBUG_VOID_RETURN;
 
1573
  return;
1705
1574
}
1706
1575
 
1707
1576
 
1720
1589
  uint key_length;
1721
1590
  TABLE_SHARE *share= table->s;
1722
1591
  TABLE_LIST table_list;
1723
 
  DBUG_ENTER("rename_temporary_table");
1724
1592
 
1725
1593
  if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1726
 
    DBUG_RETURN(1);                             /* purecov: inspected */
 
1594
    return(1);                          /* purecov: inspected */
1727
1595
 
1728
1596
  table_list.db= (char*) db;
1729
1597
  table_list.table_name= (char*) table_name;
1730
1598
  key_length= create_table_def_key(thd, key, &table_list, 1);
1731
1599
  share->set_table_cache_key(key, key_length);
1732
 
  DBUG_RETURN(0);
 
1600
  return(0);
1733
1601
}
1734
1602
 
1735
1603
 
1746
1614
    unused_tables->prev->next=table;
1747
1615
    unused_tables->prev=table;
1748
1616
    unused_tables=table;
1749
 
    check_unused();
1750
1617
  }
1751
1618
}
1752
1619
 
1771
1638
  char key[MAX_DBKEY_LENGTH];
1772
1639
  uint key_length= find->s->table_cache_key.length;
1773
1640
  TABLE *list, **prev;
1774
 
  DBUG_ENTER("unlink_open_table");
1775
1641
 
1776
1642
  safe_mutex_assert_owner(&LOCK_open);
1777
1643
 
1807
1673
 
1808
1674
  // Notify any 'refresh' threads
1809
1675
  broadcast_refresh();
1810
 
  DBUG_VOID_RETURN;
 
1676
  return;
1811
1677
}
1812
1678
 
1813
1679
 
1869
1735
  thd->mysys_var->current_cond= cond;
1870
1736
  proc_info=thd->proc_info;
1871
1737
  thd_proc_info(thd, "Waiting for table");
1872
 
  DBUG_ENTER("wait_for_condition");
1873
1738
  if (!thd->killed)
1874
1739
    (void) pthread_cond_wait(cond, mutex);
1875
1740
 
1890
1755
  thd->mysys_var->current_cond= 0;
1891
1756
  thd_proc_info(thd, proc_info);
1892
1757
  pthread_mutex_unlock(&thd->mysys_var->mutex);
1893
 
  DBUG_VOID_RETURN;
 
1758
  return;
1894
1759
}
1895
1760
 
1896
1761
 
1906
1771
 
1907
1772
bool name_lock_locked_table(THD *thd, TABLE_LIST *tables)
1908
1773
{
1909
 
  DBUG_ENTER("name_lock_locked_table");
1910
 
 
1911
1774
  /* Under LOCK TABLES we must only accept write locked tables. */
1912
1775
  tables->table= find_locked_table(thd, tables->db, tables->table_name);
1913
1776
 
1922
1785
      other statement will open this table.
1923
1786
    */
1924
1787
    wait_while_table_is_used(thd, tables->table, HA_EXTRA_FORCE_REOPEN);
1925
 
    DBUG_RETURN(false);
 
1788
    return(false);
1926
1789
  }
1927
1790
 
1928
 
  DBUG_RETURN(true);
 
1791
  return(true);
1929
1792
}
1930
1793
 
1931
1794
 
1958
1821
  TABLE_SHARE *share;
1959
1822
  char *table_name= table_list->table_name;
1960
1823
  TABLE orig_table;
1961
 
  DBUG_ENTER("reopen_name_locked_table");
1962
1824
 
1963
1825
  safe_mutex_assert_owner(&LOCK_open);
1964
1826
 
1965
1827
  if (thd->killed || !table)
1966
 
    DBUG_RETURN(true);
 
1828
    return(true);
1967
1829
 
1968
1830
  orig_table= *table;
1969
1831
 
1979
1841
      object to its original state.
1980
1842
    */
1981
1843
    *table= orig_table;
1982
 
    DBUG_RETURN(true);
 
1844
    return(true);
1983
1845
  }
1984
1846
 
1985
1847
  share= table->s;
1993
1855
  */
1994
1856
  share->version=0;
1995
1857
  table->in_use = thd;
1996
 
  check_unused();
1997
1858
 
1998
1859
  if (link_in)
1999
1860
  {
2014
1875
  table->const_table=0;
2015
1876
  table->null_row= table->maybe_null= table->force_index= 0;
2016
1877
  table->status=STATUS_NO_RECORD;
2017
 
  DBUG_RETURN(false);
 
1878
  return(false);
2018
1879
}
2019
1880
 
2020
1881
 
2037
1898
  TABLE *table;
2038
1899
  TABLE_SHARE *share;
2039
1900
  char *key_buff;
2040
 
  DBUG_ENTER("table_cache_insert_placeholder");
2041
1901
 
2042
1902
  safe_mutex_assert_owner(&LOCK_open);
2043
1903
 
2051
1911
                       &share, sizeof(*share),
2052
1912
                       &key_buff, key_length,
2053
1913
                       NULL))
2054
 
    DBUG_RETURN(NULL);
 
1914
    return(NULL);
2055
1915
 
2056
1916
  table->s= share;
2057
1917
  share->set_table_cache_key(key_buff, key, key_length);
2062
1922
  if (my_hash_insert(&open_cache, (uchar*)table))
2063
1923
  {
2064
1924
    my_free((uchar*) table, MYF(0));
2065
 
    DBUG_RETURN(NULL);
 
1925
    return(NULL);
2066
1926
  }
2067
1927
 
2068
 
  DBUG_RETURN(table);
 
1928
  return(table);
2069
1929
}
2070
1930
 
2071
1931
 
2096
1956
{
2097
1957
  char key[MAX_DBKEY_LENGTH];
2098
1958
  uint key_length;
2099
 
  DBUG_ENTER("lock_table_name_if_not_cached");
2100
1959
 
2101
1960
  key_length= (uint)(strmov(strmov(key, db) + 1, table_name) - key) + 1;
2102
1961
  VOID(pthread_mutex_lock(&LOCK_open));
2104
1963
  if (hash_search(&open_cache, (uchar *)key, key_length))
2105
1964
  {
2106
1965
    VOID(pthread_mutex_unlock(&LOCK_open));
2107
 
    DBUG_PRINT("info", ("Table is cached, name-lock is not obtained"));
2108
1966
    *table= 0;
2109
 
    DBUG_RETURN(false);
 
1967
    return(false);
2110
1968
  }
2111
1969
  if (!(*table= table_cache_insert_placeholder(thd, key, key_length)))
2112
1970
  {
2113
1971
    VOID(pthread_mutex_unlock(&LOCK_open));
2114
 
    DBUG_RETURN(true);
 
1972
    return(true);
2115
1973
  }
2116
1974
  (*table)->open_placeholder= 1;
2117
1975
  (*table)->next= thd->open_tables;
2118
1976
  thd->open_tables= *table;
2119
1977
  VOID(pthread_mutex_unlock(&LOCK_open));
2120
 
  DBUG_RETURN(false);
 
1978
  return(false);
2121
1979
}
2122
1980
 
2123
1981
 
2146
2004
{
2147
2005
  char path[FN_REFLEN];
2148
2006
  int rc;
2149
 
  DBUG_ENTER("check_if_table_exists");
2150
2007
 
2151
2008
  safe_mutex_assert_owner(&LOCK_open);
2152
2009
 
2153
2010
  *exists= true;
2154
2011
 
2155
2012
  if (get_cached_table_share(table->db, table->table_name))
2156
 
    DBUG_RETURN(false);
 
2013
    return(false);
2157
2014
 
2158
2015
  build_table_filename(path, sizeof(path) - 1, table->db, table->table_name,
2159
2016
                       reg_ext, 0);
2160
2017
 
2161
2018
  if (!access(path, F_OK))
2162
 
    DBUG_RETURN(false);
 
2019
    return(false);
2163
2020
 
2164
2021
  /* .FRM file doesn't exist. Check if some engine can provide it. */
2165
2022
 
2169
2026
  {
2170
2027
    /* Table does not exists in engines as well. */
2171
2028
    *exists= false;
2172
 
    DBUG_RETURN(false);
 
2029
    return(false);
2173
2030
  }
2174
2031
  else if (!rc)
2175
2032
  {
2176
2033
    /* Table exists in some engine and .FRM for it was created. */
2177
 
    DBUG_RETURN(false);
 
2034
    return(false);
2178
2035
  }
2179
2036
  else /* (rc > 0) */
2180
2037
  {
2181
2038
    my_printf_error(ER_UNKNOWN_ERROR, "Failed to open '%-.64s', error while "
2182
2039
                    "unpacking from engine", MYF(0), table->table_name);
2183
 
    DBUG_RETURN(true);
 
2040
    return(true);
2184
2041
  }
2185
2042
}
2186
2043
 
2226
2083
  unsigned int key_length;
2227
2084
  char *alias= table_list->alias;
2228
2085
  HASH_SEARCH_STATE state;
2229
 
  DBUG_ENTER("open_table");
2230
2086
 
2231
2087
  /* Parsing of partitioning information from .frm needs thd->lex set up. */
2232
 
  DBUG_ASSERT(thd->lex->is_lex_started);
 
2088
  assert(thd->lex->is_lex_started);
2233
2089
 
2234
2090
  /* find a unused table in the open table cache */
2235
2091
  if (refresh)
2237
2093
 
2238
2094
  /* an open table operation needs a lot of the stack space */
2239
2095
  if (check_stack_overrun(thd, STACK_MIN_SIZE_FOR_OPEN, (uchar *)&alias))
2240
 
    DBUG_RETURN(0);
 
2096
    return(0);
2241
2097
 
2242
2098
  if (thd->killed)
2243
 
    DBUG_RETURN(0);
 
2099
    return(0);
2244
2100
 
2245
2101
  key_length= (create_table_def_key(thd, key, table_list, 1) -
2246
2102
               TMP_TABLE_KEY_EXTRA);
2268
2124
        */
2269
2125
        if (table->query_id)
2270
2126
        {
2271
 
          DBUG_PRINT("error",
2272
 
                     ("query_id: %lu  server_id: %u  pseudo_thread_id: %lu",
2273
 
                      (ulong) table->query_id, (uint) thd->server_id,
2274
 
                      (ulong) thd->variables.pseudo_thread_id));
2275
2127
          my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->alias);
2276
 
          DBUG_RETURN(0);
 
2128
          return(0);
2277
2129
        }
2278
2130
        table->query_id= thd->query_id;
2279
2131
        thd->thread_specific_used= true;
2280
 
        DBUG_PRINT("info",("Using temporary table"));
2281
2132
        goto reset;
2282
2133
      }
2283
2134
    }
2286
2137
  if (flags & MYSQL_OPEN_TEMPORARY_ONLY)
2287
2138
  {
2288
2139
    my_error(ER_NO_SUCH_TABLE, MYF(0), table_list->db, table_list->table_name);
2289
 
    DBUG_RETURN(0);
 
2140
    return(0);
2290
2141
  }
2291
2142
 
2292
2143
  /*
2317
2168
          */
2318
2169
          my_error(ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG, MYF(0),
2319
2170
                   table->s->table_name.str);
2320
 
          DBUG_RETURN(0);
 
2171
          return(0);
2321
2172
        }
2322
2173
        /*
2323
2174
          When looking for a usable TABLE, ignore MERGE children, as they
2362
2213
    {
2363
2214
      table= best_table;
2364
2215
      table->query_id= thd->query_id;
2365
 
      DBUG_PRINT("info",("Using locked table"));
2366
2216
      goto reset;
2367
2217
    }
2368
2218
    /*
2373
2223
      locked tables list was created.
2374
2224
    */
2375
2225
    my_error(ER_TABLE_NOT_LOCKED, MYF(0), alias);
2376
 
    DBUG_RETURN(0);
 
2226
    return(0);
2377
2227
  }
2378
2228
 
2379
2229
  /*
2411
2261
    if (refresh)
2412
2262
      *refresh=1;
2413
2263
    VOID(pthread_mutex_unlock(&LOCK_open));
2414
 
    DBUG_RETURN(0);
 
2264
    return(0);
2415
2265
  }
2416
2266
 
2417
2267
  /*
2440
2290
       table= (TABLE*) hash_next(&open_cache, (uchar*) key, key_length,
2441
2291
                                 &state))
2442
2292
  {
2443
 
    DBUG_PRINT("tcache", ("in_use table: '%s'.'%s' 0x%lx", table->s->db.str,
2444
 
                          table->s->table_name.str, (long) table));
2445
2293
    /*
2446
2294
      Here we flush tables marked for flush.
2447
2295
      Normally, table->s->version contains the value of
2462
2310
    */
2463
2311
    if (table->needs_reopen_or_name_lock())
2464
2312
    {
2465
 
      DBUG_PRINT("note",
2466
 
                 ("Found table '%s.%s' with different refresh version",
2467
 
                  table_list->db, table_list->table_name));
2468
 
 
2469
2313
      if (flags & MYSQL_LOCK_IGNORE_FLUSH)
2470
2314
      {
2471
2315
        /* Force close at once after usage */
2478
2322
      {
2479
2323
        VOID(pthread_mutex_unlock(&LOCK_open));
2480
2324
        my_error(ER_UPDATE_TABLE_USED, MYF(0), table->s->table_name.str);
2481
 
        DBUG_RETURN(0);
 
2325
        return(0);
2482
2326
      }
2483
2327
 
2484
2328
      /*
2525
2369
      */
2526
2370
      if (refresh)
2527
2371
        *refresh=1;
2528
 
      DBUG_RETURN(0);
 
2372
      return(0);
2529
2373
    }
2530
2374
  }
2531
2375
  if (table)
2532
2376
  {
2533
 
    DBUG_PRINT("tcache", ("unused table: '%s'.'%s' 0x%lx", table->s->db.str,
2534
 
                          table->s->table_name.str, (long) table));
2535
2377
    /* Unlink the table from "unused_tables" list. */
2536
2378
    if (table == unused_tables)
2537
2379
    {                                           // First unused
2547
2389
  {
2548
2390
    /* Insert a new TABLE instance into the open cache */
2549
2391
    int error;
2550
 
    DBUG_PRINT("tcache", ("opening new table"));
2551
2392
    /* Free cache if too big */
2552
2393
    while (open_cache.records > table_cache_size && unused_tables)
2553
2394
      VOID(hash_delete(&open_cache,(uchar*) unused_tables)); /* purecov: tested */
2559
2400
      if (check_if_table_exists(thd, table_list, &exists))
2560
2401
      {
2561
2402
        VOID(pthread_mutex_unlock(&LOCK_open));
2562
 
        DBUG_RETURN(NULL);
 
2403
        return(NULL);
2563
2404
      }
2564
2405
 
2565
2406
      if (!exists)
2570
2411
        if (!(table= table_cache_insert_placeholder(thd, key, key_length)))
2571
2412
        {
2572
2413
          VOID(pthread_mutex_unlock(&LOCK_open));
2573
 
          DBUG_RETURN(NULL);
 
2414
          return(NULL);
2574
2415
        }
2575
2416
        /*
2576
2417
          Link placeholder to the open tables list so it will be automatically
2581
2422
        table->next= thd->open_tables;
2582
2423
        thd->open_tables= table;
2583
2424
        VOID(pthread_mutex_unlock(&LOCK_open));
2584
 
        DBUG_RETURN(table);
 
2425
        return(table);
2585
2426
      }
2586
2427
      /* Table exists. Let us try to open it. */
2587
2428
    }
2590
2431
    if (!(table=(TABLE*) my_malloc(sizeof(*table),MYF(MY_WME))))
2591
2432
    {
2592
2433
      VOID(pthread_mutex_unlock(&LOCK_open));
2593
 
      DBUG_RETURN(NULL);
 
2434
      return(NULL);
2594
2435
    }
2595
2436
 
2596
2437
    error= open_unireg_entry(thd, table, table_list, alias, key, key_length,
2600
2441
    {
2601
2442
      my_free((uchar*)table, MYF(0));
2602
2443
      VOID(pthread_mutex_unlock(&LOCK_open));
2603
 
      DBUG_RETURN(NULL);
 
2444
      return(NULL);
2604
2445
    }
2605
2446
    if (error < 0)
2606
2447
    {
2607
2448
      my_free((uchar*)table, MYF(0));
2608
2449
      VOID(pthread_mutex_unlock(&LOCK_open));
2609
 
      DBUG_RETURN(0); // VIEW
 
2450
      return(0); // VIEW
2610
2451
    }
2611
 
    DBUG_PRINT("info", ("inserting table '%s'.'%s' 0x%lx into the cache",
2612
 
                        table->s->db.str, table->s->table_name.str,
2613
 
                        (long) table));
2614
2452
    VOID(my_hash_insert(&open_cache,(uchar*) table));
2615
2453
  }
2616
2454
 
2617
 
  check_unused();                               // Debugging call
2618
 
 
2619
2455
  VOID(pthread_mutex_unlock(&LOCK_open));
2620
2456
  if (refresh)
2621
2457
  {
2625
2461
  table->reginfo.lock_type=TL_READ;             /* Assume read */
2626
2462
 
2627
2463
 reset:
2628
 
  DBUG_ASSERT(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
 
2464
  assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
2629
2465
 
2630
2466
  if (thd->lex->need_correct_ident())
2631
2467
    table->alias_name_used= my_strcasecmp(table_alias_charset,
2648
2484
  table->fulltext_searched= 0;
2649
2485
  table->file->ft_handler= 0;
2650
2486
  /* Catch wrong handling of the auto_increment_field_not_null. */
2651
 
  DBUG_ASSERT(!table->auto_increment_field_not_null);
 
2487
  assert(!table->auto_increment_field_not_null);
2652
2488
  table->auto_increment_field_not_null= false;
2653
2489
  if (table->timestamp_field)
2654
2490
    table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
2655
2491
  table->pos_in_table_list= table_list;
2656
2492
  table->clear_column_bitmaps();
2657
 
  DBUG_ASSERT(table->key_read == 0);
2658
 
  DBUG_RETURN(table);
 
2493
  assert(table->key_read == 0);
 
2494
  return(table);
2659
2495
}
2660
2496
 
2661
2497
 
2698
2534
  uint key,part;
2699
2535
  TABLE_LIST table_list;
2700
2536
  THD *thd= table->in_use;
2701
 
  DBUG_ENTER("reopen_table");
2702
 
  DBUG_PRINT("tcache", ("table: '%s'.'%s' 0x%lx", table->s->db.str,
2703
 
                        table->s->table_name.str, (long) table));
2704
2537
 
2705
 
  DBUG_ASSERT(table->s->ref_count == 0);
2706
 
  DBUG_ASSERT(!table->sort.io_cache);
 
2538
  assert(table->s->ref_count == 0);
 
2539
  assert(!table->sort.io_cache);
2707
2540
 
2708
2541
#ifdef EXTRA_DEBUG
2709
2542
  if (table->db_stat)
2716
2549
  table_list.table=      table;
2717
2550
 
2718
2551
  if (wait_for_locked_table_names(thd, &table_list))
2719
 
    DBUG_RETURN(1);                             // Thread was killed
 
2552
    return(1);                             // Thread was killed
2720
2553
 
2721
2554
  if (open_unireg_entry(thd, &tmp, &table_list,
2722
2555
                        table->alias,
2750
2583
  table->default_column_bitmaps();
2751
2584
  table->file->change_table_ptr(table, table->s);
2752
2585
 
2753
 
  DBUG_ASSERT(table->alias != 0);
 
2586
  assert(table->alias != 0);
2754
2587
  for (field=table->field ; *field ; field++)
2755
2588
  {
2756
2589
    (*field)->table= (*field)->orig_table= table;
2771
2604
  error=0;
2772
2605
 
2773
2606
 end:
2774
 
  DBUG_RETURN(error);
 
2607
  return(error);
2775
2608
}
2776
2609
 
2777
2610
 
2796
2629
                                      const char *table_name)
2797
2630
{
2798
2631
  TABLE *table;
2799
 
  DBUG_ENTER("close_data_files_and_morph_locks");
2800
2632
 
2801
2633
  safe_mutex_assert_owner(&LOCK_open);
2802
2634
 
2828
2660
      close_handle_and_leave_table_as_lock(table);
2829
2661
    }
2830
2662
  }
2831
 
  DBUG_VOID_RETURN;
 
2663
  return;
2832
2664
}
2833
2665
 
2834
2666
 
2861
2693
                    MYSQL_LOCK_IGNORE_GLOBAL_READ_LOCK |
2862
2694
                    MYSQL_LOCK_IGNORE_FLUSH;
2863
2695
 
2864
 
  DBUG_ENTER("reopen_tables");
2865
 
 
2866
2696
  if (!thd->open_tables)
2867
 
    DBUG_RETURN(0);
 
2697
    return(0);
2868
2698
 
2869
2699
  safe_mutex_assert_owner(&LOCK_open);
2870
2700
  if (get_locks)
2876
2706
    uint opens=0;
2877
2707
    for (table= thd->open_tables; table ; table=table->next)
2878
2708
      opens++;
2879
 
    DBUG_PRINT("tcache", ("open tables to lock: %u", opens));
2880
2709
    tables= (TABLE**) my_alloca(sizeof(TABLE*)*opens);
2881
2710
  }
2882
2711
  else
2909
2738
    }
2910
2739
  }
2911
2740
  *prev=0;
2912
 
  DBUG_PRINT("tcache", ("open tables to lock: %u",
2913
 
                        (uint) (tables_ptr - tables)));
2914
2741
  if (tables != tables_ptr)                     // Should we get back old locks
2915
2742
  {
2916
2743
    MYSQL_LOCK *lock;
2941
2768
    my_afree((uchar*) tables);
2942
2769
  }
2943
2770
  broadcast_refresh();
2944
 
  DBUG_RETURN(error);
 
2771
  return(error);
2945
2772
}
2946
2773
 
2947
2774
 
2963
2790
                                 bool send_refresh)
2964
2791
{
2965
2792
  bool found= send_refresh;
2966
 
  DBUG_ENTER("close_old_data_files");
2967
2793
 
2968
2794
  for (; table ; table=table->next)
2969
2795
  {
2970
 
    DBUG_PRINT("tcache", ("checking table: '%s'.'%s' 0x%lx",
2971
 
                          table->s->db.str, table->s->table_name.str,
2972
 
                          (long) table));
2973
 
    DBUG_PRINT("tcache", ("needs refresh: %d  is open: %u",
2974
 
                          table->needs_reopen_or_name_lock(), table->db_stat));
2975
2796
    /*
2976
2797
      Reopen marked for flush.
2977
2798
    */
3033
2854
  }
3034
2855
  if (found)
3035
2856
    broadcast_refresh();
3036
 
  DBUG_VOID_RETURN;
 
2857
  return;
3037
2858
}
3038
2859
 
3039
2860
 
3045
2866
 
3046
2867
bool table_is_used(TABLE *table, bool wait_for_name_lock)
3047
2868
{
3048
 
  DBUG_ENTER("table_is_used");
3049
2869
  do
3050
2870
  {
3051
2871
    char *key= table->s->table_cache_key.str;
3052
2872
    uint key_length= table->s->table_cache_key.length;
3053
2873
 
3054
 
    DBUG_PRINT("loop", ("table_name: %s", table->alias));
3055
2874
    HASH_SEARCH_STATE state;
3056
2875
    for (TABLE *search= (TABLE*) hash_first(&open_cache, (uchar*) key,
3057
2876
                                             key_length, &state);
3059
2878
         search= (TABLE*) hash_next(&open_cache, (uchar*) key,
3060
2879
                                    key_length, &state))
3061
2880
    {
3062
 
      DBUG_PRINT("info", ("share: 0x%lx  "
3063
 
                          "open_placeholder: %d  locked_by_name: %d "
3064
 
                          "db_stat: %u  version: %lu",
3065
 
                          (ulong) search->s,
3066
 
                          search->open_placeholder, search->locked_by_name,
3067
 
                          search->db_stat,
3068
 
                          search->s->version));
3069
2881
      if (search->in_use == table->in_use)
3070
2882
        continue;                               // Name locked by this thread
3071
2883
      /*
3077
2889
      */
3078
2890
      if ( (search->locked_by_name && wait_for_name_lock) ||
3079
2891
           (search->is_name_opened() && search->needs_reopen_or_name_lock()))
3080
 
        DBUG_RETURN(1);
 
2892
        return(1);
3081
2893
    }
3082
2894
  } while ((table=table->next));
3083
 
  DBUG_RETURN(0);
 
2895
  return(0);
3084
2896
}
3085
2897
 
3086
2898
 
3089
2901
bool wait_for_tables(THD *thd)
3090
2902
{
3091
2903
  bool result;
3092
 
  DBUG_ENTER("wait_for_tables");
3093
2904
 
3094
2905
  thd_proc_info(thd, "Waiting for tables");
3095
2906
  pthread_mutex_lock(&LOCK_open);
3113
2924
  }
3114
2925
  pthread_mutex_unlock(&LOCK_open);
3115
2926
  thd_proc_info(thd, 0);
3116
 
  DBUG_RETURN(result);
 
2927
  return(result);
3117
2928
}
3118
2929
 
3119
2930
 
3145
2956
{
3146
2957
  TABLE *table,*next,**prev, *found= 0;
3147
2958
  prev= &thd->open_tables;
3148
 
  DBUG_ENTER("drop_locked_tables");
3149
2959
 
3150
2960
  /*
3151
2961
    Note that we need to hold LOCK_open while changing the
3192
3002
    my_free((uchar*) thd->locked_tables,MYF(0));
3193
3003
    thd->locked_tables=0;
3194
3004
  }
3195
 
  DBUG_RETURN(found);
 
3005
  return(found);
3196
3006
}
3197
3007
 
3198
3008
 
3251
3061
{
3252
3062
  static ulong last_table_id= ~0UL;
3253
3063
 
3254
 
  DBUG_ENTER("assign_new_table_id");
3255
 
 
3256
3064
  /* Preconditions */
3257
 
  DBUG_ASSERT(share != NULL);
 
3065
  assert(share != NULL);
3258
3066
  safe_mutex_assert_owner(&LOCK_open);
3259
3067
 
3260
3068
  ulong tid= ++last_table_id;                   /* get next id */
3265
3073
  if (unlikely(tid == ~0UL))
3266
3074
    tid= ++last_table_id;
3267
3075
  share->table_map_id= tid;
3268
 
  DBUG_PRINT("info", ("table_id=%lu", tid));
3269
3076
 
3270
3077
  /* Post conditions */
3271
 
  DBUG_ASSERT(share->table_map_id != ~0UL);
 
3078
  assert(share->table_map_id != ~0UL);
3272
3079
 
3273
 
  DBUG_VOID_RETURN;
 
3080
  return;
3274
3081
}
3275
3082
 
3276
3083
/*
3306
3113
  int error;
3307
3114
  TABLE_SHARE *share;
3308
3115
  uint discover_retry_count= 0;
3309
 
  DBUG_ENTER("open_unireg_entry");
3310
3116
 
3311
3117
  safe_mutex_assert_owner(&LOCK_open);
3312
3118
retry:
3315
3121
                                           OPEN_VIEW |
3316
3122
                                           table_list->i_s_requested_object,
3317
3123
                                           &error)))
3318
 
    DBUG_RETURN(1);
 
3124
    return(1);
3319
3125
 
3320
3126
  while ((error= open_table_from_share(thd, share, alias,
3321
3127
                                       (uint) (HA_OPEN_KEYFILE |
3363
3169
        thd->clear_error();                 // Clear error message
3364
3170
        goto retry;
3365
3171
      }
3366
 
      DBUG_RETURN(1);
 
3172
      return(1);
3367
3173
    }
3368
3174
    if (!entry->s || !entry->s->crashed)
3369
3175
      goto err;
3444
3250
      }
3445
3251
    }
3446
3252
  }
3447
 
  DBUG_RETURN(0);
 
3253
  return(0);
3448
3254
 
3449
3255
err:
3450
3256
  release_table_share(share, RELEASE_NORMAL);
3451
 
  DBUG_RETURN(1);
 
3257
  return(1);
3452
3258
}
3453
3259
 
3454
3260
 
3489
3295
  /* Also used for indicating that prelocking is need */
3490
3296
  bool safe_to_ignore_table;
3491
3297
 
3492
 
  DBUG_ENTER("open_tables");
3493
3298
  /*
3494
3299
    temporary mem_root for new .frm parsing.
3495
3300
    TODO: variables for size
3507
3312
  */
3508
3313
  for (tables= *start; tables ;tables= tables->next_global)
3509
3314
  {
3510
 
    DBUG_PRINT("tcache", ("opening table: '%s'.'%s'  item: 0x%lx",
3511
 
                          tables->db, tables->table_name, (long) tables));
3512
 
 
3513
3315
    safe_to_ignore_table= false;
3514
3316
 
3515
3317
    /*
3532
3334
    {
3533
3335
      if (!mysql_schema_table(thd, thd->lex, tables))
3534
3336
        continue;
3535
 
      DBUG_RETURN(-1);
 
3337
      return(-1);
3536
3338
    }
3537
3339
    (*counter)++;
3538
3340
 
3541
3343
      not opened yet. Try to open the table.
3542
3344
    */
3543
3345
    if (!tables->table)
3544
 
    {
3545
3346
      tables->table= open_table(thd, tables, &new_frm_mem, &refresh, flags);
3546
 
    }
3547
 
    else
3548
 
      DBUG_PRINT("tcache", ("referenced table: '%s'.'%s' 0x%lx",
3549
 
                            tables->db, tables->table_name,
3550
 
                            (long) tables->table));
3551
3347
 
3552
3348
    if (!tables->table)
3553
3349
    {
3574
3370
      }
3575
3371
 
3576
3372
      if (safe_to_ignore_table)
3577
 
      {
3578
 
        DBUG_PRINT("info", ("open_table: ignoring table '%s'.'%s'",
3579
 
                            tables->db, tables->alias));
3580
3373
        continue;
3581
 
      }
3582
3374
 
3583
3375
      result= -1;                               // Fatal error
3584
3376
      break;
3606
3398
      tables->parent_l->table= NULL;
3607
3399
    tables->table= NULL;
3608
3400
  }
3609
 
  DBUG_PRINT("tcache", ("returning: %d", result));
3610
 
  DBUG_RETURN(result);
 
3401
  return(result);
3611
3402
}
3612
3403
 
3613
3404
 
3629
3420
                                      thr_lock_type lock_type)
3630
3421
{
3631
3422
  int error;
3632
 
  DBUG_ENTER("check_lock_and_start_stmt");
3633
3423
 
3634
3424
  if ((int) lock_type >= (int) TL_WRITE_ALLOW_READ &&
3635
3425
      (int) table->reginfo.lock_type < (int) TL_WRITE_ALLOW_READ)
3636
3426
  {
3637
3427
    my_error(ER_TABLE_NOT_LOCKED_FOR_WRITE, MYF(0),table->alias);
3638
 
    DBUG_RETURN(1);
 
3428
    return(1);
3639
3429
  }
3640
3430
  if ((error=table->file->start_stmt(thd, lock_type)))
3641
3431
  {
3642
3432
    table->file->print_error(error,MYF(0));
3643
 
    DBUG_RETURN(1);
 
3433
    return(1);
3644
3434
  }
3645
 
  DBUG_RETURN(0);
 
3435
  return(0);
3646
3436
}
3647
3437
 
3648
3438
 
3680
3470
                                thr_lock_type lock_type)
3681
3471
{
3682
3472
  TABLE_LIST *save_next_global;
3683
 
  DBUG_ENTER("open_n_lock_single_table");
3684
3473
 
3685
3474
  /* Remember old 'next' pointer. */
3686
3475
  save_next_global= table_l->next_global;
3699
3488
  /* Restore list. */
3700
3489
  table_l->next_global= save_next_global;
3701
3490
 
3702
 
  DBUG_RETURN(table_l->table);
 
3491
  return(table_l->table);
3703
3492
}
3704
3493
 
3705
3494
 
3732
3521
{
3733
3522
  TABLE *table;
3734
3523
  bool refresh;
3735
 
  DBUG_ENTER("open_ltable");
3736
3524
 
3737
3525
  thd_proc_info(thd, "Opening table");
3738
3526
  thd->current_tablenr= 0;
3753
3541
    }
3754
3542
    else
3755
3543
    {
3756
 
      DBUG_ASSERT(thd->lock == 0);      // You must lock everything at once
 
3544
      assert(thd->lock == 0);   // You must lock everything at once
3757
3545
      if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
3758
3546
        if (! (thd->lock= mysql_lock_tables(thd, &table_list->table, 1,
3759
3547
                                            lock_flags, &refresh)))
3762
3550
  }
3763
3551
 
3764
3552
  thd_proc_info(thd, 0);
3765
 
  DBUG_RETURN(table);
 
3553
  return(table);
3766
3554
}
3767
3555
 
3768
3556
 
3794
3582
{
3795
3583
  uint counter;
3796
3584
  bool need_reopen;
3797
 
  DBUG_ENTER("open_and_lock_tables_derived");
3798
 
  DBUG_PRINT("enter", ("derived handling: %d", derived));
3799
3585
 
3800
3586
  for ( ; ; ) 
3801
3587
  {
3802
3588
    if (open_tables(thd, &tables, &counter, 0))
3803
 
      DBUG_RETURN(-1);
3804
 
 
3805
 
    DBUG_EXECUTE_IF("sleep_open_and_lock_after_open", {
3806
 
      const char *old_proc_info= thd->proc_info;
3807
 
      thd->proc_info= "DBUG sleep";
3808
 
      my_sleep(6000000);
3809
 
      thd->proc_info= old_proc_info;});
 
3589
      return(-1);
3810
3590
 
3811
3591
    if (!lock_tables(thd, tables, counter, &need_reopen))
3812
3592
      break;
3813
3593
    if (!need_reopen)
3814
 
      DBUG_RETURN(-1);
 
3594
      return(-1);
3815
3595
    close_tables_for_reopen(thd, &tables);
3816
3596
  }
3817
3597
  if (derived &&
3818
3598
      (mysql_handle_derived(thd->lex, &mysql_derived_prepare) ||
3819
3599
       (thd->fill_derived_tables() &&
3820
3600
        mysql_handle_derived(thd->lex, &mysql_derived_filling))))
3821
 
    DBUG_RETURN(true); /* purecov: inspected */
3822
 
  DBUG_RETURN(0);
 
3601
    return(true); /* purecov: inspected */
 
3602
  return(0);
3823
3603
}
3824
3604
 
3825
3605
 
3846
3626
bool open_normal_and_derived_tables(THD *thd, TABLE_LIST *tables, uint flags)
3847
3627
{
3848
3628
  uint counter;
3849
 
  DBUG_ENTER("open_normal_and_derived_tables");
3850
 
  DBUG_ASSERT(!thd->fill_derived_tables());
 
3629
  assert(!thd->fill_derived_tables());
3851
3630
  if (open_tables(thd, &tables, &counter, flags) ||
3852
3631
      mysql_handle_derived(thd->lex, &mysql_derived_prepare))
3853
 
    DBUG_RETURN(true); /* purecov: inspected */
3854
 
  DBUG_RETURN(0);
 
3632
    return(true); /* purecov: inspected */
 
3633
  return(0);
3855
3634
}
3856
3635
 
3857
3636
 
3905
3684
      if (!table->placeholder() && table->lock_type >= TL_WRITE_ALLOW_WRITE)
3906
3685
      {
3907
3686
        ulonglong const flags= table->table->file->ha_table_flags();
3908
 
        DBUG_PRINT("info", ("table: %s; ha_table_flags: %s%s",
3909
 
                            table->table_name,
3910
 
                            FLAGSTR(flags, HA_BINLOG_STMT_CAPABLE),
3911
 
                            FLAGSTR(flags, HA_BINLOG_ROW_CAPABLE)));
3912
3687
        if (prev_ht && prev_ht != table->table->file->ht)
3913
3688
          multi_engine= true;
3914
3689
        prev_ht= table->table->file->ht;
3917
3692
      }
3918
3693
    }
3919
3694
 
3920
 
    DBUG_PRINT("info", ("flags_all_set: %s%s",
3921
 
                        FLAGSTR(flags_all_set, HA_BINLOG_STMT_CAPABLE),
3922
 
                        FLAGSTR(flags_all_set, HA_BINLOG_ROW_CAPABLE)));
3923
 
    DBUG_PRINT("info", ("flags_some_set: %s%s",
3924
 
                        FLAGSTR(flags_some_set, HA_BINLOG_STMT_CAPABLE),
3925
 
                        FLAGSTR(flags_some_set, HA_BINLOG_ROW_CAPABLE)));
3926
 
    DBUG_PRINT("info", ("thd->variables.binlog_format: %ld",
3927
 
                        thd->variables.binlog_format));
3928
 
    DBUG_PRINT("info", ("multi_engine: %s",
3929
 
                        multi_engine ? "true" : "false"));
3930
 
 
3931
3695
    int error= 0;
3932
3696
    if (flags_all_set == 0)
3933
3697
    {
3967
3731
               " is self-logging");
3968
3732
    }
3969
3733
 
3970
 
    DBUG_PRINT("info", ("error: %d", error));
3971
 
 
3972
3734
    if (error)
3973
3735
      return -1;
3974
3736
 
4026
3788
{
4027
3789
  TABLE_LIST *table;
4028
3790
 
4029
 
  DBUG_ENTER("lock_tables");
4030
3791
  /*
4031
3792
    We can't meet statement requiring prelocking if we already
4032
3793
    in prelocked mode.
4034
3795
  *need_reopen= false;
4035
3796
 
4036
3797
  if (!tables)
4037
 
    DBUG_RETURN(decide_logging_format(thd, tables));
 
3798
    return(decide_logging_format(thd, tables));
4038
3799
 
4039
3800
  if (!thd->locked_tables)
4040
3801
  {
4041
 
    DBUG_ASSERT(thd->lock == 0);        // You must lock everything at once
 
3802
    assert(thd->lock == 0);     // You must lock everything at once
4042
3803
    TABLE **start,**ptr;
4043
3804
    uint lock_flag= MYSQL_LOCK_NOTIFY_IF_NEED_REOPEN;
4044
3805
 
4045
3806
    if (!(ptr=start=(TABLE**) thd->alloc(sizeof(TABLE*)*count)))
4046
 
      DBUG_RETURN(-1);
 
3807
      return(-1);
4047
3808
    for (table= tables; table; table= table->next_global)
4048
3809
    {
4049
3810
      if (!table->placeholder())
4053
3814
    if (!(thd->lock= mysql_lock_tables(thd, start, (uint) (ptr - start),
4054
3815
                                       lock_flag, need_reopen)))
4055
3816
    {
4056
 
      DBUG_RETURN(-1);
 
3817
      return(-1);
4057
3818
    }
4058
3819
  }
4059
3820
  else
4074
3835
      if (!table->placeholder() &&
4075
3836
          check_lock_and_start_stmt(thd, table->table, table->lock_type))
4076
3837
      {
4077
 
        DBUG_RETURN(-1);
 
3838
        return(-1);
4078
3839
      }
4079
3840
    }
4080
3841
  }
4081
3842
 
4082
 
  DBUG_RETURN(decide_logging_format(thd, tables));
 
3843
  return(decide_logging_format(thd, tables));
4083
3844
}
4084
3845
 
4085
3846
 
4138
3899
  char cache_key[MAX_DBKEY_LENGTH], *saved_cache_key, *tmp_path;
4139
3900
  uint key_length;
4140
3901
  TABLE_LIST table_list;
4141
 
  DBUG_ENTER("open_temporary_table");
4142
 
  DBUG_PRINT("enter",
4143
 
             ("table: '%s'.'%s'  path: '%s'  server_id: %u  "
4144
 
              "pseudo_thread_id: %lu",
4145
 
              db, table_name, path,
4146
 
              (uint) thd->server_id, (ulong) thd->variables.pseudo_thread_id));
4147
3902
 
4148
3903
  table_list.db=         (char*) db;
4149
3904
  table_list.table_name= (char*) table_name;
4153
3908
  if (!(tmp_table= (TABLE*) my_malloc(sizeof(*tmp_table) + sizeof(*share) +
4154
3909
                                      strlen(path)+1 + key_length,
4155
3910
                                      MYF(MY_WME))))
4156
 
    DBUG_RETURN(0);                             /* purecov: inspected */
 
3911
    return(0);                          /* purecov: inspected */
4157
3912
 
4158
3913
  share= (TABLE_SHARE*) (tmp_table+1);
4159
3914
  tmp_path= (char*) (share+1);
4178
3933
    /* No need to lock share->mutex as this is not needed for tmp tables */
4179
3934
    free_table_share(share);
4180
3935
    my_free((char*) tmp_table,MYF(0));
4181
 
    DBUG_RETURN(0);
 
3936
    return(0);
4182
3937
  }
4183
3938
 
4184
3939
  tmp_table->reginfo.lock_type= TL_WRITE;        // Simulate locked
4206
3961
      slave_open_temp_tables++;
4207
3962
  }
4208
3963
  tmp_table->pos_in_table_list= 0;
4209
 
  DBUG_PRINT("tmptable", ("opened table: '%s'.'%s' 0x%lx", tmp_table->s->db.str,
4210
 
                          tmp_table->s->table_name.str, (long) tmp_table));
4211
 
  DBUG_RETURN(tmp_table);
 
3964
  return(tmp_table);
4212
3965
}
4213
3966
 
4214
3967
 
4217
3970
  bool error=0;
4218
3971
  handler *file;
4219
3972
  char *ext;
4220
 
  DBUG_ENTER("rm_temporary_table");
4221
3973
 
4222
3974
  strmov(ext= strend(path), reg_ext);
4223
3975
  if (my_delete(path,MYF(0)))
4231
3983
                      path, my_errno);
4232
3984
  }
4233
3985
  delete file;
4234
 
  DBUG_RETURN(error);
 
3986
  return(error);
4235
3987
}
4236
3988
 
4237
3989
 
4252
4004
 
4253
4005
static void update_field_dependencies(THD *thd, Field *field, TABLE *table)
4254
4006
{
4255
 
  DBUG_ENTER("update_field_dependencies");
4256
4007
  if (thd->mark_used_columns != MARK_COLUMNS_NONE)
4257
4008
  {
4258
4009
    MY_BITMAP *current_bitmap, *other_bitmap;
4279
4030
    if (bitmap_fast_test_and_set(current_bitmap, field->field_index))
4280
4031
    {
4281
4032
      if (thd->mark_used_columns == MARK_COLUMNS_WRITE)
4282
 
      {
4283
 
        DBUG_PRINT("warning", ("Found duplicated field"));
4284
4033
        thd->dup_field= field;
4285
 
      }
4286
 
      else
4287
 
      {
4288
 
        DBUG_PRINT("note", ("Field found before"));
4289
 
      }
4290
 
      DBUG_VOID_RETURN;
 
4034
      return;
4291
4035
    }
4292
4036
    if (table->get_fields_in_item_tree)
4293
4037
      field->flags|= GET_FIXED_FIELDS_FLAG;
4295
4039
  }
4296
4040
  else if (table->get_fields_in_item_tree)
4297
4041
    field->flags|= GET_FIXED_FIELDS_FLAG;
4298
 
  DBUG_VOID_RETURN;
 
4042
  return;
4299
4043
}
4300
4044
 
4301
4045
 
4327
4071
                   Item **ref,
4328
4072
                   bool register_tree_change __attribute__((__unused__)))
4329
4073
{
4330
 
  DBUG_ENTER("find_field_in_view");
4331
 
  DBUG_PRINT("enter",
4332
 
             ("view: '%s', field name: '%s', item name: '%s', ref 0x%lx",
4333
 
              table_list->alias, name, item_name, (ulong) ref));
4334
4074
  Field_iterator_view field_it;
4335
4075
  field_it.set(table_list);
4336
4076
  
4337
 
  DBUG_ASSERT(table_list->schema_table_reformed ||
 
4077
  assert(table_list->schema_table_reformed ||
4338
4078
              (ref != 0 && (0) != 0));
4339
4079
  for (; !field_it.end_of_fields(); field_it.next())
4340
4080
  {
4347
4087
      Item *item= field_it.create_item(thd);
4348
4088
      
4349
4089
      if (!item)
4350
 
        DBUG_RETURN(0);
 
4090
        return(0);
4351
4091
      /*
4352
4092
       *ref != NULL means that *ref contains the item that we need to
4353
4093
       replace. If the item was aliased by the user, set the alias to
4365
4105
        thd->change_item_tree(ref, item);
4366
4106
      else
4367
4107
        *ref= item;
4368
 
      DBUG_RETURN((Field*) view_ref_found);
 
4108
      return((Field*) view_ref_found);
4369
4109
    }
4370
4110
  }
4371
 
  DBUG_RETURN(0);
 
4111
  return(0);
4372
4112
}
4373
4113
 
4374
4114
 
4411
4151
    field_it(*(table_ref->join_columns));
4412
4152
  Natural_join_column *nj_col, *curr_nj_col;
4413
4153
  Field *found_field;
4414
 
  DBUG_ENTER("find_field_in_natural_join");
4415
 
  DBUG_PRINT("enter", ("field name: '%s', ref 0x%lx",
4416
 
                       name, (ulong) ref));
4417
 
  DBUG_ASSERT(table_ref->is_natural_join && table_ref->join_columns);
4418
 
  DBUG_ASSERT(*actual_table == NULL);
 
4154
 
 
4155
  assert(table_ref->is_natural_join && table_ref->join_columns);
 
4156
  assert(*actual_table == NULL);
4419
4157
 
4420
4158
  for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col; 
4421
4159
       curr_nj_col= field_it++)
4425
4163
      if (nj_col)
4426
4164
      {
4427
4165
        my_error(ER_NON_UNIQ_ERROR, MYF(0), name, thd->where);
4428
 
        DBUG_RETURN(NULL);
 
4166
        return(NULL);
4429
4167
      }
4430
4168
      nj_col= curr_nj_col;
4431
4169
    }
4432
4170
  }
4433
4171
  if (!nj_col)
4434
 
    DBUG_RETURN(NULL);
 
4172
    return(NULL);
4435
4173
 
4436
4174
  if (nj_col->view_field)
4437
4175
  {
4456
4194
    }
4457
4195
 
4458
4196
    if (!item)
4459
 
      DBUG_RETURN(NULL);
4460
 
    DBUG_ASSERT(nj_col->table_field == NULL);
 
4197
      return(NULL);
 
4198
    assert(nj_col->table_field == NULL);
4461
4199
    if (nj_col->table_ref->schema_table_reformed)
4462
4200
    {
4463
4201
      /*
4465
4203
        already('mysql_schema_table' function). So we can return
4466
4204
        ->field. It is used only for 'show & where' commands.
4467
4205
      */
4468
 
      DBUG_RETURN(((Item_field*) (nj_col->view_field->item))->field);
 
4206
      return(((Item_field*) (nj_col->view_field->item))->field);
4469
4207
    }
4470
4208
    if (register_tree_change)
4471
4209
      thd->change_item_tree(ref, item);
4476
4214
  else
4477
4215
  {
4478
4216
    /* This is a base table. */
4479
 
    DBUG_ASSERT(nj_col->view_field == NULL);
4480
 
    DBUG_ASSERT(nj_col->table_ref->table == nj_col->table_field->table);
 
4217
    assert(nj_col->view_field == NULL);
 
4218
    assert(nj_col->table_ref->table == nj_col->table_field->table);
4481
4219
    found_field= nj_col->table_field;
4482
4220
    update_field_dependencies(thd, found_field, nj_col->table_ref->table);
4483
4221
  }
4484
4222
 
4485
4223
  *actual_table= nj_col->table_ref;
4486
4224
  
4487
 
  DBUG_RETURN(found_field);
 
4225
  return(found_field);
4488
4226
}
4489
4227
 
4490
4228
 
4512
4250
{
4513
4251
  Field **field_ptr, *field;
4514
4252
  uint cached_field_index= *cached_field_index_ptr;
4515
 
  DBUG_ENTER("find_field_in_table");
4516
 
  DBUG_PRINT("enter", ("table: '%s', field name: '%s'", table->alias, name));
4517
4253
 
4518
4254
  /* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
4519
4255
  if (cached_field_index < table->s->fields &&
4536
4272
  else
4537
4273
  {
4538
4274
    if (!(field_ptr= table->field))
4539
 
      DBUG_RETURN((Field *)0);
 
4275
      return((Field *)0);
4540
4276
    for (; *field_ptr; ++field_ptr)
4541
4277
      if (!my_strcasecmp(system_charset_info, (*field_ptr)->field_name, name))
4542
4278
        break;
4552
4288
    if (!allow_rowid ||
4553
4289
        my_strcasecmp(system_charset_info, name, "_rowid") ||
4554
4290
        table->s->rowid_field_offset == 0)
4555
 
      DBUG_RETURN((Field*) 0);
 
4291
      return((Field*) 0);
4556
4292
    field= table->field[table->s->rowid_field_offset-1];
4557
4293
  }
4558
4294
 
4559
4295
  update_field_dependencies(thd, field, table);
4560
4296
 
4561
 
  DBUG_RETURN(field);
 
4297
  return(field);
4562
4298
}
4563
4299
 
4564
4300
 
4614
4350
                        bool register_tree_change, TABLE_LIST **actual_table)
4615
4351
{
4616
4352
  Field *fld;
4617
 
  DBUG_ENTER("find_field_in_table_ref");
4618
 
  DBUG_ASSERT(table_list->alias);
4619
 
  DBUG_ASSERT(name);
4620
 
  DBUG_ASSERT(item_name);
4621
 
  DBUG_PRINT("enter",
4622
 
             ("table: '%s'  field name: '%s'  item name: '%s'  ref 0x%lx",
4623
 
              table_list->alias, name, item_name, (ulong) ref));
 
4353
 
 
4354
  assert(table_list->alias);
 
4355
  assert(name);
 
4356
  assert(item_name);
4624
4357
 
4625
4358
  /*
4626
4359
    Check that the table and database that qualify the current field name
4651
4384
      (my_strcasecmp(table_alias_charset, table_list->alias, table_name) ||
4652
4385
       (db_name && db_name[0] && table_list->db && table_list->db[0] &&
4653
4386
        strcmp(db_name, table_list->db))))
4654
 
    DBUG_RETURN(0);
 
4387
    return(0);
4655
4388
 
4656
4389
  *actual_table= NULL;
4657
4390
 
4665
4398
  else if (!table_list->nested_join)
4666
4399
  {
4667
4400
    /* 'table_list' is a stored table. */
4668
 
    DBUG_ASSERT(table_list->table);
 
4401
    assert(table_list->table);
4669
4402
    if ((fld= find_field_in_table(thd, table_list->table, name, length,
4670
4403
                                  allow_rowid,
4671
4404
                                  cached_field_index_ptr)))
4691
4424
                                          check_privileges, allow_rowid,
4692
4425
                                          cached_field_index_ptr,
4693
4426
                                          register_tree_change, actual_table)))
4694
 
          DBUG_RETURN(fld);
 
4427
          return(fld);
4695
4428
      }
4696
 
      DBUG_RETURN(0);
 
4429
      return(0);
4697
4430
    }
4698
4431
    /*
4699
4432
      Non-qualified field, search directly in the result columns of the
4738
4471
        }
4739
4472
      }
4740
4473
  }
4741
 
  DBUG_RETURN(fld);
 
4474
  return(fld);
4742
4475
}
4743
4476
 
4744
4477
 
4956
4689
      item->cached_table= (!actual_table->cacheable_table || found) ?
4957
4690
                          0 : actual_table;
4958
4691
 
4959
 
      DBUG_ASSERT(thd->where);
 
4692
      assert(thd->where);
4960
4693
      /*
4961
4694
        If we found a fully qualified field we return it directly as it can't
4962
4695
        have duplicates.
5381
5114
                       !table_ref_2->is_natural_join) ?
5382
5115
                      NULL : table_ref_2;
5383
5116
 
5384
 
  DBUG_ENTER("mark_common_columns");
5385
 
  DBUG_PRINT("info", ("operand_1: %s  operand_2: %s",
5386
 
                      table_ref_1->alias, table_ref_2->alias));
5387
 
 
5388
5117
  *found_using_fields= 0;
5389
5118
 
5390
5119
  for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
5398
5127
    field_name_1= nj_col_1->name();
5399
5128
    is_using_column_1= using_fields && 
5400
5129
      test_if_string_in_list(field_name_1, using_fields);
5401
 
    DBUG_PRINT ("info", ("field_name_1=%s.%s", 
5402
 
                         nj_col_1->table_name() ? nj_col_1->table_name() : "", 
5403
 
                         field_name_1));
5404
5130
 
5405
5131
    /*
5406
5132
      Find a field with the same name in table_ref_2.
5417
5143
      if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
5418
5144
        goto err;
5419
5145
      cur_field_name_2= cur_nj_col_2->name();
5420
 
      DBUG_PRINT ("info", ("cur_field_name_2=%s.%s", 
5421
 
                           cur_nj_col_2->table_name() ? 
5422
 
                             cur_nj_col_2->table_name() : "", 
5423
 
                           cur_field_name_2));
5424
5146
 
5425
5147
      /*
5426
5148
        Compare the two columns and check for duplicate common fields.
5435
5157
      */
5436
5158
      if (!my_strcasecmp(system_charset_info, field_name_1, cur_field_name_2))
5437
5159
      {
5438
 
        DBUG_PRINT ("info", ("match c1.is_common=%d", nj_col_1->is_common));
5439
5160
        if (cur_nj_col_2->is_common ||
5440
5161
            (found && (!using_fields || is_using_column_1)))
5441
5162
        {
5479
5200
        The following assert checks that the two created items are of
5480
5201
        type Item_ident.
5481
5202
      */
5482
 
      DBUG_ASSERT(!thd->lex->current_select->no_wrap_view_item);
 
5203
      assert(!thd->lex->current_select->no_wrap_view_item);
5483
5204
      /*
5484
5205
        In the case of no_wrap_view_item == 0, the created items must be
5485
5206
        of sub-classes of Item_ident.
5486
5207
      */
5487
 
      DBUG_ASSERT(item_1->type() == Item::FIELD_ITEM ||
 
5208
      assert(item_1->type() == Item::FIELD_ITEM ||
5488
5209
                  item_1->type() == Item::REF_ITEM);
5489
 
      DBUG_ASSERT(item_2->type() == Item::FIELD_ITEM ||
 
5210
      assert(item_2->type() == Item::FIELD_ITEM ||
5490
5211
                  item_2->type() == Item::REF_ITEM);
5491
5212
 
5492
5213
      /*
5518
5239
                  eq_cond);
5519
5240
 
5520
5241
      nj_col_1->is_common= nj_col_2->is_common= true;
5521
 
      DBUG_PRINT ("info", ("%s.%s and %s.%s are common", 
5522
 
                           nj_col_1->table_name() ? 
5523
 
                             nj_col_1->table_name() : "", 
5524
 
                           nj_col_1->name(),
5525
 
                           nj_col_2->table_name() ? 
5526
 
                             nj_col_2->table_name() : "", 
5527
 
                           nj_col_2->name()));
5528
5242
 
5529
5243
      if (field_1)
5530
5244
      {
5560
5274
  result= false;
5561
5275
 
5562
5276
err:
5563
 
  DBUG_RETURN(result);
 
5277
  return(result);
5564
5278
}
5565
5279
 
5566
5280
 
5612
5326
  Natural_join_column *nj_col_1, *nj_col_2;
5613
5327
  bool result= true;
5614
5328
  List<Natural_join_column> *non_join_columns;
5615
 
  DBUG_ENTER("store_natural_using_join_columns");
5616
5329
 
5617
 
  DBUG_ASSERT(!natural_using_join->join_columns);
 
5330
  assert(!natural_using_join->join_columns);
5618
5331
 
5619
5332
  if (!(non_join_columns= new List<Natural_join_column>) ||
5620
5333
      !(natural_using_join->join_columns= new List<Natural_join_column>))
5686
5399
  result= false;
5687
5400
 
5688
5401
err:
5689
 
  DBUG_RETURN(result);
 
5402
  return(result);
5690
5403
}
5691
5404
 
5692
5405
 
5727
5440
{
5728
5441
  bool result= true;
5729
5442
 
5730
 
  DBUG_ENTER("store_top_level_join_columns");
5731
 
 
5732
5443
  /* Call the procedure recursively for each nested table reference. */
5733
5444
  if (table_ref->nested_join)
5734
5445
  {
5757
5468
          cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
5758
5469
      {
5759
5470
        /* This can happen only for JOIN ... ON. */
5760
 
        DBUG_ASSERT(table_ref->nested_join->join_list.elements == 2);
 
5471
        assert(table_ref->nested_join->join_list.elements == 2);
5761
5472
        swap_variables(TABLE_LIST*, same_level_left_neighbor, cur_table_ref);
5762
5473
      }
5763
5474
 
5784
5495
  */
5785
5496
  if (table_ref->is_natural_join)
5786
5497
  {
5787
 
    DBUG_ASSERT(table_ref->nested_join &&
 
5498
    assert(table_ref->nested_join &&
5788
5499
                table_ref->nested_join->join_list.elements == 2);
5789
5500
    List_iterator_fast<TABLE_LIST> operand_it(table_ref->nested_join->join_list);
5790
5501
    /*
5851
5562
  result= false; /* All is OK. */
5852
5563
 
5853
5564
err:
5854
 
  DBUG_RETURN(result);
 
5565
  return(result);
5855
5566
}
5856
5567
 
5857
5568
 
5921
5632
    right_neighbor points to the left-most top-level table reference in the
5922
5633
    FROM clause.
5923
5634
  */
5924
 
  DBUG_ASSERT(right_neighbor);
 
5635
  assert(right_neighbor);
5925
5636
  context->first_name_resolution_table=
5926
5637
    right_neighbor->first_leaf_for_name_resolution();
5927
5638
 
5944
5655
 
5945
5656
  Item *item;
5946
5657
  List_iterator<Item> it(fields);
5947
 
  DBUG_ENTER("setup_wild");
5948
5658
 
5949
5659
  thd->lex->current_select->cur_pos_in_select_list= 0;
5950
5660
  while (wild_num && (item= it++))
5973
5683
                             ((Item_field*) item)->table_name, &it,
5974
5684
                             any_privileges))
5975
5685
      {
5976
 
        DBUG_RETURN(-1);
 
5686
        return(-1);
5977
5687
      }
5978
5688
      if (sum_func_list)
5979
5689
      {
5990
5700
      thd->lex->current_select->cur_pos_in_select_list++;
5991
5701
  }
5992
5702
  thd->lex->current_select->cur_pos_in_select_list= UNDEF_POS;
5993
 
  DBUG_RETURN(0);
 
5703
  return(0);
5994
5704
}
5995
5705
 
5996
5706
/****************************************************************************
6006
5716
  nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
6007
5717
  List_iterator<Item> it(fields);
6008
5718
  bool save_is_item_list_lookup;
6009
 
  DBUG_ENTER("setup_fields");
6010
5719
 
6011
5720
  thd->mark_used_columns= mark_used_columns;
6012
 
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
6013
5721
  if (allow_sum_func)
6014
5722
    thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
6015
5723
  thd->where= THD::DEFAULT_WHERE;
6039
5747
      thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
6040
5748
      thd->lex->allow_sum_func= save_allow_sum_func;
6041
5749
      thd->mark_used_columns= save_mark_used_columns;
6042
 
      DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
6043
 
      DBUG_RETURN(true); /* purecov: inspected */
 
5750
      return(true); /* purecov: inspected */
6044
5751
    }
6045
5752
    if (ref)
6046
5753
      *(ref++)= item;
6055
5762
 
6056
5763
  thd->lex->allow_sum_func= save_allow_sum_func;
6057
5764
  thd->mark_used_columns= save_mark_used_columns;
6058
 
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
6059
 
  DBUG_RETURN(test(thd->is_error()));
 
5765
  return(test(thd->is_error()));
6060
5766
}
6061
5767
 
6062
5768
 
6116
5822
                  TABLE_LIST **leaves, bool select_insert)
6117
5823
{
6118
5824
  uint tablenr= 0;
6119
 
  DBUG_ENTER("setup_tables");
6120
5825
 
6121
 
  DBUG_ASSERT ((select_insert && !tables->next_name_resolution_table) || !tables || 
 
5826
  assert ((select_insert && !tables->next_name_resolution_table) || !tables || 
6122
5827
               (context->table_list && context->first_name_resolution_table));
6123
5828
  /*
6124
5829
    this is used for INSERT ... SELECT.
6146
5851
    }
6147
5852
    setup_table_map(table, table_list, tablenr);
6148
5853
    if (table_list->process_index_hints(table))
6149
 
      DBUG_RETURN(1);
 
5854
      return(1);
6150
5855
  }
6151
5856
  if (tablenr > MAX_TABLES)
6152
5857
  {
6153
5858
    my_error(ER_TOO_MANY_TABLES,MYF(0),MAX_TABLES);
6154
 
    DBUG_RETURN(1);
 
5859
    return(1);
6155
5860
  }
6156
5861
 
6157
5862
  /* Precompute and store the row types of NATURAL/USING joins. */
6158
5863
  if (setup_natural_join_row_types(thd, from_clause, context))
6159
 
    DBUG_RETURN(1);
 
5864
    return(1);
6160
5865
 
6161
 
  DBUG_RETURN(0);
 
5866
  return(0);
6162
5867
}
6163
5868
 
6164
5869
 
6280
5985
  Field_iterator_table_ref field_iterator;
6281
5986
  bool found;
6282
5987
  char name_buff[NAME_LEN+1];
6283
 
  DBUG_ENTER("insert_fields");
6284
5988
 
6285
5989
  if (db_name && lower_case_table_names)
6286
5990
  {
6311
6015
    Field *field;
6312
6016
    TABLE *table= tables->table;
6313
6017
 
6314
 
    DBUG_ASSERT(tables->is_leaf_for_name_resolution());
 
6018
    assert(tables->is_leaf_for_name_resolution());
6315
6019
 
6316
6020
    if ((table_name && my_strcasecmp(table_alias_charset, table_name, tables->alias)) || 
6317
6021
        (db_name && strcmp(tables->db,db_name)))
6337
6041
      Item *item;
6338
6042
 
6339
6043
      if (!(item= field_iterator.create_item(thd)))
6340
 
        DBUG_RETURN(true);
 
6044
        return(true);
6341
6045
 
6342
6046
      if (!found)
6343
6047
      {
6365
6069
          */
6366
6070
          Natural_join_column *nj_col;
6367
6071
          if (!(nj_col= field_iterator.get_natural_column_ref()))
6368
 
            DBUG_RETURN(true);
6369
 
          DBUG_ASSERT(nj_col->table_field);
 
6072
            return(true);
 
6073
          assert(nj_col->table_field);
6370
6074
          field_table= nj_col->table_ref->table;
6371
6075
          if (field_table)
6372
6076
          {
6391
6095
      table->used_fields= table->s->fields;
6392
6096
  }
6393
6097
  if (found)
6394
 
    DBUG_RETURN(false);
 
6098
    return(false);
6395
6099
 
6396
6100
  /*
6397
6101
    TODO: in the case when we skipped all columns because there was a
6403
6107
  else
6404
6108
    my_error(ER_BAD_TABLE_ERROR, MYF(0), table_name);
6405
6109
 
6406
 
  DBUG_RETURN(true);
 
6110
  return(true);
6407
6111
}
6408
6112
 
6409
6113
 
6442
6146
  */
6443
6147
  bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
6444
6148
  select_lex->is_item_list_lookup= 0;
6445
 
  DBUG_ENTER("setup_conds");
6446
6149
 
6447
6150
  thd->mark_used_columns= MARK_COLUMNS_READ;
6448
 
  DBUG_PRINT("info", ("thd->mark_used_columns: %d", thd->mark_used_columns));
6449
6151
  select_lex->cond_count= 0;
6450
6152
  select_lex->between_count= 0;
6451
6153
  select_lex->max_equal_elems= 0;
6490
6192
  thd->thd_marker= save_thd_marker;
6491
6193
 
6492
6194
  thd->lex->current_select->is_item_list_lookup= save_is_item_list_lookup;
6493
 
  DBUG_RETURN(test(thd->is_error()));
 
6195
  return(test(thd->is_error()));
6494
6196
 
6495
6197
err_no_arena:
6496
6198
  select_lex->is_item_list_lookup= save_is_item_list_lookup;
6497
 
  DBUG_RETURN(1);
 
6199
  return(1);
6498
6200
}
6499
6201
 
6500
6202
 
6531
6233
  Item *value, *fld;
6532
6234
  Item_field *field;
6533
6235
  TABLE *table= 0;
6534
 
  DBUG_ENTER("fill_record");
6535
6236
 
6536
6237
  /*
6537
6238
    Reset the table->auto_increment_field_not_null as it is valid for
6571
6272
      goto err;
6572
6273
    }
6573
6274
  }
6574
 
  DBUG_RETURN(thd->is_error());
 
6275
  return(thd->is_error());
6575
6276
err:
6576
6277
  if (table)
6577
6278
    table->auto_increment_field_not_null= false;
6578
 
  DBUG_RETURN(true);
 
6279
  return(true);
6579
6280
}
6580
6281
 
6581
6282
 
6606
6307
  List_iterator_fast<Item> v(values);
6607
6308
  Item *value;
6608
6309
  TABLE *table= 0;
6609
 
  DBUG_ENTER("fill_record");
6610
6310
 
6611
6311
  Field *field;
6612
6312
  /*
6631
6331
    if (value->save_in_field(field, 0) < 0)
6632
6332
      goto err;
6633
6333
  }
6634
 
  DBUG_RETURN(thd->is_error());
 
6334
  return(thd->is_error());
6635
6335
 
6636
6336
err:
6637
6337
  if (table)
6638
6338
    table->auto_increment_field_not_null= false;
6639
 
  DBUG_RETURN(true);
 
6339
  return(true);
6640
6340
}
6641
6341
 
6642
6342
 
6648
6348
  FILEINFO *file;
6649
6349
  TABLE_SHARE share;
6650
6350
  THD *thd;
6651
 
  DBUG_ENTER("mysql_rm_tmp_tables");
6652
6351
 
6653
6352
  if (!(thd= new THD))
6654
 
    DBUG_RETURN(1);
 
6353
    return(1);
6655
6354
  thd->thread_stack= (char*) &thd;
6656
6355
  thd->store_globals();
6657
6356
 
6709
6408
  }
6710
6409
  delete thd;
6711
6410
  my_pthread_setspecific_ptr(THR_THD,  0);
6712
 
  DBUG_RETURN(0);
 
6411
  return(0);
6713
6412
}
6714
6413
 
6715
6414
 
6788
6487
  TABLE *table;
6789
6488
  TABLE_SHARE *share;
6790
6489
  bool result= 0, signalled= 0;
6791
 
  DBUG_ENTER("remove_table_from_cache");
6792
 
  DBUG_PRINT("enter", ("table: '%s'.'%s'  flags: %u", db, table_name, flags));
6793
6490
 
6794
6491
  key_length=(uint) (strmov(strmov(key,db)+1,table_name)-key)+1;
6795
6492
  for (;;)
6804
6501
                                   &state))
6805
6502
    {
6806
6503
      THD *in_use;
6807
 
      DBUG_PRINT("tcache", ("found table: '%s'.'%s' 0x%lx", table->s->db.str,
6808
 
                            table->s->table_name.str, (long) table));
6809
6504
 
6810
6505
      table->s->version=0L;             /* Free when thread is ready */
6811
6506
      if (!(in_use=table->in_use))
6812
6507
      {
6813
 
        DBUG_PRINT("info",("Table was not in use"));
6814
6508
        relink_unused(table);
6815
6509
      }
6816
6510
      else if (in_use != thd)
6817
6511
      {
6818
 
        DBUG_PRINT("info", ("Table was in use by other thread"));
6819
6512
        /*
6820
6513
          Mark that table is going to be deleted from cache. This will
6821
6514
          force threads that are in mysql_lock_tables() (but not yet
6824
6517
        in_use->some_tables_deleted= 1;
6825
6518
        if (table->is_name_opened())
6826
6519
        {
6827
 
          DBUG_PRINT("info", ("Found another active instance of the table"));
6828
6520
          result=1;
6829
6521
        }
6830
6522
        /* Kill delayed insert threads */
6861
6553
        }
6862
6554
      }
6863
6555
      else
6864
 
      {
6865
 
        DBUG_PRINT("info", ("Table was in use by current thread. db_stat: %u",
6866
 
                            table->db_stat));
6867
6556
        result= result || (flags & RTFC_OWNED_BY_THD_FLAG);
6868
 
      }
6869
6557
    }
6870
6558
    while (unused_tables && !unused_tables->s->version)
6871
6559
      VOID(hash_delete(&open_cache,(uchar*) unused_tables));
6872
6560
 
6873
 
    DBUG_PRINT("info", ("Removing table from table_def_cache"));
6874
6561
    /* Remove table from table definition cache if it's not in use */
6875
6562
    if ((share= (TABLE_SHARE*) hash_search(&table_def_cache,(uchar*) key,
6876
6563
                                           key_length)))
6877
6564
    {
6878
 
      DBUG_PRINT("info", ("share version: %lu  ref_count: %u",
6879
 
                          share->version, share->ref_count));
6880
6565
      share->version= 0;                          // Mark for delete
6881
6566
      if (share->ref_count == 0)
6882
6567
      {
6892
6577
        reopen their tables
6893
6578
      */
6894
6579
      broadcast_refresh();
6895
 
      DBUG_PRINT("info", ("Waiting for refresh signal"));
6896
6580
      if (!(flags & RTFC_CHECK_KILLED_FLAG) || !thd->killed)
6897
6581
      {
6898
6582
        dropping_tables++;
6920
6604
    }
6921
6605
    break;
6922
6606
  }
6923
 
  DBUG_RETURN(result);
 
6607
  return(result);
6924
6608
}
6925
6609
 
6926
6610
 
6958
6642
open_system_tables_for_read(THD *thd, TABLE_LIST *table_list,
6959
6643
                            Open_tables_state *backup)
6960
6644
{
6961
 
  DBUG_ENTER("open_system_tables_for_read");
6962
 
 
6963
6645
  thd->reset_n_backup_open_tables_state(backup);
6964
6646
 
6965
6647
  uint count= 0;
6971
6653
    if (!table)
6972
6654
      goto error;
6973
6655
 
6974
 
    DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_SYSTEM);
 
6656
    assert(table->s->table_category == TABLE_CATEGORY_SYSTEM);
6975
6657
 
6976
6658
    table->use_all_columns();
6977
6659
    table->reginfo.lock_type= tables->lock_type;
6989
6671
                                 MYSQL_LOCK_IGNORE_FLUSH, &not_used);
6990
6672
  }
6991
6673
  if (thd->lock)
6992
 
    DBUG_RETURN(false);
 
6674
    return(false);
6993
6675
 
6994
6676
error:
6995
6677
  close_system_tables(thd, backup);
6996
6678
 
6997
 
  DBUG_RETURN(true);
 
6679
  return(true);
6998
6680
}
6999
6681
 
7000
6682
 
7036
6718
TABLE *
7037
6719
open_system_table_for_update(THD *thd, TABLE_LIST *one_table)
7038
6720
{
7039
 
  DBUG_ENTER("open_system_table_for_update");
7040
 
 
7041
6721
  TABLE *table= open_ltable(thd, one_table, one_table->lock_type, 0);
7042
6722
  if (table)
7043
6723
  {
7044
 
    DBUG_ASSERT(table->s->table_category == TABLE_CATEGORY_SYSTEM);
 
6724
    assert(table->s->table_category == TABLE_CATEGORY_SYSTEM);
7045
6725
    table->use_all_columns();
7046
6726
  }
7047
6727
 
7048
 
  DBUG_RETURN(table);
 
6728
  return(table);
7049
6729
}
7050
6730
 
7051
6731
/**