~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Mark Atwood
  • Date: 2011-10-08 04:50:51 UTC
  • mfrom: (2430.1.1 rf)
  • Revision ID: me@mark.atwood.name-20111008045051-6ha1qiy7k2a9c3jv
Tags: 2011.10.27
mergeĀ lp:~olafvdspek/drizzle/refactor2

Show diffs side-by-side

added added

removed removed

Lines of Context:
91
91
 
92
92
static bool eval_const_cond(COND *cond)
93
93
{
94
 
  return ((Item_func*) cond)->val_int() ? true : false;
 
94
    return ((Item_func*) cond)->val_int() ? true : false;
95
95
}
96
96
 
97
97
/*
99
99
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
100
100
  The following variable occupies 64 addresses.
101
101
*/
102
 
const char *subq_sj_cond_name= "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
 
102
const char *subq_sj_cond_name=
 
103
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
103
104
 
104
105
static void copy_blobs(Field **ptr)
105
106
{
106
107
  for (; *ptr ; ptr++)
107
108
  {
108
109
    if ((*ptr)->flags & BLOB_FLAG)
109
 
    {
110
110
      ((Field_blob *) (*ptr))->copy();
111
 
    }
112
111
  }
113
112
}
114
113
 
122
121
  Select_Lex *select_lex= &lex->select_lex;
123
122
  DRIZZLE_SELECT_START(session->getQueryString()->c_str());
124
123
 
125
 
  if (select_lex->master_unit()->is_union() or
 
124
  if (select_lex->master_unit()->is_union() ||
126
125
      select_lex->master_unit()->fake_select_lex)
127
126
  {
128
127
    res= drizzle_union(session, lex, result, &lex->unit,
133
132
    Select_Lex_Unit *unit= &lex->unit;
134
133
    unit->set_limit(unit->global_parameters);
135
134
    session->session_marker= 0;
136
 
 
137
135
    /*
138
136
      'options' of select_query will be set in JOIN, as far as JOIN for
139
137
      every PS/SP execution new, we will not need reset this flag if
155
153
                      result, unit, select_lex);
156
154
  }
157
155
  res|= session->is_error();
158
 
 
159
156
  if (unlikely(res))
160
 
  {
161
157
    result->abort();
162
 
  }
163
158
 
164
159
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
165
160
  return res;
220
215
    Item *item= ref->outer_ref;
221
216
    Item **item_ref= ref->ref;
222
217
    Item_ref *new_ref;
223
 
 
224
218
    /*
225
219
      @todo this field item already might be present in the select list.
226
220
      In this case instead of adding new field item we could use an
244
238
    {
245
239
      Item_sum *sum_func;
246
240
      if (ref->in_sum_func->nest_level > select->nest_level)
247
 
      {
248
241
        direct_ref= true;
249
 
      }
250
242
      else
251
243
      {
252
244
        for (sum_func= ref->in_sum_func; sum_func &&
261
253
        }
262
254
      }
263
255
    }
264
 
 
265
256
    new_ref= direct_ref ?
266
257
              new Item_direct_ref(ref->context, item_ref, ref->table_name,
267
258
                          ref->field_name, ref->alias_name_used) :
268
259
              new Item_ref(ref->context, item_ref, ref->table_name,
269
260
                          ref->field_name, ref->alias_name_used);
270
 
 
271
261
    ref->outer_ref= new_ref;
272
262
    ref->ref= &ref->outer_ref;
273
263
 
274
264
    if (!ref->fixed && ref->fix_fields(session, 0))
275
 
    {
276
265
      return true;
277
 
    }
278
266
    session->used_tables|= item->used_tables();
279
267
  }
280
268
  return res;
306
294
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
307
295
{
308
296
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
309
 
 
310
297
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
311
298
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
312
 
 
313
299
  if (where)
314
300
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
315
 
 
316
301
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
317
302
  {
318
303
    if (join_tab->ref.cond_guards[i])
421
406
    session->set_proc_info("init");
422
407
    session->used_tables=0;                         // Updated by setup_fields
423
408
    if ((err= join->prepare(rref_pointer_array, tables, wild_num, conds, og_num, order, group, having, select_lex, unit)))
424
 
    {
425
409
      goto err;
426
 
    }
427
410
  }
428
411
 
429
412
  err= join->optimize();
439
422
  }
440
423
 
441
424
  if (session->is_error())
442
 
  {
443
425
    goto err;
444
 
  }
445
426
 
446
427
  join->exec();
447
428
 
474
455
{
475
456
  int error;
476
457
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
477
 
  {
478
458
    return 0;                           // Fatal error flag is set
479
 
  }
480
 
 
481
459
  if (select)
482
460
  {
483
461
    select->head=table;
484
462
    table->reginfo.impossible_range=0;
485
463
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
486
464
                                          limit, 0, false)) == 1)
487
 
    {
488
465
      return(select->quick->records);
489
 
    }
490
 
 
491
466
    if (error == -1)
492
467
    {
493
468
      table->reginfo.impossible_range=1;
494
469
      return 0;
495
470
    }
496
471
  }
497
 
 
498
472
  return(HA_POS_ERROR);                 /* This shouldn't happend */
499
473
}
500
474
 
516
490
{
517
491
  uint32_t found;
518
492
  for (found=0; bits & 1 ; found++,bits>>=1) ;
519
 
 
520
493
  return found;
521
494
}
522
495
 
525
498
  int res;
526
499
  if (a->getTable()->tablenr != b->getTable()->tablenr)
527
500
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
528
 
 
529
501
  if (a->getKey() != b->getKey())
530
502
    return static_cast<int>((a->getKey() - b->getKey()));
531
 
 
532
503
  if (a->getKeypart() != b->getKeypart())
533
504
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
534
 
 
535
505
  // Place const values before other ones
536
506
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
537
507
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
538
508
    return res;
539
 
 
540
509
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
541
510
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
542
511
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
669
638
    prev= &key_end;
670
639
    uint found_eq_constant= 0;
671
640
    {
672
 
      for (uint32_t i= 0; i < keyuse->size()-1; i++, use++)
 
641
      uint32_t i;
 
642
 
 
643
      for (i= 0; i < keyuse->size()-1; i++, use++)
673
644
      {
674
645
        if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
675
646
          use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
676
 
 
677
647
        if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
678
648
        {
679
649
          if (prev->getKeypart() + 1 < use->getKeypart() ||
680
650
              ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
681
 
          {
682
651
            continue;                           /* remove */
683
 
          }
684
652
        }
685
653
        else if (use->getKeypart() != 0)                // First found must be 0
686
 
        {
687
654
          continue;
688
 
        }
689
655
 
690
656
#ifdef HAVE_VALGRIND
691
657
        /* Valgrind complains about overlapped memcpy when save_pos==use. */
696
662
        found_eq_constant= ! use->getUsedTables();
697
663
        /* Save ptr to first use */
698
664
        if (! use->getTable()->reginfo.join_tab->keyuse)
699
 
        {
700
665
          use->getTable()->reginfo.join_tab->keyuse= save_pos;
701
 
        }
702
666
        use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
703
667
        save_pos++;
704
668
      }
705
 
 
706
 
      uint32_t i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
 
669
      i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
707
670
      reinterpret_cast<optimizer::KeyUse*>(keyuse->buffer)[i] = key_end;
708
671
      keyuse->set_size(i);
709
672
    }
728
691
      To avoid bad matches, we don't make ref_table_rows less than 100.
729
692
    */
730
693
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
731
 
 
732
694
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
733
695
    {
734
696
      uint32_t tablenr;
739
701
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
740
702
      }
741
703
    }
742
 
 
743
704
    /*
744
705
      Outer reference (external field) is constant for single executing
745
706
      of subquery
746
707
    */
747
708
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
748
 
    {
749
709
      keyuse->setTableRows(1);
750
 
    }
751
710
  }
752
711
}
753
712
 
780
739
  if (join->group_list)
781
740
  { /* Collect all query fields referenced in the GROUP clause. */
782
741
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
783
 
    {
784
 
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0, (unsigned char*) &indexed_fields);
785
 
    }
 
742
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
 
743
                               (unsigned char*) &indexed_fields);
786
744
  }
787
745
  else if (join->select_distinct)
788
746
  { /* Collect all query fields referenced in the SELECT clause. */
790
748
    List<Item>::iterator select_items_it(select_items.begin());
791
749
    Item *item;
792
750
    while ((item= select_items_it++))
793
 
    {
794
 
      item->walk(&Item::collect_item_field_processor, 0, (unsigned char*) &indexed_fields);
795
 
    }
 
751
      item->walk(&Item::collect_item_field_processor, 0,
 
752
                 (unsigned char*) &indexed_fields);
796
753
  }
797
754
  else
798
 
  {
799
755
    return;
800
 
  }
801
756
 
802
757
  if (indexed_fields.size() == 0)
803
 
  {
804
758
    return;
805
 
  }
806
759
 
807
760
  /* Intersect the keys of all group fields. */
808
761
  cur_item= indexed_fields_it++;
847
800
 
848
801
  if (jt1->dependent & jt2->table->map)
849
802
    return 1;
850
 
 
851
803
  if (jt2->dependent & jt1->table->map)
852
804
    return -1;
853
 
 
854
805
  if (jt1->found_records > jt2->found_records)
855
806
    return 1;
856
 
 
857
807
  if (jt1->found_records < jt2->found_records)
858
808
    return -1;
859
 
 
860
809
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
861
810
}
862
811
 
870
819
 
871
820
  if (jt1->dependent & jt2->table->map)
872
821
    return 1;
873
 
 
874
822
  if (jt2->dependent & jt1->table->map)
875
823
    return -1;
876
 
 
877
824
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
878
825
}
879
826
 
893
840
      uint32_t flags=field->flags;
894
841
      fields++;
895
842
      rec_length+=field->pack_length();
896
 
 
897
843
      if (flags & BLOB_FLAG)
898
 
      {
899
844
        blobs++;
900
 
      }
901
 
 
902
845
      if (!(flags & NOT_NULL_FLAG))
903
 
      {
904
846
        null_fields++;
905
 
      }
906
847
    }
907
848
  }
908
 
 
909
849
  if (null_fields)
910
 
  {
911
850
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
912
 
  }
913
 
 
914
851
  if (join_tab->table->maybe_null)
915
 
  {
916
852
    rec_length+=sizeof(bool);
917
 
  }
918
 
 
919
853
  if (blobs)
920
854
  {
921
855
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
1000
934
    res->quick_fix_field();
1001
935
  }
1002
936
  else
1003
 
  {
1004
937
    *e1= e2;
1005
 
  }
1006
938
}
1007
939
 
