~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.h

  • Committer: Brian Aker
  • Date: 2009-05-29 23:52:19 UTC
  • mfrom: (1039.2.1 working)
  • Revision ID: brian@gaz-20090529235219-f9hodej510x4bwyj
Merge Jay

Show diffs side-by-side

added added

removed removed

Lines of Context:
160
160
  SQL_SELECT *select;
161
161
} JOIN_CACHE;
162
162
 
163
 
 
164
 
/*
165
 
  The structs which holds the join connections and join states
166
 
*/
167
 
enum join_type { JT_UNKNOWN,JT_SYSTEM,JT_CONST,JT_EQ_REF,JT_REF,JT_MAYBE_REF,
168
 
                 JT_ALL, JT_RANGE, JT_NEXT, JT_REF_OR_NULL,
169
 
                 JT_UNIQUE_SUBQUERY, JT_INDEX_SUBQUERY, JT_INDEX_MERGE};
170
 
 
171
163
class JOIN;
172
164
 
 
165
/** The states in which a nested loop join can be in */
173
166
enum enum_nested_loop_state
174
167
{
175
 
  NESTED_LOOP_KILLED= -2, NESTED_LOOP_ERROR= -1,
176
 
  NESTED_LOOP_OK= 0, NESTED_LOOP_NO_MORE_ROWS= 1,
177
 
  NESTED_LOOP_QUERY_LIMIT= 3, NESTED_LOOP_CURSOR_LIMIT= 4
 
168
  NESTED_LOOP_KILLED= -2,
 
169
  NESTED_LOOP_ERROR= -1,
 
170
  NESTED_LOOP_OK= 0,
 
171
  NESTED_LOOP_NO_MORE_ROWS= 1,
 
172
  NESTED_LOOP_QUERY_LIMIT= 3,
 
173
  NESTED_LOOP_CURSOR_LIMIT= 4
178
174
};
179
175
 
 
176
/** Description of a join type */
 
177
enum join_type 
 
178
 
179
  JT_UNKNOWN,
 
180
  JT_SYSTEM,
 
181
  JT_CONST,
 
182
  JT_EQ_REF,
 
183
  JT_REF,
 
184
  JT_MAYBE_REF,
 
185
        JT_ALL,
 
186
  JT_RANGE,
 
187
  JT_NEXT,
 
188
  JT_REF_OR_NULL,
 
189
  JT_UNIQUE_SUBQUERY,
 
190
  JT_INDEX_SUBQUERY,
 
191
  JT_INDEX_MERGE
 
192
};
180
193
 
181
194
/* Values for JOIN_TAB::packed_info */
182
195
#define TAB_INFO_HAVE_VALUE 1
376
389
  List<Item> *fields;
377
390
} ROLLUP;
378
391
 
379
 
 
380
 
class JOIN :public Sql_alloc
381
 
