~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Jay Pipes
  • Date: 2009-05-31 19:33:00 UTC
  • mto: This revision was merged to the branch mainline in revision 1046.
  • Revision ID: jpipes@serialcoder-20090531193300-35vctlpwhv81hm9u
No code changes...only indentation and style cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
79
79
    return ((Item_func*) cond)->val_int() ? true : false;
80
80
}
81
81
 
82
 
 
83
82
/*
84
83
  This is used to mark equalities that were made from i-th IN-equality.
85
84
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
144
143
  return(res);
145
144
}
146
145
 
147
 
 
148
146
/*
149
147
  Fix fields referenced from inner selects.
150
148
 
185
183
    true  an error occured
186
184
    false ok
187
185
*/
188
 
 
189
 
bool
190
 
fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
191
 
                 Item **ref_pointer_array)
 
186
bool fix_inner_refs(Session *session, 
 
187
                    List<Item> &all_fields, 
 
188
                    Select_Lex *select, 
 
189
                    Item **ref_pointer_array)
192
190
{
193
191
  Item_outer_ref *ref;
194
192
  bool res= false;
365
363
      creation
366
364
    */
367
365
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
368
 
        (select_options & SELECT_DESCRIBE))
 
366
        (select_options & SELECT_DESCRIBE))
369
367
    {
370
368
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
371
369
      {
372
 
        //here is EXPLAIN of subselect or derived table
373
 
        if (join->change_result(result))
374
 
        {
375
 
          return(true);
376
 
        }
 
370
        //here is EXPLAIN of subselect or derived table
 
371
        if (join->change_result(result))
 
372
        {
 
373
          return(true);
 
374
        }
377
375
      }
378
376
      else
379
377
      {
380
378
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
381
379
                               conds, og_num, order, group, having, select_lex, unit)))
382
 
        {
383
 
          goto err;
384
 
        }
 
380
        {
 
381
          goto err;
 
382
        }
385
383
      }
386
384
    }
387
385
    free_join= 0;
390
388
  else
391
389
  {
392
390
    if (!(join= new JOIN(session, fields, select_options, result)))
393
 
        return(true);
 
391
      return(true);
394
392
    session->set_proc_info("init");
395
393
    session->used_tables=0;                         // Updated by setup_fields
396
394
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
920
918
          The result of this is that we're missing some 'ref' accesses.
921
919
          TODO: OptimizerTeam: Fix this
922
920
        */
923
 
        if (!new_fields->val->const_item())
924
 
        {
925
 
          /*
926
 
            If the value matches, we can use the key reference.
927
 
            If not, we keep it until we have examined all new values
928
 
          */
929
 
          if (old->val->eq(new_fields->val, old->field->binary()))
930
 
          {
931
 
            old->level= and_level;
932
 
            old->optimize= ((old->optimize & new_fields->optimize &
933
 
                             KEY_OPTIMIZE_EXISTS) |
934
 
                            ((old->optimize | new_fields->optimize) &
935
 
                             KEY_OPTIMIZE_REF_OR_NULL));
936
 
            old->null_rejecting= (old->null_rejecting &&
937
 
                                  new_fields->null_rejecting);
938
 
          }
939
 
        }
940
 
        else if (old->eq_func && new_fields->eq_func &&
941
 
                 old->val->eq_by_collation(new_fields->val,
942
 
                                           old->field->binary(),
943
 
                                           old->field->charset()))
 
921
        if (!new_fields->val->const_item())
 
922
        {
 
923
          /*
 
924
            If the value matches, we can use the key reference.
 
925
            If not, we keep it until we have examined all new values
 
926
          */
 
927
          if (old->val->eq(new_fields->val, old->field->binary()))
 
928
          {
 
929
            old->level= and_level;
 
930
            old->optimize= ((old->optimize & new_fields->optimize &
 
931
                KEY_OPTIMIZE_EXISTS) |
 
932
                ((old->optimize | new_fields->optimize) &
 
933
                KEY_OPTIMIZE_REF_OR_NULL));
 
934
                  old->null_rejecting= (old->null_rejecting &&
 
935
                                        new_fields->null_rejecting);
 
936
          }
 
937
        }
 
938
        else if (old->eq_func && new_fields->eq_func &&
 
939
                      old->val->eq_by_collation(new_fields->val,
 
940
                                                old->field->binary(),
 
941
                                                old->field->charset()))
944
942
 
945
 
        {
946
 
          old->level= and_level;
947
 
          old->optimize= ((old->optimize & new_fields->optimize &
948
 
                           KEY_OPTIMIZE_EXISTS) |
949
 
                          ((old->optimize | new_fields->optimize) &
950
 
                           KEY_OPTIMIZE_REF_OR_NULL));
951
 
          old->null_rejecting= (old->null_rejecting &&
952
 
                                new_fields->null_rejecting);
953
 
        }
954
 
        else if (old->eq_func && new_fields->eq_func &&
955
 
                 ((old->val->const_item() && old->val->is_null()) ||
956
 
                  new_fields->val->is_null()))
957
 
        {
958
 
          /* field = expression OR field IS NULL */
959
 
          old->level= and_level;
960
 
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
961
 
          /*
962
 
            Remember the NOT NULL value unless the value does not depend
963
 
            on other tables.
 
943
        {
 
944
          old->level= and_level;
 
945
          old->optimize= ((old->optimize & new_fields->optimize &
 
946
              KEY_OPTIMIZE_EXISTS) |
 
947
              ((old->optimize | new_fields->optimize) &
 
948
              KEY_OPTIMIZE_REF_OR_NULL));
 
949
                old->null_rejecting= (old->null_rejecting &&
 
950
                                      new_fields->null_rejecting);
 
951
        }
 
952
        else if (old->eq_func && new_fields->eq_func &&
 
953
          ((old->val->const_item() && old->val->is_null()) ||
 
954
                        new_fields->val->is_null()))
 
955
        {
 
956
          /* field = expression OR field IS NULL */
 
957
          old->level= and_level;
 
958
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
959
          /*
 
960
                  Remember the NOT NULL value unless the value does not depend
 
961
                  on other tables.
 
962
                */
 
963
          if (!old->val->used_tables() && old->val->is_null())
 
964
            old->val= new_fields->val;
 
965
                /* The referred expression can be NULL: */
 
966
                old->null_rejecting= 0;
 
967
        }
 
968
        else
 
969
        {
 
970
          /*
 
971
            We are comparing two different const.  In this case we can't
 
972
            use a key-lookup on this so it's better to remove the value
 
973
            and let the range optimzier handle it
964
974
          */
965
 
          if (!old->val->used_tables() && old->val->is_null())
966
 
            old->val= new_fields->val;
967
 
          /* The referred expression can be NULL: */
968
 
          old->null_rejecting= 0;
969
 
        }
970
 
        else
971
 
        {
972
 
          /*
973
 
            We are comparing two different const.  In this case we can't
974
 
            use a key-lookup on this so it's better to remove the value
975
 
            and let the range optimzier handle it
976
 
          */
977
 
          if (old == --first_free)              // If last item
978
 
            break;
979
 
          *old= *first_free;                    // Remove old value
980
 
          old--;                                // Retry this value
981
 
        }
 
975
          if (old == --first_free)              // If last item
 
976
            break;
 
977
          *old= *first_free;                    // Remove old value
 
978
          old--;                                // Retry this value
 
979
        }
982
980
      }
983
981
    }
984
982
  }
997
995
  return first_free;
998
996
}
999
997
 
1000
 
 
1001
998
/**
1002
999
  Add a possible key to array of possible keys if it's usable as a key
1003
1000
 
1017
1014
  @returns
1018
1015
    *key_fields is incremented if we stored a key in the array
1019
1016
*/
1020
 
 
1021
 
static void
1022
 
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
1023
 
              Field *field, bool eq_func, Item **value, uint32_t num_values,
