~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to server/sql_select.cc

Removed/replaced BUG symbols and standardized TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
250
250
{
251
251
  bool res;
252
252
  register SELECT_LEX *select_lex = &lex->select_lex;
253
 
  DBUG_ENTER("handle_select");
254
253
  MYSQL_SELECT_START();
255
254
 
256
255
  if (select_lex->master_unit()->is_union() || 
280
279
                      setup_tables_done_option,
281
280
                      result, unit, select_lex);
282
281
  }
283
 
  DBUG_PRINT("info",("res: %d  report_error: %d", res,
284
 
                     thd->is_error()));
285
282
  res|= thd->is_error();
286
283
  if (unlikely(res))
287
284
    result->abort();
288
285
 
289
286
  MYSQL_SELECT_END();
290
 
  DBUG_RETURN(res);
 
287
  return(res);
291
288
}
292
289
 
293
290
 
416
413
{
417
414
  int res;
418
415
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
419
 
  DBUG_ENTER("setup_without_group");
420
416
 
421
417
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
422
418
  res= setup_conds(thd, tables, leaves, conds);
428
424
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
429
425
                          group, hidden_group_fields);
430
426
  thd->lex->allow_sum_func= save_allow_sum_func;
431
 
  DBUG_RETURN(res);
 
427
  return(res);
432
428
}
433
429
 
434
430
/*****************************************************************************
457
453
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
458
454
              SELECT_LEX_UNIT *unit_arg)
459
455
{
460
 
  DBUG_ENTER("JOIN::prepare");
461
 
 
462
456
  // to prevent double initialization on EXPLAIN
463
457
  if (optimized)
464
 
    DBUG_RETURN(0);
 
458
    return(0);
465
459
 
466
460
  conds= conds_init;
467
461
  order= order_init;
488
482
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
489
483
                                    tables_list, &select_lex->leaf_tables,
490
484
                                    false))
491
 
      DBUG_RETURN(-1);
 
485
      return(-1);
492
486
 
493
487
  TABLE_LIST *table_ptr;
494
488
  for (table_ptr= select_lex->leaf_tables;
504
498
                          select_lex->leaf_tables, fields_list,
505
499
                          all_fields, &conds, order, group_list,
506
500
                          &hidden_group_fields))
507
 
    DBUG_RETURN(-1);                            /* purecov: inspected */
 
501
    return(-1);                         /* purecov: inspected */
508
502
 
509
503
  ref_pointer_array= *rref_pointer_array;
510
504
  
519
513
                          having->check_cols(1)));
520
514
    select_lex->having_fix_field= 0;
521
515
    if (having_fix_rc || thd->is_error())
522
 
      DBUG_RETURN(-1);                          /* purecov: inspected */
 
516
      return(-1);                               /* purecov: inspected */
523
517
    thd->lex->allow_sum_func= save_allow_sum_func;
524
518
  }
525
519
 
538
532
      if (subselect->substype() == Item_subselect::IN_SUBS)
539
533
        in_subs= (Item_in_subselect*)subselect;
540
534
 
541
 
      DBUG_PRINT("info", ("Checking if subq can be converted to semi-join"));
542
535
      /*
543
536
        Check if we're in subquery that is a candidate for flattening into a
544
537
        semi-join (which is done done in flatten_subqueries()). The
567
560
          do_semijoin &&
568
561
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
569
562
      {
570
 
        DBUG_PRINT("info", ("Subquery is semi-join conversion candidate"));
571
563
        {
572
564
          if (!in_subs->left_expr->fixed &&
573
565
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
574
566
          {
575
 
            DBUG_RETURN(-1);
 
567
            return(-1);
576
568
          }
577
569
          /*
578
570
            Check that the right part of the subselect contains no more than one
583
575
              ((Item_in_subselect*)subselect)->left_expr->cols()))
584
576
          {
585
577
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
586
 
            DBUG_RETURN(-1);
 
578
            return(-1);
587
579
          }
588
580
        }
589
581
 
593
585
      }
594
586
      else
595
587
      {
596
 
        DBUG_PRINT("info", ("Subquery can't be converted to semi-join"));
597
588
        bool do_materialize= !test(thd->variables.optimizer_switch &
598
589
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
599
590
        /*
642
633
            Item_subselect::RES_OK)
643
634
        {
644
635
          select_lex->fix_prepare_information(thd, &conds, &having);
645
 
          DBUG_RETURN((trans_res == Item_subselect::RES_ERROR));
 
636
          return((trans_res == Item_subselect::RES_ERROR));
646
637
        }
647
638
      }
648
639
    }
678
669
 
679
670
  if (select_lex->inner_refs_list.elements &&
680
671
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
681
 
    DBUG_RETURN(-1);
 
672
    return(-1);
682
673
 
683
674
  /*
684
675
    Check if there are references to un-aggregated columns when computing 
690
681
  {
691
682
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
692
683
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
693
 
    DBUG_RETURN(-1);
 
684
    return(-1);
694
685
  }
695
686
  {
696
687
    /* Caclulate the number of groups */
723
714
  if (alloc_func_list())
724
715
    goto err;
725
716
 
726
 
  DBUG_RETURN(0); // All OK
 
717
  return(0); // All OK
727
718
 
728
719
err:
729
 
  DBUG_RETURN(-1);                              /* purecov: inspected */
 
720
  return(-1);                           /* purecov: inspected */
730
721
}
731
722
 
732
723
 
996
987
  int cur_range= 0;
997
988
  uint i;
998
989
 
999
 
  DBUG_ENTER("setup_semijoin_dups_elimination");
1000
990
  /*
1001
991
    First pass: locate the duplicate-generating ranges and pick the strategies.
1002
992
  */
1174
1164
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1175
1165
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1176
1166
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1177
 
          DBUG_RETURN(true);
 
1167
          return(true);
1178
1168
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
1179
1169
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1180
1170
        sjtbl->rowid_len= jt_rowid_offset;
1207
1197
        jump_to= tab;
1208
1198
    }
1209
1199
  }
1210
 
  DBUG_RETURN(false);
 
1200
  return(false);
1211
1201
}
1212
1202
 
1213
1203
 
1241
1231
int
1242
1232
JOIN::optimize()
1243
1233
{
1244
 
  DBUG_ENTER("JOIN::optimize");
1245
1234
  // to prevent double initialization on EXPLAIN
1246
1235
  if (optimized)
1247
 
    DBUG_RETURN(0);
 
1236
    return(0);
1248
1237
  optimized= 1;
1249
1238
 
1250
1239
  thd_proc_info(thd, "optimizing");
1301
1290
  if (thd->is_error())
1302
1291
  {
1303
1292
    error= 1;
1304
 
    DBUG_PRINT("error",("Error from optimize_cond"));
1305
 
    DBUG_RETURN(1);
 
1293
    return(1);
1306
1294
  }
1307
1295
 
1308
1296
  {
1310
1298
    if (thd->is_error())
1311
1299
    {
1312
1300
      error= 1;
1313
 
      DBUG_PRINT("error",("Error from optimize_cond"));
1314
 
      DBUG_RETURN(1);
 
1301
      return(1);
1315
1302
    }
1316
1303
    if (select_lex->where)
1317
1304
      select_lex->cond_value= cond_value;
1321
1308
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
1322
1309
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1323
1310
    {                                           /* Impossible cond */
1324
 
      DBUG_PRINT("info", (having_value == Item::COND_FALSE ? 
1325
 
                            "Impossible HAVING" : "Impossible WHERE"));
1326
1311
      zero_result_cause=  having_value == Item::COND_FALSE ?
1327
1312
                           "Impossible HAVING" : "Impossible WHERE";
1328
1313
      error= 0;
1329
 
      DBUG_RETURN(0);
 
1314
      return(0);
1330
1315
    }
1331
1316
  }
1332
1317
 
1344
1329
    {
1345
1330
      if (res == HA_ERR_KEY_NOT_FOUND)
1346
1331
      {
1347
 
        DBUG_PRINT("info",("No matching min/max row"));
1348
1332
        zero_result_cause= "No matching min/max row";
1349
1333
        error=0;
1350
 
        DBUG_RETURN(0);
 
1334
        return(0);
1351
1335
      }
1352
1336
      if (res > 1)
1353
1337
      {
1354
1338
        error= res;
1355
 
        DBUG_PRINT("error",("Error from opt_sum_query"));
1356
 
        DBUG_RETURN(1);
 
1339
        return(1);
1357
1340
      }
1358
1341
      if (res < 0)
1359
1342
      {
1360
 
        DBUG_PRINT("info",("No matching min/max row"));
1361
1343
        zero_result_cause= "No matching min/max row";
1362
1344
        error=0;
1363
 
        DBUG_RETURN(0);
 
1345
        return(0);
1364
1346
      }
1365
 
      DBUG_PRINT("info",("Select tables optimized away"));
1366
1347
      zero_result_cause= "Select tables optimized away";
1367
1348
      tables_list= 0;                           // All tables resolved
1368
1349
      /*
1379
1360
      {
1380
1361
        COND *table_independent_conds=
1381
1362
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1382
 
        DBUG_EXECUTE("where",
1383
 
                     print_where(table_independent_conds,
1384
 
                                 "where after opt_sum_query()",
1385
 
                                 QT_ORDINARY););
1386
1363
        conds= table_independent_conds;
1387
1364
      }
1388
1365
    }
1389
1366
  }
1390
1367
  if (!tables_list)
1391
1368
  {
1392
 
    DBUG_PRINT("info",("No tables"));
1393
1369
    error= 0;
1394
 
    DBUG_RETURN(0);
 
1370
    return(0);
1395
1371
  }
1396
1372
  error= -1;                                    // Error is sent to client
1397
1373
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1401
1377
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1402
1378
      thd->is_fatal_error)
1403
1379
  {
1404
 
    DBUG_PRINT("error",("Error: make_join_statistics() failed"));
1405
 
    DBUG_RETURN(1);
 
1380
    return(1);
1406
1381
  }
1407
1382
 
1408
1383
  /* Remove distinct if only const tables */
1410
1385
  thd_proc_info(thd, "preparing");
1411
1386
  if (result->initialize_tables(this))
1412
1387
  {
1413
 
    DBUG_PRINT("error",("Error: initialize_tables() failed"));
1414
 
    DBUG_RETURN(1);                             // error == -1
 
1388
    return(1);                          // error == -1
1415
1389
  }
1416
1390
  if (const_table_map != found_const_table_map &&
1417
1391
      !(select_options & SELECT_DESCRIBE) &&
1420
1394
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1421
1395
  {
1422
1396
    zero_result_cause= "no matching row in const table";
1423
 
    DBUG_PRINT("error",("Error: %s", zero_result_cause));
1424
1397
    error= 0;
1425
 
    DBUG_RETURN(0);
 
1398
    return(0);
1426
1399
  }
1427
1400
  if (!(thd->options & OPTION_BIG_SELECTS) &&
1428
1401
      best_read > (double) thd->variables.max_join_size &&
1430
1403
  {                                             /* purecov: inspected */
1431
1404
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1432
1405
    error= -1;
1433
 
    DBUG_RETURN(1);
 
1406
    return(1);
1434
1407
  }
1435
1408
  if (const_tables && !thd->locked_tables &&
1436
1409
      !(select_options & SELECT_NO_UNLOCK))
1445
1418
  if (error)
1446
1419
  {                                             /* purecov: inspected */
1447
1420
    error= -1;                                  /* purecov: inspected */
1448
 
    DBUG_PRINT("error",("Error: make_select() failed"));
1449
 
    DBUG_RETURN(1);
 
1421
    return(1);
1450
1422
  }
1451
1423
  
1452
1424
  reset_nj_counters(join_list);
1462
1434
  {
1463
1435
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1464
1436
    conds->update_used_tables();
1465
 
    DBUG_EXECUTE("where",
1466
 
                 print_where(conds,
1467
 
                             "after substitute_best_equal",
1468
 
                             QT_ORDINARY););
1469
1437
  }
1470
1438
 
1471
1439
  /*
1493
1461
  {
1494
1462
    zero_result_cause=
1495
1463
      "Impossible WHERE noticed after reading const tables";
1496
 
    DBUG_RETURN(0);                             // error == 0
 
1464
    return(0);                          // error == 0
1497
1465
  }
1498
1466
 
1499
1467
  error= -1;                                    /* if goto err */
1505
1473
    if (thd->is_error())
1506
1474
    {
1507
1475
      error= 1;
1508
 
      DBUG_PRINT("error",("Error from remove_const"));
1509
 
      DBUG_RETURN(1);
 
1476
      return(1);
1510
1477
    }
1511
1478
 
1512
1479
    /*
1634
1601
        group_list= 0;
1635
1602
    }
1636
1603
    else if (thd->is_fatal_error)                       // End of memory
1637
 
      DBUG_RETURN(1);
 
1604
      return(1);
1638
1605
  }
1639
1606
  simple_group= 0;
1640
1607
  {
1645
1612
    if (thd->is_error())
1646
1613
    {
1647
1614
      error= 1;
1648
 
      DBUG_PRINT("error",("Error from remove_const"));
1649
 
      DBUG_RETURN(1);
 
1615
      return(1);
1650
1616
    }
1651
1617
    if (old_group_list && !group_list)
1652
1618
      select_distinct= 0;
1700
1666
 
1701
1667
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1702
1668
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1703
 
    DBUG_RETURN(1);
 
1669
    return(1);
1704
1670
 
1705
1671
  /* Create all structures needed for materialized subquery execution. */
1706
1672
  if (setup_subquery_materialization())
1707
 
    DBUG_RETURN(1);
 
1673
    return(1);
1708
1674
 
1709
1675
  /*
1710
1676
    is this simple IN subquery?
1724
1690
        save_index_subquery_explain_info(join_tab, where);
1725
1691
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
1726
1692
        error= 0;
1727
 
        DBUG_RETURN(unit->item->
 
1693
        return(unit->item->
1728
1694
                    change_engine(new
1729
1695
                                  subselect_uniquesubquery_engine(thd,
1730
1696
                                                                  join_tab,
1738
1704
        save_index_subquery_explain_info(join_tab, where);
1739
1705
        join_tab[0].type= JT_INDEX_SUBQUERY;
1740
1706
        error= 0;
1741
 
        DBUG_RETURN(unit->item->
 
1707
        return(unit->item->
1742
1708
                    change_engine(new
1743
1709
                                  subselect_indexsubquery_engine(thd,
1744
1710
                                                                 join_tab,
1755
1721
      error= 0;
1756
1722
      conds= remove_additional_cond(conds);
1757
1723
      save_index_subquery_explain_info(join_tab, conds);
1758
 
      DBUG_RETURN(unit->item->
 
1724
      return(unit->item->
1759
1725
                  change_engine(new subselect_indexsubquery_engine(thd,
1760
1726
                                                                   join_tab,
1761
1727
                                                                   unit->item,
1777
1743
      join_tab[i].table->prepare_for_position();
1778
1744
  }
1779
1745
 
1780
 
  DBUG_EXECUTE("info",TEST_join(this););
1781
 
 
1782
1746
  if (const_tables != tables)
1783
1747
  {
1784
1748
    /*
1793
1757
        ((order && simple_order) || (group_list && simple_group)))
1794
1758
    {
1795
1759
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1796
 
        DBUG_RETURN(1);
 
1760
        return(1);
1797
1761
      }
1798
1762
    }
1799
1763
    
1832
1796
  if (select_options & SELECT_DESCRIBE)
1833
1797
  {
1834
1798
    error= 0;
1835
 
    DBUG_RETURN(0);
 
1799
    return(0);
1836
1800
  }
1837
1801
  having= 0;
1838
1802
 
1853
1817
  /* Create a tmp table if distinct or if the sort is too complicated */
1854
1818
  if (need_tmp)
1855
1819
  {
1856
 
    DBUG_PRINT("info",("Creating tmp table"));
1857
1820
    thd_proc_info(thd, "Creating tmp table");
1858
1821
 
1859
1822
    init_items_ref_array();
1882
1845
                           tmp_rows_limit,
1883
1846
                           (char *) "")))
1884
1847
                {
1885
 
      DBUG_RETURN(1);
 
1848
      return(1);
1886
1849
    }
1887
1850
 
1888
1851
    /*
1902
1865
    /* if group or order on first table, sort first */
1903
1866
    if (group_list && simple_group)
1904
1867
    {
1905
 
      DBUG_PRINT("info",("Sorting for group"));
1906
1868
      thd_proc_info(thd, "Sorting for group");
1907
1869
      if (create_sort_index(thd, this, group_list,
1908
1870
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
1910
1872
          make_sum_func_list(all_fields, fields_list, 1) ||
1911
1873
          setup_sum_funcs(thd, sum_funcs))
1912
1874
      {
1913
 
        DBUG_RETURN(1);
 
1875
        return(1);
1914
1876
      }
1915
1877
      group_list=0;
1916
1878
    }
1919
1881
      if (make_sum_func_list(all_fields, fields_list, 0) ||
1920
1882
          setup_sum_funcs(thd, sum_funcs))
1921
1883
      {
1922
 
        DBUG_RETURN(1);
 
1884
        return(1);
1923
1885
      }
1924
1886
 
1925
1887
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1926
1888
      {
1927
 
        DBUG_PRINT("info",("Sorting for order"));
1928
1889
        thd_proc_info(thd, "Sorting for order");
1929
1890
        if (create_sort_index(thd, this, order,
1930
1891
                              HA_POS_ERROR, HA_POS_ERROR, true))
1931
1892
        {
1932
 
          DBUG_RETURN(1);
 
1893
          return(1);
1933
1894
        }
1934
1895
        order=0;
1935
1896
      }
1969
1930
    */
1970
1931
    if (select_lex->uncacheable && !is_top_level_join() &&
1971
1932
        init_save_join_tab())
1972
 
      DBUG_RETURN(-1);                         /* purecov: inspected */
 
1933
      return(-1);                         /* purecov: inspected */
1973
1934
  }
1974
1935
 
1975
1936
  error= 0;
1976
 
  DBUG_RETURN(0);
 
1937
  return(0);
1977
1938
}
1978
1939
 
1979
1940
 
1989
1950
int
1990
1951
JOIN::reinit()
1991
1952
{
1992
 
  DBUG_ENTER("JOIN::reinit");
1993
 
 
1994
1953
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1995
1954
                                    select_lex->offset_limit->val_uint() :
1996
1955
                                    0ULL);
2028
1987
      func->clear();
2029
1988
  }