1008
940
bool create_ref_for_key(Join *join,
1068
1000
  {
1069
1001
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
1070
1002
    {
1071
 
      while (keyuse->getKeypart() != i or ((~used_tables) & keyuse->getUsedTables()))
1072
 
      {
 
1003
      while (keyuse->getKeypart() != i ||
 
1004
             ((~used_tables) & keyuse->getUsedTables()))
1073
1005
        keyuse++;       /* Skip other parts */
1074
 
      }
1075
1006
 
1076
1007
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
1077
1008
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
1078
1009
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
1079
1010
      if (keyuse->isNullRejected())
1080
 
      {
1081
1011
        j->ref.null_rejecting |= 1 << i;
1082
 
      }
1083
 
 
1084
1012
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
1085
1013
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
1086
1014
      {         // Compare against constant
1089
1017
                           maybe_null ?  key_buff : 0,
1090
1018
                           keyinfo->key_part[i].length, keyuse->getVal());
1091
1019
        if (session->is_fatal_error)
1092
 
        {
1093
1020
          return true;
1094
 
        }
1095
1021
        tmp.copy();
1096
1022
      }
1097
1023
      else
1098
 
      {
1099
1024
        *ref_key++= get_store_key(session,
1100
 
                                  keyuse,join->const_table_map,
1101
 
                                  &keyinfo->key_part[i],
1102
 
                                  key_buff, maybe_null);
1103
 
      }
1104
 
 
 
1025
          keyuse,join->const_table_map,
 
1026
          &keyinfo->key_part[i],
 
1027
          key_buff, maybe_null);
1105
1028
      /*
1106
1029
        Remember if we are going to use REF_OR_NULL
1107
1030
        But only if field _really_ can be null i.e. we force AM_REF
1114
1037
  }
1115
1038
  *ref_key= 0;       // end_marker
1116
1039
  if (j->type == AM_CONST)
1117
 
  {
1118
1040
    j->table->const_table= 1;
1119
 
  }
1120
 
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) || keyparts != keyinfo->key_parts || null_ref_key)
 
1041
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
 
1042
           keyparts != keyinfo->key_parts || null_ref_key)
1121
1043
  {
1122
1044
    /* Must read with repeat */
1123
1045
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
1135
1057
    j->type= AM_CONST;
1136
1058
  }
1137
1059
  else
1138
 
  {
1139
1060
    j->type= AM_EQ_REF;
1140
 
  }
1141
 
 
1142
1061
  return 0;
1143
1062
}
1144
1063
 
1216
1135
            not_null_item is the t1.f1, but it's referred_tab is 0.
1217
1136
          */
1218
1137
          if (!referred_tab || referred_tab->join != join)
1219
 
          {
1220
1138
            continue;
1221
 
          }
1222
1139
          notnull= new Item_func_isnotnull(not_null_item);
1223
 
 
1224
1140
          /*
1225
1141
            We need to do full fix_fields() call here in order to have correct
1226
1142
            notnull->const_item(). This is needed e.g. by test_quick_select
1228
1144
            called.
1229
1145
          */
1230
1146
          if (notnull->fix_fields(join->session, &notnull))
1231
 
          {
1232
1147
            return;
1233
 
          }
1234
 
 
1235
1148
          add_cond_and_fix(&referred_tab->select_cond, notnull);
1236
1149
        }
1237
1150
      }
1238
1151
    }
1239
1152
  }
 
1153
  return;
1240
1154
}
1241
1155
 
1242
1156
/**
1308
1222
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
1309
1223
  {
1310
1224
    if (tables & 1 && !eq_ref_table(join, order, *tab))
1311
 
    {
1312
1225
      return 0;
1313
 
    }
1314
1226
  }
1315
1227
  return 1;
1316
1228
}
1337
1249
bool eq_ref_table(Join *join, Order *start_order, JoinTable *tab)
1338
1250
{
1339
1251
  if (tab->cached_eq_ref_table)                 // If cached
1340
 
  {
1341
1252
    return tab->eq_ref_table;
1342
 
  }
1343
 
 
1344
1253
  tab->cached_eq_ref_table=1;
1345
 
 
1346
1254
  /* We can skip const tables only if not an outer table */
1347
1255
  if (tab->type == AM_CONST && !tab->first_inner)
1348
 
  {
1349
1256
    return (tab->eq_ref_table=1);
1350
 
  }
1351
 
 
1352
1257
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
1353
 
  {
1354
1258
    return (tab->eq_ref_table=0);               // We must use this
1355
 
  }
1356
 
 
1357
1259
  Item **ref_item=tab->ref.items;
1358
1260
  Item **end=ref_item+tab->ref.key_parts;
1359
1261
  uint32_t found=0;
1369
1271
        if ((*ref_item)->eq(order->item[0],0))
1370
1272
          break;
1371
1273
      }
1372
 
 
1373
1274
      if (order)
1374
1275
      {
1375
1276
        found++;
1377
1278
        order->used|=map;
1378
1279
        continue;                               // Used in order_st BY
1379
1280
      }
1380
 
 
1381
1281
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
1382
 
      {
1383
1282
        return (tab->eq_ref_table= 0);
1384
 
      }
1385
1283
    }
1386
1284
  }
1387
1285
  /* Check that there was no reference to table before sort order */
1426
1324
    while ((item= li++))
1427
1325
    {
1428
1326
      if (item->contains(field))
1429
 
      {
1430
1327
        goto finish;
1431
 
      }
1432
1328
    }
1433
1329
    in_upper_level= true;
1434
1330
    cond_equal= cond_equal->upper_levels;
1435
1331
  }
1436
1332
  in_upper_level= false;
1437
 
 
1438
1333
finish:
1439
1334
  *inherited_fl= in_upper_level;
1440
1335
  return item;
1569
1464
      left_item_equal= new Item_equal(left_item_equal);
1570
1465
      cond_equal->current_level.push_back(left_item_equal);
1571
1466
      if (copy_item_name)
1572
 
      {
1573
1467
        left_item_equal->name = item->name;
1574
 
      }
1575
1468
    }
1576
1469
    if (right_copyfl)
1577
1470
    {
1579
1472
      right_item_equal= new Item_equal(right_item_equal);
1580
1473
      cond_equal->current_level.push_back(right_item_equal);
1581
1474
      if (copy_item_name)
1582
 
      {
1583
1475
        right_item_equal->name = item->name;
1584
 
      }
1585
1476
    }
1586
1477
 
1587
1478
    if (left_item_equal)
1606
1497
      {
1607
1498
        right_item_equal->add((Item_field *) left_item);
1608
1499
        if (copy_item_name)
1609
 
        {
1610
1500
          right_item_equal->name = item->name;
1611
 
        }
1612
1501
      }
1613
1502
      else
1614
1503
      {
1617
1506
                                               (Item_field *) right_item);
1618
1507
        cond_equal->current_level.push_back(item_equal);
1619
1508
        if (copy_item_name)
1620
 
        {
1621
1509
          item_equal->name = item->name;
1622
 
        }
1623
1510
      }
1624
1511
    }
1625
1512
    return true;
1660
1547
          eq_item->quick_fix_field();
1661
1548
          item= eq_item;
1662
1549
        }
1663
 
 
1664
 
        if ((cs != ((Item_func *) item)->compare_collation()) || !cs->coll->propagate())
1665
 
        {
 
1550
        if ((cs != ((Item_func *) item)->compare_collation()) ||
 
1551
            !cs->coll->propagate(cs, 0, 0))
1666
1552
          return false;
1667
 
        }
1668
1553
      }
1669
1554
 
1670
1555
      Item_equal *item_equal = find_item_equal(cond_equal,
1691
1576
      return true;
1692
1577
    }
1693
1578
  }
1694
 
 
1695
1579
  return false;
1696
1580
}
1697
1581
 
1740
1624
                                       (Item_row *) right_item,
1741
1625
                                       cond_equal, eq_list);
1742
1626
      if (!is_converted)
1743
 
      {
1744
1627
        session->lex().current_select->cond_count++;
1745
 
      }
1746
1628
    }
1747
1629
    else
1748
1630
    {
1809
1691
                                cond_equal, eq_list);
1810
1692
    }
1811
1693
    else
1812
 
    {
1813
1694
      return check_simple_equality(left_item, right_item, item, cond_equal);
1814
 
    }
1815
1695
  }
1816
1696
  return false;
1817
1697
}
1911
1791
          re-execution of any prepared statement/stored procedure.
1912
1792
        */
1913
1793
        if (check_equality(session, item, &cond_equal, &eq_list))
1914
 
        {
1915
1794
          li.remove();
1916
 
        }
1917
1795
      }
1918
1796
 
1919
1797
      List<Item_equal>::iterator it(cond_equal.current_level.begin());
1969
1847
    if (check_equality(session, cond, &cond_equal, &eq_list))