1024
 
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
1017
static void add_key_field(KEY_FIELD **key_fields,
 
1018
                          uint32_t and_level,
 
1019
                          Item_func *cond,
 
1020
                          Field *field,
 
1021
                          bool eq_func,
 
1022
                          Item **value,
 
1023
                          uint32_t num_values,
 
1024
                          table_map usable_tables,
 
1025
                          SARGABLE_PARAM **sargables)
1025
1026
{
1026
1027
  uint32_t exists_optimize= 0;
1027
1028
  if (!(field->flags & PART_KEY_FLAG))
1049
1050
    {
1050
1051
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
1051
1052
          !field->table->maybe_null || field->null_ptr)
1052
 
        return;                                 // Can't use left join optimize
 
1053
        return;                                 // Can't use left join optimize
1053
1054
      exists_optimize= KEY_OPTIMIZE_EXISTS;
1054
1055
    }
1055
1056
    else
1060
1061
      stat[0].keys|= possible_keys;             // Add possible keys
1061
1062
 
1062
1063
      /*
1063
 
        Save the following cases:
1064
 
        Field op constant
1065
 
        Field LIKE constant where constant doesn't start with a wildcard
1066
 
        Field = field2 where field2 is in a different table
1067
 
        Field op formula
1068
 
        Field IS NULL
1069
 
        Field IS NOT NULL
1070
 
         Field BETWEEN ...
1071
 
         Field IN ...
 
1064
        Save the following cases:
 
1065
        Field op constant
 
1066
        Field LIKE constant where constant doesn't start with a wildcard
 
1067
        Field = field2 where field2 is in a different table
 
1068
        Field op formula
 
1069
        Field IS NULL
 
1070
        Field IS NOT NULL
 
1071
        Field BETWEEN ...
 
1072
        Field IN ...
1072
1073
      */
1073
1074
      stat[0].key_dependent|= used_tables;
1074
1075
 
1094
1095
        (*sargables)->num_values= num_values;
1095
1096
      }
1096
1097
      /*
1097
 
        We can't always use indexes when comparing a string index to a
1098
 
        number. cmp_type() is checked to allow compare of dates to numbers.
 
1098
        We can't always use indexes when comparing a string index to a
 
1099
        number. cmp_type() is checked to allow compare of dates to numbers.
1099
1100
        eq_func is NEVER true when num_values > 1
1100
1101
       */
1101
1102
      if (!eq_func)
1189
1190
  @returns
1190
1191
    *key_fields is incremented if we stored a key in the array
1191
1192
*/
1192
 
 
1193
 
static void
1194
 
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
1195
 
                     Item_func *cond, Item_field *field_item,
1196
 
                     bool eq_func, Item **val,
1197
 
                     uint32_t num_values, table_map usable_tables,
1198
 
                     SARGABLE_PARAM **sargables)
 
1193
static void add_key_equal_fields(KEY_FIELD **key_fields,
 
1194
                                 uint32_t and_level,
 
1195
                                 Item_func *cond,
 
1196
                                 Item_field *field_item,
 
1197
                                 bool eq_func,
 
1198
                                 Item **val,
 
1199
                                 uint32_t num_values,
 
1200
                                 table_map usable_tables,
 
1201
                                 SARGABLE_PARAM **sargables)
1199
1202
{
1200
1203
  Field *field= field_item->field;
1201
1204
  add_key_field(key_fields, and_level, cond, field,
1221
1224
  }
1222
1225
}
1223
1226
 
1224
 
static void
1225
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
1226
 
               COND *cond, table_map usable_tables,
1227
 
               SARGABLE_PARAM **sargables)
 
1227
static void add_key_fields(JOIN *join, 
 
1228
                           KEY_FIELD **key_fields,
 
1229
                           uint32_t *and_level,
 
1230
                           COND *cond,
 
1231
                           table_map usable_tables,
 
1232
                           SARGABLE_PARAM **sargables)
1228
1233
{
1229
1234
  if (cond->type() == Item_func::COND_ITEM)
1230
1235
  {
1238
1243
        add_key_fields(join, key_fields, and_level, item, usable_tables,
1239
1244
                       sargables);
1240
1245
      for (; org_key_fields != *key_fields ; org_key_fields++)
1241
 
        org_key_fields->level= *and_level;
 
1246
        org_key_fields->level= *and_level;
1242
1247
    }
1243
1248
    else
1244
1249
    {
1248
1253
      Item *item;
1249
1254
      while ((item=li++))
1250
1255
      {
1251
 
        KEY_FIELD *start_key_fields= *key_fields;
1252
 
        (*and_level)++;
1253
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
1254
 
                       sargables);
1255
 
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
1256
 
                                     *key_fields,++(*and_level));
 
1256
        KEY_FIELD *start_key_fields= *key_fields;
 
1257
        (*and_level)++;
 
1258
              add_key_fields(join, key_fields, and_level, item, usable_tables,
 
1259
                            sargables);
 
1260
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
1261
                  *key_fields,++(*and_level));
1257
1262
      }
1258
1263
    }
1259
1264
    return;
1297
1302
    Item **values;
1298
1303
    // BETWEEN, IN, NE
1299
1304
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
1300
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
1305
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
1301
1306
    {
1302
1307
      values= cond_func->arguments()+1;
1303
1308
      if (cond_func->functype() == Item_func::NE_FUNC &&
1337
1342
                     cond_func->functype() == Item_func::EQUAL_FUNC);
1338
1343
 
1339
1344
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
1340
 
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
1345
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
1341
1346
    {
1342
1347
      add_key_equal_fields(key_fields, *and_level, cond_func,
1343
1348
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
1346
1351
                           sargables);
1347
1352
    }
1348
1353
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
1349
 
        cond_func->functype() != Item_func::LIKE_FUNC &&
1350
 
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
1354
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
1355
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
1351
1356
    {
1352
1357
      add_key_equal_fields(key_fields, *and_level, cond_func,
1353
 
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
1354
 
                           equal_func,
 
1358
                       (Item_field*) (cond_func->arguments()[1])->real_item(), equal_func,
1355
1359
                           cond_func->arguments(),1,usable_tables,
1356
1360
                           sargables);
1357
1361
    }
1360
1364
  case Item_func::OPTIMIZE_NULL:
1361
1365
    /* column_name IS [NOT] NULL */
1362
1366
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
1363
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
1367
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
1364
1368
    {
1365
1369
      Item *tmp=new Item_null;
1366
1370
      if (unlikely(!tmp))                       // Should never be true
1367
 
        return;
 
1371
        return;
1368
1372
      add_key_equal_fields(key_fields, *and_level, cond_func,
1369
1373
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
1370
1374
                    cond_func->functype() == Item_func::ISNULL_FUNC,
1429
1433
  return found;
1430
1434
}
1431
1435
 
1432
 
static void
1433
 
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
1436
static void add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
1434
1437
{
1435
1438
  Field *field=key_field->field;
1436
1439
  Table *form= field->table;
1441
1444
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
1442
1445
    {
1443
1446
      if (!(form->keys_in_use_for_query.test(key)))
1444
 
        continue;
 
1447
        continue;
1445
1448
 
1446
1449
      uint32_t key_parts= (uint32_t) form->key_info[key].key_parts;
1447
1450
      for (uint32_t part=0 ; part <  key_parts ; part++)
1448
1451
      {
1449
 
        if (field->eq(form->key_info[key].key_part[part].field))
1450
 
        {
1451
 
          keyuse.table= field->table;
1452
 
          keyuse.val =  key_field->val;
1453
 
          keyuse.key =  key;
1454
 
          keyuse.keypart=part;
1455
 
          keyuse.keypart_map= (key_part_map) 1 << part;
1456
 
          keyuse.used_tables=key_field->val->used_tables();
1457
 
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
1458
 
          keyuse.null_rejecting= key_field->null_rejecting;
1459
 
          keyuse.cond_guard= key_field->cond_guard;
1460
 
          keyuse.sj_pred_no= key_field->sj_pred_no;
1461
 
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
1462
 
        }
 
1452
        if (field->eq(form->key_info[key].key_part[part].field))
 
1453
        {
 
1454
          keyuse.table= field->table;
 
1455
          keyuse.val =  key_field->val;
 
1456
          keyuse.key =  key;
 
1457
          keyuse.keypart=part;
 
1458
          keyuse.keypart_map= (key_part_map) 1 << part;
 
1459
          keyuse.used_tables=key_field->val->used_tables();
 
1460
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
1461
                keyuse.null_rejecting= key_field->null_rejecting;
 
1462
                keyuse.cond_guard= key_field->cond_guard;
 
1463
                keyuse.sj_pred_no= key_field->sj_pred_no;
 
1464
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
1465
        }
1463
1466
      }
1464
1467
    }
1465
1468
  }
1466
1469
}
1467
1470
 
1468
 
static int
1469
 
sort_keyuse(KEYUSE *a,KEYUSE *b)
 
1471
static int sort_keyuse(KEYUSE *a,KEYUSE *b)
1470
1472
{
1471
1473
  int res;
1472
1474
  if (a->table->tablenr != b->table->tablenr)
1484
1486
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
1485
1487
}
1486
1488
 
1487
 
 
1488
1489
/*
1489
1490
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
1490
1491
 
1519
1520
    @endcode
1520
1521
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
1521
1522
*/
1522
 
 
1523
 
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
1524
 
                                  KEY_FIELD **end, uint32_t *and_level,
 
1523
static void add_key_fields_for_nj(JOIN *join,
 
1524
                                  TableList *nested_join_table,
 
1525
                                  KEY_FIELD **end,
 
1526
                                  uint32_t *and_level,
1525
1527
                                  SARGABLE_PARAM **sargables)
1526
1528
{
1527
1529
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
1555
1557
                   sargables);
1556
1558
}
1557
1559
 