2030
1989
 
2031
 
  DBUG_RETURN(0);
 
1990
  return(0);
2032
1991
}
2033
1992
 
2034
1993
/**
2082
2041
{
2083
2042
  List<Item> *columns_list= &fields_list;
2084
2043
  int      tmp_error;
2085
 
  DBUG_ENTER("JOIN::exec");
2086
2044
 
2087
2045
  thd_proc_info(thd, "executing");
2088
2046
  error= 0;
2124
2082
    /* Single select (without union) always returns 0 or 1 row */
2125
2083
    thd->limit_found_rows= send_records;
2126
2084
    thd->examined_row_count= 0;
2127
 
    DBUG_VOID_RETURN;
 
2085
    return;
2128
2086
  }
2129
2087
  /*
2130
2088
    Don't reset the found rows count if there're no tables as
2142
2100
                            select_options,
2143
2101
                            zero_result_cause,
2144
2102
                            having);
2145
 
    DBUG_VOID_RETURN;
 
2103
    return;
2146
2104
  }
2147
2105
 
2148
2106
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2149
2107
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2150
 
    DBUG_VOID_RETURN;
 
2108
    return;
2151
2109
 
2152
2110
  if (select_options & SELECT_DESCRIBE)
2153
2111
  {
2180
2138
                    order != 0 && !skip_sort_order,
2181
2139
                    select_distinct,
2182
2140
                    !tables ? "No tables used" : NullS);
2183
 
    DBUG_VOID_RETURN;
 
2141
    return;
2184
2142
  }
2185
2143
 
2186
2144
  JOIN *curr_join= this;
2211
2169
 
2212
2170
    /* Copy data to the temporary table */
2213
2171
    thd_proc_info(thd, "Copying to tmp table");
2214
 
    DBUG_PRINT("info", ("%s", thd->proc_info));
2215
2172
    if (!curr_join->sort_and_group &&
2216
2173
        curr_join->const_tables != curr_join->tables)
2217
2174
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2218
2175
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2219
2176
    {
2220
2177
      error= tmp_error;
2221
 
      DBUG_VOID_RETURN;
 
2178
      return;
2222
2179
    }
2223
2180
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2224
2181
    
2235
2192
        if (change_to_use_tmp_fields(thd, items1,
2236
2193
                                     tmp_fields_list1, tmp_all_fields1,
2237
2194
                                     fields_list.elements, all_fields))
2238
 
          DBUG_VOID_RETURN;
 
2195
          return;
2239
2196
      }
2240
2197
      else
2241
2198
      {
2242
2199
        if (change_refs_to_tmp_fields(thd, items1,
2243
2200
                                      tmp_fields_list1, tmp_all_fields1,
2244
2201
                                      fields_list.elements, all_fields))
2245
 
          DBUG_VOID_RETURN;
 
2202
          return;
2246
2203
      }
2247
2204
      curr_join->tmp_all_fields1= tmp_all_fields1;
2248
2205
      curr_join->tmp_fields_list1= tmp_fields_list1;
2284
2241
 
2285
2242
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
2286
2243
    {                                   /* Must copy to another table */
2287
 
      DBUG_PRINT("info",("Creating group table"));
2288
 
      
2289
2244
      /* Free first data from old join */
2290
2245
      curr_join->join_free();
2291
2246
      if (make_simple_join(curr_join, curr_tmp_table))
2292
 
        DBUG_VOID_RETURN;
 
2247
        return;
2293
2248
      calc_group_buffer(curr_join, group_list);
2294
2249
      count_field_types(select_lex, &curr_join->tmp_table_param,
2295
2250
                        curr_join->tmp_all_fields1,
2323
2278
                                                1, curr_join->select_options,
2324
2279
                                                HA_POS_ERROR,
2325
2280
                                                (char *) "")))
2326
 
          DBUG_VOID_RETURN;
 
2281
          return;
2327
2282
        curr_join->exec_tmp_table2= exec_tmp_table2;
2328
2283
      }
2329
2284
      if (curr_join->group_list)
2331
2286
        thd_proc_info(thd, "Creating sort index");
2332
2287
        if (curr_join->join_tab == join_tab && save_join_tab())
2333
2288
        {
2334
 
          DBUG_VOID_RETURN;
 
2289
          return;
2335
2290
        }
2336
2291
        if (create_sort_index(thd, curr_join, curr_join->group_list,
2337
2292
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
2338
2293
            make_group_fields(this, curr_join))
2339
2294
        {
2340
 
          DBUG_VOID_RETURN;
 
2295
          return;
2341
2296
        }
2342
2297
        sortorder= curr_join->sortorder;
2343
2298
      }
2344
2299
      
2345
2300
      thd_proc_info(thd, "Copying to group table");
2346
 
      DBUG_PRINT("info", ("%s", thd->proc_info));
2347
2301
      tmp_error= -1;
2348
2302
      if (curr_join != this)
2349
2303
      {
2361
2315
      }
2362
2316
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2363
2317
                                        1, true))
2364
 
        DBUG_VOID_RETURN;
 
2318
        return;
2365
2319
      curr_join->group_list= 0;
2366
2320
      if (!curr_join->sort_and_group &&
2367
2321
          curr_join->const_tables != curr_join->tables)
2370
2324
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2371
2325
      {
2372
2326
        error= tmp_error;
2373
 
        DBUG_VOID_RETURN;
 
2327
        return;
2374
2328
      }
2375
2329
      end_read_record(&curr_join->join_tab->read_record);
2376
2330
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2383
2337
        if (change_to_use_tmp_fields(thd, items2,
2384
2338
                                     tmp_fields_list2, tmp_all_fields2, 
2385
2339
                                     fields_list.elements, tmp_all_fields1))
2386
 
          DBUG_VOID_RETURN;
 
2340
          return;
2387
2341
        curr_join->tmp_fields_list2= tmp_fields_list2;
2388
2342
        curr_join->tmp_all_fields2= tmp_all_fields2;
2389
2343
      }
2405
2359
        curr_join->tmp_having->update_used_tables();
2406
2360
      if (remove_duplicates(curr_join, curr_tmp_table,
2407
2361
                            *curr_fields_list, curr_join->tmp_having))
2408
 
        DBUG_VOID_RETURN;
 
2362
        return;
2409
2363
      curr_join->tmp_having=0;
2410
2364
      curr_join->select_distinct=0;
2411
2365
    }
2412
2366
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2413
2367
    if (make_simple_join(curr_join, curr_tmp_table))
2414
 
      DBUG_VOID_RETURN;
 
2368
      return;
2415
2369
    calc_group_buffer(curr_join, curr_join->group_list);
2416
2370
    count_field_types(select_lex, &curr_join->tmp_table_param, 
2417
2371
                      *curr_all_fields, 0);
2422
2376
  {
2423
2377
    if (make_group_fields(this, curr_join))
2424
2378
    {
2425
 
      DBUG_VOID_RETURN;
 
2379
      return;
2426
2380
    }
2427
2381
    if (!items3)
2428
2382
    {
2454
2408
                                      1, true) || 
2455
2409
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2456
2410
        thd->is_fatal_error)
2457
 
      DBUG_VOID_RETURN;
 
2411
      return;
2458
2412
  }
2459
2413
  if (curr_join->group_list || curr_join->order)
2460
2414
  {
2461
 
    DBUG_PRINT("info",("Sorting for send_fields"));
2462
2415
    thd_proc_info(thd, "Sorting result");
2463
2416
    /* If we have already done the group, add HAVING to sorted table */
2464
2417
    if (curr_join->tmp_having && ! curr_join->group_list && 
2477
2430
      {
2478
2431
        if (!curr_table->select)
2479
2432
          if (!(curr_table->select= new SQL_SELECT))
2480
 
            DBUG_VOID_RETURN;
 
2433
            return;
2481
2434
        if (!curr_table->select->cond)
2482
2435
          curr_table->select->cond= sort_table_cond;
2483
2436
        else                                    // This should never happen
2485
2438
          if (!(curr_table->select->cond=
2486
2439
                new Item_cond_and(curr_table->select->cond,
2487
2440
                                  sort_table_cond)))
2488
 
            DBUG_VOID_RETURN;
 
2441
            return;
2489
2442
          /*
2490
2443
            Item_cond_and do not need fix_fields for execution, its parameters
2491
2444
            are fixed or do not need fix_fields, too
2494
2447
        }
2495
2448
        curr_table->select_cond= curr_table->select->cond;
2496
2449
        curr_table->select_cond->top_level_item();
2497
 
        DBUG_EXECUTE("where",print_where(curr_table->select->cond,
2498
 
                                         "select and having",
2499
 
                                         QT_ORDINARY););
2500
2450
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2501
2451
                                                   ~ (table_map) 0,
2502
2452
                                                   ~used_tables, 0);
2503
 
        DBUG_EXECUTE("where",print_where(curr_join->tmp_having,
2504
 
                                         "having after sort",
2505
 
                                         QT_ORDINARY););
2506
2453
      }
2507
2454
    }
2508
2455
    {
2533
2480
      }
2534
2481
      if (curr_join->join_tab == join_tab && save_join_tab())
2535
2482
      {
2536
 
        DBUG_VOID_RETURN;
 
2483
        return;
2537
2484
      }
2538
2485
      /*
2539
2486
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2551
2498
                            (select_options & OPTION_FOUND_ROWS ?
2552
2499
                             HA_POS_ERROR : unit->select_limit_cnt),
2553
2500
                            curr_join->group_list ? true : false))
2554
 
        DBUG_VOID_RETURN;
 
2501
        return;
2555
2502
      sortorder= curr_join->sortorder;
2556
2503
      if (curr_join->const_tables != curr_join->tables &&
2557
2504
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2569
2516
  if (thd->is_error())
2570
2517
  {
2571
2518
    error= thd->is_error();
2572
 
    DBUG_VOID_RETURN;
 
2519
    return;
2573
2520
  }
2574
2521
  curr_join->having= curr_join->tmp_having;
2575
2522
  curr_join->fields= curr_fields_list;
2576
2523
 
2577
2524
  {
2578
2525
    thd_proc_info(thd, "Sending data");
2579
 
    DBUG_PRINT("info", ("%s", thd->proc_info));
2580
2526
    result->send_fields(*curr_fields_list,
2581
2527
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2582
2528
    error= do_select(curr_join, curr_fields_list, NULL);
2585
2531
 
2586
2532
  /* Accumulate the counts from all join iterations of all join parts. */
2587
2533
  thd->examined_row_count+= curr_join->examined_rows;
2588
 
  DBUG_PRINT("counts", ("thd->examined_row_count: %lu",
2589
 
                        (ulong) thd->examined_row_count));
2590
2534
 
2591
2535
  /* 
2592
2536
    With EXPLAIN EXTENDED we have to restore original ref_array
2598
2542
      select_lex->linkage == DERIVED_TABLE_TYPE)      
2599
2543
    set_items_ref_array(items0);
2600
2544
 
2601
 
  DBUG_VOID_RETURN;
 
2545
  return;
2602
2546
}
2603
2547
 
2604
2548
 
2612
2556
int
2613
2557
JOIN::destroy()
2614
2558
{
2615
 
  DBUG_ENTER("JOIN::destroy");
2616
2559
  select_lex->join= 0;
2617
2560
 
2618
2561
  if (tmp_join)
2625
2568
    }
2626
2569
    tmp_join->tmp_join= 0;
2627
2570
    tmp_table_param.copy_field=0;
2628
 
    DBUG_RETURN(tmp_join->destroy());
 
2571
    return(tmp_join->destroy());
2629
2572
  }
2630
2573
  cond_equal= 0;
2631
2574
 
2636
2579
    free_tmp_table(thd, exec_tmp_table2);
2637
2580
  delete select;
2638
2581
  delete_dynamic(&keyuse);
2639
 
  DBUG_RETURN(error);
 
2582
  return(error);
2640
2583
}
2641
2584
 
2642
2585
 
2695
2638
{
2696
2639
  bool err;
2697
2640
  bool free_join= 1;
2698
 
  DBUG_ENTER("mysql_select");
2699
2641
 
2700
2642
  select_lex->context.resolve_in_select_list= true;
2701
2643
  JOIN *join;
2714
2656
        //here is EXPLAIN of subselect or derived table
2715
2657
        if (join->change_result(result))
2716
2658
        {
2717
 
          DBUG_RETURN(true);
 
2659
          return(true);
2718
2660
        }
2719
2661
      }
2720
2662
      else
2733
2675
  else
2734
2676
  {
2735
2677
    if (!(join= new JOIN(thd, fields, select_options, result)))
2736
 
        DBUG_RETURN(true);
 
2678
        return(true);
2737
2679
    thd_proc_info(thd, "init");
2738
2680
    thd->used_tables=0;                         // Updated by setup_fields
2739
2681
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2779
2721
  {
2780
2722
    thd_proc_info(thd, "end");
2781
2723
    err|= select_lex->cleanup();
2782
 
    DBUG_RETURN(err || thd->is_error());
 
2724
    return(err || thd->is_error());
2783
2725
  }
2784
 
  DBUG_RETURN(join->error);
 
2726
  return(join->error);
2785
2727
}
2786
2728
 
2787
2729
 
2859
2801
  TABLE_LIST *emb_tbl_nest= NULL;
2860
2802
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2861
2803
  THD *thd= parent_join->thd;
2862
 
  DBUG_ENTER("convert_subq_to_sj");
2863
2804
 
2864
2805
  /*
2865
2806
    1. Find out where to put the predicate into.
2918
2859
      */
2919
2860
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2920
2861
      {
2921
 
        DBUG_RETURN(true);
 
2862
        return(true);
2922
2863
      }
2923
2864
      wrap_nest->embedding= outer_tbl->embedding;
2924
2865
      wrap_nest->join_list= outer_tbl->join_list;
2963
2904
  NESTED_JOIN *nested_join;
2964
2905
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2965
2906
  {
2966
 
    DBUG_RETURN(true);
 
2907
    return(true);
2967
2908
  }
2968
2909
  nested_join= sj_nest->nested_join;
2969
2910
 
3043
2984
  thd->lex->current_select=subq_lex;
3044
2985
  if (!subq_pred->left_expr->fixed &&
3045
2986
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
3046
 
    DBUG_RETURN(true);
 
2987
    return(true);
3047
2988
  thd->lex->current_select=save_lex;
3048
2989
 
3049
2990
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
3105
3046
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3106
3047
  subq_lex->master_unit()->exclude_level();
3107
3048
 
3108
 
  DBUG_EXECUTE("where",
3109
 
               print_where(sj_nest->sj_on_expr,"SJ-EXPR", QT_ORDINARY););
3110
 
 
3111
3049
  /* Inject sj_on_expr into the parent's WHERE or ON */
3112
3050
  if (emb_tbl_nest)
3113
3051
  {
3123
3061
    parent_join->select_lex->where= parent_join->conds;
3124
3062
  }
3125
3063
 
3126
 
  DBUG_RETURN(false);
 
3064
  return(false);
3127
3065
}
3128
3066
 
3129
3067
 
3145
3083
{
3146
3084
  Item_in_subselect **in_subq;
3147
3085
  Item_in_subselect **in_subq_end;
3148
 
  DBUG_ENTER("JOIN::flatten_subqueries");
3149
3086
 
3150
3087
  if (sj_subselects.elements() == 0)
3151
 
    DBUG_RETURN(false);
 
3088
    return(false);
3152
3089
 
3153
3090
  /* 1. Fix children subqueries */
3154
3091
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
3157
3094
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3158
3095
    child_join->outer_tables = child_join->tables;
3159
3096
    if (child_join->flatten_subqueries())
3160
 
      DBUG_RETURN(true);
 
3097
      return(true);
3161
3098
    (*in_subq)->sj_convert_priority= 
3162
3099
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3163
3100
  }
3178
3115
       in_subq++)
3179
3116
  {
3180
3117
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3181
 
      DBUG_RETURN(true);
 
3118
      return(true);
3182
3119
  }
3183
3120
 
3184
3121
  for (in_subq= sj_subselects.front(); 
3187
3124
       in_subq++)
3188
3125
  {
3189
3126
    if (convert_subq_to_sj(this, *in_subq))
3190
 
      DBUG_RETURN(true);
 
3127
      return(true);
3191
3128
  }
3192
3129
 
3193
3130
  /* 3. Finalize those we didn't convert */
3199
3136
    (*in_subq)->fixed= 0;
3200
3137
    res= (*in_subq)->select_transformer(child_join);
3201
3138
    if (res == Item_subselect::RES_ERROR)
3202
 
      DBUG_RETURN(true);
 
3139
      return(true);
3203
3140
 
3204
3141
    (*in_subq)->changed= 1;
3205
3142
    (*in_subq)->fixed= 1;
3207
3144
    Item *substitute= (*in_subq)->substitution;
3208
3145
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
3209
3146
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3210
 
      DBUG_RETURN(true);
 
3147
      return(true);
3211
3148
 
3212
3149
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3213
 
    //  DBUG_RETURN(true);
 
3150
    //  return(true);
3214
3151
  }
3215
3152
  sj_subselects.clear();
3216
 
  DBUG_RETURN(false);
 
3153
  return(false);
3217
3154
}
3218
3155
 