1970
1848
    {
1971
1849
      int n= cond_equal.current_level.size() + eq_list.size();
1972
 
 
1973
1850
      if (n == 0)
1974
 
      {
1975
1851
        return new Item_int((int64_t) 1,1);
1976
 
      }
1977
1852
      else if (n == 1)
1978
1853
      {
1979
1854
        if ((item_equal= cond_equal.current_level.pop()))
1982
1857
          item_equal->update_used_tables();
1983
1858
        }
1984
1859
        else
1985
 
        {
1986
1860
          item_equal= (Item_equal *) eq_list.pop();
1987
 
        }
1988
1861
        set_if_bigger(session->lex().current_select->max_equal_elems,
1989
1862
                      item_equal->members());
1990
1863
        return item_equal;
2104
1977
  {
2105
1978
    cond= build_equal_items_for_cond(session, cond, inherited);
2106
1979
    cond->update_used_tables();
2107
 
 
2108
 
    if (cond->type() == Item::COND_ITEM && ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2109
 
    {
 
1980
    if (cond->type() == Item::COND_ITEM &&
 
1981
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
2110
1982
      cond_equal= &((Item_cond_and*) cond)->cond_equal;
2111
 
    }
2112
1983
    else if (cond->type() == Item::FUNC_ITEM &&
2113
1984
             ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
2114
1985
    {
2178
2049
    outer_ref= 1;
2179
2050
    cmp= -1;
2180
2051
  }
2181
 
 
2182
2052
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
2183
2053
  {
2184
2054
    outer_ref= 1;
2185
2055
    cmp++;
2186
2056
  }
2187
 
 
2188
2057
  if (outer_ref)
2189
 
  {
2190
2058
    return cmp;
2191
 
  }
2192
 
 
2193
2059
  JoinTable **idx= (JoinTable **) table_join_idx;
2194
2060
  cmp= idx[field2->field->getTable()->tablenr]-idx[field1->field->getTable()->tablenr];
2195
 
 
2196
2061
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
2197
2062
}
2198
2063
 
2245
2110
  Item_equal_iterator it(item_equal->begin());
2246
2111
  Item *head;
2247
2112
  if (item_const)
2248
 
  {
2249
2113
    head= item_const;
2250
 
  }
2251
2114
  else
2252
2115
  {
2253
2116
    head= item_equal->get_first();
2261
2124
    if (upper)
2262
2125
    {
2263
2126
      if (item_const && upper->get_const())
2264
 
      {
2265
2127
        item= 0;
2266
 
      }
2267
2128
      else
2268
2129
      {
2269
2130
        Item_equal_iterator li(item_equal->begin());
2270
2131
        while ((item= li++) != item_field)
2271
2132
        {
2272
2133
          if (item->find_item_equal(upper_levels) == upper)
2273
 
          {
2274
2134
            break;
2275
 
          }
2276
2135
        }
2277
2136
      }
2278
2137
    }
2279
2138
    if (item == item_field)
2280
2139
    {
2281
2140
      if (eq_item)
2282
 
      {
2283
2141
        eq_list.push_back(eq_item);
2284
 
      }
2285
 
 
2286
2142
      eq_item= new Item_func_eq(item_field, head);
2287
 
 
2288
2143
      if (!eq_item)
2289
 
      {
2290
2144
        return 0;
2291
 
      }
2292
2145
      eq_item->set_cmp_func();
2293
2146
      eq_item->quick_fix_field();
2294
2147
   }
2297
2150
  if (!cond && !&eq_list.front())
2298
2151
  {
2299
2152
    if (!eq_item)
2300
 
    {
2301
2153
      return new Item_int((int64_t) 1,1);
2302
 
    }
2303
2154
    return eq_item;
2304
2155
  }
2305
2156
 
2306
2157
  if (eq_item)
2307
 
  {
2308
2158
    eq_list.push_back(eq_item);
2309
 
  }
2310
 
 
2311
2159
  if (!cond)
2312
 
  {
2313
2160
    cond= new Item_cond_and(eq_list);
2314
 
  }
2315
2161
  else
2316
2162
  {
2317
2163
    assert(cond->type() == Item::COND_ITEM);
2400
2246
          break;
2401
2247
      }
2402
2248
    }
2403
 
 
2404
 
    if (cond->type() == Item::COND_ITEM && !((Item_cond*)cond)->argument_list()->size())
2405
 
    {
 
2249
    if (cond->type() == Item::COND_ITEM &&
 
2250
        !((Item_cond*)cond)->argument_list()->size())
2406
2251
      cond= new Item_int((int32_t)cond->val_bool());
2407
 
    }
2408
2252
 
2409
2253
  }
2410
2254
  else if (cond->type() == Item::FUNC_ITEM &&
2413
2257
    item_equal= (Item_equal *) cond;
2414
2258
    item_equal->sort(&compare_fields_by_table_order, table_join_idx);
2415
2259
    if (cond_equal && &cond_equal->current_level.front() == item_equal)
2416
 
    {
2417
2260
      cond_equal= 0;
2418
 
    }
2419
 
 
2420
2261
    return eliminate_item_equal(0, cond_equal, item_equal);
2421
2262
  }
2422
2263
  else
2423
 
  {
2424
2264
    cond->transform(&Item::replace_equal_field, 0);
2425
 
  }
2426
 
 
2427
2265
  return cond;
2428
2266
}
2429
2267
 
2512
2350
 
2513
2351
    return;
2514
2352
  }
2515
 
 
2516
2353
  if (cond->eq_cmp_result() == Item::COND_OK)
2517
 
  {
2518
2354
    return;                                     // Not a boolean function
2519
 
  }
2520
2355
 
2521
2356
  Item_bool_func2 *func=  (Item_bool_func2*) cond;
2522
2357
  Item **args= func->arguments();
2584
2419
Item *remove_additional_cond(Item* conds)
2585
2420
{
2586
2421
  if (conds->name == in_additional_cond)
2587
 
  {
2588
2422
    return 0;
2589
 
  }
2590
 
 
2591
2423
  if (conds->type() == Item::COND_ITEM)
2592
2424
  {
2593
2425
    Item_cond *cnd= (Item_cond*) conds;
2599
2431
      {
2600
2432
        li.remove();
2601
2433
        if (cnd->argument_list()->size() == 1)
2602
 
        {
2603
2434
          return &cnd->argument_list()->front();
2604
 
        }
2605
 
 
2606
2435
        return conds;
2607
2436
      }
2608
2437
    }
2649
2478
      Item **args= func->arguments();
2650
2479
      bool left_const= args[0]->const_item();
2651
2480
      bool right_const= args[1]->const_item();
2652
 
      if (!(left_const && right_const) && args[0]->result_type() == args[1]->result_type())
 
2481
      if (!(left_const && right_const) &&
 
2482
          args[0]->result_type() == args[1]->result_type())
2653
2483
      {
2654
2484
        if (right_const)
2655
2485
        {
2656
 
          resolve_const_item(session, &args[1], args[0]);
 
2486
                resolve_const_item(session, &args[1], args[0]);
2657
2487
          func->update_used_tables();
2658
 
          change_cond_ref_to_const(session, save_list, and_father, and_father,
2659
 
                                   args[0], args[1]);
 
2488
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
2489
                                        args[0], args[1]);
2660
2490
        }
2661
2491
        else if (left_const)
2662
2492
        {
2663
 
          resolve_const_item(session, &args[0], args[1]);
 
2493
                resolve_const_item(session, &args[0], args[1]);
2664
2494
          func->update_used_tables();
2665
 
          change_cond_ref_to_const(session, save_list, and_father, and_father,
2666
 
                                   args[1], args[0]);
 
2495
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
2496
                                        args[1], args[0]);
2667
2497
        }
2668
2498
      }
2669
2499
    }
2791
2621
      join->cur_embedding_map |= next_emb->getNestedJoin()->nj_map;
2792
2622
    }
2793
2623
 
2794
 
    if (next_emb->getNestedJoin()->join_list.size() != next_emb->getNestedJoin()->counter_)
2795
 
    {
 
2624
    if (next_emb->getNestedJoin()->join_list.size() !=
 
2625
        next_emb->getNestedJoin()->counter_)
2796
2626
      break;
2797
 
    }
2798
2627
 
2799
2628
    /*
2800
2629
      We're currently at Y or Z-bracket as depicted in the above picture.
2809
2638
{
2810
2639
  Session *session= join->session;
2811
2640
 
2812
 
  if (conds == NULL)
2813
 
  {
 
2641
  if (!conds)
2814
2642
    *cond_value= Item::COND_TRUE;
2815
 
  }
2816
2643
  else
2817
2644
  {
2818
2645
    /*
2864
2691
    {
2865
2692
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
2866
2693
      if (! new_item)
2867
 
      {
2868
 
        li.remove();
2869
 
      }
 
2694
              li.remove();
2870
2695
      else if (item != new_item)
2871
2696
      {
2872
2697
        li.replace(new_item);
2873
2698
        should_fix_fields= true;
2874
2699
      }
2875
 
 
2876
2700
      if (*cond_value == Item::COND_UNDEF)
2877
 
      {
2878
 
        *cond_value= tmp_cond_value;
2879
 
      }
 
2701
              *cond_value= tmp_cond_value;
2880
2702
 
2881
2703
      switch (tmp_cond_value)
2882
2704
      {
2904
2726
    }
2905
2727
 
2906
2728
    if (should_fix_fields)
2907
 
    {
2908
2729
      cond->update_used_tables();
2909
 
    }
2910
2730
 
2911
2731
    if (! ((Item_cond*) cond)->argument_list()->size() || *cond_value != Item::COND_OK)
2912
 
    {
2913
2732
      return (COND*) NULL;
2914
 
    }
2915
2733
 
2916
2734
    if (((Item_cond*) cond)->argument_list()->size() == 1)
2917
2735
    {
2918
2736
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
2919
2737
      item= &((Item_cond*) cond)->argument_list()->front();
2920
2738
      ((Item_cond*) cond)->argument_list()->clear();
2921
 
 
2922
2739
      return item;
2923
2740
    }
2924
2741
  }
3010
2827
    if (left_item->eq(right_item,1))
3011
2828
    {
3012
2829
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
3013
 
      {
3014
 
        return (COND*) NULL;                    /* Comparison of identical items */
3015
 
      }
 
2830
              return (COND*) NULL;                      /* Comparison of identical items */
3016
2831
    }
3017
2832
  }
3018
2833
  *cond_value= Item::COND_OK;
3061
2876
{
3062
2877
  if (cond->type() == Item::COND_ITEM)
3063
2878
  {
3064
 
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
 
2879
    bool and_level= (((Item_cond*) cond)->functype()
 
2880
                     == Item_func::COND_AND_FUNC);
3065
2881
    List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
3066
 
 
3067
2882
    Item *item;
3068
2883
    while ((item=li++))
3069
2884
    {
3071
2886
      if (res)                                  // Is a const value
3072
2887
      {
3073
2888
        if (and_level)
3074
 
        {
3075
 
          return true;
3076
 
        }
3077
 
      }
3078
 
      else if (and_level == false)
3079
 
      {
3080
 
        return false;
3081
 
      }
 
2889
          return 1;
 
2890
      }
 
2891
      else if (!and_level)
 
2892
        return 0;
3082
2893
    }
3083
 
    return and_level ? false : true;
 
2894
    return and_level ? 0 : 1;
3084
2895
  }
3085
2896
  else if (cond->eq_cmp_result() != Item::COND_OK)
3086
2897
  {                                             // boolan compare function
3087
2898
    Item_func* func= (Item_func*) cond;
3088
2899
    if (func->functype() != Item_func::EQUAL_FUNC &&
3089
2900
              func->functype() != Item_func::EQ_FUNC)
3090
 
    {
3091
 
      return false;
3092
 
    }
3093
 
 
 
2901
      return 0;
3094
2902
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
3095
2903
    Item *right_item= ((Item_func*) cond)->arguments()[1];
3096
 
 
3097
2904
    if (left_item->eq(comp_item,1))
3098
2905
    {
3099
2906
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
3100
2907
      {
3101
2908
        if (*const_item)
3102
 
        {
3103
2909
          return right_item->eq(*const_item, 1);
3104
 
        }
3105
2910
        *const_item=right_item;
3106
 
        return true;
 
2911
        return 1;
3107
2912
      }
3108
2913
    }
3109
2914
    else if (right_item->eq(comp_item,1))
3111
2916
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
3112
2917
      {
3113
2918
        if (*const_item)
3114
 
        {
3115
2919
          return left_item->eq(*const_item, 1);
3116
 
        }
3117
2920
        *const_item=left_item;
3118
 
        return true;
 
2921
        return 1;
3119
2922
      }
3120
2923
    }
3121
2924
  }
3122
 
 
3123
 
  return false;
 
2925
  return 0;
3124
2926
}
3125
2927
 
3126
2928
/**
3180
2982
  }
3181
2983
  else
3182
2984
  {
3183
 
    if ((join->sort_and_group) && !tmp_tbl->precomputed_group_by)
3184
 
    {
 
2985
    if ((join->sort_and_group) &&
 
2986
        !tmp_tbl->precomputed_group_by)
3185
2987
      end_select= end_send_group;
3186
 
    }
3187
2988
    else
3188
 
    {
3189
2989
      end_select= end_send;
3190
 
    }
3191
2990
  }
3192
 
 
3193
2991
  return end_select;
3194
2992
}
3195
2993
 
3228
3026
      }
3229
3027
    }
3230
3028
  }
3231
 
 
3232
3029
  /* Set up select_end */
3233
3030
  Next_select_func end_select= setup_end_select_func(join);
3234
3031
  if (join->tables)
3237
3034
 
3238
3035
    join_tab=join->join_tab+join->const_tables;
3239
3036
  }
3240
 
 
3241
3037
  join->send_records=0;
3242
3038
  if (join->tables == join->const_tables)