1558
 
 
1559
1560
/**
1560
1561
  Update keyuse array with all possible keys we can use to fetch rows.
1561
1562
 
1697
1698
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
1698
1699
    {
1699
1700
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
1700
 
        use->table->const_key_parts[use->key]|= use->keypart_map;
 
1701
        use->table->const_key_parts[use->key]|= use->keypart_map;
 
1702
      if (use->key == prev->key && use->table == prev->table)
1701
1703
      {
1702
 
        if (use->key == prev->key && use->table == prev->table)
1703
 
        {
1704
 
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
1705
 
            continue;                           /* remove */
1706
 
        }
1707
 
        else if (use->keypart != 0)             // First found must be 0
1708
 
          continue;
 
1704
        if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
1705
          continue;                             /* remove */
1709
1706
      }
 
1707
      else if (use->keypart != 0)               // First found must be 0
 
1708
        continue;
1710
1709
 
1711
1710
#ifdef HAVE_purify
1712
1711
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
1717
1716
      found_eq_constant= !use->used_tables;
1718
1717
      /* Save ptr to first use */
1719
1718
      if (!use->table->reginfo.join_tab->keyuse)
1720
 
        use->table->reginfo.join_tab->keyuse=save_pos;
 
1719
        use->table->reginfo.join_tab->keyuse=save_pos;
1721
1720
      use->table->reginfo.join_tab->checked_keys.set(use->key);
1722
1721
      save_pos++;
1723
1722
    }
1747
1746
      To avoid bad matches, we don't make ref_table_rows less than 100.
1748
1747
    */
1749
1748
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
1750
 
    if (keyuse->used_tables &
1751
 
        (map= (keyuse->used_tables & ~join->const_table_map &
1752
 
               ~OUTER_REF_TABLE_BIT)))
 
1749
    if (keyuse->used_tables & (map= (keyuse->used_tables & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
1753
1750
    {
1754
1751
      uint32_t tablenr;
1755
1752
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
1756
1753
      if (map == 1)                     // Only one table
1757
1754
      {
1758
 
        Table *tmp_table=join->all_tables[tablenr];
1759
 
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
 
1755
        Table *tmp_table=join->all_tables[tablenr];
 
1756
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
1760
1757
      }
1761
1758
    }
1762
1759
    /*
1827
1824
    join_tab->const_keys|= possible_keys;
1828
1825
}
1829
1826
 
1830
 
 
1831
1827
/*****************************************************************************
1832
1828
  Go through all combinations of not marked tables and find the one
1833
1829
  which uses least records
1911
1907
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
1912
1908
}
1913
1909
 
1914
 
 
1915
1910
/**
1916
1911
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
1917
1912
*/
1918
 
 
1919
1913
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
1920
1914
{
1921
1915
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
1945
1939
      fields++;
1946
1940
      rec_length+=field->pack_length();
1947
1941
      if (flags & BLOB_FLAG)
1948
 
        blobs++;
 
1942
        blobs++;
1949
1943
      if (!(flags & NOT_NULL_FLAG))
1950
 
        null_fields++;
 
1944
        null_fields++;
1951
1945
    }
1952
1946
  }
1953
1947
  if (null_fields)
2008
2002
  @return
2009
2003
    returns 1 if there was some conversion made when the field was stored.
2010
2004
*/
2011
 
 
2012
 
bool
2013
 
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
2005
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
2014
2006
{
2015
2007
  bool error;
2016
2008
  Table *table= field->table;
2314
2306
    true   Yes
2315
2307
    false  No
2316
2308
*/
2317
 
 
2318
 
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno,
2319
 
                            bool other_tbls_ok)
 
2309
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
2320
2310
{
2321
2311
  if (item->const_item())
2322
2312
    return true;
2379
2369
  }
2380
2370
}
2381
2371
 
2382
 
 
2383
2372
#define ICP_COND_USES_INDEX_ONLY 10
2384
2373
 
2385
2374
/*
2409
2398
  RETURN
2410
2399
    Index condition, or NULL if no condition could be inferred.
2411
2400
*/
2412
 
 
2413
 
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
2414
 
                          bool other_tbls_ok)
 