3219
3156
 
3366
3303
int pull_out_semijoin_tables(JOIN *join)
3367
3304
{
3368
3305
  TABLE_LIST *sj_nest;
3369
 
  DBUG_ENTER("pull_out_semijoin_tables");
3370
3306
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3371
3307
   
3372
3308
  /* Try pulling out of the each of the semi-joins */
3385
3321
        if (tbl->table->map & join->const_table_map)
3386
3322
        {
3387
3323
          pulled_tables |= tbl->table->map;
3388
 
          DBUG_PRINT("info", ("Table %s pulled out (reason: constant)",
3389
 
                              tbl->table->alias));
3390
3324
        }
3391
3325
      }
3392
3326
    }
3411
3345
          {
3412
3346
            pulled_a_table= true;
3413
3347
            pulled_tables |= tbl->table->map;
3414
 
            DBUG_PRINT("info", ("Table %s pulled out (reason: func dep)",
3415
 
                                tbl->table->alias));
3416
3348
          }
3417
3349
        }
3418
3350
      }
3422
3354
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
3423
3355
    {
3424
3356
      (sj_nest)->sj_inner_tables= 0;
3425
 
      DBUG_PRINT("info", ("All semi-join nest tables were pulled out"));
3426
3357
      while ((tbl= child_li++))
3427
3358
      {
3428
3359
        if (tbl->table)
3447
3378
      }
3448
3379
    }
3449
3380
  }
3450
 
  DBUG_RETURN(0);
 
3381
  return(0);
3451
3382
}
3452
3383
 
3453
3384
/*****************************************************************************
3461
3392
                                      const key_map *keys,ha_rows limit)
3462
3393
{
3463
3394
  int error;
3464
 
  DBUG_ENTER("get_quick_record_count");
3465
3395
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
3466
 
    DBUG_RETURN(0);                           // Fatal error flag is set
 
3396
    return(0);                           // Fatal error flag is set
3467
3397
  if (select)
3468
3398
  {
3469
3399
    select->head=table;
3470
3400
    table->reginfo.impossible_range=0;
3471
3401
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
3472
3402
                                          limit, 0, false)) == 1)
3473
 
      DBUG_RETURN(select->quick->records);
 
3403
      return(select->quick->records);
3474
3404
    if (error == -1)
3475
3405
    {
3476
3406
      table->reginfo.impossible_range=1;
3477
 
      DBUG_RETURN(0);
 
3407
      return(0);
3478
3408
    }
3479
 
    DBUG_PRINT("warning",("Couldn't use record count on const keypart"));
3480
3409
  }
3481
 
  DBUG_RETURN(HA_POS_ERROR);                    /* This shouldn't happend */
 
3410
  return(HA_POS_ERROR);                 /* This shouldn't happend */
3482
3411
}
3483
3412
 
3484
3413
/*
3519
3448
  table_map outer_join=0;
3520
3449
  SARGABLE_PARAM *sargables= 0;
3521
3450
  JOIN_TAB *stat_vector[MAX_TABLES+1];
3522
 
  DBUG_ENTER("make_join_statistics");
3523
3451
 
3524
3452
  table_count=join->tables;
3525
3453
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3526
3454
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3527
3455
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3528
3456
  if (!stat || !stat_ref || !table_vector)
3529
 
    DBUG_RETURN(1);                             // Eom /* purecov: inspected */
 
3457
    return(1);                          // Eom /* purecov: inspected */
3530
3458
 
3531
3459
  join->best_ref=stat_vector;
3532
3460
 
3550
3478
    if(error)
3551
3479
    {
3552
3480
        table->file->print_error(error, MYF(0));
3553
 
        DBUG_RETURN(1);
 
3481
        return(1);
3554
3482
    }
3555
3483
    table->quick_keys.clear_all();
3556
3484
    table->reginfo.join_tab=s;
3636
3564
      {
3637
3565
        join->tables=0;                 // Don't use join->table
3638
3566
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3639
 
        DBUG_RETURN(1);
 
3567
        return(1);
3640
3568
      }
3641
3569
      s->key_dependent= s->dependent;
3642
3570
    }
3646
3574
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3647
3575
                            conds, join->cond_equal,
3648
3576
                            ~outer_join, join->select_lex, &sargables))
3649
 
      DBUG_RETURN(1);
 
3577
      return(1);
3650
3578
 
3651
3579
  /* Read tables with 0 or 1 rows (system tables) */
3652
3580
  join->const_table_map= 0;
3662
3590
    if ((tmp=join_read_const_table(s, p_pos)))
3663
3591
    {
3664
3592
      if (tmp > 0)
3665
 
        DBUG_RETURN(1);                 // Fatal error
 
3593
        return(1);                      // Fatal error
3666
3594
    }
3667
3595
    else
3668
3596
      found_const_table_map|= s->table->map;
3734
3662
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3735
3663
          {
3736
3664
            if (tmp > 0)
3737
 
              DBUG_RETURN(1);                   // Fatal error
 
3665
              return(1);                        // Fatal error
3738
3666
          }
3739
3667
          else
3740
3668
            found_const_table_map|= table->map;
3783
3711
                set_position(join,const_count++,s,start_keyuse);
3784
3712
                if (create_ref_for_key(join, s, start_keyuse,
3785
3713
                                       found_const_table_map))
3786
 
                  DBUG_RETURN(1);
 
3714
                  return(1);
3787
3715
                if ((tmp=join_read_const_table(s,
3788
3716
                                               join->positions+const_count-1)))
3789
3717
                {
3790
3718
                  if (tmp > 0)
3791
 
                    DBUG_RETURN(1);                     // Fatal error
 
3719
                    return(1);                  // Fatal error
3792
3720
                }
3793
3721
                else
3794
3722
                  found_const_table_map|= table->map;
3826
3754
  }
3827
3755
 
3828
3756
  if (pull_out_semijoin_tables(join))
3829
 
    DBUG_RETURN(true);
 
3757
    return(true);
3830
3758
 
3831
3759
  /* Calc how many (possible) matched records in each table */
3832
3760
 
3868
3796
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
3869
3797
                          1, &error);
3870
3798
      if (!select)
3871
 
        DBUG_RETURN(1);
 
3799
        return(1);
3872
3800
      records= get_quick_record_count(join->thd, select, s->table,
3873
3801
                                      &s->const_keys, join->row_limit);
3874
3802
      s->quick=select->quick;
3914
3842
  {
3915
3843
    optimize_keyuse(join, keyuse_array);
3916
3844
    if (choose_plan(join, all_table_map & ~join->const_table_map))
3917
 
      DBUG_RETURN(true);
 
3845
      return(true);
3918
3846
  }
3919
3847
  else
3920
3848
  {
3923
3851
    join->best_read=1.0;
3924
3852
  }
3925
3853
  /* Generate an execution plan from the found optimal join order. */
3926
 
  DBUG_RETURN(join->thd->killed || get_best_combination(join));
 
3854
  return(join->thd->killed || get_best_combination(join));
3927
3855
}
3928
3856
 
3929
3857
 
4116
4044
        !field->table->maybe_null || field->null_ptr)
4117
4045
      return;                                   // Not a key. Skip it
4118
4046
    exists_optimize= KEY_OPTIMIZE_EXISTS;
4119
 
    DBUG_ASSERT(num_values == 1);
 
4047
    assert(num_values == 1);
4120
4048
  }
4121
4049
  else
4122
4050
  {
4228
4156
    For the moment eq_func is always true. This slot is reserved for future
4229
4157
    extensions where we want to remembers other things than just eq comparisons
4230
4158
  */
4231
 
  DBUG_ASSERT(eq_func);
 
4159
  assert(eq_func);
4232
4160
  /* Store possible eq field */
4233
4161
  (*key_fields)->field=         field;
4234
4162
  (*key_fields)->eq_func=       eq_func;
4389
4317
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4390
4318
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4391
4319
        values--;
4392
 
      DBUG_ASSERT(cond_func->functype() != Item_func::IN_FUNC ||
 
4320
      assert(cond_func->functype() != Item_func::IN_FUNC ||
4393
4321
                  cond_func->argument_count() != 2);
4394
4322
      add_key_equal_fields(key_fields, *and_level, cond_func,
4395
4323
                           (Item_field*) (cond_func->key_item()->real_item()),
4616
4544
  bool have_another = false;
4617
4545
  table_map tables= 0;
4618
4546
  TABLE_LIST *table;
4619
 
  DBUG_ASSERT(nested_join_table->nested_join);
 
4547
  assert(nested_join_table->nested_join);
4620
4548
 
4621
4549
  while ((table= li++) || (have_another && (li=li2, have_another=false,
4622
4550
                                            (table= li++))))
4811
4739
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4812
4740
    keyuse->elements=i;
4813
4741
  }
4814
 
  DBUG_EXECUTE("opt", print_keyuse_array(keyuse););
4815
4742
  return false;
4816
4743
}
4817
4744
 
5031
4958
  double tmp;
5032
4959
  ha_rows rec;
5033
4960
  uint best_is_sj_inside_out=    0;
5034
 
  DBUG_ENTER("best_access_path");
5035
4961
 
5036
4962
  if (s->keyuse)
5037
4963
  {                                            /* Use key if possible */
5169
5095
              check if all of them are covered.
5170
5096
            */
5171
5097
            sj_inside_out_scan= true;
5172
 
            DBUG_PRINT("info", ("Using sj InsideOut scan"));
5173
5098
            if (!n_fixed_parts)
5174
5099
            {
5175
5100
              /*
5611
5536
      join->unit->select_limit_cnt >= records)
5612
5537
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
5613
5538
 
5614
 
  DBUG_VOID_RETURN;
 
5539
  return;
5615
5540
}
5616
5541
 
5617
5542
 
5644
5569
  uint search_depth= join->thd->variables.optimizer_search_depth;
5645
5570
  uint prune_level=  join->thd->variables.optimizer_prune_level;
5646
5571
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5647
 
  DBUG_ENTER("choose_plan");
5648
5572
 
5649
5573
  join->cur_embedding_map= 0;
5650
5574
  reset_nj_counters(join->join_list);
5673
5597
      */
5674
5598
      join->best_read= DBL_MAX;
5675
5599
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5676
 
        DBUG_RETURN(true);
 
5600
        return(true);
5677
5601
    } 
5678
5602
    else
5679
5603
    {
5681
5605
        /* Automatically determine a reasonable value for 'search_depth' */
5682
5606
        search_depth= determine_search_depth(join);
5683
5607
      if (greedy_search(join, join_tables, search_depth, prune_level))
5684
 
        DBUG_RETURN(true);
 
5608
        return(true);
5685
5609
    }
5686
5610
  }
5687
5611
 
5693
5617
  */
5694
5618
  if (join->thd->lex->is_single_level_stmt())
5695
5619
    join->thd->status_var.last_query_cost= join->best_read;
5696
 
  DBUG_RETURN(false);
 
5620
  return(false);
5697
5621
}
5698
5622
 
5699
5623
 
5959
5883
  POSITION  best_pos;
5960
5884
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5961
5885
 
5962
 
  DBUG_ENTER("greedy_search");
5963
 
 
5964
5886
  /* number of tables that remain to be optimized */
5965
5887
  size_remain= my_count_bits(remaining_tables);
5966
5888
 
5969
5891
    join->best_read= DBL_MAX;
5970
5892
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5971
5893
                                         read_time, search_depth, prune_level))
5972
 
      DBUG_RETURN(true);
 
5894
      return(true);
5973
5895
 
5974
5896
    if (size_remain <= search_depth)