3243
3039
  {
3271
3067
    assert(join->tables);
3272
3068
    error= sub_select(join,join_tab,0);
3273
3069
    if (error == NESTED_LOOP_OK || error == NESTED_LOOP_NO_MORE_ROWS)
3274
 
    {
3275
3070
      error= sub_select(join,join_tab,1);
3276
 
    }
3277
 
 
3278
3071
    if (error == NESTED_LOOP_QUERY_LIMIT)
3279
 
    {
3280
3072
      error= NESTED_LOOP_OK;                    /* select_limit used */
3281
 
    }
3282
3073
  }
3283
 
 
3284
3074
  if (error == NESTED_LOOP_NO_MORE_ROWS)
3285
 
  {
3286
3075
    error= NESTED_LOOP_OK;
3287
 
  }
3288
3076
 
3289
3077
  if (error == NESTED_LOOP_OK)
3290
3078
  {
3300
3088
      */
3301
3089
      join->join_free();                        // Unlock all cursors
3302
3090
      if (join->result->send_eof())
3303
 
      {
3304
3091
        rc= 1;                                  // Don't send error
3305
 
      }
3306
3092
    }
3307
3093
  }
3308
3094
  else
3309
 
  {
3310
3095
    rc= -1;
3311
 
  }
3312
 
 
3313
3096
  if (table)
3314
3097
  {
3315
3098
    int tmp, new_errno= 0;
3317
3100
    {
3318
3101
      new_errno= tmp;
3319
3102
    }
3320
 
 
3321
3103
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
3322
3104
    {
3323
3105
      new_errno= tmp;
3324
3106
    }
3325
 
 
3326
3107
    if (new_errno)
3327
 
    {
3328
3108
      table->print_error(new_errno,MYF(0));
3329
 
    }
3330
3109
  }
3331
3110
  return(join->session->is_error() ? -1 : rc);
3332
3111
}
3342
3121
      rc= sub_select(join,join_tab,end_of_records);
3343
3122
    return rc;
3344
3123
  }
3345
 
 
3346
3124
  if (join->session->getKilled())               // If aborted by user
3347
3125
  {
3348
3126
    join->session->send_kill_message();
3349
3127
    return NESTED_LOOP_KILLED;
3350
3128
  }
3351
 
 
3352
3129
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
3353
3130
  {
3354
3131
    if (! join_tab->cache.store_record_in_cache())
3357
3134
  }
3358
3135
  rc= flush_cached_records(join, join_tab, true);
3359
3136
  if (rc == NESTED_LOOP_OK || rc == NESTED_LOOP_NO_MORE_ROWS)
3360
 
  {
3361
3137
    rc= sub_select(join, join_tab, end_of_records);
3362
 
  }
3363
 
 
3364
3138
  return rc;
3365
3139
}
3366
3140
 
3487
3261
{
3488
3262
  join_tab->table->null_row=0;
3489
3263
  if (end_of_records)
3490
 
  {
3491
3264
    return (*join_tab->next_select)(join,join_tab+1,end_of_records);
3492
 
  }
3493
3265
 
3494
3266
  int error;
3495
3267
  enum_nested_loop_state rc;
3499
3271
  {
3500
3272
    /* If not the last table, plunge down the nested loop */
3501
3273
    if (join_tab < join->join_tab + join->tables - 1)
3502
 
    {
3503
3274
      rc= (*join_tab->next_select)(join, join_tab + 1, 0);
3504
 
    }
3505
3275
    else
3506
3276
    {
3507
3277
      join->resume_nested_loop= false;
3539
3309
    rc= evaluate_join_record(join, join_tab, error);
3540
3310
  }
3541
3311
 
3542
 
  if (rc == NESTED_LOOP_NO_MORE_ROWS and join_tab->last_inner && !join_tab->found)
3543
 
  {
 
3312
  if (rc == NESTED_LOOP_NO_MORE_ROWS &&
 
3313
      join_tab->last_inner && !join_tab->found)
3544
3314
    rc= evaluate_null_complemented_join_record(join, join_tab);
3545
 
  }
3546
3315
 
3547
3316
  if (rc == NESTED_LOOP_NO_MORE_ROWS)
3548
 
  {
3549
3317
    rc= NESTED_LOOP_OK;
3550
 
  }
3551
 
 
3552
3318
  return rc;
3553
3319
}
3554
3320
 
3584
3350
  {
3585
3351
    table->status= 0;
3586
3352
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3587
 
    {
3588
3353
      error= HA_ERR_KEY_NOT_FOUND;
3589
 
    }
3590
3354
    else
3591
3355
    {
3592
3356
      error=table->cursor->index_read_idx_map(table->getInsertRecord(),tab->ref.key,
3600
3364
      tab->table->mark_as_null_row();
3601
3365
      table->emptyRecord();
3602
3366
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3603
 
      {
3604
3367
        return table->report_error(error);
3605
 
      }
3606
3368
      return -1;
3607
3369
    }
3608
3370
    table->storeRecord();
3660
3422
                                      make_prev_keypart_map(tab->ref.key_parts),
3661
3423
                                      HA_READ_KEY_EXACT);
3662
3424
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3663
 
    {
3664
3425
      return table->report_error(error);
3665
 
    }
3666
3426
  }
3667
3427
  table->null_row=0;
3668
3428
  return table->status ? -1 : 0;
3696
3456
  {
3697
3457
    error= table->cursor->startIndexScan(tab->ref.key, tab->sorted);
3698
3458
    if (error != 0)
3699
 
    {
3700
3459
      return table->report_error(error);
3701
 
    }
3702
3460
  }
3703
3461
 
3704
3462
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
3705
3463
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
3706
3464
  {
3707
3465
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
3708
 
    {
3709
 
      return -1;
3710
 
    }
 
3466
        return -1;
3711
3467
  }
3712
3468
 
3713
3469
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3714
 
  {
3715
3470
    return -1;
3716
 
  }
3717
 
 
3718
3471
  if ((error=table->cursor->index_read_map(table->getInsertRecord(),
3719
3472
                                         tab->ref.key_buff,
3720
3473
                                         make_prev_keypart_map(tab->ref.key_parts),
3721
3474
                                         HA_READ_KEY_EXACT)))
3722
3475
  {
3723
3476
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3724
 
    {
3725
3477
      return table->report_error(error);
3726
 
    }
3727
3478
    return -1;
3728
3479
  }
3729
3480
 
3745
3496
    if (error != 0)
3746
3497
      return table->report_error(error);
3747
3498
  }
3748
 
 
3749
3499
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
3750
 
  {
3751
3500
    return -1;
3752
 
  }
3753
 
 
3754
3501
  if ((error=table->cursor->index_read_last_map(table->getInsertRecord(),
3755
3502
                                              tab->ref.key_buff,
3756
3503
                                              make_prev_keypart_map(tab->ref.key_parts))))
3757
3504
  {
3758
3505
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
3759
 
    {
3760
3506
      return table->report_error(error);
3761
 
    }
3762
3507
    return -1;
3763
3508
  }
3764
 
 
3765
3509
  return 0;
3766
3510
}
3767
3511
 
3798
3542
    return 0;
3799
3543
  }
3800
3544
  else
3801
 
  {
3802
3545
    return join_read_next_same(info);
3803
 
  }
3804
3546
}
3805
3547
 
3806
3548
int join_read_next_same(ReadRecord *info)
3829
3571
  JoinTable *tab=table->reginfo.join_tab;
3830
3572
 
3831
3573
  if ((error=table->cursor->index_prev(table->getInsertRecord())))
3832
 
  {
3833
3574
    return table->report_error(error);
3834
 
  }
3835
 
 
3836
3575
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
3837
3576
                      tab->ref.key_length))
3838
3577
  {
3839
3578
    table->status=STATUS_NOT_FOUND;
3840
3579
    error= -1;
3841
3580
  }
3842
 
 
3843
3581
  return error;
3844
3582
}
3845
3583
 
3846
3584
int join_init_quick_read_record(JoinTable *tab)
3847
3585
{
3848
3586
  if (test_if_quick_select(tab) == -1)
3849
 
  {
3850
3587
    return -1;                                  /* No possible records */
3851
 
  }
3852
 
 
3853
3588
  return join_init_read_record(tab);
3854
3589
}
3855
3590
 
3858
3593
  tab->read_record.init_reard_record_sequential();
3859
3594
 
3860
3595
  if (tab->read_record.cursor->startTableScan(1))
3861
 
  {
3862
3596
    return 1;
3863
 
  }
3864
3597
  return (*tab->read_record.read_record)(&tab->read_record);
3865
3598
}
3866
3599
 
3875
3608
int join_init_read_record(JoinTable *tab)
3876
3609
{
3877
3610
  if (tab->select && tab->select->quick && tab->select->quick->reset())
3878
 
  {
3879
3611
    return 1;
3880
 
  }
3881
3612
 
3882
3613
  if (tab->read_record.init_read_record(tab->join->session, tab->table, tab->select, 1, true))
3883
 
  {
3884
3614
    return 1;
3885
 
  }
3886
3615
 
3887
3616
  return (*tab->read_record.read_record)(&tab->read_record);
3888
3617
}
3946
3675
      key_copy(tab->insideout_buf, info->record, key, 0);
3947
3676
 
3948
3677
      if ((error=info->cursor->index_next(info->record)))
3949
 
      {
3950
3678
        return info->table->report_error(error);
3951
 
      }
3952
3679
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
3953
3680
                      tab->insideout_buf, key->key_length));
3954
3681
    tab->insideout_match_tab->found_match= 0;
3955
3682
    return 0;
3956
3683
  }
3957
3684
  else
3958
 
  {
3959
3685
    return join_read_next(info);
3960
 
  }
3961
3686
}
3962
3687
 
3963
3688
int join_read_next(ReadRecord *info)
3964
3689
{
3965
3690
  int error;
3966
3691
  if ((error=info->cursor->index_next(info->record)))
3967
 
  {
3968
3692
    return info->table->report_error(error);
3969
 
  }
3970
3693
  return 0;
3971
3694
}
3972
3695
 
3986
3709
  tab->read_record.cursor=table->cursor;
3987
3710
  tab->read_record.index=tab->index;
3988
3711
  tab->read_record.record=table->getInsertRecord();
3989
 
 
3990
3712
  if (!table->cursor->inited)
3991
3713
  {
3992
3714
    error= table->cursor->startIndexScan(tab->index, 1);
3993
3715
    if (error != 0)
3994
 
    {
3995
3716
      return table->report_error(error);
3996
 
    }
3997
3717
  }
3998
 
 
3999
3718
  if ((error= tab->table->cursor->index_last(tab->table->getInsertRecord())))
4000
 
  {
4001
3719
    return table->report_error(error);
4002
 
  }
4003
3720
 
4004
3721
  return 0;
4005
3722
}
4008
3725
{
4009
3726
  int error;
4010
3727
  if ((error= info->cursor->index_prev(info->record)))
4011
 
  {
4012
3728
    return info->table->report_error(error);
4013
 
  }
4014
3729
 
4015
3730
  return 0;
4016
3731
}
4025
3740
  /* First read according to key which is NOT NULL */
4026
3741
  *tab->ref.null_ref_key= 0;                    // Clear null byte
4027
3742
  if ((res= join_read_always_key(tab)) >= 0)