2401
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
2415
2402
{
2416
2403
  if (!cond)
2417
2404
    return NULL;
2422
2409
    {
2423
2410
      Item_cond_and *new_cond=new Item_cond_and;
2424
2411
      if (!new_cond)
2425
 
        return (COND*) 0;
 
2412
        return (COND*) 0;
2426
2413
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2427
2414
      Item *item;
2428
2415
      while ((item=li++))
2429
2416
      {
2430
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
2431
 
        if (fix)
2432
 
          new_cond->argument_list()->push_back(fix);
 
2417
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
2418
        if (fix)
 
2419
          new_cond->argument_list()->push_back(fix);
2433
2420
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
2434
2421
      }
2435
2422
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
2436
2423
        cond->marker= ICP_COND_USES_INDEX_ONLY;
2437
2424
      switch (new_cond->argument_list()->elements) {
2438
2425
      case 0:
2439
 
        return (COND*) 0;
 
2426
        return (COND*) 0;
2440
2427
      case 1:
2441
 
        return new_cond->argument_list()->head();
 
2428
        return new_cond->argument_list()->head();
2442
2429
      default:
2443
 
        new_cond->quick_fix_field();
2444
 
        return new_cond;
 
2430
        new_cond->quick_fix_field();
 
2431
        return new_cond;
2445
2432
      }
2446
2433
    }
2447
2434
    else /* It's OR */
2448
2435
    {
2449
2436
      Item_cond_or *new_cond=new Item_cond_or;
2450
2437
      if (!new_cond)
2451
 
        return (COND*) 0;
 
2438
        return (COND*) 0;
2452
2439
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2453
2440
      Item *item;
2454
2441
      while ((item=li++))
2455
2442
      {
2456
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
2457
 
        if (!fix)
2458
 
          return (COND*) 0;
2459
 
        new_cond->argument_list()->push_back(fix);
 
2443
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
2444
        if (!fix)
 
2445
          return (COND*) 0;
 
2446
        new_cond->argument_list()->push_back(fix);
2460
2447
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
2461
2448
      }
2462
2449
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
2487
2474
      /* Create new top level AND item */
2488
2475
      Item_cond_and *new_cond=new Item_cond_and;
2489
2476
      if (!new_cond)
2490
 
        return (COND*) 0;
 
2477
        return (COND*) 0;
2491
2478
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2492
2479
      Item *item;
2493
2480
      while ((item=li++))
2494
2481
      {
2495
 
        Item *fix= make_cond_remainder(item, exclude_index);
2496
 
        if (fix)
 
2482
        Item *fix= make_cond_remainder(item, exclude_index);
 
2483
        if (fix)
2497
2484
        {
2498
 
          new_cond->argument_list()->push_back(fix);
 
2485
          new_cond->argument_list()->push_back(fix);
2499
2486
          tbl_map |= fix->used_tables();
2500
2487
        }
2501
2488
      }
2502
2489
      switch (new_cond->argument_list()->elements) {
2503
2490
      case 0:
2504
 
        return (COND*) 0;
 
2491
        return (COND*) 0;
2505
2492
      case 1:
2506
 
        return new_cond->argument_list()->head();
 
2493
        return new_cond->argument_list()->head();
2507
2494
      default:
2508
 
        new_cond->quick_fix_field();
 
2495
        new_cond->quick_fix_field();
2509
2496
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
2510
 
        return new_cond;
 
2497
        return new_cond;
2511
2498
      }
2512
2499
    }
2513
2500
    else /* It's OR */
2514
2501
    {
2515
2502
      Item_cond_or *new_cond=new Item_cond_or;
2516
2503
      if (!new_cond)
2517
 
        return (COND*) 0;
 
2504
        return (COND*) 0;
2518
2505
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
2519
2506
      Item *item;
2520
2507
      while ((item=li++))
2521
2508
      {
2522
 
        Item *fix= make_cond_remainder(item, false);
2523
 
        if (!fix)
2524
 
          return (COND*) 0;
2525
 
        new_cond->argument_list()->push_back(fix);
 
2509
        Item *fix= make_cond_remainder(item, false);
 
2510
        if (!fix)
 
2511
          return (COND*) 0;
 
2512
        new_cond->argument_list()->push_back(fix);
2526
2513
        tbl_map |= fix->used_tables();
2527
2514
      }
2528
2515
      new_cond->quick_fix_field();
2534
2521
  return cond;
2535
2522
}
2536
2523
 
2537
 
 
2538
2524
/*
2539
2525
  Try to extract and push the index condition
2540
2526
 
2560
2546
    if (idx_cond)
2561
2547
    {
2562
2548
      tab->pre_idx_push_select_cond= tab->select_cond;
2563
 
      Item *idx_remainder_cond=
2564
 
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
2549
      Item *idx_remainder_cond= tab->table->file->idx_cond_push(keyno, idx_cond);
2565
2550
 
2566
2551
      /*
2567
2552
        Disable eq_ref's "lookup cache" if we've pushed down an index
2583
2568
        else
2584
2569
        {
2585
2570
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
2586
 
          tab->select_cond->quick_fix_field();
 
2571
          tab->select_cond->quick_fix_field();
2587
2572
          ((Item_cond_and*)tab->select_cond)->used_tables_cache=
2588
2573
            row_cond->used_tables() | idx_remainder_cond->used_tables();
2589
2574
        }
2691
2676
        continue;                               // Used in order_st BY
2692
2677
      }
2693
2678
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
2694
 
        return (tab->eq_ref_table=0);
 
2679
        return (tab->eq_ref_table= 0);
2695
2680
    }
2696
2681
  }
2697
2682
  /* Check that there was no reference to table before sort order */
2703
2688
      continue;
2704
2689
    }
2705
2690
    if (start_order->depend_map & map)
2706
 
      return (tab->eq_ref_table=0);
 
2691
      return (tab->eq_ref_table= 0);
2707
2692
  }
2708
 
  return tab->eq_ref_table=1;
 
2693
  return tab->eq_ref_table= 1;
2709
2694
}
2710
2695
 
2711
 
 
2712
2696
/**
2713
2697
  Find the multiple equality predicate containing a field.
2714
2698
 
2727
2711
    - Item_equal for the found multiple equality predicate if a success;
2728
2712
    - NULL otherwise.
2729
2713
*/
2730
 
 
2731
 
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
2732
 
                            bool *inherited_fl)
 
2714
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
2733
2715
{
2734
2716
  Item_equal *item= 0;
2735
2717
  bool in_upper_level= false;
2750
2732
  return item;
2751
2733
}
2752
2734
 
2753
 
 
2754
2735
/**
2755
2736
  Check whether an equality can be used to build multiple equalities.
2756
2737
 
2832
2813
  @retval
2833
2814
    false   otherwise
2834
2815
*/
2835
 
 
2836
 
static bool check_simple_equality(Item *left_item, Item *right_item,
2837
 
                                  Item *item, COND_EQUAL *cond_equal)
 
2816
static bool check_simple_equality(Item *left_item,
 
2817
                                  Item *right_item,
 
2818
                                  Item *item,
 
2819
                                  COND_EQUAL *cond_equal)
2838
2820
{
2839
2821
  if (left_item->type() == Item::FIELD_ITEM &&
2840
2822
      right_item->type() == Item::FIELD_ITEM &&
2995
2977
  return false;
2996
2978
}
2997
2979
 
2998
 
 
2999
2980
/**
3000
2981
  Convert row equalities into a conjunction of regular equalities.
3001
2982
 
3021
3002
  @retval
3022
3003
    false   otherwise
3023
3004
*/
3024
 
 
3025
 
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row,
3026
 
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
3005
static bool check_row_equality(Session *session,
 
3006
                               Item *left_row, 
 
3007
                               Item_row *right_row,
 
3008
                               COND_EQUAL *cond_equal,
 
3009
                               List<Item>* eq_list)
3027
3010
{
3028
3011
  uint32_t n= left_row->cols();
3029
3012
  for (uint32_t i= 0 ; i < n; i++)
3060
3043
  return true;
3061
3044
}
3062
3045
 
3063
 
 
3064
3046
/**
3065
3047
  Eliminate row equalities and form multiple equalities predicates.
3066
3048
 
3090
3072
           or, if the equality is neither a simple one nor a row equality,
3091
3073
           or, if the procedure fails by a fatal error.
3092
3074
*/
3093
 
 
3094
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal,
3095
 
                           List<Item> *eq_list)
 
3075
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
3096
3076
{
3097
3077
  if (item->type() == Item::FUNC_ITEM &&
3098
3078
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
3115
3095
  return false;
3116
3096
}
3117
3097
 
3118
 
 
3119
3098
/**
3120
3099
  Replace all equality predicates in a condition by multiple equality items.
3121
3100
 
3179
3158
  @return
3180
3159
    pointer to the transformed condition
3181
3160
*/
3182
 
 
3183
 
static COND *build_equal_items_for_cond(Session *session, COND *cond,
3184
 
                                        COND_EQUAL *inherited)
 
3161
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
3185
3162
{
3186
3163
  Item_equal *item_equal;
3187
3164
  COND_EQUAL cond_equal;
3277
3254
        {
3278
3255
          item_equal->fix_length_and_dec();
3279
3256
          item_equal->update_used_tables();
3280
 
        }
 
3257
        }
3281
3258
        else
3282
3259
          item_equal= (Item_equal *) eq_list.pop();
3283
3260
        set_if_bigger(session->lex->current_select->max_equal_elems,
3289
3266
        /*
3290
3267
          Here a new AND level must be created. It can happen only
3291
3268
          when a row equality is processed as a standalone predicate.
3292
 
        */
 
3269
        */
3293
3270
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
3294
3271
        and_cond->quick_fix_field();
3295
3272
        List<Item> *args= and_cond->argument_list();
3323
3300
  return cond;
3324
3301
}
3325
3302
 
3326
 
 
3327
3303
/**
3328
3304
  Build multiple equalities for a condition and all on expressions that
3329
3305
  inherit these multiple equalities.
3442
3418
  return cond;
3443
3419
}
3444
3420
 
3445
 
 
3446
3421
/**
3447
3422
  Compare field items by table order in the execution plan.
3448
3423
 
3462
3437
  @retval
3463
3438
    0  otherwise
3464
3439
*/
3465
 
 
3466
3440
static int compare_fields_by_table_order(Item_field *field1,
3467
 
                                  Item_field *field2,
3468
 
                                  void *table_join_idx)
 
3441
                                         Item_field *field2,
 
3442
                                         void *table_join_idx)