5975
5897
    {
5977
5899
        'join->best_positions' contains a complete optimal extension of the
5978
5900
        current partial QEP.
5979
5901
      */
5980
 
      DBUG_EXECUTE("opt", print_plan(join, join->tables,
5981
 
                                     record_count, read_time, read_time,
5982
 
                                     "optimal"););
5983
 
      DBUG_RETURN(false);
 
5902
      return(false);
5984
5903
    }
5985
5904
 
5986
5905
    /* select the first table in the optimal extension as most promising */
5998
5917
    JOIN_TAB *pos= join->best_ref[best_idx];
5999
5918
    while (pos && best_table != pos)
6000
5919
      pos= join->best_ref[++best_idx];
6001
 
    DBUG_ASSERT((pos != NULL)); // should always find 'best_table'
 
5920
    assert((pos != NULL)); // should always find 'best_table'
6002
5921
    /* move 'best_table' at the first free position in the array of joins */
6003
5922
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
6004
5923
 
6009
5928
    remaining_tables&= ~(best_table->table->map);
6010
5929
    --size_remain;
6011
5930
    ++idx;
6012
 
 
6013
 
    DBUG_EXECUTE("opt", print_plan(join, join->tables,
6014
 
                                   record_count, read_time, read_time,
6015
 
                                   "extended"););
6016
5931
  } while (true);
6017
5932
}
6018
5933
 
6143
6058
                                 uint      search_depth,
6144
6059
                                 uint      prune_level)
6145
6060
{
6146
 
  DBUG_ENTER("best_extension_by_limited_search");
6147
 
 
6148
6061
  THD *thd= join->thd;
6149
6062
  if (thd->killed)  // Abort
6150
 
    DBUG_RETURN(true);
6151
 
 
6152
 
  DBUG_EXECUTE("opt", print_plan(join, idx, read_time, record_count, idx,
6153
 
                                 "SOFAR:"););
 
6063
    return(true);
6154
6064
 
6155
6065
  /* 
6156
6066
     'join' is a partial plan with lower cost than the best plan so far,
6160
6070
  double best_record_count= DBL_MAX;
6161
6071
  double best_read_time=    DBL_MAX;
6162
6072
 
6163
 
  DBUG_EXECUTE("opt", print_plan(join, idx, record_count, read_time, read_time,
6164
 
                                "part_plan"););
6165
 
 
6166
6073
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6167
6074
  {
6168
6075
    table_map real_table_bit= s->table->map;
6191
6098
      if ((current_read_time +
6192
6099
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6193
6100
      {
6194
 
        DBUG_EXECUTE("opt", print_plan(join, idx+1,
6195
 
                                       current_record_count,
6196
 
                                       read_time,
6197
 
                                       (current_read_time +
6198
 
                                        current_record_count / 
6199
 
                                        (double) TIME_FOR_COMPARE),
6200
 
                                       "prune_by_cost"););
6201
6101
        restore_prev_nj_state(s);
6202
6102
        restore_prev_sj_state(remaining_tables, s);
6203
6103
        continue;
6225
6125
        }
6226
6126
        else
6227
6127
        {
6228
 
          DBUG_EXECUTE("opt", print_plan(join, idx+1,
6229
 
                                         current_record_count,
6230
 
                                         read_time,
6231
 
                                         current_read_time,
6232
 
                                         "pruned_by_heuristic"););
6233
6128
          restore_prev_nj_state(s);
6234
6129
          restore_prev_sj_state(remaining_tables, s);
6235
6130
          continue;
6246
6141
                                             current_read_time,
6247
6142
                                             search_depth - 1,
6248
6143
                                             prune_level))
6249
 
          DBUG_RETURN(true);
 
6144
          return(true);
6250
6145
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6251
6146
      }
6252
6147
      else
6266
6161
                 sizeof(POSITION) * (idx + 1));
6267
6162
          join->best_read= current_read_time - 0.001;
6268
6163
        }
6269
 
        DBUG_EXECUTE("opt", print_plan(join, idx+1,
6270
 
                                       current_record_count,
6271
 
                                       read_time,
6272
 
                                       current_read_time,
6273
 
                                       "full_plan"););
6274
6164
      }
6275
6165
      restore_prev_nj_state(s);
6276
6166
      restore_prev_sj_state(remaining_tables, s);
6277
6167
    }
6278
6168
  }
6279
 
  DBUG_RETURN(false);
 
6169
  return(false);
6280
6170
}
6281
6171
 
6282
6172
 
6293
6183
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6294
6184
          double read_time)
6295
6185
{
6296
 
  DBUG_ENTER("find_best");
6297
6186
  THD *thd= join->thd;
6298
6187
  if (thd->killed)
6299
 
    DBUG_RETURN(true);
 
6188
    return(true);
6300
6189
  if (!rest_tables)
6301
6190
  {
6302
 
    DBUG_PRINT("best",("read_time: %g  record_count: %g",read_time,
6303
 
                       record_count));
6304
 
 
6305
6191
    read_time+=record_count/(double) TIME_FOR_COMPARE;
6306
6192
    if (join->sort_by_table &&
6307
6193
        join->sort_by_table !=
6313
6199
             sizeof(POSITION)*idx);
6314
6200
      join->best_read= read_time - 0.001;
6315
6201
    }
6316
 
    DBUG_RETURN(false);
 
6202
    return(false);
6317
6203
  }
6318
6204
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6319
 
    DBUG_RETURN(false);                                 /* Found better before */
 
6205
    return(false);                                      /* Found better before */
6320
6206
 
6321
6207
  JOIN_TAB *s;
6322
6208
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6352
6238
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6353
6239
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6354
6240
                      current_record_count,current_read_time))
6355
 
          DBUG_RETURN(true);
 
6241
          return(true);
6356
6242
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6357
6243
      }
6358
6244
      restore_prev_nj_state(s);
6361
6247
        break;                          // Don't test all combinations
6362
6248
    }
6363
6249
  }
6364
 
  DBUG_RETURN(false);
 
6250
  return(false);
6365
6251
}
6366
6252
 
6367
6253
 
6524
6410
  KEYUSE *keyuse;
6525
6411
  uint table_count;
6526
6412
  THD *thd=join->thd;
6527
 
  DBUG_ENTER("get_best_combination");
6528
6413
 
6529
6414
  table_count=join->tables;
6530
6415
  if (!(join->join_tab=join_tab=
6531
6416
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6532
 
    DBUG_RETURN(true);
 
6417
    return(true);
6533
6418
 
6534
6419
  join->full_join=0;
6535
6420
 
6543
6428
    form->reginfo.join_tab=j;
6544
6429
    if (!*j->on_expr_ref)
6545
6430
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
6546
 
    DBUG_PRINT("info",("type: %d", j->type));
6547
6431
    if (j->type == JT_CONST)
6548
6432
      continue;                                 // Handled in make_join_stat..
6549
6433
 
6559
6443
        join->full_join=1;
6560
6444
    }
6561
6445
    else if (create_ref_for_key(join, j, keyuse, used_tables))
6562
 
      DBUG_RETURN(true);                        // Something went wrong
 
6446
      return(true);                        // Something went wrong
6563
6447
  }
6564
6448
 
6565
6449
  for (i=0 ; i < table_count ; i++)
6566
6450
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6567
6451
  update_depend_map(join);
6568
 
  DBUG_RETURN(0);
 
6452
  return(0);
6569
6453
}
6570
6454
 
6571
6455
 
6577
6461
  uint keyparts,length,key;
6578
6462
  TABLE *table;
6579
6463
  KEY *keyinfo;
6580
 
  DBUG_ENTER("create_ref_for_key");
6581
6464
 
6582
6465
  /*  Use best key from find_best */
6583
6466
  table=j->table;
6619
6502
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6620
6503
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
6621
6504
  {
6622
 
    DBUG_RETURN(true);
 
6505
    return(true);
6623
6506
  }
6624
6507
  j->ref.key_buff2=j->ref.key_buff+ALIGN_SIZE(length);
6625
6508
  j->ref.key_err=1;
6652
6535
                           maybe_null ?  key_buff : 0,
6653
6536
                           keyinfo->key_part[i].length, keyuse->val);
6654
6537
        if (thd->is_fatal_error)
6655
 
          DBUG_RETURN(true);
 
6538
          return(true);
6656
6539
        tmp.copy();
6657
6540
      }
6658
6541
      else
6693
6576
  }
6694
6577
  else
6695
6578
    j->type=JT_EQ_REF;
6696
 
  DBUG_RETURN(0);
 
6579
  return(0);
6697
6580
}
6698
6581
 
6699
6582
 
6768
6651
{
6769
6652
  TABLE **tableptr;
6770
6653
  JOIN_TAB *join_tab;
6771
 
  DBUG_ENTER("make_simple_join");
6772
6654
 
6773
6655
  /*
6774
6656
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
6777
6659
  if (!join->table_reexec)
6778
6660
  {
6779
6661
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
6780
 
      DBUG_RETURN(true);                        /* purecov: inspected */
 
6662
      return(true);                        /* purecov: inspected */
6781
6663
    if (join->tmp_join)
6782
6664
      join->tmp_join->table_reexec= join->table_reexec;
6783
6665
  }
6785
6667
  {
6786
6668
    if (!(join->join_tab_reexec=
6787
6669
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
6788
 
      DBUG_RETURN(true);                        /* purecov: inspected */
 
6670
      return(true);                        /* purecov: inspected */
6789
6671
    if (join->tmp_join)
6790
6672
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6791
6673
  }
6828
6710
  bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
6829
6711
  tmp_table->status=0;
6830
6712
  tmp_table->null_row=0;
6831
 
  DBUG_RETURN(false);
 
6713
  return(false);
6832
6714
}
6833
6715
 
6834
6716
 
6901
6783
 
6902
6784
static void add_not_null_conds(JOIN *join)
6903
6785
{
6904
 
  DBUG_ENTER("add_not_null_conds");
6905
6786
  for (uint i=join->const_tables ; i < join->tables ; i++)
6906
6787
  {
6907
6788
    JOIN_TAB *tab=join->join_tab+i;
6915
6796
        {
6916
6797
          Item *item= tab->ref.items[keypart];
6917
6798
          Item *notnull;
6918
 
          DBUG_ASSERT(item->type() == Item::FIELD_ITEM);
 
6799
          assert(item->type() == Item::FIELD_ITEM);
6919
6800
          Item_field *not_null_item= (Item_field*)item;
6920
6801
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
6921
6802
          /*
6926
6807
          if (!referred_tab || referred_tab->join != join)
6927
6808
            continue;
6928
6809
          if (!(notnull= new Item_func_isnotnull(not_null_item)))
6929
 
            DBUG_VOID_RETURN;
 
6810
            return;
6930
6811
          /*
6931
6812
            We need to do full fix_fields() call here in order to have correct
6932
6813
            notnull->const_item(). This is needed e.g. by test_quick_select 
6934
6815
            called.
6935
6816
          */
6936
6817
          if (notnull->fix_fields(join->thd, &notnull))
6937
 
            DBUG_VOID_RETURN;
6938
 
          DBUG_EXECUTE("where",print_where(notnull,
6939
 
                                           referred_tab->table->alias,
6940
 
                                           QT_ORDINARY););
 
6818
            return;
6941
6819
          add_cond_and_fix(&referred_tab->select_cond, notnull);
6942
6820
        }
6943
6821
      }
6944
6822
    }
6945
6823
  }
6946
 
  DBUG_VOID_RETURN;
 
6824
  return;
6947
6825
}
6948
6826
 
6949
6827
/**
6966
6844
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
6967
6845
{
6968
6846
  COND *tmp;
6969
 
  DBUG_ASSERT(cond != 0);
 
6847
  assert(cond != 0);
6970
6848
  if (tab == root_tab)
6971
6849
    return cond;
6972
6850
  if ((tmp= add_found_match_trig_cond(tab->first_upper, cond, root_tab)))
7025
6903
static void
7026
6904
make_outerjoin_info(JOIN *join)
7027
6905
{
7028
 
  DBUG_ENTER("make_outerjoin_info");
7029
6906
  for (uint i=join->const_tables ; i < join->tables ; i++)
7030
6907
  {
7031
6908
    JOIN_TAB *tab=join->join_tab+i;
7072
6949
      nested_join->first_nested->last_inner= tab;
7073
6950
    }
7074
6951
  }
7075
 
  DBUG_VOID_RETURN;
 
6952
  return;
7076
6953
}
7077
6954
 
7078
6955
 
7080
6957
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
7081
6958
{
7082
6959
  THD *thd= join->thd;
7083
 
  DBUG_ENTER("make_join_select");
7084
6960
  if (select)
7085
6961
  {
7086
6962
    add_not_null_conds(join);
7098
6974
          make_cond_for_table(cond,
7099
6975
                              join->const_table_map,
7100
6976
                              (table_map) 0, 1);
7101
 
        DBUG_EXECUTE("where",print_where(const_cond,"constants", QT_ORDINARY););
7102
6977
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
7103
6978
             tab < join->join_tab+join->tables ; tab++)
7104
6979
        {
7112
6987
              continue;
7113
6988
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7114
6989
            if (!tmp)
7115
 
              DBUG_RETURN(1);
 
6990
              return(1);
7116
6991
            tmp->quick_fix_field();
7117
6992
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7118
6993
                                    new Item_cond_and(cond_tab->select_cond,
7119
6994
                                                      tmp);
7120
6995
            if (!cond_tab->select_cond)
7121
 
              DBUG_RETURN(1);
 
6996
              return(1);
7122
6997
            cond_tab->select_cond->quick_fix_field();
7123
6998
          }       
7124
6999
        }
7125
7000
        if (const_cond && !const_cond->val_int())
7126
7001
        {
7127
 
          DBUG_PRINT("info",("Found impossible WHERE condition"));
7128
 
          DBUG_RETURN(1);        // Impossible const condition
 
7002
          return(1);     // Impossible const condition
7129
7003
        }
7130
7004
      }
7131
7005
    }
7192
7066
            in the ON part of an OUTER JOIN. In this case we want the code
7193
7067
            below to check if we should use 'quick' instead.
7194
7068
          */
7195
 
          DBUG_PRINT("info", ("Item_int"));
7196
7069
          tmp= new Item_int((longlong) 1,1);    // Always true
7197
7070
        }
7198
7071
 
7200
7073
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7201
7074
          tab->type == JT_EQ_REF)
7202
7075
      {
7203
 
        DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7204
7076
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7205
7077
                                       thd->memdup((uchar*) select,
7206
7078
                                                   sizeof(*select)));
7207
7079
        if (!sel)
7208
 
          DBUG_RETURN(1);                       // End of memory
 
7080
          return(1);                    // End of memory
7209
7081
        /*
7210
7082
          If tab is an inner table of an outer join operation,
7211
7083
          add a match guard to the pushed down predicate.
7219
7091
            a cond, so neutralize the hack above.
7220
7092
          */
7221
7093
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7222
 
            DBUG_RETURN(1);
 
7094
            return(1);
7223
7095
          tab->select_cond=sel->cond=tmp;
7224
7096
          /* Push condition to storage engine if this is enabled
7225
7097
             and the condition is not guarded */
7240
7112
          tab->select_cond= sel->cond= NULL;
7241
7113
 
7242
7114
        sel->head=tab->table;
7243
 
        DBUG_EXECUTE("where",print_where(tmp,tab->table->alias, QT_ORDINARY););
7244
7115
        if (tab->quick)
