~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2011-10-18 17:07:03 UTC
  • mto: This revision was merged to the branch mainline in revision 2444.
  • Revision ID: brian@tangent.org-20111018170703-cwtp14k5tykbp2xz
Enable syslog and format sql_select

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