3469
3443
{
3470
3444
  int cmp= 0;
3471
3445
  bool outer_ref= 0;
3486
3460
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
3487
3461
}
3488
3462
 
3489
 
 
3490
3463
/**
3491
3464
  Generate minimal set of simple equalities equivalent to a multiple equality.
3492
3465
 
3526
3499
    a pointer to the simple generated equality, if success.
3527
3500
    - 0, otherwise.
3528
3501
*/
3529
 
 
3530
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
3531
 
                                  Item_equal *item_equal)
 
3502
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
3532
3503
{
3533
3504
  List<Item> eq_list;
3534
3505
  Item_func_eq *eq_item= 0;
3598
3569
  return cond;
3599
3570
}
3600
3571
 
3601
 
 
3602
3572
/**
3603
3573
  Substitute every field reference in a condition by the best equal field
3604
3574
  and eliminate all multiple equality predicates.
3694
3664
  return cond;
3695
3665
}
3696
3666
 
3697
 
 
3698
3667
/**
3699
3668
  Check appearance of new constant items in multiple equalities
3700
3669
  of a condition after reading a constant table.
3707
3676
  @param cond       condition whose multiple equalities are to be checked
3708
3677
  @param table      constant table that has been read
3709
3678
*/
3710
 
 
3711
3679
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
3712
3680
{
3713
3681
  if (!(cond->used_tables() & tab->table->map))
3760
3728
  }
3761
3729
}
3762
3730
 
3763
 
 
3764
3731
/*
3765
3732
  change field = field to field = const for each found field = const in the
3766
3733
  and_level
3779
3746
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
3780
3747
    Item *item;
3781
3748
    while ((item=li++))
3782
 
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item,
3783
 
                               field, value);
 
3749
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item, field, value);
3784
3750
    return;
3785
3751
  }
3786
3752
  if (cond->eq_cmp_result() == Item::COND_OK)
3805
3771
    {
3806
3772
      session->change_item_tree(args + 1, tmp);
3807
3773
      func->update_used_tables();
3808
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
3809
 
          && and_father != cond && !left_item->const_item())
 
3774
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
3775
                and_father != cond && 
 
3776
          ! left_item->const_item())
3810
3777
      {
3811
 
        cond->marker=1;
3812
 
        COND_CMP *tmp2;
3813
 
        if ((tmp2=new COND_CMP(and_father,func)))
3814
 
          save_list->push_back(tmp2);
 
3778
        cond->marker=1;
 
3779
        COND_CMP *tmp2;
 
3780
        if ((tmp2=new COND_CMP(and_father,func)))
 
3781
          save_list->push_back(tmp2);
3815
3782
      }
3816
3783
      func->set_cmp_func();
3817
3784
    }
3830
3797
      session->change_item_tree(args, tmp);
3831
3798
      value= tmp;
3832
3799
      func->update_used_tables();
3833
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
3834
 
          && and_father != cond && !right_item->const_item())
 
3800
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
3801
          and_father != cond && 
 
3802
          ! right_item->const_item())
3835
3803
      {
3836
3804
        args[0]= args[1];                       // For easy check
3837
3805
        session->change_item_tree(args + 1, value);
3838
 
        cond->marker=1;
3839
 
        COND_CMP *tmp2;
3840
 
        if ((tmp2=new COND_CMP(and_father,func)))
3841
 
          save_list->push_back(tmp2);
 
3806
        cond->marker=1;
 
3807
        COND_CMP *tmp2;
 
3808
        if ((tmp2=new COND_CMP(and_father,func)))
 
3809
          save_list->push_back(tmp2);
3842
3810
      }
3843
3811
      func->set_cmp_func();
3844
3812
    }
3876
3844
  return conds;
3877
3845
}
3878
3846
 
3879
 
static void
3880
 
propagate_cond_constants(Session *session, I_List<COND_CMP> *save_list,
3881
 
                         COND *and_father, COND *cond)
 
3847
static void propagate_cond_constants(Session *session, 
 
3848
                                     I_List<COND_CMP> *save_list, 
 
3849
                                     COND *and_father, 
 
3850
                                     COND *cond)
3882
3851
{
3883
3852
  if (cond->type() == Item::COND_ITEM)
3884
3853
  {
3907
3876
  else if (and_father != cond && !cond->marker)         // In a AND group
3908
3877
  {
3909
3878
    if (cond->type() == Item::FUNC_ITEM &&
3910
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
3911
 
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
3879
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
3880
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
3912
3881
    {
3913
3882
      Item_func_eq *func=(Item_func_eq*) cond;
3914
3883
      Item **args= func->arguments();
3917
3886
      if (!(left_const && right_const) &&
3918
3887
          args[0]->result_type() == args[1]->result_type())
3919
3888
      {
3920
 
        if (right_const)
3921
 
        {
3922
 
          resolve_const_item(session, &args[1], args[0]);
3923
 
          func->update_used_tables();
3924
 
          change_cond_ref_to_const(session, save_list, and_father, and_father,
3925
 
                                   args[0], args[1]);
3926
 
        }
3927
 
        else if (left_const)
3928
 
        {
3929
 
          resolve_const_item(session, &args[0], args[1]);
3930
 
          func->update_used_tables();
3931
 
          change_cond_ref_to_const(session, save_list, and_father, and_father,
3932
 
                                   args[1], args[0]);
3933
 
        }
 
3889
        if (right_const)
 
3890
        {
 
3891
                resolve_const_item(session, &args[1], args[0]);
 
3892
          func->update_used_tables();
 
3893
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
3894
                                        args[0], args[1]);
 
3895
        }
 
3896
        else if (left_const)
 
3897
        {
 
3898
                resolve_const_item(session, &args[0], args[1]);
 
3899
          func->update_used_tables();
 
3900
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
3901
                                        args[1], args[0]);
 
3902
        }
3934
3903
      }
3935
3904
    }
3936
3905
  }
4131
4100
  return(conds);
4132
4101
}
4133
4102
 
4134
 
 
4135
4103
/**
4136
4104
  Remove const and eq items.
4137
4105
 
4142
4110
    - COND_TRUE   : always true ( 1 = 1 )
4143
4111
    - COND_FALSE  : always false        ( 1 = 2 )
4144
4112
*/
4145
 
 
4146
 
COND *
4147
 
remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
4113
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
4148
4114
{
4149
4115
  if (cond->type() == Item::COND_ITEM)
4150
4116
  {
4333
4299
    true    can be used
4334
4300
    false   cannot be used
4335
4301
*/
4336
 
static bool
4337
 
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
4302
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
4338
4303
{
4339
4304
  return r->const_item() &&
4340
4305
    /* elements must be compared as dates */
4362
4327
      bool res=const_expression_in_where(item, comp_item, const_item);
4363
4328
      if (res)                                  // Is a const value
4364
4329
      {
4365
 
        if (and_level)
4366
 
          return 1;
 
4330
        if (and_level)
 
4331
          return 1;
4367
4332
      }
4368
4333
      else if (!and_level)
4369
 
        return 0;
 
4334
        return 0;
4370
4335
    }
4371
4336
    return and_level ? 0 : 1;
4372
4337
  }
4374
4339
  {                                             // boolan compare function
4375
4340
    Item_func* func= (Item_func*) cond;
4376
4341
    if (func->functype() != Item_func::EQUAL_FUNC &&
4377
 
        func->functype() != Item_func::EQ_FUNC)
 
4342
              func->functype() != Item_func::EQ_FUNC)
4378
4343
      return 0;
4379
4344
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
4380
4345
    Item *right_item= ((Item_func*) cond)->arguments()[1];
4382
4347
    {
4383
4348
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
4384
4349
      {
4385
 
        if (*const_item)
4386
 
          return right_item->eq(*const_item, 1);
4387
 
        *const_item=right_item;
4388
 
        return 1;
 
4350
        if (*const_item)
 
4351
          return right_item->eq(*const_item, 1);
 
4352
        *const_item=right_item;
 
4353
        return 1;
4389
4354
      }
4390
4355
    }
4391
4356
    else if (right_item->eq(comp_item,1))
4392
4357
    {
4393
4358
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
4394
4359
      {
4395
 
        if (*const_item)
4396
 
          return left_item->eq(*const_item, 1);
4397
 
        *const_item=left_item;
4398
 
        return 1;
 
4360
        if (*const_item)
 
4361
          return left_item->eq(*const_item, 1);
 
4362
        *const_item=left_item;
 
4363
        return 1;
4399
4364
      }
4400
4365
    }
4401
4366
  }
4402
4367
  return 0;
4403
4368
}
4404
4369
 
4405
 
 
4406
4370
/**
4407
4371
  @details
4408
4372
  Rows produced by a join sweep may end up in a temporary table or be sent
4414
4378
  @return
4415
4379
    end_select function to use. This function can't fail.
4416
4380
*/
4417
 
 
4418
4381
Next_select_func setup_end_select_func(JOIN *join)
4419
4382
{
4420
4383
  Table *table= join->tmp_table;
4429
4392
    {
4430
4393
      if (table->s->keys)
4431
4394
      {
4432
 
        end_select=end_update;
 
4395
        end_select= end_update;
4433
4396
      }
4434
4397
      else
4435
4398
      {
4436
 
        end_select=end_unique_update;
 
4399
        end_select= end_unique_update;
4437
4400
      }
4438
4401
    }
4439
4402
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
4581
4544
  return(join->session->is_error() ? -1 : rc);
4582
4545
}
4583
4546
 
4584
 
enum_nested_loop_state sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
4547
enum_nested_loop_state sub_select_cache(JOIN *join, JOIN_TAB *join_tab, bool end_of_records)
4585
4548
{
4586
4549
  enum_nested_loop_state rc;
4587
4550
 
4937
4900
  {
4938
4901
    table->status= 0;
4939
4902
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
4940
 
      error=HA_ERR_KEY_NOT_FOUND;
 
4903
      error= HA_ERR_KEY_NOT_FOUND;
4941
4904
    else
4942
4905
    {
4943
4906
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
5153
5116
    table->status=STATUS_NOT_FOUND;
5154
5117
    error= -1;
5155
5118
  }
5156
 
 
5157
5119
  return error;
5158
5120
}
5159
5121
 
5564
5526
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
5565
5527
{
5566
5528
  if (used_table && !(cond->used_tables() & used_table) &&
5567
 
      /*
5568
 
        Exclude constant conditions not checked at optimization time if
5569
 
        the table we are pushing conditions to is the first one.
5570
 
        As a result, such conditions are not considered as already checked
5571
 
        and will be checked at execution time, attached to the first table.
5572
 
      */
5573
 
      !((used_table & 1) && cond->is_expensive()))
 
5529
    /*
 
5530
      Exclude constant conditions not checked at optimization time if
 
5531
      the table we are pushing conditions to is the first one.
 
5532
      As a result, such conditions are not considered as already checked
 
5533
      and will be checked at execution time, attached to the first table.
 
5534
    */
 
5535
    !((used_table & 1) && cond->is_expensive()))
5574
5536
    return (COND*) 0;                           // Already checked
5575
5537
  if (cond->type() == Item::COND_ITEM)
5576
5538
  {
5579
5541
      /* Create new top level AND item */
5580
5542
      Item_cond_and *new_cond=new Item_cond_and;
5581
5543
      if (!new_cond)
5582
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
5544
        return (COND*) 0;                       // OOM /* purecov: inspected */
5583
5545
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
5584
5546
      Item *item;
5585
5547
      while ((item=li++))
5586
5548
      {
5587
 
        Item *fix= make_cond_for_table(item,tables,used_table,
5588
 
                                      exclude_expensive_cond);
5589
 
        if (fix)
5590
 
          new_cond->argument_list()->push_back(fix);
 
5549
        Item *fix= make_cond_for_table(item,tables,used_table,
 
5550
                                            exclude_expensive_cond);
 
5551
        if (fix)
 
5552
          new_cond->argument_list()->push_back(fix);
5591
5553
      }
5592
 
      switch (new_cond->argument_list()->elements) {
5593
 
      case 0:
5594
 
        return (COND*) 0;                       // Always true
5595
 
      case 1:
5596
 
        return new_cond->argument_list()->head();
5597
 
      default:
5598
 
        /*
5599
 
          Item_cond_and do not need fix_fields for execution, its parameters
5600
 
          are fixed or do not need fix_fields, too
5601
 
        */
5602
 
        new_cond->quick_fix_field();
5603
 
        new_cond->used_tables_cache=
5604
 
          ((Item_cond_and*) cond)->used_tables_cache &
5605
 
          tables;
5606
 
        return new_cond;
 
5554
      switch (new_cond->argument_list()->elements) 
 
5555
      {
 
5556
        case 0:
 
5557
          return (COND*) 0;                     // Always true
 
5558
        case 1:
 
5559
          return new_cond->argument_list()->head();
 
5560
        default:
 
5561
          /*
 
5562
            Item_cond_and do not need fix_fields for execution, its parameters
 
5563
            are fixed or do not need fix_fields, too
 
5564
          */
 
5565
          new_cond->quick_fix_field();
 
5566
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
 
5567
          return new_cond;
5607
5568
      }
5608
5569
    }
5609
5570
    else
5610
5571
    {                                           // Or list
5611
5572
      Item_cond_or *new_cond=new Item_cond_or;
5612
5573
      if (!new_cond)
5613
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
5574
        return (COND*) 0;                       // OOM /* purecov: inspected */
5614
5575
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
5615
5576
      Item *item;
5616
5577
      while ((item=li++))
5617
5578
      {
5618
 
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
5619
 
        if (!fix)
5620
 
          return (COND*) 0;                     // Always true
5621
 
        new_cond->argument_list()->push_back(fix);
 
5579
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
5580
        if (!fix)
 
5581
          return (COND*) 0;                     // Always true
 
5582
        new_cond->argument_list()->push_back(fix);
5622
5583
      }
5623
5584
      /*
5624
 
        Item_cond_and do not need fix_fields for execution, its parameters
5625
 
        are fixed or do not need fix_fields, too
 
5585
        Item_cond_and do not need fix_fields for execution, its parameters
 
5586
        are fixed or do not need fix_fields, too
5626
5587
      */
5627
5588
      new_cond->quick_fix_field();
5628
5589
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
5670
5631
  return cond;
5671
5632
}
5672
5633
 
5673
 
 
5674
 
static Item *
5675
 
part_of_refkey(Table *table,Field *field)
 
5634
static Item *part_of_refkey(Table *table,Field *field)
5676
5635
{
5677
5636
  if (!table->reginfo.join_tab)
5678
5637
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
5698
5657
  return (Item*) 0;
5699
5658
}
5700
5659
 
5701
 
 
5702
5660
/**
5703
5661
  Test if one can use the key to resolve order_st BY.
5704
5662
 
5719
5677
  @retval
5720
5678
    -1   Reverse key can be used
5721
5679
*/
5722
 
 
5723
 
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
5724
 
                                uint32_t *used_key_parts)
 
5680
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
5725
5681
{
5726
5682
  KEY_PART_INFO *key_part,*key_part_end;
5727
5683
  key_part=table->key_info[idx].key_part;
5790
5746
  return(reverse);
5791
5747
}
5792
5748
 
5793
 
 
5794
5749
/**
5795
5750
  Test if a second key is the subkey of the first one.
5796
5751
 
5806
5761
  @retval
5807
5762
    0   no sub key
5808
5763
*/
5809
 
 
5810
 
inline bool
5811
 
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
5812
 
          KEY_PART_INFO *ref_key_part_end)
 