7245
7116
        {
7246
7117
          /* Use quick key read if it's a constant and it's not used
7263
7134
        {
7264
7135
          if (!tab->const_keys.is_clear_all() &&
7265
7136
              tab->table->reginfo.impossible_range)
7266
 
            DBUG_RETURN(1);
 
7137
            return(1);
7267
7138
        }
7268
7139
        else if (tab->type == JT_ALL && ! use_quick_range)
7269
7140
        {
7270
7141
          if (!tab->const_keys.is_clear_all() &&
7271
7142
              tab->table->reginfo.impossible_range)
7272
 
            DBUG_RETURN(1);                             // Impossible range
 
7143
            return(1);                          // Impossible range
7273
7144
          /*
7274
7145
            We plan to scan all rows.
7275
7146
            Check again if we should use an index.
7315
7186
                                          HA_POS_ERROR :
7316
7187
                                          join->unit->select_limit_cnt),0,
7317
7188
                                          false) < 0)
7318
 
                DBUG_RETURN(1);                 // Impossible WHERE
 
7189
                return(1);                      // Impossible WHERE
7319
7190
            }
7320
7191
            else
7321
7192
              sel->cond=orig_cond;
7349
7220
                                         current_map,
7350
7221
                                         current_map, 0)))
7351
7222
            {
7352
 
              DBUG_EXECUTE("where",print_where(tmp,"cache", QT_ORDINARY););
7353
7223
              tab->cache.select=(SQL_SELECT*)
7354
7224
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7355
7225
              tab->cache.select->cond=tmp;
7382
7252
            continue;
7383
7253
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7384
7254
          if (!tmp)
7385
 
            DBUG_RETURN(1);
 
7255
            return(1);
7386
7256
          tmp->quick_fix_field();
7387
7257
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7388
7258
                                    new Item_cond_and(cond_tab->select_cond,tmp);
7389
7259
          if (!cond_tab->select_cond)
7390
 
            DBUG_RETURN(1);
 
7260
            return(1);
7391
7261
          cond_tab->select_cond->quick_fix_field();
7392
7262
        }       
7393
7263
      }
7420
7290
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7421
7291
                                                     tmp_cond,
7422
7292
                                                     first_inner_tab)))
7423
 
              DBUG_RETURN(1);
 
7293
              return(1);
7424
7294
            /* 
7425
7295
              Now add the guard turning the predicate off for 
7426
7296
              the null complemented row.
7427
7297
            */ 
7428
 
            DBUG_PRINT("info", ("Item_func_trig_cond"));
7429
7298
            tmp_cond= new Item_func_trig_cond(tmp_cond,
7430
7299
                                              &first_inner_tab->
7431
7300
                                              not_null_compl);
7432
 
            DBUG_PRINT("info", ("Item_func_trig_cond 0x%lx",
7433
 
                                (ulong) tmp_cond));
7434
7301
            if (tmp_cond)
7435
7302
              tmp_cond->quick_fix_field();
7436
7303
            /* Add the predicate to other pushed down predicates */
7437
 
            DBUG_PRINT("info", ("Item_cond_and"));
7438
7304
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7439
7305
                                  new Item_cond_and(cond_tab->select_cond,
7440
7306
                                                    tmp_cond);
7441
 
            DBUG_PRINT("info", ("Item_cond_and 0x%lx",
7442
 
                                (ulong)cond_tab->select_cond));
7443
7307
            if (!cond_tab->select_cond)
7444
 
              DBUG_RETURN(1);
 
7308
              return(1);
7445
7309
            cond_tab->select_cond->quick_fix_field();
7446
7310
          }              
7447
7311
        }
7449
7313
      }
7450
7314
    }
7451
7315
  }
7452
 
  DBUG_RETURN(0);
 
7316
  return(0);
7453
7317
}
7454
7318
 
7455
7319
 
7711
7575
 
7712
7576
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7713
7577
{
7714
 
  DBUG_ENTER("push_index_cond");
7715
7578
  Item *idx_cond;
7716
7579
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7717
7580
      tab->join->thd->variables.engine_condition_pushdown)
7718
7581
  {
7719
 
    DBUG_EXECUTE("where",
7720
 
                 print_where(tab->select_cond, "full cond", QT_ORDINARY););
7721
 
 
7722
7582
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7723
7583
                                  other_tbls_ok);
7724
7584
 
7725
 
    DBUG_EXECUTE("where",
7726
 
                 print_where(idx_cond, "idx cond", QT_ORDINARY););
7727
 
 
7728
7585
    if (idx_cond)
7729
7586
    {
7730
7587
      tab->pre_idx_push_select_cond= tab->select_cond;
7744
7601
 
7745
7602
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
7746
7603
 
7747
 
      DBUG_EXECUTE("where",
7748
 
                   print_where(row_cond, "remainder cond", QT_ORDINARY););
7749
 
      
7750
7604
      if (row_cond)
7751
7605
      {
7752
7606
        if (!idx_remainder_cond)
7763
7617
        tab->select_cond= idx_remainder_cond;
7764
7618
      if (tab->select)
7765
7619
      {
7766
 
        DBUG_EXECUTE("where",
7767
 
                     print_where(tab->select->cond,
7768
 
                                 "select_cond",
7769
 
                                 QT_ORDINARY););
7770
 
 
7771
7620
        tab->select->cond= tab->select_cond;
7772
7621
      }
7773
7622
    }
7774
7623
  }
7775
 
  DBUG_VOID_RETURN;
 
7624
  return;
7776
7625
}
7777
7626
 
7778
7627
 
7835
7684
  uint i;
7836
7685
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7837
7686
  bool sorted= 1;
7838
 
  DBUG_ENTER("make_join_readinfo");
7839
7687
 
7840
7688
  for (i=join->const_tables ; i < join->tables ; i++)
7841
7689
  {
8017
7865
      }
8018
7866
      break;
8019
7867
    default:
8020
 
      DBUG_PRINT("error",("Table type %d found",tab->type)); /* purecov: deadcode */
8021
7868
      break;                                    /* purecov: deadcode */
8022
7869
    case JT_UNKNOWN:
8023
7870
    case JT_MAYBE_REF:
8025
7872
    }
8026
7873
  }
8027
7874
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
8028
 
  DBUG_RETURN(false);
 
7875
  return(false);
8029
7876
}
8030
7877
 
8031
7878
 
8144
7991
  */
8145
7992
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
8146
7993
  bool can_unlock= full;
8147
 
  DBUG_ENTER("JOIN::join_free");
8148
7994
 
8149
7995
  cleanup(full);
8150
7996
 
8187
8033
    lock= 0;
8188
8034
  }
8189
8035
 
8190
 
  DBUG_VOID_RETURN;
 
8036
  return;
8191
8037
}
8192
8038
 
8193
8039
 
8205
8051
 
8206
8052
void JOIN::cleanup(bool full)
8207
8053
{
8208
 
  DBUG_ENTER("JOIN::cleanup");
8209
 
 
8210
8054
  if (table)
8211
8055
  {
8212
8056
    JOIN_TAB *tab,*end;
8266
8110
    }
8267
8111
    tmp_table_param.cleanup();
8268
8112
  }
8269
 
  DBUG_VOID_RETURN;
 
8113
  return;
8270
8114
}
8271
8115
 
8272
8116
 
8319
8163
      if (order)
8320
8164
      {
8321
8165
        found++;
8322
 
        DBUG_ASSERT(!(order->used & map));
 
8166
        assert(!(order->used & map));
8323
8167
        order->used|=map;
8324
8168
        continue;                               // Used in ORDER BY
8325
8169
      }
8438
8282
  table_map first_table= join->join_tab[join->const_tables].table->map;
8439
8283
  table_map not_const_tables= ~join->const_table_map;
8440
8284
  table_map ref;
8441
 
  DBUG_ENTER("remove_const");
8442
8285
 
8443
8286
  prev_ptr= &first_order;
8444
8287
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8455
8298
    {
8456
8299
      if (order->item[0]->with_subselect)
8457
8300
        order->item[0]->val_str(&order->item[0]->str_value);
8458
 
      DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8459
8301
      continue;                                 // skip const item
8460
8302
    }
8461
8303
    else
8467
8309
        Item *comp_item=0;
8468
8310
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8469
8311
        {
8470
 
          DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8471
8312
          continue;
8472
8313
        }
8473
8314
        if ((ref=order_tables & (not_const_tables ^ first_table)))
8475
8316
          if (!(order_tables & first_table) &&
8476
8317
              only_eq_ref_tables(join,first_order, ref))
8477
8318
          {
8478
 
            DBUG_PRINT("info",("removing: %s", order->item[0]->full_name()));
8479
8319
            continue;
8480
8320
          }
8481
8321
          *simple_order=0;                      // Must do a temp table to sort
8490
8330
    *prev_ptr=0;
8491
8331
  if (prev_ptr == &first_order)                 // Nothing to sort/group
8492
8332
    *simple_order=1;
8493
 
  DBUG_PRINT("exit",("simple_order: %d",(int) *simple_order));
8494
 
  DBUG_RETURN(first_order);
 
8333
  return(first_order);
8495
8334
}
8496
8335
 
8497
8336
 
8500
8339
                 List<Item> &fields, bool send_row, ulonglong select_options,
8501
8340
                 const char *info, Item *having)
8502
8341
{
8503
 
  DBUG_ENTER("return_zero_rows");
8504
 
 
8505
8342
  if (select_options & SELECT_DESCRIBE)
8506
8343
  {
8507
8344
    select_describe(join, false, false, false, info);
8508
 
    DBUG_RETURN(0);
 
8345
    return(0);
8509
8346
  }
8510
8347
 
8511
8348
  join->join_free();
8532
8369
  }
8533
8370
  /* Update results for FOUND_ROWS */
8534
8371
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
8535
 
  DBUG_RETURN(0);
 
8372
  return(0);
8536
8373
}
8537
8374
 
8538
8375
/*
9472
9309
    cond= new Item_cond_and(eq_list);
9473
9310
  else
9474
9311
  {
9475
 
    DBUG_ASSERT(cond->type() == Item::COND_ITEM);
 
9312
    assert(cond->type() == Item::COND_ITEM);
9476
9313
    ((Item_cond *) cond)->add_at_head(&eq_list);
9477
9314
  }
9478
9315
 
9953
9790
  NESTED_JOIN *nested_join;
9954
9791
  TABLE_LIST *prev_table= 0;
9955
9792
  List_iterator<TABLE_LIST> li(*join_list);
9956
 
  DBUG_ENTER("simplify_joins");
9957
9793
 
9958
9794
  /* 
9959
9795
    Try to simplify join operations from join_list.
9986
9822
 
9987
9823
        if (!table->prep_on_expr || expr != table->on_expr)
9988
9824
        {
9989
 
          DBUG_ASSERT(expr);
 
9825
          assert(expr);
9990
9826
 
9991
9827
          table->on_expr= expr;
9992
9828
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
10029
9865
          conds= and_conds(conds, table->on_expr);
10030
9866
          conds->top_level_item();
10031
9867
          /* conds is always a new item as both cond and on_expr existed */
10032
 
          DBUG_ASSERT(!conds->fixed);
 
9868
          assert(!conds->fixed);
10033
9869
          conds->fix_fields(join->thd, &conds);
10034
9870
        }
10035
9871
        else
10113
9949
      li.replace(nested_join->join_list);
10114
9950
    }
10115
9951
  }
10116
 
  DBUG_RETURN(conds); 
 
9952
  return(conds); 
10117
9953
}
10118
9954
 
10119
9955
 
10142
9978
{
10143
9979
  List_iterator<TABLE_LIST> li(*join_list);
10144
9980
  TABLE_LIST *table;
10145
 
  DBUG_ENTER("build_bitmap_for_nested_joins");
10146
9981
  while ((table= li++))
10147
9982
  {
10148
9983
    NESTED_JOIN *nested_join;
10169
10004
      }
10170
10005
    }
10171
10006
  }
10172
 
  DBUG_RETURN(first_unused);
 
10007
  return(first_unused);
10173
10008
}
10174
10009
 
10175
10010
 
10187
10022
{
10188
10023
  List_iterator<TABLE_LIST> li(*join_list);
10189
10024
  TABLE_LIST *table;
10190
 
  DBUG_ENTER("reset_nj_counters");
10191
10025
  while ((table= li++))
10192
10026
  {
10193
10027
    NESTED_JOIN *nested_join;
10197
10031
      reset_nj_counters(&nested_join->join_list);
10198
10032
    }
10199
10033
  }
10200
 
  DBUG_VOID_RETURN;
 
10034
  return;
10201
10035
}
10202
10036
 
10203
10037
 
10412
10246
              Item::cond_result *cond_value)
10413
10247
{
10414
10248
  THD *thd= join->thd;
10415
 
  DBUG_ENTER("optimize_cond");
10416
10249
 
10417
10250
  if (!conds)
10418
10251
    *cond_value= Item::COND_TRUE;
10426
10259
      predicate. Substitute a constant instead of this field if the
10427
10260
      multiple equality contains a constant.
10428
10261
    */ 
10429
 
    DBUG_EXECUTE("where", print_where(conds, "original", QT_ORDINARY););
10430
10262
    conds= build_equal_items(join->thd, conds, NULL, join_list,
10431
10263
                             &join->cond_equal);
10432
 
    DBUG_EXECUTE("where",print_where(conds,"after equal_items", QT_ORDINARY););
10433
10264
 
10434
10265
    /* change field = field to field = const for each found field = const */
10435
10266
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
10437
10268
      Remove all instances of item == item
10438
10269
      Remove all and-levels where CONST item != CONST item
10439
10270
    */
10440
 
    DBUG_EXECUTE("where",print_where(conds,"after const change", QT_ORDINARY););
10441
10271
    conds= remove_eq_conds(thd, conds, cond_value) ;
10442
 
    DBUG_EXECUTE("info",print_where(conds,"after remove", QT_ORDINARY););
10443
10272
  }
10444
 
  DBUG_RETURN(conds);
 
10273
  return(conds);
10445
10274
}
10446
10275
 
10447
10276
 
10826
10655
                               item->name, item->unsigned_flag);
10827
10656
    break;
10828
10657
  case STRING_RESULT:
10829
 
    DBUG_ASSERT(item->collation.collation);
 
10658
    assert(item->collation.collation);
10830
10659
  
10831
10660
    enum enum_field_types type;
10832
10661
    /*
10892
10721
  case ROW_RESULT:
10893
10722
  default:
10894
10723
    // This case should never be choosen
10895
 
    DBUG_ASSERT(0);
 
10724
    assert(0);
10896
10725
    new_field= 0;
10897
10726
    break;
10898
10727
  }
11052
10881
  case Item::VARBIN_ITEM:
11053
10882
    if (make_copy_field)
11054
10883
    {
11055
 
      DBUG_ASSERT(((Item_result_field*)item)->result_field);
 
10884
      assert(((Item_result_field*)item)->result_field);
11056
10885
      *from_field= ((Item_result_field*)item)->result_field;
11057
10886
    }
11058
10887
    return create_tmp_field_from_item(thd, item, table,
11153
10982
  MI_COLUMNDEF *recinfo;
11154
10983
  uint total_uneven_bit_length= 0;
11155
10984
  bool force_copy_fields= param->force_copy_fields;
11156
 
  DBUG_ENTER("create_tmp_table");
11157
 
  DBUG_PRINT("enter",
11158
 
             ("distinct: %d  save_sum_fields: %d  rows_limit: %lu  group: %d",
11159
 
              (int) distinct, (int) save_sum_fields,
11160
 
              (ulong) rows_limit,test(group)));
11161
10985
 
11162
10986
  status_var_increment(thd->status_var.created_tmp_tables);
11163
10987
 
11239
11063
  {
11240
11064
    if (temp_pool_slot != MY_BIT_NONE)
11241
11065
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11242
 
    DBUG_RETURN(NULL);                          /* purecov: inspected */
 
11066
    return(NULL);                               /* purecov: inspected */
11243
11067
  }
11244
11068
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11245
11069
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11247
11071
    if (temp_pool_slot != MY_BIT_NONE)