4028
 
  {
4029
3743
    return res;
4030
 
  }
4031
3744
 
4032
3745
  /* Then read key with null value */
4033
3746
  *tab->ref.null_ref_key= 1;                    // Set null byte
4038
3751
{
4039
3752
  int error;
4040
3753
  if ((error= join_read_next_same(info)) >= 0)
4041
 
  {
4042
3754
    return error;
4043
 
  }
4044
3755
  JoinTable *tab= info->table->reginfo.join_tab;
4045
3756
 
4046
3757
  /* Test if we have already done a read after null key */
4047
3758
  if (*tab->ref.null_ref_key)
4048
 
  {
4049
3759
    return -1;                                  // All keys read
4050
 
  }
4051
 
 
4052
3760
  *tab->ref.null_ref_key= 1;                    // Set null byte
4053
 
 
4054
3761
  return safe_index_read(tab);                  // then read null keys
4055
3762
}
4056
3763
 
4059
3766
  int idx= -1;
4060
3767
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
4061
3768
 
4062
 
  if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
 
3769
  if (!join->first_record || end_of_records ||
 
3770
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4063
3771
  {
4064
 
    if (join->first_record or
 
3772
    if (join->first_record ||
4065
3773
        (end_of_records && !join->group && !join->group_optimized_away))
4066
3774
    {
4067
3775
      if (idx < (int) join->send_group_parts)
4076
3784
            join->clear();
4077
3785
 
4078
3786
            while ((item= it++))
4079
 
            {
4080
3787
              item->no_rows_in_result();
4081
 
            }
4082
3788
          }
4083
3789
          if (join->having && join->having->val_int() == 0)
4084
 
          {
4085
3790
            error= -1;                          // Didn't satisfy having
4086
 
          }
4087
3791
          else
4088
3792
          {
4089
3793
            if (join->do_send_rows)
4096
3800
              error= 1;
4097
3801
          }
4098
3802
        }
4099
 
 
4100
3803
        if (error > 0)
4101
 
        {
4102
3804
          return(NESTED_LOOP_ERROR);
4103
 
        }
4104
 
 
4105
3805
        if (end_of_records)
4106
 
        {
4107
3806
          return(NESTED_LOOP_OK);
4108
 
        }
4109
 
 
4110
3807
        if (join->send_records >= join->unit->select_limit_cnt &&
4111
3808
            join->do_send_rows)
4112
3809
        {
4113
3810
          if (!(join->select_options & OPTION_FOUND_ROWS))
4114
 
          {
4115
3811
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
4116
 
          }
4117
3812
          join->do_send_rows=0;
4118
3813
          join->unit->select_limit_cnt = HA_POS_ERROR;
4119
3814
        }
4134
3829
    else
4135
3830
    {
4136
3831
      if (end_of_records)
4137
 
      {
4138
3832
        return(NESTED_LOOP_OK);
4139
 
      }
4140
3833
      join->first_record=1;
4141
3834
      test_if_item_cache_changed(join->group_fields);
4142
3835
    }
4148
3841
      */
4149
3842
      copy_fields(&join->tmp_table_param);
4150
3843
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4151
 
      {
4152
3844
        return(NESTED_LOOP_ERROR);
4153
 
      }
4154
3845
      return(ok_code);
4155
3846
    }
4156
3847
  }
4169
3860
    join->session->send_kill_message();
4170
3861
    return NESTED_LOOP_KILLED;
4171
3862
  }
4172
 
 
4173
 
  if (!join->first_record or end_of_records or (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
 
3863
  if (!join->first_record || end_of_records ||
 
3864
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
4174
3865
  {
4175
 
    if (join->first_record or (end_of_records && !join->group))
 
3866
    if (join->first_record || (end_of_records && !join->group))
4176
3867
    {
4177
3868
      int send_group_parts= join->send_group_parts;
4178
3869
      if (idx < send_group_parts)
4182
3873
          /* No matching rows for group function */
4183
3874
          join->clear();
4184
3875
        }
4185
 
 
4186
3876
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
4187
 
 
4188
3877
        if (!join->having || join->having->val_int())
4189
3878
        {
4190
3879
          int error= table->cursor->insertRecord(table->getInsertRecord());
4195
3884
            return NESTED_LOOP_ERROR;
4196
3885
          }
4197
3886
        }
4198
 
 
4199
3887
        if (join->rollup.getState() != Rollup::STATE_NONE)
4200
3888
        {
4201
3889
          if (join->rollup_write_data((uint32_t) (idx+1), table))
4202
 
          {
4203
3890
            return NESTED_LOOP_ERROR;
4204
 
          }
4205
3891
        }
4206
 
 
4207
3892
        if (end_of_records)
4208
 
        {
4209
3893
          return NESTED_LOOP_OK;
4210
 
        }
4211
3894
      }
4212
3895
    }
4213
3896
    else
4214
3897
    {
4215
3898
      if (end_of_records)
4216
 
      {
4217
3899
        return NESTED_LOOP_OK;
4218
 
      }
4219
3900
      join->first_record=1;
4220
3901
      test_if_item_cache_changed(join->group_fields);
4221
3902
    }
4223
3904
    {
4224
3905
      copy_fields(&join->tmp_table_param);
4225
3906
      if (copy_funcs(join->tmp_table_param.items_to_copy, join->session))
4226
 
      {
4227
3907
        return NESTED_LOOP_ERROR;
4228
 
      }
4229
 
 
4230
3908
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
4231
 
      {
4232
3909
        return NESTED_LOOP_ERROR;
4233
 
      }
4234
 
 
4235
3910
      return NESTED_LOOP_OK;
4236
3911
    }
4237
3912
  }
4238
 
 
4239
3913
  if (update_sum_func(join->sum_funcs))
4240
 
  {
4241
3914
    return NESTED_LOOP_ERROR;
4242
 
  }
4243
 
 
4244
3915
  return NESTED_LOOP_OK;
4245
3916
}
4246
3917
 
4337
4008
      and will be checked at execution time, attached to the first table.
4338
4009
    */
4339
4010
    !((used_table & 1) && cond->is_expensive()))
4340
 
  {
4341
4011
    return (COND*) 0;                           // Already checked
4342
 
  }
4343
 
 
4344
4012
  if (cond->type() == Item::COND_ITEM)
4345
4013
  {
4346
4014
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4347
4015
    {
4348
4016
      /* Create new top level AND item */
4349
4017
      Item_cond_and *new_cond=new Item_cond_and;
4350
 
      if (new_cond == NULL)
4351
 
      {
 
4018
      if (!new_cond)
4352
4019
        return (COND*) 0;
4353
 
      }
4354
4020
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
4355
4021
      Item *item;
4356
4022
      while ((item=li++))
4358
4024
        Item *fix= make_cond_for_table(item,tables,used_table,
4359
4025
                                            exclude_expensive_cond);
4360
4026
        if (fix)
4361
 
        {
4362
4027
          new_cond->argument_list()->push_back(fix);
4363
 
        }
4364
4028
      }
4365
4029
      switch (new_cond->argument_list()->size())
4366
4030
      {
4367
4031
        case 0:
4368
4032
          return (COND*) 0;                     // Always true
4369
 
 
4370
4033
        case 1:
4371
4034
          return &new_cond->argument_list()->front();
4372
 
 
4373
4035
        default:
4374
4036
          /*
4375
4037
            Item_cond_and do not need fix_fields for execution, its parameters
4383
4045
    else
4384
4046
    {                                           // Or list
4385
4047
      Item_cond_or *new_cond=new Item_cond_or;
4386
 
      if (new_cond == NULL)
4387
 
      {
 
4048
      if (!new_cond)
4388
4049
        return (COND*) 0;
4389
 
      }
4390
4050
      List<Item>::iterator li(((Item_cond*) cond)->argument_list()->begin());
4391
4051
      Item *item;
4392
4052
      while ((item=li++))
4393
4053
      {
4394
4054
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
4395
4055
        if (!fix)
4396
 
        {
4397
4056
          return (COND*) 0;                     // Always true
4398
 
        }
4399
4057
        new_cond->argument_list()->push_back(fix);
4400
4058
      }
4401
4059
      /*
4405
4063
      new_cond->quick_fix_field();
4406
4064
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
4407
4065
      new_cond->top_level_item();
4408
 
 
4409
4066
      return new_cond;
4410
4067
    }
4411
4068
  }
4422
4079
        non-constant, so that they are not evaluated at optimization time.
4423
4080
      */
4424
4081
      (!used_table && exclude_expensive_cond && cond->is_expensive()))
4425
 
  {
4426
4082
    return (COND*) 0;                           // Can't check this yet
4427
 
  }
4428
 
 
4429
4083
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
4430
 
  {
4431
4084
    return cond;                                // Not boolean op
4432
 
  }
4433
4085
 
4434
4086
  /*
4435
4087
    Remove equalities that are guaranteed to be true by use of 'ref' access
4457
4109
static Item *part_of_refkey(Table *table,Field *field)
4458
4110
{
4459
4111
  if (!table->reginfo.join_tab)
4460
 
  {
4461
4112
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
4462
 
  }
4463
4113
 
4464
4114
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
4465
4115
  if (ref_parts)
4471
4121
    for (part=0 ; part < ref_parts ; part++)
4472
4122
    {
4473
4123
      if (table->reginfo.join_tab->ref.cond_guards[part])
4474
 
      {
4475
4124
        return 0;
4476
 
      }
4477
4125
    }
4478
4126
 
4479
4127
    for (part=0 ; part < ref_parts ; part++,key_part++)
4488
4136
      }
4489
4137
    }
4490
4138
  }
4491
 
 
4492
4139
  return (Item*) 0;
4493
4140
}
4494
4141
 
4532
4179
      These are already skipped in the ORDER BY by const_expression_in_where()
4533
4180
    */
4534
4181
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
4535
 
    {
4536
4182
      key_part++;
4537
 
    }
4538
4183
 
4539
4184
    if (key_part == key_part_end)
4540
4185
    {
4553
4198
        const_key_parts=table->const_key_parts[table->getShare()->getPrimaryKey()];
4554
4199
 
4555
4200
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
4556
 
        {
4557
4201
          key_part++;
4558
 
        }
4559
 
 
4560
4202
        /*
4561
4203
         The primary and secondary key parts were all const (i.e. there's
4562
4204
         one row).  The sorting doesn't matter.
4563
4205
        */
4564
4206
        if (key_part == key_part_end && reverse == 0)
4565
 
        {
4566
4207
          return 1;
4567
 
        }
4568
4208
      }
4569
4209
      else
4570
 
      {
4571
4210
        return 0;
4572
 
      }
4573
4211
    }
4574
4212
 
4575
4213
    if (key_part->field != field)
4576
 
    {
4577
4214
      return 0;
4578
 
    }
4579
4215
 
4580
4216
    /* set flag to 1 if we can use read-next on key, else to -1 */
4581
4217
    flag= ((order->asc == !(key_part->key_part_flag & HA_REVERSE_SORT)) ?
4582
4218
           1 : -1);
4583
4219
    if (reverse && flag != reverse)
4584
 
    {
4585
4220
      return 0;
4586
 
    }