5764
inline bool is_subkey(KEY_PART_INFO *key_part,
 
5765
                      KEY_PART_INFO *ref_key_part,
 
5766
                            KEY_PART_INFO *ref_key_part_end)
5813
5767
{
5814
5768
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
5815
5769
    if (!key_part->field->eq(ref_key_part->field))
5828
5782
    - MAX_KEY                   If we can't use other key
5829
5783
    - the number of found key   Otherwise
5830
5784
*/
5831
 
 
5832
 
static uint
5833
 
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
5834
 
               const key_map *usable_keys)
 
5785
static uint32_t test_if_subkey(order_st *order,
 
5786
                               Table *table,
 
5787
                               uint32_t ref,
 
5788
                               uint32_t ref_key_parts,
 
5789
                                     const key_map *usable_keys)
5835
5790
{
5836
5791
  uint32_t nr;
5837
5792
  uint32_t min_length= UINT32_MAX;
5856
5811
  return best;
5857
5812
}
5858
5813
 
5859
 
 
5860
5814
/**
5861
5815
  Check if GROUP BY/DISTINCT can be optimized away because the set is
5862
5816
  already known to be distinct.
5914
5868
  return 0;
5915
5869
}
5916
5870
 
5917
 
 
5918
5871
/**
5919
5872
  Helper function for list_contains_unique_index.
5920
5873
  Find a field reference in a list of order_st structures.
6002
5955
  @retval
6003
5956
    1    We can use an index.
6004
5957
*/
6005
 
bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit, bool no_changes, const key_map *map)
 
5958
bool test_if_skip_sort_order(JOIN_TAB *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
6006
5959
{
6007
5960
  int32_t ref_key;
6008
5961
  uint32_t ref_key_parts;
6067
6020
    if (! usable_keys.test(ref_key))
6068
6021
    {
6069
6022
      /*
6070
 
        We come here when ref_key is not among usable_keys
 
6023
        We come here when ref_key is not among usable_keys
6071
6024
      */
6072
6025
      uint32_t new_ref_key;
6073
6026
      /*
6074
 
        If using index only read, only consider other possible index only
6075
 
        keys
 
6027
        If using index only read, only consider other possible index only
 
6028
        keys
6076
6029
      */
6077
6030
      if (table->covering_keys.test(ref_key))
6078
 
        usable_keys&= table->covering_keys;
 
6031
        usable_keys&= table->covering_keys;
6079
6032
      if (tab->pre_idx_push_select_cond)
6080
6033
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
6081
6034
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
6082
6035
                                       &usable_keys)) < MAX_KEY)
6083
6036
      {
6084
 
        /* Found key that can be used to retrieve data in sorted order */
6085
 
        if (tab->ref.key >= 0)
6086
 
        {
 
6037
        /* Found key that can be used to retrieve data in sorted order */
 
6038
        if (tab->ref.key >= 0)
 
6039
        {
6087
6040
          /*
6088
6041
            We'll use ref access method on key new_ref_key. In general case
6089
6042
            the index search tuple for new_ref_key will be different (e.g.
6099
6052
          if (create_ref_for_key(tab->join, tab, keyuse,
6100
6053
                                 tab->join->const_table_map))
6101
6054
            return(0);
6102
 
        }
6103
 
        else
6104
 
        {
 
6055
        }
 
6056
        else
 
6057
        {
6105
6058
          /*
6106
6059
            The range optimizer constructed QUICK_RANGE for ref_key, and
6107
6060
            we want to use instead new_ref_key as the index. We can't
6122
6075
                                        true) <=
6123
6076
              0)
6124
6077
            return(0);
6125
 
        }
 
6078
        }
6126
6079
        ref_key= new_ref_key;
6127
6080
      }
6128
6081
    }
6170
6123
      keys|= table->covering_keys;
6171
6124
 
6172
6125
      /*
6173
 
        We are adding here also the index specified in FORCE INDEX clause,
6174
 
        if any.
 
6126
        We are adding here also the index specified in FORCE INDEX clause,
 
6127
        if any.
6175
6128
        This is to allow users to use index in order_st BY.
6176
6129
      */
6177
6130
      if (table->force_index)
6178
 
        keys|= (group ? table->keys_in_use_for_group_by :
6179
 
                           table->keys_in_use_for_order_by);
 
6131
        keys|= (group ? table->keys_in_use_for_group_by :
 
6132
                                table->keys_in_use_for_order_by);
6180
6133
      keys&= usable_keys;
6181
6134
    }
6182
6135
    else
6202
6155
          rationally. It's easy to demonstrate that using
6203
6156
          temporary table + filesort could be cheaper for grouping
6204
6157
          queries too.
6205
 
        */
 
6158
        */
6206
6159
        if (is_covering ||
6207
6160
            select_limit != HA_POS_ERROR ||
6208
6161
            (ref_key < 0 && (group || table->force_index)))
6220
6173
              With a grouping query each group containing on average
6221
6174
              rec_per_key records produces only one row that will
6222
6175
              be included into the result set.
6223
 
            */
 
6176
            */
6224
6177
            if (select_limit > table_records/rec_per_key)
6225
6178
                select_limit= table_records;
6226
6179
            else
6235
6188
            So the estimate for L/fanout(tk,tn) will be too optimistic
6236
6189
            and as result we'll choose an index scan when using ref/range
6237
6190
            access + filesort will be cheaper.
6238
 
          */
 
6191
          */
6239
6192
          select_limit= (ha_rows) (select_limit < fanout ?
6240
6193
                                   1 : select_limit/fanout);
6241
6194
          /*
6266
6219
            TODO. Use the formula for a disk sweep sequential access
6267
6220
            to calculate the cost of accessing data rows for one
6268
6221
            index entry.
6269
 
          */
 
6222
          */
6270
6223
          index_scan_time= select_limit/rec_per_key *
6271
6224
                           cmin(rec_per_key, table->file->scan_time());
6272
6225
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
6289
6242
              best_key_direction= direction;
6290
6243
            }
6291
6244
          }
6292
 
        }
 
6245
        }
6293
6246
      }
6294
6247
    }
6295
6248
    if (best_key >= 0)
6310
6263
      if (!no_changes)
6311
6264
      {
6312
6265
        if (!quick_created)
6313
 
        {
 
6266
        {
6314
6267
          tab->index= best_key;
6315
6268
          tab->read_first_record= best_key_direction > 0 ?
6316
6269
                                  join_read_first:join_read_last;
6365
6318
    if (select && select->quick)
6366
6319
    {
6367
6320
      /*
6368
 
        Don't reverse the sort order, if it's already done.
 
6321
        Don't reverse the sort order, if it's already done.
6369
6322
        (In some cases test_if_order_by_key() can be called multiple times
6370
6323
      */
6371
6324
      if (!select->quick->reverse_sorted())
6384
6337
        }
6385
6338
 
6386
6339
        /* order_st BY range_key DESC */
6387
 
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
6388
 
                                    used_key_parts, &error);
6389
 
        if (!tmp || error)
6390
 
        {
6391
 
          delete tmp;
6392
 
          select->quick= save_quick;
6393
 
          tab->limit= 0;
6394
 
          return(0);            // Reverse sort not supported
6395
 
        }
6396
 
        select->quick=tmp;
 
6340
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
6341
                                          used_key_parts, &error);
 
6342
        if (!tmp || error)
 
6343
        {
 
6344
          delete tmp;
 
6345
                select->quick= save_quick;
 
6346
                tab->limit= 0;
 
6347
          return(0);            // Reverse sort not supported
 
6348
        }
 
6349
        select->quick=tmp;
6397
6350
      }