11248
11072
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11249
11073
    free_root(&own_root, MYF(0));               /* purecov: inspected */
11250
 
    DBUG_RETURN(NULL);                          /* purecov: inspected */
 
11074
    return(NULL);                               /* purecov: inspected */
11251
11075
  }
11252
11076
  param->items_to_copy= copy_func;
11253
11077
  strmov(tmpname,path);
11434
11258
      null_count= 0;
11435
11259
    }
11436
11260
  }
11437
 
  DBUG_ASSERT(fieldnr == (uint) (reg_field - table->field));
11438
 
  DBUG_ASSERT(field_count >= (uint) (reg_field - table->field));
 
11261
  assert(fieldnr == (uint) (reg_field - table->field));
 
11262
  assert(field_count >= (uint) (reg_field - table->field));
11439
11263
  field_count= fieldnr;
11440
11264
  *reg_field= 0;
11441
11265
  *blob_field= 0;                               // End marker
11628
11452
 
11629
11453
  if (group)
11630
11454
  {
11631
 
    DBUG_PRINT("info",("Creating group key in temporary table"));
11632
11455
    table->group=group;                         /* Table is grouped by key */
11633
11456
    param->group_buff=group_buff;
11634
11457
    share->keys=1;
11696
11519
      'param->hidden_field_count' extra columns, whose null bits are stored
11697
11520
      in the first 'hidden_null_pack_length' bytes of the row.
11698
11521
    */
11699
 
    DBUG_PRINT("info",("hidden_field_count: %d", param->hidden_field_count));
11700
 
 
11701
11522
    if (blob_count)
11702
11523
    {
11703
11524
      /*
11796
11617
 
11797
11618
  thd->mem_root= mem_root_save;
11798
11619
 
11799
 
  DBUG_RETURN(table);
 
11620
  return(table);
11800
11621
 
11801
11622
err:
11802
11623
  thd->mem_root= mem_root_save;
11803
11624
  free_tmp_table(thd,table);                    /* purecov: inspected */
11804
11625
  if (temp_pool_slot != MY_BIT_NONE)
11805
11626
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11806
 
  DBUG_RETURN(NULL);                            /* purecov: inspected */
 
11627
  return(NULL);                         /* purecov: inspected */
11807
11628
}
11808
11629
 
11809
11630
 
11864
11685
  uint blob_count, null_pack_length, null_count;
11865
11686
  uchar *null_flags;
11866
11687
  uchar *pos;
11867
 
  DBUG_ENTER("create_duplicate_weedout_tmp_table");
11868
11688
  
11869
11689
  /*
11870
11690
    STEP 1: Get temporary table name
11907
11727
  {
11908
11728
    if (temp_pool_slot != MY_BIT_NONE)
11909
11729
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11910
 
    DBUG_RETURN(NULL);
 
11730
    return(NULL);
11911
11731
  }
11912
11732
  strmov(tmpname,path);
11913
11733
  
11953
11773
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11954
11774
                               &my_charset_bin);
11955
11775
    if (!field)
11956
 
      DBUG_RETURN(0);
 
11776
      return(0);
11957
11777
    field->table= table;
11958
11778
    field->key_start.init(0);
11959
11779
    field->part_of_key.init(0);
11980
11800
    share->db_plugin= ha_lock_engine(0, myisam_hton);
11981
11801
    table->file= get_new_handler(share, &table->mem_root,
11982
11802
                                 share->db_type());
11983
 
    DBUG_ASSERT(uniq_tuple_length_arg <= table->file->max_key_length());
 
11803
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
11984
11804
  }
11985
11805
  else
11986
11806
  {
12073
11893
  //// keyinfo= param->keyinfo;
12074
11894
  if (true)
12075
11895
  {
12076
 
    DBUG_PRINT("info",("Creating group key in temporary table"));
12077
11896
    share->keys=1;
12078
11897
    share->uniques= test(using_unique_constraint);
12079
11898
    table->key_info=keyinfo;
12119
11938
    goto err;
12120
11939
 
12121
11940
  thd->mem_root= mem_root_save;
12122
 
  DBUG_RETURN(table);
 
11941
  return(table);
12123
11942
 
12124
11943
err:
12125
11944
  thd->mem_root= mem_root_save;
12126
11945
  free_tmp_table(thd,table);                    /* purecov: inspected */
12127
11946
  if (temp_pool_slot != MY_BIT_NONE)
12128
11947
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
12129
 
  DBUG_RETURN(NULL);                            /* purecov: inspected */
 
11948
  return(NULL);                         /* purecov: inspected */
12130
11949
}
12131
11950
 
12132
11951
/****************************************************************************/
12311
12130
  MI_KEYDEF keydef;
12312
12131
  MI_UNIQUEDEF uniquedef;
12313
12132
  TABLE_SHARE *share= table->s;
12314
 
  DBUG_ENTER("create_myisam_tmp_table");
12315
12133
 
12316
12134
  if (share->keys)
12317
12135
  {                                             // Get keys for ni_create
12408
12226
  }
12409
12227
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12410
12228
  share->db_record_offset= 1;
12411
 
  DBUG_RETURN(0);
 
12229
  return(0);
12412
12230
 err:
12413
 
  DBUG_RETURN(1);
 
12231
  return(1);
12414
12232
}
12415
12233
 
12416
12234
 
12419
12237
{
12420
12238
  MEM_ROOT own_root= entry->mem_root;
12421
12239
  const char *save_proc_info;
12422
 
  DBUG_ENTER("free_tmp_table");
12423
 
  DBUG_PRINT("enter",("table: %s",entry->alias));
12424
12240
 
12425
12241
  save_proc_info=thd->proc_info;
12426
12242
  thd_proc_info(thd, "removing tmp table");
12447
12263
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12448
12264
  thd_proc_info(thd, save_proc_info);
12449
12265
 
12450
 
  DBUG_VOID_RETURN;
 
12266
  return;
12451
12267
}
12452
12268
 
12453
12269
/**
12464
12280
  TABLE_SHARE share;
12465
12281
  const char *save_proc_info;
12466
12282
  int write_err;
12467
 
  DBUG_ENTER("create_myisam_from_heap");
12468
12283
 
12469
12284
  if (table->s->db_type() != heap_hton || 
12470
12285
      error != HA_ERR_RECORD_FILE_FULL)
12471
12286
  {
12472
12287
    table->file->print_error(error,MYF(0));
12473
 
    DBUG_RETURN(1);
 
12288
    return(1);
12474
12289
  }
12475
12290
  new_table= *table;
12476
12291
  share= *table->s;
12478
12293
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12479
12294
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12480
12295
                                        new_table.s->db_type())))
12481
 
    DBUG_RETURN(1);                             // End of memory
 
12296
    return(1);                          // End of memory
12482
12297
 
12483
12298
  save_proc_info=thd->proc_info;
12484
12299
  thd_proc_info(thd, "converting HEAP to MyISAM");
12519
12334
  while (!table->file->rnd_next(new_table.record[1]))
12520
12335
  {
12521
12336
    write_err= new_table.file->ha_write_row(new_table.record[1]);
12522
 
    DBUG_EXECUTE_IF("raise_error", write_err= HA_ERR_FOUND_DUPP_KEY ;);
12523
12337
    if (write_err)
12524
12338
      goto err;
12525
12339
  }
12551
12365
      "Copying to tmp table on disk" : save_proc_info);
12552
12366
    thd_proc_info(thd, new_proc_info);
12553
12367
  }
12554
 
  DBUG_RETURN(0);
 
12368
  return(0);
12555
12369
 
12556
12370
 err:
12557
 
  DBUG_PRINT("error",("Got error: %d",write_err));
12558
12371
  table->file->print_error(write_err, MYF(0));
12559
12372
  (void) table->file->ha_rnd_end();
12560
12373
  (void) new_table.file->close();
12564
12377
  delete new_table.file;
12565
12378
  thd_proc_info(thd, save_proc_info);
12566
12379
  table->mem_root= new_table.mem_root;
12567
 
  DBUG_RETURN(1);
 
12380
  return(1);
12568
12381
}
12569
12382
 
12570
12383
 
12594
12407
    {
12595
12408
      if (table->s->keys)
12596
12409
      {
12597
 
        DBUG_PRINT("info",("Using end_update"));
12598
12410
        end_select=end_update;
12599
12411
      }
12600
12412
      else
12601
12413
      {
12602
 
        DBUG_PRINT("info",("Using end_unique_update"));
12603
12414
        end_select=end_unique_update;
12604
12415
      }
12605
12416
    }
12606
12417
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
12607
12418
    {
12608
 
      DBUG_PRINT("info",("Using end_write_group"));
12609
12419
      end_select=end_write_group;
12610
12420
    }
12611
12421
    else
12612
12422
    {
12613
 
      DBUG_PRINT("info",("Using end_write"));
12614
12423
      end_select=end_write;
12615
12424
      if (tmp_tbl->precomputed_group_by)
12616
12425
      {
12657
12466
  int rc= 0;
12658
12467
  enum_nested_loop_state error= NESTED_LOOP_OK;
12659
12468
  JOIN_TAB *join_tab= NULL;
12660
 
  DBUG_ENTER("do_select");
12661
12469
  
12662
12470
  join->tmp_table= table;                       /* Save for easy recursion */
12663
12471
  join->fields= fields;
12698
12506
      */
12699
12507
      join->examined_rows++;
12700
12508
      join->thd->row_count++;
12701
 
      DBUG_ASSERT(join->examined_rows <= 1);
 
12509
      assert(join->examined_rows <= 1);
12702
12510
    }
12703
12511
    else if (join->send_row_on_empty_set())
12704
12512
    {
12708
12516
  }
12709
12517
  else
12710
12518
  {
12711
 
    DBUG_ASSERT(join->tables);
 
12519
    assert(join->tables);
12712
12520
    error= sub_select(join,join_tab,0);
12713
12521
    if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
12714
12522
      error= sub_select(join,join_tab,1);
12734
12542
      if (join->result->send_eof())
12735
12543
        rc= 1;                                  // Don't send error
12736
12544
    }
12737
 
    DBUG_PRINT("info",("%ld records output", (long) join->send_records));
12738
12545
  }
12739
12546
  else
12740
12547
    rc= -1;
12743
12550
    int tmp, new_errno= 0;
12744
12551
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
12745
12552
    {
12746
 
      DBUG_PRINT("error",("extra(HA_EXTRA_NO_CACHE) failed"));
12747
12553
      new_errno= tmp;
12748
12554
    }
12749
12555
    if ((tmp=table->file->ha_index_or_rnd_end()))
12750
12556
    {
12751
 
      DBUG_PRINT("error",("ha_index_or_rnd_end() failed"));
12752
12557
      new_errno= tmp;
12753
12558
    }
12754
12559
    if (new_errno)
12755
12560
      table->file->print_error(new_errno,MYF(0));
12756
12561
  }
12757
 
#ifndef DBUG_OFF
12758
 
  if (rc)
12759
 
  {
12760
 
    DBUG_PRINT("error",("Error: do_select() failed"));
12761
 
  }
12762
 
#endif
12763
 
  DBUG_RETURN(join->thd->is_error() ? -1 : rc);
 
12562
  return(join->thd->is_error() ? -1 : rc);
12764
12563
}
12765
12564
 
12766
12565
 
13095
12894
    join->thd->send_kill_message();
13096
12895
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
13097
12896
  }
13098
 
  DBUG_PRINT("info", ("select cond 0x%lx", (ulong)select_cond));
13099
12897
  if (!select_cond || select_cond->val_int())
13100
12898
  {
13101
12899
    /*
13178
12976
    */
13179
12977
    join->examined_rows++;
13180
12978
    join->thd->row_count++;
13181
 
    DBUG_PRINT("counts", ("join->examined_rows++: %lu",
13182
 
                          (ulong) join->examined_rows));
13183
12979
 
13184
12980
    if (found)
13185
12981
    {
13409
13205
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
13410
13206
{
13411
13207
  int error;
13412
 
  DBUG_ENTER("join_read_const_table");
13413
13208
  TABLE *table=tab->table;
13414
13209
  table->const_table=1;
13415
13210
  table->null_row=0;
13424
13219
      pos->records_read=0.0;
13425
13220
      pos->ref_depend_map= 0;
13426
13221
      if (!table->maybe_null || error > 0)
13427
 
        DBUG_RETURN(error);
 
13222
        return(error);
13428
13223
    }
13429
13224
  }
13430
13225
  else
13450
13245
      pos->records_read=0.0;
13451
13246
      pos->ref_depend_map= 0;
13452
13247
      if (!table->maybe_null || error > 0)
13453
 
        DBUG_RETURN(error);
 
13248
        return(error);
13454
13249
    }
13455
13250
  }
13456
13251
  if (*tab->on_expr_ref && !table->null_row)
13481
13276
           embedding->nested_join->join_list.head() == embedded);
13482
13277
  }
13483
13278
 
13484
 
  DBUG_RETURN(0);
 
13279
  return(0);
13485
13280
}
13486
13281
 
13487
13282
 
13982
13777
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13983
13778
         bool end_of_records)
13984
13779
{
13985
 
  DBUG_ENTER("end_send");
13986
13780
  if (!end_of_records)
13987
13781
  {
13988
13782
    int error;
13989
13783
    if (join->having && join->having->val_int() == 0)
13990
 
      DBUG_RETURN(NESTED_LOOP_OK);               // Didn't match having
 
13784
      return(NESTED_LOOP_OK);               // Didn't match having
13991
13785
    error=0;
13992
13786
    if (join->do_send_rows)
13993
13787
      error=join->result->send_data(*join->fields);
13994
13788
    if (error)
13995
 
      DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */
 
13789
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
13996
13790
    if (++join->send_records >= join->unit->select_limit_cnt &&
13997
13791
        join->do_send_rows)
13998
13792
    {
14026
13820
          join->do_send_rows= 0;
14027
13821
          if (join->unit->fake_select_lex)
14028
13822
            join->unit->fake_select_lex->select_limit= 0;
14029
 
          DBUG_RETURN(NESTED_LOOP_OK);
 
13823
          return(NESTED_LOOP_OK);
14030
13824
        }
14031
13825
      }
14032
 
      DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
 
13826
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
14033
13827
    }
14034
13828
    else if (join->send_records >= join->fetch_limit)
14035
13829
    {
14037
13831
        There is a server side cursor and all rows for
14038
13832
        this fetch request are sent.
14039
13833
      */
14040
 
      DBUG_RETURN(NESTED_LOOP_CURSOR_LIMIT);
 
13834
      return(NESTED_LOOP_CURSOR_LIMIT);
14041
13835
    }
14042
13836
  }
14043
13837
 
14044
 
  DBUG_RETURN(NESTED_LOOP_OK);
 
13838
  return(NESTED_LOOP_OK);
14045
13839
}
14046
13840
 
14047
13841
 
14052
13846
{
14053
13847
  int idx= -1;
14054
13848
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
14055
 
  DBUG_ENTER("end_send_group");
14056
13849
 
14057
13850
  if (!join->first_record || end_of_records ||
14058
13851
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14089
13882
          }
14090
13883
        }
14091
13884
        if (error > 0)
14092
 
          DBUG_RETURN(NESTED_LOOP_ERROR);        /* purecov: inspected */
 
13885
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
14093
13886
        if (end_of_records)
14094
 
          DBUG_RETURN(NESTED_LOOP_OK);
 
13887
          return(NESTED_LOOP_OK);
14095
13888
        if (join->send_records >= join->unit->select_limit_cnt &&
14096
13889
            join->do_send_rows)
14097
13890
        {
14098
13891
          if (!(join->select_options & OPTION_FOUND_ROWS))
14099
 
            DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
13892
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
14100
13893
          join->do_send_rows=0;
14101
13894
          join->unit->select_limit_cnt = HA_POS_ERROR;
14102
13895
        }