4587
4221
    reverse=flag;                               // Remember if reverse
4588
4222
    key_part++;
4589
4223
  }
4590
4224
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
4591
4225
    (uint32_t) (key_part - table->key_info[idx].key_part);
4592
 
  if (reverse == -1 && !(table->index_flags(idx) & HA_READ_PREV))
4593
 
  {
 
4226
  if (reverse == -1 && !(table->index_flags(idx) &
 
4227
                         HA_READ_PREV))
4594
4228
    reverse= 0;                                 // Index can't be used
4595
 
  }
4596
 
 
4597
4229
  return(reverse);
4598
4230
}
4599
4231
 
4617
4249
                      KeyPartInfo *ref_key_part_end)
4618
4250
{
4619
4251
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
4620
 
  {
4621
4252
    if (! key_part->field->eq(ref_key_part->field))
4622
 
    {
4623
4253
      return 0;
4624
 
    }
4625
 
  }
4626
 
 
4627
4254
  return 1;
4628
4255
}
4629
4256
 
4714
4341
           key_part < key_part_end;
4715
4342
           key_part++)
4716
4343
      {
4717
 
        if (key_part->field->maybe_null() || ! find_func(key_part->field, data))
4718
 
        {
 
4344
        if (key_part->field->maybe_null() ||
 
4345
            ! find_func(key_part->field, data))
4719
4346
          break;
4720
 
        }
4721
4347
      }
4722
 
 
4723
4348
      if (key_part == key_part_end)
4724
 
      {
4725
4349
        return 1;
4726
 
      }
4727
4350
    }
4728
4351
  }
4729
4352
  return 0;
4843
4466
    }
4844
4467
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
4845
4468
    if (usable_keys.none())
4846
 
    {
4847
4469
      return 0;                                 // No usable keys
4848
 
    }
4849
4470
  }
4850
4471
 
4851
4472
  ref_key= -1;
4855
4476
    ref_key=       tab->ref.key;
4856
4477
    ref_key_parts= tab->ref.key_parts;
4857
4478
    if (tab->type == AM_REF_OR_NULL)
4858
 
    {
4859
4479
      return 0;
4860
 
    }
4861
4480
  }
4862
4481
  else if (select && select->quick)             // Range found by optimizer/range
4863
4482
  {
4872
4491
    if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
4873
4492
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
4874
4493
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
4875
 
    {
4876
4494
      return 0;
4877
 
    }
4878
4495
    ref_key=       select->quick->index;
4879
4496
    ref_key_parts= select->quick->used_key_parts;
4880
4497
  }
4896
4513
      */
4897
4514
      if (table->covering_keys.test(ref_key))
4898
4515
        usable_keys&= table->covering_keys;
4899
 
 
4900
4516
      if (tab->pre_idx_push_select_cond)
4901
4517
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
4902
 
 
4903
4518
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
4904
4519
                                       &usable_keys)) < MAX_KEY)
4905
4520
      {
4918
4533
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
4919
4534
            keyuse++;
4920
4535
 
4921
 
          if (create_ref_for_key(tab->join, tab, keyuse, tab->join->const_table_map))
4922
 
          {
 
4536
          if (create_ref_for_key(tab->join, tab, keyuse,
 
4537
                                 tab->join->const_table_map))
4923
4538
            return 0;
4924
 
          }
4925
4539
        }
4926
4540
        else
4927
4541
        {
4944
4558
                                        tab->join->unit->select_limit_cnt,0,
4945
4559
                                        true) <=
4946
4560
              0)
4947
 
          {
4948
4561
            return 0;
4949
 
          }
4950
4562
        }
4951
4563
        ref_key= new_ref_key;
4952
4564
      }
4991
4603
        index order and not using join cache
4992
4604
        */
4993
4605
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
4994
 
      {
4995
4606
        return 0;
4996
 
      }
4997
4607
      keys= *table->cursor->keys_to_use_for_scanning();
4998
4608
      keys|= table->covering_keys;
4999
4609
 
5003
4613
        This is to allow users to use index in order_st BY.
5004
4614
      */
5005
4615
      if (table->force_index)
5006
 
      {
5007
4616
        keys|= (group ? table->keys_in_use_for_group_by :
5008
4617
                                table->keys_in_use_for_order_by);
5009
 
      }
5010
4618
      keys&= usable_keys;
5011
4619
    }
5012
4620
    else
5013
 
    {
5014
4621
      keys= usable_keys;
5015
 
    }
5016
4622
 
5017
4623
    cur_pos= join->getPosFromOptimalPlan(tablenr);
5018
4624
    read_time= cur_pos.getCost();
5019
 
 
5020
4625
    for (uint32_t i= tablenr+1; i < join->tables; i++)
5021
4626
    {
5022
4627
      cur_pos= join->getPosFromOptimalPlan(i);
5048
4653
          double index_scan_time;
5049
4654
          KeyInfo *keyinfo= tab->table->key_info+nr;
5050
4655
          if (select_limit == HA_POS_ERROR)
5051
 
          {
5052
4656
            select_limit= table_records;
5053
 
          }
5054
 
 
5055
4657
          if (group)
5056
4658
          {
5057
4659
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
5062
4664
              be included into the result set.
5063
4665
            */
5064
4666
            if (select_limit > table_records/rec_per_key)
5065
 
            {
5066
 
              select_limit= table_records;
5067
 
            }
 
4667
                select_limit= table_records;
5068
4668
            else
5069
 
            {
5070
4669
              select_limit= (ha_rows) (select_limit*rec_per_key);
5071
 
            }
5072
4670
          }
5073
4671
          /*
5074
4672
            If tab=tk is not the last joined table tn then to get first
5093
4691
            <=> N > table->quick_condition_rows.
5094
4692
          */
5095
4693
          if (select_limit > table->quick_condition_rows)
5096
 
          {
5097
4694
            select_limit= table_records;
5098
 
          }
5099
4695
          else
5100
 
          {
5101
4696
            select_limit= (ha_rows) (select_limit *
5102
4697
                                     (double) table_records /
5103
 
                                     table->quick_condition_rows);
5104
 
          }
 
4698
                                      table->quick_condition_rows);
5105
4699
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
5106
4700
          set_if_bigger(rec_per_key, 1.0);
5107
4701
          /*
5115
4709
            to calculate the cost of accessing data rows for one
5116
4710
            index entry.
5117
4711
          */
5118
 
          index_scan_time= select_limit/rec_per_key * min(rec_per_key, table->cursor->scan_time());
5119
 
 
5120
 
          if (is_covering || (ref_key < 0 && (group || table->force_index)) || index_scan_time < read_time)
 
4712
          index_scan_time= select_limit/rec_per_key *
 
4713
                           min(rec_per_key, table->cursor->scan_time());
 
4714
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
 
4715
              index_scan_time < read_time)
5121
4716
          {
5122
4717
            ha_rows quick_records= table_records;
5123
4718
            if (is_best_covering && !is_covering)
5124
 
            {
5125
4719
              continue;
5126
 
            }
5127
 
 
5128
4720
            if (table->quick_keys.test(nr))
5129
 
            {
5130
4721
              quick_records= table->quick_rows[nr];
5131
 
            }
5132
 
 
5133
4722
            if (best_key < 0 ||
5134
4723
                (select_limit <= min(quick_records,best_records) ?
5135
4724
                 keyinfo->key_parts < best_key_parts :
5145
4734
        }
5146
4735
      }
5147
4736
    }
5148
 
 
5149
4737
    if (best_key >= 0)
5150
4738
    {
5151
4739
      bool quick_created= false;
5206
4794
      order_direction= best_key_direction;
5207
4795
    }
5208
4796
    else
5209
 
    {
5210
4797
      return 0;
5211
 
    }
5212
4798
  }
5213
4799
 
5214
4800
check_reverse_order:
5263
4849
    }
5264
4850
  }
5265
4851
  else if (select && select->quick)
5266
 
  {
5267
4852
    select->quick->sorted= 1;
5268
 
  }
5269
 
 
5270
4853
  return 1;
5271
4854
}
5272
4855
 
5324
4907
      test_if_skip_sort_order(tab,order,select_limit,0,
5325
4908
                              is_order_by ?  &table->keys_in_use_for_order_by :
5326
4909
                              &table->keys_in_use_for_group_by))
5327
 
  {
5328
4910
    return 0;
5329
 
  }
5330
 
 
5331
4911
  for (Order *ord= join->order; ord; ord= ord->next)
5332
 
  {
5333
4912
    length++;
5334
 
  }
5335
 
 
5336
4913
  join->sortorder= make_unireg_sortorder(order, &length, join->sortorder);
5337
4914
  table->sort.io_cache= new internal::io_cache_st;
5338
4915
  table->status=0;                              // May be wrong if quick_select
5426
5003
    if (error)
5427
5004
    {
5428
5005
      if (error == HA_ERR_RECORD_DELETED)
5429
 
      {
5430
5006
        continue;
5431
 
      }
5432
5007
      if (error == HA_ERR_END_OF_FILE)
5433
 
      {
5434
5008
        break;
5435
 
      }
5436
5009
      goto err;
5437
5010
    }
5438
5011
    if (having && !having->val_int())
5439
5012
    {
5440
5013
      if ((error=cursor->deleteRecord(record)))
5441
 
      {
5442
5014
        goto err;
5443
 
      }
5444
5015
      error=cursor->rnd_next(record);
5445
5016
      continue;
5446
5017
    }
5454
5025
      if ((error=cursor->rnd_next(record)))
5455
5026
      {
5456
5027
        if (error == HA_ERR_RECORD_DELETED)
5457
 
        {
5458
5028
          continue;
5459
 
        }
5460
5029
        if (error == HA_ERR_END_OF_FILE)
5461
 
        {
5462
5030
          break;
5463
 
        }
5464
 
 
5465
5031
        goto err;
5466
5032
      }
5467
5033
      if (table->compare_record(first_field) == 0)
5468
5034
      {
5469
5035
        if ((error=cursor->deleteRecord(record)))
5470
 
        {
5471
5036
          goto err;
5472
 
        }
5473
5037
      }
5474
5038
      else if (!found)
5475
5039
      {
5478
5042
      }
5479
5043
    }
5480
5044
    if (!found)
5481
 
    {
5482
5045
      break;                                    // End of cursor
5483
 
    }
5484
5046
    /* Move current position to the next row */
5485
5047
    error= cursor->rnd_pos(record, cursor->ref);
5486
5048
  }
5490
5052
err:
5491
5053
  cursor->extra(HA_EXTRA_NO_CACHE);
5492
5054
  if (error)
5493
 
  {
5494
5055
    table->print_error(error,MYF(0));
5495
 
  }
5496
5056
  return 1;
5497
5057
}
5498
5058
 
5536
5096
  hash_init(&hash, &my_charset_bin, (uint32_t) cursor.stats.records, 0, key_length, (hash_get_key) 0, 0, 0);
5537
5097
 
5538
5098
  if ((error= cursor.startTableScan(1)))
5539
 
  {
5540
5099
    goto err;
5541
 
  }
5542
5100
 
5543
5101
  key_pos= &key_buffer[0];
5544
5102
  for (;;)