6398
6351
    }
6399
6352
    else if (tab->type != JT_NEXT &&
6400
6353
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
6401
6354
    {
6402
6355
      /*
6403
 
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
 
6356
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
6404
6357
 
6405
 
        Use a traversal function that starts by reading the last row
6406
 
        with key part (A) and then traverse the index backwards.
 
6358
        Use a traversal function that starts by reading the last row
 
6359
        with key part (A) and then traverse the index backwards.
6407
6360
      */
6408
6361
      tab->read_first_record= join_read_last_key;
6409
6362
      tab->read_record.read_record= join_read_prev_same;
6566
6519
    if (error)
6567
6520
    {
6568
6521
      if (error == HA_ERR_RECORD_DELETED)
6569
 
        continue;
 
6522
        continue;
6570
6523
      if (error == HA_ERR_END_OF_FILE)
6571
 
        break;
 
6524
        break;
6572
6525
      goto err;
6573
6526
    }
6574
6527
    if (having && !having->val_int())
6575
6528
    {
6576
6529
      if ((error=file->ha_delete_row(record)))
6577
 
        goto err;
 
6530
        goto err;
6578
6531
      error=file->rnd_next(record);
6579
6532
      continue;
6580
6533
    }
6592
6545
    {
6593
6546
      if ((error=file->rnd_next(record)))
6594
6547
      {
6595
 
        if (error == HA_ERR_RECORD_DELETED)
6596
 
          continue;
6597
 
        if (error == HA_ERR_END_OF_FILE)
6598
 
          break;
6599
 
        goto err;
 
6548
        if (error == HA_ERR_RECORD_DELETED)
 
6549
          continue;
 
6550
        if (error == HA_ERR_END_OF_FILE)
 
6551
          break;
 
6552
        goto err;
6600
6553
      }
6601
6554
      if (table->compare_record(first_field) == 0)
6602
6555
      {
6603
 
        if ((error=file->ha_delete_row(record)))
6604
 
          goto err;
 
6556
        if ((error=file->ha_delete_row(record)))
 
6557
          goto err;
6605
6558
      }
6606
6559
      else if (!found)
6607
6560
      {
6608
 
        found=1;
6609
 
        file->position(record); // Remember position
 
6561
        found= 1;
 
6562
        file->position(record); // Remember position
6610
6563
      }
6611
6564
    }
6612
6565
    if (!found)
6735
6688
  return(1);
6736
6689
}
6737
6690
 
6738
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
6739
 
                                  SORT_FIELD *sortorder)
 
6691
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
6740
6692
{
6741
6693
  uint32_t count;
6742
6694
  SORT_FIELD *sort,*pos;
6779
6731
  bool last_record;
6780
6732
  CACHE_FIELD *copy,*end_field;
6781
6733
 
6782
 
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
6783
 
  pos=tab->cache.pos;
6784
 
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
6734
  last_record= tab->cache.record_nr++ == tab->cache.ptr_record;
 
6735
  pos= tab->cache.pos;
 
6736
  for (copy= tab->cache.field, end_field= copy+tab->cache.fields;
6785
6737
       copy < end_field;
6786
6738
       copy++)
6787
6739
  {
6857
6809
    != 0;
6858
6810
}
6859
6811
 
6860
 
 
6861
 
bool
6862
 
cp_buffer_from_ref(Session *session, TABLE_REF *ref)
 
6812
bool cp_buffer_from_ref(Session *session, TABLE_REF *ref)
6863
6813
{
6864
6814
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
6865
6815
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
6877
6827
  return result;
6878
6828
}
6879
6829
 
6880
 
 
6881
6830
/*****************************************************************************
6882
6831
  Group and order functions
6883
6832
*****************************************************************************/
6914
6863
  @retval
6915
6864
    true  if error occurred
6916
6865
*/
6917
 
 
6918
 
static bool
6919
 
find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables,
6920
 
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
6921
 
                   bool is_group_field)
 
6866
static bool find_order_in_list(Session *session, 
 
6867
                               Item **ref_pointer_array, 
 
6868
                               TableList *tables,
 
6869
                               order_st *order,
 
6870
                               List<Item> &fields,
 
6871
                               List<Item> &all_fields,
 
6872
                               bool is_group_field)
6922
6873
{
6923
6874
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
6924
6875
  Item::Type order_item_type;
7044
6995
  return false;
7045
6996
}
7046
6997
 
7047
 
 
7048
6998
/**
7049
6999
  Change order to point at item in select list.
7050
7000
 
7051
7001
  If item isn't a number and doesn't exits in the select list, add it the
7052
7002
  the field list.
7053
7003
*/
7054
 
 
7055
 
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
7056
 
                List<Item> &fields, List<Item> &all_fields, order_st *order)
 
7004
int setup_order(Session *session,
 
7005
                Item **ref_pointer_array,
 
7006
                TableList *tables,
 
7007
                            List<Item> &fields,
 
7008
                List<Item> &all_fields,
 
7009
                order_st *order)
7057
7010
{
7058
7011
  session->where="order clause";
7059
7012
  for (; order; order=order->next)
7065
7018
  return 0;
7066
7019
}
7067
7020
 
7068
 
 
7069
7021
/**
7070
7022
  Intitialize the GROUP BY list.
7071
7023
 
7091
7043
  @retval
7092
7044
    1  error (probably out of memory)
7093
7045
*/
7094
 
 
7095
 
int
7096
 
setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
7097
 
            List<Item> &fields, List<Item> &all_fields, order_st *order,
7098
 
            bool *hidden_group_fields)
 
7046
int setup_group(Session *session,
 
7047
                Item **ref_pointer_array,
 
7048
                TableList *tables,
 
7049
                      List<Item> &fields,
 
7050
                List<Item> &all_fields,
 
7051
                order_st *order,
 
7052
                      bool *hidden_group_fields)
7099
7053
{
7100
7054
  *hidden_group_fields=0;
7101
7055
  order_st *ord;
7549
7503
    {
7550
7504
      if (item->type() == Item::FIELD_ITEM)
7551
7505
      {
7552
 
        item_field= item->get_tmp_table_item(session);
 
7506
        item_field= item->get_tmp_table_item(session);
7553
7507
      }
7554
7508
      else if ((field= item->get_tmp_table_field()))
7555
7509
      {
7556
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
7557
 
          item_field= ((Item_sum*) item)->result_item(field);
7558
 
        else
7559
 
          item_field= (Item*) new Item_field(field);
7560
 
        if (!item_field)
7561
 
          return(true);                    // Fatal error
 
7510
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
7511
          item_field= ((Item_sum*) item)->result_item(field);
 
7512
        else
 
7513
          item_field= (Item*) new Item_field(field);
 
7514
        if (!item_field)
 
7515
          return(true);                    // Fatal error
7562
7516
 
7563
7517
        if (item->real_item()->type() != Item::FIELD_ITEM)
7564
7518
          field->orig_table= 0;
7565
 
        item_field->name= item->name;
 
7519
        item_field->name= item->name;
7566
7520
        if (item->type() == Item::REF_ITEM)
7567
7521
        {
7568
7522
          Item_field *ifield= (Item_field *) item_field;
7572
7526
        }
7573
7527
      }
7574
7528
      else
7575
 
        item_field= item;
 
7529
        item_field= item;
7576
7530
    }
7577
7531
    res_all_fields.push_back(item_field);
7578
7532
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
7630
7584
  return session->is_fatal_error;
7631
7585
}
7632
7586
 
7633
 
 
7634
 
 
7635
7587
/******************************************************************************
7636
7588
  Code for calculating functions
7637
7589
******************************************************************************/
7638
7590
 
7639
 
 
7640
7591
/**
7641
7592
  Call ::setup for all sum functions.
7642
7593