{
382
 
  JOIN(const JOIN &rhs);                        /**< not implemented */
383
 
  JOIN& operator=(const JOIN &rhs);             /**< not implemented */
384
 
public:
385
 
  JOIN_TAB *join_tab,**best_ref;
386
 
  JOIN_TAB **map2table;    ///< mapping between table indexes and JOIN_TABs
387
 
  JOIN_TAB *join_tab_save; ///< saved join_tab for subquery reexecution
388
 
  Table    **table,**all_tables;
389
 
  /**
390
 
    The table which has an index that allows to produce the requried ordering.
391
 
    A special value of 0x1 means that the ordering will be produced by
392
 
    passing 1st non-const table to filesort(). NULL means no such table exists.
393
 
  */
394
 
  Table    *sort_by_table;
395
 
  uint     tables;        /**< Number of tables in the join */
396
 
  uint32_t     outer_tables;  /**< Number of tables that are not inside semijoin */
397
 
  uint32_t     const_tables;
398
 
  uint     send_group_parts;
399
 
  bool     sort_and_group,first_record,full_join,group, no_field_update;
400
 
  bool     do_send_rows;
401
 
  /**
402
 
    true when we want to resume nested loop iterations when
403
 
    fetching data from a cursor
404
 
  */
405
 
  bool     resume_nested_loop;
406
 
  table_map const_table_map,found_const_table_map,outer_join;
407
 
  ha_rows  send_records,found_records,examined_rows,row_limit, select_limit;
408
 
  /**
409
 
    Used to fetch no more than given amount of rows per one
410
 
    fetch operation of server side cursor.
411
 
    The value is checked in end_send and end_send_group in fashion, similar
412
 
    to offset_limit_cnt:
413
 
      - fetch_limit= HA_POS_ERROR if there is no cursor.
414
 
      - when we open a cursor, we set fetch_limit to 0,
415
 
      - on each fetch iteration we add num_rows to fetch to fetch_limit
416
 
  */
417
 
  ha_rows  fetch_limit;
418
 
  POSITION positions[MAX_TABLES+1],best_positions[MAX_TABLES+1];
419
 
 
420
 
  /* *
421
 
    Bitmap of nested joins embedding the position at the end of the current
422
 
    partial join (valid only during join optimizer run).
423
 
  */
424
 
  nested_join_map cur_embedding_map;
425
 
 
426
 
  double   best_read;
427
 
  List<Item> *fields;
428
 
  List<Cached_item> group_fields, group_fields_cache;
429
 
  Table    *tmp_table;
430
 
  /// used to store 2 possible tmp table of SELECT
431
 
  Table    *exec_tmp_table1, *exec_tmp_table2;
432
 
  Session          *session;
433
 
  Item_sum  **sum_funcs, ***sum_funcs_end;
434
 
  /** second copy of sumfuncs (for queries with 2 temporary tables */
435
 
  Item_sum  **sum_funcs2, ***sum_funcs_end2;
436
 
  Item      *having;
437
 
  Item      *tmp_having; ///< To store having when processed temporary table
438
 
  Item      *having_history; ///< Store having for explain
439
 
  uint64_t  select_options;
440
 
  select_result *result;
441
 
  Tmp_Table_Param tmp_table_param;
442
 
  DRIZZLE_LOCK *lock;
443
 
  /// unit structure (with global parameters) for this select
444
 
  Select_Lex_Unit *unit;
445
 
  /// select that processed
446
 
  Select_Lex *select_lex;
447
 
  /**
448
 
    true <=> optimizer must not mark any table as a constant table.
449
 
    This is needed for subqueries in form "a IN (SELECT .. UNION SELECT ..):
450
 
    when we optimize the select that reads the results of the union from a
451
 
    temporary table, we must not mark the temp. table as constant because
452
 
    the number of rows in it may vary from one subquery execution to another.
453
 
  */
454
 
  bool no_const_tables;
455
 
 
456
 
  JOIN *tmp_join; ///< copy of this JOIN to be used with temporary tables
457
 
  ROLLUP rollup;                                ///< Used with rollup
458
 
 
459
 
  bool select_distinct;                         ///< Set if SELECT DISTINCT
460
 
  /**
461
 
    If we have the GROUP BY statement in the query,
462
 
    but the group_list was emptied by optimizer, this
463
 
    flag is true.
464
 
    It happens when fields in the GROUP BY are from
465
 
    constant table
466
 
  */
467
 
  bool group_optimized_away;
468
 
 
469
 
  /*
470
 
    simple_xxxxx is set if order_st/GROUP BY doesn't include any references
471
 
    to other tables than the first non-constant table in the JOIN.
472
 
    It's also set if order_st/GROUP BY is empty.
473
 
  */
474
 
  bool simple_order, simple_group;
475
 
  /**
476
 
    Is set only in case if we have a GROUP BY clause
477
 
    and no order_st BY after constant elimination of 'order'.
478
 
  */
479
 
  bool no_order;
480
 
  /** Is set if we have a GROUP BY and we have order_st BY on a constant. */
481
 
  bool          skip_sort_order;
482
 
 
483
 
  bool need_tmp, hidden_group_fields;
484
 
  DYNAMIC_ARRAY keyuse;
485
 
  Item::cond_result cond_value, having_value;
486
 
  List<Item> all_fields; ///< to store all fields that used in query
487
 
  ///Above list changed to use temporary table
488
 
  List<Item> tmp_all_fields1, tmp_all_fields2, tmp_all_fields3;
489
 
  ///Part, shared with list above, emulate following list
490
 
  List<Item> tmp_fields_list1, tmp_fields_list2, tmp_fields_list3;
491
 
  List<Item> &fields_list; ///< hold field list passed to mysql_select
492
 
  int error;
493
 
 
494
 
  order_st *order, *group_list; //hold parameters of mysql_select
495
 
  COND *conds;                            // ---"---
496
 
  Item *conds_history;                    // store WHERE for explain
497
 
  TableList *tables_list;           ///<hold 'tables' parameter of mysql_select
498
 
  List<TableList> *join_list;       ///< list of joined tables in reverse order
499
 
  COND_EQUAL *cond_equal;
500
 
  SQL_SELECT *select;                ///<created in optimisation phase
501
 
  JOIN_TAB *return_tab;              ///<used only for outer joins
502
 
  Item **ref_pointer_array; ///<used pointer reference for this select
503
 
  // Copy of above to be used with different lists
504
 
  Item **items0, **items1, **items2, **items3, **current_ref_pointer_array;
505
 
  uint32_t ref_pointer_array_size; ///< size of above in bytes
506
 
  const char *zero_result_cause; ///< not 0 if exec must return zero result
507
 
 
508
 
  bool union_part; ///< this subselect is part of union
509
 
  bool optimized; ///< flag to avoid double optimization in EXPLAIN
510
 
 
511
 
  Array<Item_in_subselect> sj_subselects;
512
 
 
513
 
  /* Descriptions of temporary tables used to weed-out semi-join duplicates */
514
 
  SJ_TMP_TABLE  *sj_tmp_tables;
515
 
 
516
 
  table_map cur_emb_sj_nests;
517
 
 
518
 
  /*
519
 
    storage for caching buffers allocated during query execution.
520
 
    These buffers allocations need to be cached as the thread memory pool is
521
 
    cleared only at the end of the execution of the whole query and not caching
522
 
    allocations that occur in repetition at execution time will result in
523
 
    excessive memory usage.
524
 
  */
525
 
  SORT_FIELD *sortorder;                        // make_unireg_sortorder()
526
 
  Table **table_reexec;                         // make_simple_join()
527
 
  JOIN_TAB *join_tab_reexec;                    // make_simple_join()
528
 
  /* end of allocation caching storage */
529
 
 
530
 
  JOIN(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
531
 
       select_result *result_arg)
532
 
    :fields_list(fields_arg), sj_subselects(session_arg->mem_root, 4)
533
 
  {
534
 
    init(session_arg, fields_arg, select_options_arg, result_arg);
535
 
  }
536
 
 
537
 
  void init(Session *session_arg, List<Item> &fields_arg, uint64_t select_options_arg,
538
 
       select_result *result_arg)
539
 
  {
540
 
    join_tab= join_tab_save= 0;
541
 
    table= 0;
542
 
    tables= 0;
543
 
    const_tables= 0;
544
 
    join_list= 0;
545
 
    sort_and_group= 0;
546
 
    first_record= 0;
547
 
    do_send_rows= 1;
548
 
    resume_nested_loop= false;
549
 
    send_records= 0;
550
 
    found_records= 0;
551
 
    fetch_limit= HA_POS_ERROR;
552
 
    examined_rows= 0;
553
 
    exec_tmp_table1= 0;
554
 
    exec_tmp_table2= 0;
555
 
    sortorder= 0;
556
 
    table_reexec= 0;
557
 
    join_tab_reexec= 0;
558
 
    session= session_arg;
559
 
    sum_funcs= sum_funcs2= 0;
560
 
    having= tmp_having= having_history= 0;
561
 
    select_options= select_options_arg;
562
 
    result= result_arg;
563
 
    lock= session_arg->lock;
564
 
    select_lex= 0; //for safety
565
 
    tmp_join= 0;
566
 
    select_distinct= test(select_options & SELECT_DISTINCT);
567
 
    no_order= 0;
568
 
    simple_order= 0;
569
 
    simple_group= 0;
570
 
    skip_sort_order= 0;
571
 
    need_tmp= 0;
572
 
    hidden_group_fields= 0; /*safety*/
573
 
    error= 0;
574
 
    select= 0;
575
 
    return_tab= 0;
576
 
    ref_pointer_array= items0= items1= items2= items3= 0;
577
 
    ref_pointer_array_size= 0;
578
 
    zero_result_cause= 0;
579
 
    optimized= 0;
580
 
    cond_equal= 0;
581
 
    group_optimized_away= 0;
582
 
 
583
 
    all_fields= fields_arg;
584
 
    if (&fields_list != &fields_arg) /* only copy if not same*/
585
 
      fields_list= fields_arg;
586
 
    memset(&keyuse, 0, sizeof(keyuse));
587
 
    tmp_table_param.init();
588
 
    tmp_table_param.end_write_records= HA_POS_ERROR;
589
 
    rollup.state= ROLLUP::STATE_NONE;
590
 
    sj_tmp_tables= NULL;
591
 
 
592
 
    no_const_tables= false;
593
 
  }
594
 
 
595
 
  int prepare(Item ***rref_pointer_array, TableList *tables, uint32_t wind_num,
596
 
              COND *conds, uint32_t og_num, order_st *order, order_st *group,
597
 
              Item *having, Select_Lex *select, Select_Lex_Unit *unit);
598
 
  int optimize();
599
 
  int reinit();
600
 
  void exec();
601
 
  int destroy();
602
 
  void restore_tmp();
603
 
  bool alloc_func_list();
604
 
  bool flatten_subqueries();
605
 
  bool setup_subquery_materialization();
606
 
  bool make_sum_func_list(List<Item> &all_fields, List<Item> &send_fields,
607
 
                          bool before_group_by, bool recompute= false);
608
 
 
609
 
  inline void set_items_ref_array(Item **ptr)
610
 
  {
611
 
    memcpy(ref_pointer_array, ptr, ref_pointer_array_size);
612
 
    current_ref_pointer_array= ptr;
613
 
  }
614
 
  inline void init_items_ref_array()
615
 
  {
616
 
    items0= ref_pointer_array + all_fields.elements;
617
 
    memcpy(items0, ref_pointer_array, ref_pointer_array_size);
618
 
    current_ref_pointer_array= items0;
619
 
  }
620
 
 
621
 
  bool rollup_init();
622
 
  bool rollup_make_fields(List<Item> &all_fields, List<Item> &fields,
623
 
                          Item_sum ***func);
624
 
  int rollup_send_data(uint32_t idx);
625
 
  int rollup_write_data(uint32_t idx, Table *table);
626
 
  void remove_subq_pushed_predicates(Item **where);
627
 
  /**
628
 
    Release memory and, if possible, the open tables held by this execution
629
 
    plan (and nested plans). It's used to release some tables before
630
 
    the end of execution in order to increase concurrency and reduce
631
 
    memory consumption.
632
 
  */
633
 
  void join_free();
634
 
  /** Cleanup this JOIN, possibly for reuse */
635
 
  void cleanup(bool full);
636
 
  void clear();
637
 
  bool save_join_tab();
638
 
  bool init_save_join_tab();
639
 
  bool send_row_on_empty_set()
640
 
  {
641
 
    return (do_send_rows && tmp_table_param.sum_func_count != 0 &&
642
 
            !group_list);
643
 
  }
644
 
  bool change_result(select_result *result);
645
 
  bool is_top_level_join() const
646
 
  {
647
 
    return (unit == &session->lex->unit && (unit->fake_select_lex == 0 ||
648
 
                                        select_lex == unit->fake_select_lex));
649
 
  }
650
 
};
651
 
 
 
392
#include "drizzled/join.h"
652
393
 
653
394
typedef struct st_select_check {
654
395
  uint32_t const_ref,reg_ref;