14117
13910
    else
14118
13911
    {
14119
13912
      if (end_of_records)
14120
 
        DBUG_RETURN(NESTED_LOOP_OK);
 
13913
        return(NESTED_LOOP_OK);
14121
13914
      join->first_record=1;
14122
13915
      VOID(test_if_item_cache_changed(join->group_fields));
14123
13916
    }
14129
13922
      */
14130
13923
      copy_fields(&join->tmp_table_param);
14131
13924
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14132
 
        DBUG_RETURN(NESTED_LOOP_ERROR);
14133
 
      DBUG_RETURN(ok_code);
 
13925
        return(NESTED_LOOP_ERROR);
 
13926
      return(ok_code);
14134
13927
    }
14135
13928
  }
14136
13929
  if (update_sum_func(join->sum_funcs))
14137
 
    DBUG_RETURN(NESTED_LOOP_ERROR);
14138
 
  DBUG_RETURN(NESTED_LOOP_OK);
 
13930
    return(NESTED_LOOP_ERROR);
 
13931
  return(NESTED_LOOP_OK);
14139
13932
}
14140
13933
 
14141
13934
 
14145
13938
          bool end_of_records)
14146
13939
{
14147
13940
  TABLE *table=join->tmp_table;
14148
 
  DBUG_ENTER("end_write");
14149
13941
 
14150
13942
  if (join->thd->killed)                        // Aborted by user
14151
13943
  {
14152
13944
    join->thd->send_kill_message();
14153
 
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
13945
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
14154
13946
  }
14155
13947
  if (!end_of_records)
14156
13948
  {
14184
13976
                                    join->tmp_table_param.start_recinfo,
14185
13977
                                    &join->tmp_table_param.recinfo,
14186
13978
                                    error, 1))
14187
 
          DBUG_RETURN(NESTED_LOOP_ERROR);        // Not a table_is_full error
 
13979
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
14188
13980
        table->s->uniques=0;                    // To ensure rows are the same
14189
13981
      }
14190
13982
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
14191
13983
          join->do_send_rows)
14192
13984
      {
14193
13985
        if (!(join->select_options & OPTION_FOUND_ROWS))
14194
 
          DBUG_RETURN(NESTED_LOOP_QUERY_LIMIT);
 
13986
          return(NESTED_LOOP_QUERY_LIMIT);
14195
13987
        join->do_send_rows=0;
14196
13988
        join->unit->select_limit_cnt = HA_POS_ERROR;
14197
 
        DBUG_RETURN(NESTED_LOOP_OK);
 
13989
        return(NESTED_LOOP_OK);
14198
13990
      }
14199
13991
    }
14200
13992
  }
14201
13993
end:
14202
 
  DBUG_RETURN(NESTED_LOOP_OK);
 
13994
  return(NESTED_LOOP_OK);
14203
13995
}
14204
13996
 
14205
13997
/* ARGSUSED */
14212
14004
  TABLE *table=join->tmp_table;
14213
14005
  ORDER   *group;
14214
14006
  int     error;
14215
 
  DBUG_ENTER("end_update");
14216
14007
 
14217
14008
  if (end_of_records)
14218
 
    DBUG_RETURN(NESTED_LOOP_OK);
 
14009
    return(NESTED_LOOP_OK);
14219
14010
  if (join->thd->killed)                        // Aborted by user
14220
14011
  {
14221
14012
    join->thd->send_kill_message();
14222
 
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14013
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
14223
14014
  }
14224
14015
 
14225
14016
  join->found_records++;
14244
14035
                                          table->record[0])))
14245
14036
    {
14246
14037
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14247
 
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14038
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14248
14039
    }
14249
 
    DBUG_RETURN(NESTED_LOOP_OK);
 
14040
    return(NESTED_LOOP_OK);
14250
14041
  }
14251
14042
 
14252
14043
  /*
14270
14061
                                join->tmp_table_param.start_recinfo,
14271
14062
                                &join->tmp_table_param.recinfo,
14272
14063
                                error, 0))
14273
 
      DBUG_RETURN(NESTED_LOOP_ERROR);            // Not a table_is_full error
 
14064
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
14274
14065
    /* Change method to update rows */
14275
14066
    table->file->ha_index_init(0, 0);
14276
14067
    join->join_tab[join->tables-1].next_select=end_unique_update;
14277
14068
  }
14278
14069
  join->send_records++;
14279
 
  DBUG_RETURN(NESTED_LOOP_OK);
 
14070
  return(NESTED_LOOP_OK);
14280
14071
}
14281
14072
 
14282
14073
 
14288
14079
{
14289
14080
  TABLE *table=join->tmp_table;
14290
14081
  int     error;
14291
 
  DBUG_ENTER("end_unique_update");
14292
14082
 
14293
14083
  if (end_of_records)
14294
 
    DBUG_RETURN(NESTED_LOOP_OK);
 
14084
    return(NESTED_LOOP_OK);
14295
14085
  if (join->thd->killed)                        // Aborted by user
14296
14086
  {
14297
14087
    join->thd->send_kill_message();
14298
 
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14088
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
14299
14089
  }
14300
14090
 
14301
14091
  init_tmptable_sum_functions(join->sum_funcs);
14309
14099
    if ((int) table->file->get_dup_key(error) < 0)
14310
14100
    {
14311
14101
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14312
 
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14102
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14313
14103
    }
14314
14104
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
14315
14105
    {
14316
14106
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14317
 
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14107
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14318
14108
    }
14319
14109
    restore_record(table,record[1]);
14320
14110
    update_tmptable_sum_func(join->sum_funcs,table);
14322
14112
                                          table->record[0])))
14323
14113
    {
14324
14114
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14325
 
      DBUG_RETURN(NESTED_LOOP_ERROR);            /* purecov: inspected */
 
14115
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14326
14116
    }
14327
14117
  }
14328
 
  DBUG_RETURN(NESTED_LOOP_OK);
 
14118
  return(NESTED_LOOP_OK);
14329
14119
}
14330
14120
 
14331
14121
 
14336
14126
{
14337
14127
  TABLE *table=join->tmp_table;
14338
14128
  int     idx= -1;
14339
 
  DBUG_ENTER("end_write_group");
14340
14129
 
14341
14130
  if (join->thd->killed)
14342
14131
  {                                             // Aborted by user
14343
14132
    join->thd->send_kill_message();
14344
 
    DBUG_RETURN(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
14133
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
14345
14134
  }
14346
14135
  if (!join->first_record || end_of_records ||
14347
14136
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14365
14154
                                               join->tmp_table_param.start_recinfo,
14366
14155
                                                &join->tmp_table_param.recinfo,
14367
14156
                                               error, 0))
14368
 
            DBUG_RETURN(NESTED_LOOP_ERROR);
 
14157
            return(NESTED_LOOP_ERROR);
14369
14158
        }
14370
14159
        if (join->rollup.state != ROLLUP::STATE_NONE)
14371
14160
        {
14372
14161
          if (join->rollup_write_data((uint) (idx+1), table))
14373
 
            DBUG_RETURN(NESTED_LOOP_ERROR);
 
14162
            return(NESTED_LOOP_ERROR);
14374
14163
        }
14375
14164
        if (end_of_records)
14376
 
          DBUG_RETURN(NESTED_LOOP_OK);
 
14165
          return(NESTED_LOOP_OK);
14377
14166
      }
14378
14167
    }
14379
14168
    else
14380
14169
    {
14381
14170
      if (end_of_records)
14382
 
        DBUG_RETURN(NESTED_LOOP_OK);
 
14171
        return(NESTED_LOOP_OK);
14383
14172
      join->first_record=1;
14384
14173
      VOID(test_if_item_cache_changed(join->group_fields));
14385
14174
    }
14388
14177
      copy_fields(&join->tmp_table_param);
14389
14178
      copy_funcs(join->tmp_table_param.items_to_copy);
14390
14179
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14391
 
        DBUG_RETURN(NESTED_LOOP_ERROR);
14392
 
      DBUG_RETURN(NESTED_LOOP_OK);
 
14180
        return(NESTED_LOOP_ERROR);
 
14181
      return(NESTED_LOOP_OK);
14393
14182
    }
14394
14183
  }
14395
14184
  if (update_sum_func(join->sum_funcs))
14396
 
    DBUG_RETURN(NESTED_LOOP_ERROR);
14397
 
  DBUG_RETURN(NESTED_LOOP_OK);
 
14185
    return(NESTED_LOOP_ERROR);
 
14186
  return(NESTED_LOOP_OK);
14398
14187
}
14399
14188
 
14400
14189
 
14701
14490
  key_part_map const_key_parts=table->const_key_parts[idx];
14702
14491
  int reverse=0;
14703
14492
  my_bool on_primary_key= false;
14704
 
  DBUG_ENTER("test_if_order_by_key");
14705
14493
 
14706
14494
  for (; order ; order=order->next, const_key_parts>>=1)
14707
14495
  {
14738
14526
         one row).  The sorting doesn't matter.
14739
14527
        */
14740
14528
        if (key_part == key_part_end && reverse == 0)
14741
 
          DBUG_RETURN(1);
 
14529
          return(1);
14742
14530
      }
14743
14531
      else
14744
 
        DBUG_RETURN(0);
 
14532
        return(0);
14745
14533
    }
14746
14534
 
14747
14535
    if (key_part->field != field)
14748
 
      DBUG_RETURN(0);
 
14536
      return(0);
14749
14537
 
14750
14538
    /* set flag to 1 if we can use read-next on key, else to -1 */
14751
14539
    flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
14752
14540
           1 : -1);
14753
14541
    if (reverse && flag != reverse)
14754
 
      DBUG_RETURN(0);
 
14542
      return(0);
14755
14543
    reverse=flag;                               // Remember if reverse
14756
14544
    key_part++;
14757
14545
  }
14760
14548
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
14761
14549
                         HA_READ_PREV))
14762
14550
    reverse= 0;                                 // Index can't be used
14763
 
  DBUG_RETURN(reverse);
 
14551
  return(reverse);
14764
14552
}
14765
14553
 
14766
14554
 
15018
14806
  SQL_SELECT *select=tab->select;
15019
14807
  key_map usable_keys;
15020
14808
  QUICK_SELECT_I *save_quick= 0;
15021
 
  DBUG_ENTER("test_if_skip_sort_order");
15022
14809
 
15023
14810
  /*
15024
14811
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
15032
14819
    if (item->type() != Item::FIELD_ITEM)
15033
14820
    {
15034
14821
      usable_keys.clear_all();
15035
 
      DBUG_RETURN(0);
 
14822
      return(0);
15036
14823
    }
15037
14824
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
15038
14825
    if (usable_keys.is_clear_all())
15039
 
      DBUG_RETURN(0);                                   // No usable keys
 
14826
      return(0);                                        // No usable keys
15040
14827
  }
15041
14828
 
15042
14829
  ref_key= -1;
15046
14833
    ref_key=       tab->ref.key;
15047
14834
    ref_key_parts= tab->ref.key_parts;
15048
14835
    if (tab->type == JT_REF_OR_NULL)
15049
 
      DBUG_RETURN(0);
 
14836
      return(0);
15050
14837
  }
15051
14838
  else if (select && select->quick)             // Range found by opt_range
15052
14839
  {
15061
14848
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
15062
14849
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
15063
14850
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
15064
 
      DBUG_RETURN(0);
 
14851
      return(0);
15065
14852
    ref_key=       select->quick->index;
15066
14853
    ref_key_parts= select->quick->used_key_parts;
15067
14854
  }
15105
14892
 
15106
14893
          if (create_ref_for_key(tab->join, tab, keyuse, 
15107
14894
                                 tab->join->const_table_map))
15108
 
            DBUG_RETURN(0);
 
14895
            return(0);
15109
14896
        }
15110
14897
        else
15111
14898
        {
15128
14915
                                        tab->join->unit->select_limit_cnt,0,
15129
14916
                                        true) <=
15130
14917
              0)
15131
 
            DBUG_RETURN(0);
 
14918
            return(0);
15132
14919
        }
15133
14920
        ref_key= new_ref_key;
15134
14921
      }
15172
14959
        index order and not using join cache
15173
14960
        */
15174
14961
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
15175
 
        DBUG_RETURN(0);
 
14962
        return(0);
15176
14963
      keys= *table->file->keys_to_use_for_scanning();
15177
14964
      keys.merge(table->covering_keys);
15178
14965
 
15348
15135
            We need to change the access method so as the quick access
15349
15136
            method is actually used.
15350
15137
          */
15351
 
          DBUG_ASSERT(tab->select->quick);
 
15138
          assert(tab->select->quick);
15352
15139
          tab->type=JT_ALL;
15353
15140
          tab->use_quick=1;
15354
15141
          tab->ref.key= -1;
15363
15150
      order_direction= best_key_direction;
15364
15151
    }
15365
15152
    else
15366
 
      DBUG_RETURN(0); 
 
15153
      return(0); 
15367
15154
  } 
15368
15155
 
15369
15156
check_reverse_order:                  
15387
15174
        {
15388
15175
          tab->limit= 0;
15389
15176
          select->quick= save_quick;
15390
 
          DBUG_RETURN(0);                   // Use filesort
 
15177
          return(0);                   // Use filesort
15391
15178
        }
15392
15179
            
15393
15180
        /* ORDER BY range_key DESC */
15398
15185
          delete tmp;
15399
15186
          select->quick= save_quick;
15400
15187
          tab->limit= 0;
15401
 
          DBUG_RETURN(0);               // Reverse sort not supported
 
15188
          return(0);            // Reverse sort not supported
15402
15189
        }
15403
15190
        select->quick=tmp;
15404
15191
      }
15418
15205
  }
15419
15206
  else if (select && select->quick)
15420
15207
    select->quick->sorted= 1;
15421
 
  DBUG_RETURN(1);
 
15208
  return(1);
15422
15209
}
15423
15210
 
15424
15211
 
15461
15248
  TABLE *table;
15462
15249
  SQL_SELECT *select;
15463
15250
  JOIN_TAB *tab;
15464
 
  DBUG_ENTER("create_sort_index");
15465
15251
 
15466
15252
  if (join->tables == join->const_tables)
15467
 
    DBUG_RETURN(0);                             // One row, no need to sort
 
15253
    return(0);                          // One row, no need to sort
15468
15254
  tab=    join->join_tab + join->const_tables;
15469
15255
  table=  tab->table;
15470
15256
  select= tab->select;
15481
15267
      test_if_skip_sort_order(tab,order,select_limit,0, 
15482
15268
                              is_order_by ?  &table->keys_in_use_for_order_by :
15483
15269
                              &table->keys_in_use_for_group_by))
15484
 
    DBUG_RETURN(0);
 
15270
    return(0);
15485
15271
  for (ORDER *ord= join->order; ord; ord= ord->next)
15486
15272
    length++;
15487
15273
  if (!(join->sortorder= 
15550
15336
    table->key_read=0;
15551
15337
    table->file->extra(HA_EXTRA_NO_KEYREAD);
15552
15338
  }
15553
 
  DBUG_RETURN(table->sort.found_records == HA_POS_ERROR);
 
15339
  return(table->sort.found_records == HA_POS_ERROR);
15554
15340
err:
15555
 
  DBUG_RETURN(-1);
 
15341
  return(-1);
15556
15342
}
15557
15343
 