5552
5110
    if ((error=cursor.rnd_next(record)))
5553
5111
    {
5554
5112
      if (error == HA_ERR_RECORD_DELETED)
5555
 
      {
5556
5113
        continue;
5557
 
      }
5558
 
 
5559
5114
      if (error == HA_ERR_END_OF_FILE)
5560
 
      {
5561
5115
        break;
5562
 
      }
5563
 
 
5564
5116
      goto err;
5565
5117
    }
5566
5118
    if (having && !having->val_int())
5567
5119
    {
5568
5120
      if ((error=cursor.deleteRecord(record)))
5569
 
      {
5570
5121
        goto err;
5571
 
      }
5572
5122
      continue;
5573
5123
    }
5574
5124
 
5588
5138
        goto err;
5589
5139
    }
5590
5140
    else
5591
 
    {
5592
5141
      (void) my_hash_insert(&hash, org_key_pos);
5593
 
    }
5594
5142
    key_pos+=extra_length;
5595
5143
  }
5596
5144
  hash_free(&hash);
5603
5151
  cursor.extra(HA_EXTRA_NO_CACHE);
5604
5152
  (void) cursor.endTableScan();
5605
5153
  if (error)
5606
 
  {
5607
5154
    table->print_error(error,MYF(0));
5608
 
  }
5609
 
 
5610
5155
  return 1;
5611
5156
}
5612
5157
 
5625
5170
  {
5626
5171
    Item *item= order->item[0]->real_item();
5627
5172
    pos->field= 0; pos->item= 0;
5628
 
 
5629
5173
    if (item->type() == Item::FIELD_ITEM)
5630
 
    {
5631
5174
      pos->field= ((Item_field*) item)->field;
5632
 
    }
5633
5175
    else if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item())
5634
 
    {
5635
5176
      pos->field= ((Item_sum*) item)->get_tmp_table_field();
5636
 
    }
5637
5177
    else if (item->type() == Item::COPY_STR_ITEM)
5638
5178
    {                                           // Blob patch
5639
5179
      pos->item= ((Item_copy_string*) item)->item;
5640
5180
    }
5641
5181
    else
5642
 
    {
5643
5182
      pos->item= *order->item;
5644
 
    }
5645
 
 
5646
5183
    pos->reverse=! order->asc;
5647
5184
  }
5648
5185
  *length=count;
5679
5216
    }
5680
5217
  }
5681
5218
  else
5682
 
  {
5683
5219
    no_prev_key= true;
5684
 
  }
5685
 
 
5686
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) || no_prev_key)
5687
 
  {
 
5220
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
 
5221
      no_prev_key)
5688
5222
    return 1;
5689
 
  }
5690
 
  return (memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length) != 0);
 
5223
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
 
5224
    != 0;
5691
5225
}
5692
5226
 
5693
5227
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
5780
5314
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
5781
5315
  select_item= find_item_in_list(session, order_item, fields, &counter, REPORT_EXCEPT_NOT_FOUND, &resolution);
5782
5316
  if (!select_item)
5783
 
  {
5784
5317
    return true; /* The item is not unique, or some other error occured. */
5785
 
  }
5786
5318
 
5787
5319
 
5788
5320
  /* Check whether the resolved field is not ambiguos. */
5794
5326
      original field name, we should additionaly check if we have conflict
5795
5327
      for this name (in case if we would perform lookup in all tables).
5796
5328
    */
5797
 
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed && order_item->fix_fields(session, order->item))
5798
 
    {
 
5329
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
 
5330
        order_item->fix_fields(session, order->item))
5799
5331
      return true;
5800
 
    }
5801
5332
 
5802
5333
    /* Lookup the current GROUP field in the FROM clause. */
5803
5334
    order_item_type= order_item->type();
5808
5339
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
5809
5340
                                       NULL, &view_ref, IGNORE_ERRORS, false);
5810
5341
      if (!from_field)
5811
 
      {
5812
5342
        from_field= (Field*) not_found_field;
5813
 
      }
5814
5343
    }
5815
5344
 
5816
5345
    if (from_field == not_found_field ||
5869
5398
      (order_item->fix_fields(session, order->item) ||
5870
5399
       (order_item= *order->item)->check_cols(1) ||
5871
5400
       session->is_fatal_error))
5872
 
  {
5873
5401
    return true; /* Wrong field. */
5874
 
  }
5875
5402
 
5876
5403
  uint32_t el= all_fields.size();
5877
5404
  all_fields.push_front(order_item); /* Add new field to field list. */
5897
5424
  for (; order; order= order->next)
5898
5425
  {
5899
5426
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields, all_fields, false))
5900
 
    {
5901
5427
      return 1;
5902
 
    }
5903
5428
  }
5904
5429
  return 0;
5905
5430
}
5938
5463
                      bool *hidden_group_fields)
5939
5464
{
5940
5465
  *hidden_group_fields=0;
 
5466
  Order *ord;
5941
5467
 
5942
 
  if (order == NULL)
5943
 
  {
 
5468
  if (!order)
5944
5469
    return 0;                           /* Everything is ok */
5945
 
  }
5946
5470
 
5947
5471
  uint32_t org_fields=all_fields.size();
5948
5472
 
5949
5473
  session->setWhere("group statement");
5950
 
  for (Order *ord= order; ord; ord= ord->next)
 
5474
  for (ord= order; ord; ord= ord->next)
5951
5475
  {
5952
5476
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
5953
5477
                           all_fields, true))
5954
 
    {
5955
5478
      return 1;
5956
 
    }
5957
5479
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
5958
5480
    if ((*ord->item)->with_sum_func)
5959
5481
    {
5961
5483
      return 1;
5962
5484
    }
5963
5485
  }
5964
 
 
5965
5486
  /* MODE_ONLY_FULL_GROUP_BY */
5966
5487
  {
5967
5488
    /*
5997
5518
        {
5998
5519
          /* Skip fields from previous expressions. */
5999
5520
          if (field->marker < cur_pos_in_select_list)
6000
 
          {
6001
5521
            goto next_field;
6002
 
          }
6003
 
 
6004
5522
          /* Found a field from the next expression. */
6005
5523
          if (field->marker > cur_pos_in_select_list)
6006
 
          {
6007
5524
            break;
6008
 
          }
6009
 
 
6010
5525
          /*
6011
5526
            Check whether the field occur in the GROUP BY list.
6012
5527
            Throw the error later if the field isn't found.
6013
5528
          */
6014
 
          for (Order *ord= order; ord; ord= ord->next)
6015
 
          {
 
5529
          for (ord= order; ord; ord= ord->next)
6016
5530
            if ((*ord->item)->eq((Item*)field, 0))
6017
 
            {
6018
5531
              goto next_field;
6019
 
            }
6020
 
          }
6021
 
 
6022
5532
          /*
6023
5533
            @todo change ER_WRONG_FIELD_WITH_GROUP to more detailed ER_NON_GROUPING_FIELD_USED
6024
5534
          */
6031
5541
      cur_pos_in_select_list++;
6032
5542
    }
6033
5543
  }
6034
 
 
6035
5544
  if (org_fields != all_fields.size())
6036
 
  {
6037
5545
    *hidden_group_fields=1;                     // group fields is not used
6038
 
  }
6039
 
 
6040
5546
  return 0;
6041
5547
}
6042
5548
 
6058
5564
 
6059
5565
  *all_order_by_fields_used= 1;
6060
5566
  while (Item* item=li++)
6061
 
  {
6062
5567
    item->marker=0;                     /* Marker that field is not used */
6063
 
  }
6064
5568
 
6065
5569
  prev= &group;  group=0;
6066
5570
  for (order=order_list ; order; order=order->next)
6073
5577
      (*ord->item)->marker=1;
6074
5578
    }
6075
5579
    else
6076
 
    {
6077
5580
      *all_order_by_fields_used= 0;
6078
 
    }
6079
5581
  }
6080
5582
 
6081
5583
  li= fields.begin();
6089
5591
      */
6090
5592
      Order *ord_iter;
6091
5593
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
6092
 
      {
6093
5594
        if ((*ord_iter->item)->eq(item, 1))
6094
 
        {
6095
5595
          goto next_item;
6096
 
        }
6097
 
      }
6098
5596
 
6099
5597
      Order *ord=(Order*) session->mem.calloc(sizeof(Order));
6100
5598
 
6123
5621
  List<Item>::iterator li(fields.begin());
6124
5622
  Item *field;
6125
5623
 
6126
 
  param->field_count=param->sum_func_count=param->func_count= param->hidden_field_count=0;
 
5624
  param->field_count=param->sum_func_count=param->func_count=
 
5625
    param->hidden_field_count=0;
6127
5626
  param->quick_group=1;
6128
 
 
6129
5627
  while ((field=li++))
6130
5628
  {
6131
5629
    Item::Type real_type= field->real_item()->type();
6132
5630
    if (real_type == Item::FIELD_ITEM)
6133
 
    {
6134
5631
      param->field_count++;
6135
 
    }
6136
5632
    else if (real_type == Item::SUM_FUNC_ITEM)
6137
5633
    {
6138
5634
      if (! field->const_item())
6142
5638
            sum_item->depended_from() == select_lex)
6143
5639
        {
6144
5640
          if (!sum_item->quick_group)
6145
 
          {
6146
5641
            param->quick_group=0;                       // UDF SUM function
6147
 
          }
6148
5642
          param->sum_func_count++;
6149
5643
 
6150
5644
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
6162
5656
    {
6163
5657
      param->func_count++;
6164
5658
      if (reset_with_sum_func)
6165
 
      {
6166
 
        field->with_sum_func= 0;
6167
 
      }
 
5659
        field->with_sum_func=0;
6168
5660
    }
6169
5661
  }
6170
5662
}
6184
5676
int test_if_item_cache_changed(List<Cached_item> &list)
6185
5677
{
6186
5678
  List<Cached_item>::iterator li(list.begin());
6187
 
  int idx= -1;
 
5679
  int idx= -1,i;
6188
5680
  Cached_item *buff;
6189
5681
 
6190
 
  for (int i=(int) list.size()-1 ; (buff=li++) ; i--)
 
5682
  for (i=(int) list.size()-1 ; (buff=li++) ; i--)
6191
5683
  {
6192
5684
    if (buff->cmp())
6193
 
      idx= i;
 
5685
      idx=i;
6194
5686
  }
6195
 
  return idx;
 
5687
  return(idx);
6196
5688
}
6197
5689
 
6198
5690
/**
6238
5730
  res_all_fields.clear();
6239
5731
  List<Item>::iterator itr(res_all_fields.begin());
6240
5732
  List<Item> extra_funcs;
6241
 
  uint32_t border= all_fields.size() - elements;
 
5733
  uint32_t i, border= all_fields.size() - elements;
6242
5734
 
6243
5735
  if (param->field_count &&
6244
5736
      !(copy= param->copy_field= new CopyField[param->field_count]))
6245
 
  {
6246
5737
    return true;
6247
 
  }
6248
5738
 
6249
5739
  param->copy_funcs.clear();
6250
 
  for (uint32_t i= 0; (pos= li++); i++)
 
5740
  for (i= 0; (pos= li++); i++)
6251
5741
  {
6252
5742
    Field *field;
6253
5743
    unsigned char *tmp;
6264
5754
        item->name= ref->name;
6265
5755
      }
6266
5756
      pos= item;
6267
 
 
6268
5757
      if (item->field->flags & BLOB_FLAG)
6269
5758
      {
6270
5759
        pos= new Item_copy_string(pos);
6293
5782
                Field_varstring::val_int
6294
5783
              */