15558
15344
/*****************************************************************************
15601
15387
  ulong reclength,offset;
15602
15388
  uint field_count;
15603
15389
  THD *thd= join->thd;
15604
 
  DBUG_ENTER("remove_duplicates");
15605
15390
 
15606
15391
  entry->reginfo.lock_type=TL_WRITE;
15607
15392
 
15618
15403
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
15619
15404
  {                    // only const items with no OPTION_FOUND_ROWS
15620
15405
    join->unit->select_limit_cnt= 1;            // Only send first row
15621
 
    DBUG_RETURN(0);
 
15406
    return(0);
15622
15407
  }
15623
15408
  Field **first_field=entry->field+entry->s->fields - field_count;
15624
15409
  offset= (field_count ? 
15640
15425
                                  having);
15641
15426
 
15642
15427
  free_blobs(first_field);
15643
 
  DBUG_RETURN(error);
 
15428
  return(error);
15644
15429
}
15645
15430
 
15646
15431
 
15652
15437
  uchar *record;
15653
15438
  int error;
15654
15439
  ulong reclength= table->s->reclength-offset;
15655
 
  DBUG_ENTER("remove_dup_with_compare");
15656
15440
 
15657
15441
  org_record=(char*) (record=table->record[0])+offset;
15658
15442
  new_record=(char*) table->record[1]+offset;
15720
15504
  }
15721
15505
 
15722
15506
  file->extra(HA_EXTRA_NO_CACHE);
15723
 
  DBUG_RETURN(0);
 
15507
  return(0);
15724
15508
err:
15725
15509
  file->extra(HA_EXTRA_NO_CACHE);
15726
15510
  if (error)
15727
15511
    file->print_error(error,MYF(0));
15728
 
  DBUG_RETURN(1);
 
15512
  return(1);
15729
15513
}
15730
15514
 
15731
15515
 
15748
15532
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
15749
15533
  uint *field_lengths,*field_length;
15750
15534
  HASH hash;
15751
 
  DBUG_ENTER("remove_dup_with_hash_index");
15752
15535
 
15753
15536
  if (!my_multi_malloc(MYF(MY_WME),
15754
15537
                       &key_buffer,
15757
15540
                       &field_lengths,
15758
15541
                       (uint) (field_count*sizeof(*field_lengths)),
15759
15542
                       NullS))
15760
 
    DBUG_RETURN(1);
 
15543
    return(1);
15761
15544
 
15762
15545
  {
15763
15546
    Field **ptr;
15768
15551
      (*field_length++)= length;
15769
15552
      total_length+= length;
15770
15553
    }
15771
 
    DBUG_PRINT("info",("field_count: %u  key_length: %lu  total_length: %lu",
15772
 
                       field_count, key_length, total_length));
15773
 
    DBUG_ASSERT(total_length <= key_length);
 
15554
    assert(total_length <= key_length);
15774
15555
    key_length= total_length;
15775
15556
    extra_length= ALIGN_SIZE(key_length)-key_length;
15776
15557
  }
15779
15560
                key_length, (hash_get_key) 0, 0, 0))
15780
15561
  {
15781
15562
    my_free((char*) key_buffer,MYF(0));
15782
 
    DBUG_RETURN(1);
 
15563
    return(1);
15783
15564
  }
15784
15565
 
15785
15566
  file->ha_rnd_init(1);
15831
15612
  hash_free(&hash);
15832
15613
  file->extra(HA_EXTRA_NO_CACHE);
15833
15614
  (void) file->ha_rnd_end();
15834
 
  DBUG_RETURN(0);
 
15615
  return(0);
15835
15616
 
15836
15617
err:
15837
15618
  my_free((char*) key_buffer,MYF(0));
15840
15621
  (void) file->ha_rnd_end();
15841
15622
  if (error)
15842
15623
    file->print_error(error,MYF(0));
15843
 
  DBUG_RETURN(1);
 
15624
  return(1);
15844
15625
}
15845
15626
 
15846
15627
 
15849
15630
{
15850
15631
  uint count;
15851
15632
  SORT_FIELD *sort,*pos;
15852
 
  DBUG_ENTER("make_unireg_sortorder");
15853
15633
 
15854
15634
  count=0;
15855
15635
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
15879
15659
    pos->reverse=! order->asc;
15880
15660
  }
15881
15661
  *length=count;
15882
 
  DBUG_RETURN(sort);
 
15662
  return(sort);
15883
15663
}
15884
15664
 
15885
15665
 
15899
15679
  CACHE_FIELD *copy,**blob_ptr;
15900
15680
  JOIN_CACHE  *cache;
15901
15681
  JOIN_TAB *join_tab;
15902
 
  DBUG_ENTER("join_init_cache");
15903
15682
 
15904
15683
  cache= &tables[table_count].cache;
15905
15684
  cache->fields=blobs=0;
15926
15705
  {
15927
15706
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
15928
15707
    cache->buff=0;                              /* purecov: inspected */
15929
 
    DBUG_RETURN(1);                             /* purecov: inspected */
 
15708
    return(1);                          /* purecov: inspected */
15930
15709
  }
15931
15710
  copy=cache->field;
15932
15711
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
16003
15782
  *blob_ptr=0;                                  /* End sequentel */
16004
15783
  size=max(thd->variables.join_buff_size, cache->length);
16005
15784
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
16006
 
    DBUG_RETURN(1);                             /* Don't use cache */ /* purecov: inspected */
 
15785
    return(1);                          /* Don't use cache */ /* purecov: inspected */
16007
15786
  cache->end=cache->buff+size;
16008
15787
  reset_cache_write(cache);
16009
 
  DBUG_RETURN(0);
 
15788
  return(0);
16010
15789
}
16011
15790
 
16012
15791
 
16678
16457
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
16679
16458
{
16680
16459
  table_map map= (table_map) 0;
16681
 
  DBUG_ENTER("get_sort_by_table");
16682
16460
 
16683
16461
  if (!a)
16684
16462
    a=b;                                        // Only one need to be given
16688
16466
  for (; a && b; a=a->next,b=b->next)
16689
16467
  {
16690
16468
    if (!(*a->item)->eq(*b->item,1))
16691
 
      DBUG_RETURN(0);
 
16469
      return(0);
16692
16470
    map|=a->item[0]->used_tables();
16693
16471
  }
16694
16472
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
16695
 
    DBUG_RETURN(0);
 
16473
    return(0);
16696
16474
 
16697
16475
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
16698
16476
  if (map != tables->table->map)
16699
 
    DBUG_RETURN(0);                             // More than one table
16700
 
  DBUG_PRINT("exit",("sort by table: %d",tables->table->tablenr));
16701
 
  DBUG_RETURN(tables->table);
 
16477
    return(0);                          // More than one table
 
16478
  return(tables->table);
16702
16479
}
16703
16480
 
16704
16481
 
16769
16546
      }
16770
16547
      default:
16771
16548
        /* This case should never be choosen */
16772
 
        DBUG_ASSERT(0);
 
16549
        assert(0);
16773
16550
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
16774
16551
      }
16775
16552
    }
16852
16629
 
16853
16630
int test_if_item_cache_changed(List<Cached_item> &list)
16854
16631
{
16855
 
  DBUG_ENTER("test_if_item_cache_changed");
16856
16632
  List_iterator<Cached_item> li(list);
16857
16633
  int idx= -1,i;
16858
16634
  Cached_item *buff;
16862
16638
    if (buff->cmp())
16863
16639
      idx=i;
16864
16640
  }
16865
 
  DBUG_PRINT("info", ("idx: %d", idx));
16866
 
  DBUG_RETURN(idx);
 
16641
  return(idx);
16867
16642
}
16868
16643
 
16869
16644
 
16910
16685
  List_iterator_fast<Item> itr(res_all_fields);
16911
16686
  List<Item> extra_funcs;
16912
16687
  uint i, border= all_fields.elements - elements;
16913
 
  DBUG_ENTER("setup_copy_fields");
16914
16688
 
16915
16689
  if (param->field_count && 
16916
16690
      !(copy=param->copy_field= new Copy_field[param->field_count]))
17016
16790
  */
17017
16791
  param->copy_funcs.concat(&extra_funcs);
17018
16792
 
17019
 
  DBUG_RETURN(0);
 
16793
  return(0);
17020
16794
 
17021
16795
 err:
17022
16796
  if (copy)
17023
16797
    delete [] param->copy_field;                        // This is never 0
17024
16798
  param->copy_field=0;
17025
16799
err2:
17026
 
  DBUG_RETURN(true);
 
16800
  return(true);
17027
16801
}
17028
16802
 
17029
16803
 
17063
16837
bool JOIN::alloc_func_list()
17064
16838
{
17065
16839
  uint func_count, group_parts;
17066
 
  DBUG_ENTER("alloc_func_list");
17067
16840
 
17068
16841
  func_count= tmp_table_param.sum_func_count;
17069
16842
  /*
17097
16870
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
17098
16871
                                      sizeof(Item_sum***) * (group_parts+1));
17099
16872
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
17100
 
  DBUG_RETURN(sum_funcs == 0);
 
16873
  return(sum_funcs == 0);
17101
16874
}
17102
16875
 
17103
16876
 
17121
16894
  List_iterator_fast<Item> it(field_list);
17122
16895
  Item_sum **func;
17123
16896
  Item *item;
17124
 
  DBUG_ENTER("make_sum_func_list");
17125
16897
 
17126
16898
  if (*sum_funcs && !recompute)
17127
 
    DBUG_RETURN(false); /* We have already initialized sum_funcs. */
 
16899
    return(false); /* We have already initialized sum_funcs. */
17128
16900
 
17129
16901
  func= sum_funcs;
17130
16902
  while ((item=it++))
17138
16910
  {
17139
16911
    rollup.state= ROLLUP::STATE_READY;
17140
16912
    if (rollup_make_fields(field_list, send_fields, &func))
17141
 
      DBUG_RETURN(true);                        // Should never happen
 
16913
      return(true);                     // Should never happen
17142
16914
  }
17143
16915
  else if (rollup.state == ROLLUP::STATE_NONE)
17144
16916
  {
17146
16918
      sum_funcs_end[i]= func;
17147
16919
  }
17148
16920
  else if (rollup.state == ROLLUP::STATE_READY)
17149
 
    DBUG_RETURN(false);                         // Don't put end marker
 
16921
    return(false);                         // Don't put end marker
17150
16922
  *func=0;                                      // End marker
17151
 
  DBUG_RETURN(false);
 
16923
  return(false);
17152
16924
}
17153
16925
 
17154
16926
 
17177
16949
{
17178
16950
  List_iterator_fast<Item> it(all_fields);
17179
16951
  Item *item_field,*item;
17180
 
  DBUG_ENTER("change_to_use_tmp_fields");
17181
16952
 
17182
16953
  res_selected_fields.empty();
17183
16954
  res_all_fields.empty();
17204
16975
        else
17205
16976
          item_field= (Item*) new Item_field(field);
17206
16977
        if (!item_field)
17207
 
          DBUG_RETURN(true);                    // Fatal error
 
16978
          return(true);                    // Fatal error
17208
16979
 
17209
16980
        if (item->real_item()->type() != Item::FIELD_ITEM)
17210
16981
          field->orig_table= 0;
17216
16987
          ifield->table_name= iref->table_name;
17217
16988
          ifield->db_name= iref->db_name;
17218
16989
        }
17219
 
#ifndef DBUG_OFF
17220
 
        if (!item_field->name)
17221
 
        {
17222
 
          char buff[256];
17223
 
          String str(buff,sizeof(buff),&my_charset_bin);
17224
 
          str.length(0);
17225
 
          item->print(&str, QT_ORDINARY);
17226
 
          item_field->name= sql_strmake(str.ptr(),str.length());
17227
 
        }
17228
 
#endif
17229
16990
      }
17230
16991
      else
17231
16992
        item_field= item;
17239
17000
  for (i= 0; i < border; i++)
17240
17001
    itr++;
17241
17002
  itr.sublist(res_selected_fields, elements);
17242
 
  DBUG_RETURN(false);
 
17003
  return(false);
17243
17004
}
17244
17005
 
17245
17006
 
17309
17070
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
17310
17071
{
17311
17072
  Item_sum *func;
17312
 
  DBUG_ENTER("setup_sum_funcs");
17313
17073
  while ((func= *(func_ptr++)))
17314
17074
  {
17315
17075
    if (func->setup(thd))
17316
 
      DBUG_RETURN(true);
 
17076
      return(true);
17317
17077
  }
17318
 
  DBUG_RETURN(false);
 
17078
  return(false);
17319
17079
}
17320
17080
 
17321
17081
 
17397
17157
 
17398
17158
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
17399
17159
{
17400
 
  DBUG_ENTER("add_ref_to_table_cond");
17401
17160
  if (!join_tab->ref.key_parts)
17402
 
    DBUG_RETURN(false);
 
17161
    return(false);
17403
17162
 
17404
17163
  Item_cond_and *cond=new Item_cond_and();
17405
17164
  TABLE *table=join_tab->table;
17406
17165
  int error;
17407
17166
  if (!cond)
17408
 
    DBUG_RETURN(true);
 
17167
    return(true);
17409
17168
 
17410
17169
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
17411
17170
  {
17415
17174
    cond->add(new Item_func_equal(new Item_field(field), value));
17416
17175
  }
17417
17176
  if (thd->is_fatal_error)
17418
 
    DBUG_RETURN(true);
 
17177
    return(true);
17419
17178
 
17420
17179
  if (!cond->fixed)
17421
17180
    cond->fix_fields(thd, (Item**)&cond);
17428
17187
                                          &error)))
17429
17188
    join_tab->select_cond=cond;
17430
17189
 
17431
 
  DBUG_RETURN(error ? true : false);
 
17190
  return(error ? true : false);
17432
17191
}
17433
17192
 
17434
17193
 
17901
17660
  Item *item_null= new Item_null();
17902
17661
  CHARSET_INFO *cs= system_charset_info;
17903
17662
  int quick_type;
17904
 
  DBUG_ENTER("select_describe");
17905
 
  DBUG_PRINT("info", ("Select 0x%lx, type %s, message %s",
17906
 
                      (ulong)join->select_lex, join->select_lex->type,
17907
 
                      message ? message : "NULL"));
17908
17663
  /* Don't log this into the slow query log */
17909
17664
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17910
17665
  join->unit->offset_limit_cnt= 0;
18368
18123
       unit= unit->next_unit())
18369
18124
  {
18370
18125
    if (mysql_explain_union(thd, unit, result))
18371
 
      DBUG_VOID_RETURN;
 
18126
      return;
18372
18127
  }
18373
 
  DBUG_VOID_RETURN;
 
18128
  return;
18374
18129
}
18375
18130
 
18376
18131
 
18377
18132
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18378
18133
{
18379
 
  DBUG_ENTER("mysql_explain_union");
18380
18134
  bool res= 0;
18381
18135
  SELECT_LEX *first= unit->first_select();
18382
18136
 
18428
18182
                        first->options | thd->options | SELECT_DESCRIBE,
18429
18183
                        result, unit, first);
18430
18184
  }
18431
 
  DBUG_RETURN(res || thd->is_error());
 
18185
  return(res || thd->is_error());
18432
18186
}
18433
18187
 
18434
18188
 
18503
18257
      }
18504
18258
    }
18505
18259
  }
18506
 
  DBUG_ASSERT(tables->elements >= 1);
 
18260
  assert(tables->elements >= 1);
18507
18261
  print_table_array(thd, str, table, table + tables->elements);
18508
18262
}
18509
18263
 
18751
18505
 
18752
18506
bool JOIN::change_result(select_result *res)
18753
18507
{
18754
 
  DBUG_ENTER("JOIN::change_result");
18755
18508
  result= res;
18756
18509
  if (result->prepare(fields_list, select_lex->master_unit()) ||
18757
18510
                     result->prepare2())
18758
18511
  {
18759
 
    DBUG_RETURN(true);
 
18512
    return(true);
18760
18513
  }
18761
 
  DBUG_RETURN(false);
 
18514
  return(false);
18762
18515
}
18763
18516
 
18764
18517
/**