6295
5784
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
6296
 
        {
6297
5785
          goto err;
6298
 
        }
6299
 
 
6300
5786
        if (copy)
6301
5787
        {
6302
5788
          copy->set(tmp, item->result_field);
6323
5809
      */
6324
5810
      pos=new Item_copy_string(pos);
6325
5811
      if (i < border)                           // HAVING, order_st and GROUP BY
6326
 
      {
6327
5812
        extra_funcs.push_back(pos);
6328
 
      }
6329
5813
      else 
6330
 
      {
6331
 
        param->copy_funcs.push_back(pos);
6332
 
      }
 
5814
                                param->copy_funcs.push_back(pos);
6333
5815
    }
6334
5816
    res_all_fields.push_back(pos);
6335
5817
    ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
6337
5819
  }
6338
5820
  param->copy_field_end= copy;
6339
5821
 
6340
 
  for (uint32_t i= 0; i < border; i++)
6341
 
  {
 
5822
  for (i= 0; i < border; i++)
6342
5823
    itr++;
6343
 
  }
6344
5824
  itr.sublist(res_selected_fields, elements);
6345
5825
  /*
6346
5826
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
6352
5832
 
6353
5833
err:
6354
5834
  if (copy)
6355
 
  {
6356
5835
    delete[] param->copy_field;
6357
 
  }
6358
5836
  param->copy_field=0;
6359
5837
  return true;
6360
5838
}
6367
5845
*/
6368
5846
void copy_fields(Tmp_Table_Param *param)
6369
5847
{
6370
 
  for (CopyField *ptr= param->copy_field; ptr != param->copy_field_end; ptr++)
6371
 
  {
 
5848
  CopyField *ptr= param->copy_field;
 
5849
  CopyField *end= param->copy_field_end;
 
5850
 
 
5851
  for (; ptr != end; ptr++)
6372
5852
    (*ptr->do_copy)(ptr);
6373
 
  }
6374
5853
 
6375
5854
  List<Item>::iterator it(param->copy_funcs.begin());
6376
5855
  Item_copy_string *item;
6377
5856
  while ((item = (Item_copy_string*) it++))
6378
 
  {
6379
5857
    item->copy();
6380
 
  }
6381
5858
}
6382
5859
 
6383
5860
/**
6414
5891
  {
6415
5892
    Field *field;
6416
5893
 
6417
 
    if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) or
6418
 
        (item->type() == Item::FUNC_ITEM and ((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
6419
 
    {
 
5894
    if ((item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM) ||
 
5895
        (item->type() == Item::FUNC_ITEM &&
 
5896
         ((Item_func*)item)->functype() == Item_func::SUSERVAR_FUNC))
6420
5897
      item_field= item;
6421
 
    }
6422
5898
    else
6423
5899
    {
6424
5900
      if (item->type() == Item::FIELD_ITEM)
6428
5904
      else if ((field= item->get_tmp_table_field()))
6429
5905
      {
6430
5906
        if (item->type() == Item::SUM_FUNC_ITEM && field->getTable()->group)
6431
 
        {
6432
5907
          item_field= ((Item_sum*) item)->result_item(field);
6433
 
        }
6434
5908
        else
6435
 
        {
6436
5909
          item_field= (Item*) new Item_field(field);
6437
 
        }
6438
 
 
6439
 
        if (item_field == NULL)
6440
 
        {
 
5910
        if (!item_field)
6441
5911
          return true;                    // Fatal error
6442
 
        }
6443
5912
 
6444
5913
        if (item->real_item()->type() != Item::FIELD_ITEM)
6445
 
        {
6446
5914
          field->orig_table= 0;
6447
 
        }
6448
 
 
6449
5915
        item_field->name= item->name;
6450
 
 
6451
5916
        if (item->type() == Item::REF_ITEM)
6452
5917
        {
6453
5918
          Item_field *ifield= (Item_field *) item_field;
6457
5922
        }
6458
5923
      }
6459
5924
      else
6460
 
      {
6461
5925
        item_field= item;
6462
 
      }
6463
5926
    }
6464
5927
    res_all_fields.push_back(item_field);
6465
 
    ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]= item_field;
 
5928
    ref_pointer_array[((i < border)? all_fields.size()-i-1 : i-border)]=
 
5929
      item_field;
6466
5930
  }
6467
5931
 
6468
5932
  List<Item>::iterator itr(res_all_fields.begin());
6510
5974
 
6511
5975
  List<Item>::iterator itr(res_all_fields.begin());
6512
5976
  for (i= 0; i < border; i++)
6513
 
  {
6514
5977
    itr++;
6515
 
  }
6516
5978
  itr.sublist(res_selected_fields, elements);
6517
5979
 
6518
5980
  return session->is_fatal_error;
6539
6001
  while ((func= *(func_ptr++)))
6540
6002
  {
6541
6003
    if (func->setup(session))
6542
 
    {
6543
6004
      return true;
6544
 
    }
6545
6005
  }
6546
6006
  return false;
6547
6007
}
6550
6010
{
6551
6011
  Item_sum *func;
6552
6012
  while ((func= *(func_ptr++)))
6553
 
  {
6554
6013
    func->reset_field();
6555
 
  }
6556
6014
}
6557
6015
 
6558
6016
/** Update record 0 in tmp_table from record 1. */
6560
6018
{
6561
6019
  Item_sum *func;
6562
6020
  while ((func= *(func_ptr++)))
6563
 
  {
6564
6021
    func->update_field();
6565
 
  }
6566
6022
}
6567
6023
 
6568
6024
/** Copy result of sum functions to record in tmp_table. */
6569
6025
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
6570
6026
{
6571
6027
  for (; func_ptr != end_ptr ; func_ptr++)
6572
 
  {
6573
6028
    (void) (*func_ptr)->save_in_result_field(1);
6574
 
  }
 
6029
  return;
6575
6030
}
6576
6031
 
6577
6032
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
6581
6036
    if ((*func_ptr)->reset())
6582
6037
      return 1;
6583
6038
  }
6584
 
 
6585
6039
  /* If rollup, calculate the upper sum levels */
6586
6040
  for ( ; *func_ptr ; func_ptr++)
6587
6041
  {
6588
6042
    if ((*func_ptr)->add())
6589
 
    {
6590
6043
      return 1;
6591
 
    }
6592
6044
  }
6593
6045
  return 0;
6594
6046
}
6597
6049
{
6598
6050
  Item_sum *func;
6599
6051
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
6600
 
  {
6601
6052
    if (func->add())
6602
 
    {
6603
6053
      return 1;
6604
 
    }
6605
 
  }
6606
6054
  return 0;
6607
6055
}
6608
6056
 
6620
6068
      are extended to return status code.
6621
6069
    */
6622
6070
    if (session->is_error())
6623
 
    {
6624
6071
      return true;
6625
 
    }
6626
6072
  }
6627
6073
  return false;
6628
6074
}
6712
6158
      else if (item->type() == Item::FUNC_ITEM)
6713
6159
      {
6714
6160
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
6715
 
        {
6716
6161
          return 1;
6717
 
        }
6718
6162
      }
6719
6163
    }
6720
 
 
6721
6164
    if (arg_changed)
6722
6165
    {
6723
6166
      expr->maybe_null= 1;
6742
6185
      str->append(STRING_WITH_LEN(" left join "));
6743
6186
    }
6744
6187
    else if (curr->straight)
6745
 
    {
6746
6188
      str->append(STRING_WITH_LEN(" straight_join "));
6747
 
    }
6748
6189
    else
6749
 
    {
6750
6190
      str->append(STRING_WITH_LEN(" join "));
6751
 
    }
6752
6191
    curr->print(session, str);
6753
6192
    if (curr->on_expr)
6754
6193
    {
6773
6212
  TableList **table= new (session->mem) TableList*[tables->size()];
6774
6213
 
6775
6214
  for (TableList **t= table + (tables->size() - 1); t >= table; t--)
6776
 
  {
6777
6215
    *t= ti++;
6778
 
  }
6779
6216
  assert(tables->size() >= 1);
6780
6217
  print_table_array(session, str, table, table + tables->size());
6781
6218
}
6783
6220
void Select_Lex::print(Session *session, String *str)
6784
6221
{
6785
6222
  /* QQ: session may not be set for sub queries, but this should be fixed */
6786
 
  if (not session)
6787
 
  {
 
6223
  if(not session)
6788
6224
    session= current_session;
6789
 
  }
6790
6225
 
6791
6226
 
6792
6227
  str->append(STRING_WITH_LEN("select "));
6794
6229
  /* First add options */
6795
6230
  if (options & SELECT_STRAIGHT_JOIN)
6796
6231
    str->append(STRING_WITH_LEN("straight_join "));
6797
 
 
6798
6232
  if (options & SELECT_DISTINCT)
6799
6233
    str->append(STRING_WITH_LEN("distinct "));
6800
 
 
6801
6234
  if (options & SELECT_SMALL_RESULT)
6802
6235
    str->append(STRING_WITH_LEN("sql_small_result "));
6803
 
 
6804
6236
  if (options & SELECT_BIG_RESULT)
6805
6237
    str->append(STRING_WITH_LEN("sql_big_result "));
6806
 
 
6807
6238
  if (options & OPTION_BUFFER_RESULT)
6808
6239
    str->append(STRING_WITH_LEN("sql_buffer_result "));
6809
 
 
6810
6240
  if (options & OPTION_FOUND_ROWS)
6811
6241
    str->append(STRING_WITH_LEN("sql_calc_found_rows "));
6812
6242
 
6817
6247
  while ((item= it++))
6818
6248
  {
6819
6249
    if (first)
6820
 
    {
6821
6250
      first= 0;
6822
 
    }
6823
6251
    else
6824
 
    {
6825
6252
      str->append(',');
6826
 
    }
6827
6253
    item->print_item_w_name(str);
6828
6254
  }
6829
6255
 
6854
6280
  {
6855
6281
    str->append(STRING_WITH_LEN(" where "));
6856
6282
    if (cur_where)
6857
 
    {
6858
6283
      cur_where->print(str);
6859
 
    }
6860
6284
    else
6861
 
    {
6862
6285
      str->append(cond_value != Item::COND_FALSE ? "1" : "0");
6863
 
    }
6864
6286
  }
6865
6287
 
6866
6288
  // group by & olap
6890
6312
  {
6891
6313
    str->append(STRING_WITH_LEN(" having "));
6892
6314
    if (cur_having)
6893
 
    {
6894
6315
      cur_having->print(str);
6895
 
    }
6896
6316
    else
6897
 
    {
6898
6317
      str->append(having_value != Item::COND_FALSE ? "1" : "0");
6899
 
    }
6900
6318
  }
6901
6319
 
6902
6320
  if (order_list.size())