~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Lee Bieber
  • Date: 2010-08-21 22:42:44 UTC
  • mto: (1727.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 1728.
  • Revision ID: lbieber@kalebral-2.local-20100821224244-kh3gmsvi45dlbuu1
For the feature request (https://blueprints.launchpad.net/drizzle/+spec/limit-maximum-sort-size) 
that is requesting the ability to cap various buffers, we first tried setting the join buffer to 
1 to see how that would affect the test results and expose test results that need to 
sorted (by adding --sorted_result). 

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
#include "config.h"
17
17
 
39
39
#include <drizzled/statement.h>
40
40
#include <drizzled/statement/alter_table.h>
41
41
#include "drizzled/probes.h"
42
 
#include "drizzled/session/cache.h"
 
42
#include "drizzled/session_list.h"
43
43
#include "drizzled/global_charset_info.h"
44
44
 
45
45
#include "drizzled/plugin/logging.h"
46
46
#include "drizzled/plugin/query_rewrite.h"
47
 
#include "drizzled/plugin/query_cache.h"
48
47
#include "drizzled/plugin/authorization.h"
49
48
#include "drizzled/optimizer/explain_plan.h"
50
49
#include "drizzled/pthread_globals.h"
51
 
#include "drizzled/plugin/event_observer.h"
52
50
 
53
51
#include <limits.h>
54
52
 
55
53
#include <bitset>
56
54
#include <algorithm>
57
 
#include <boost/date_time.hpp>
 
55
 
58
56
#include "drizzled/internal/my_sys.h"
59
57
 
60
58
using namespace std;
170
168
  bool error= 0;
171
169
  Query_id &query_id= Query_id::get_query_id();
172
170
 
173
 
  DRIZZLE_COMMAND_START(session->thread_id, command);
 
171
  DRIZZLE_COMMAND_START(session->thread_id,
 
172
                        command);
174
173
 
175
174
  session->command= command;
176
175
  session->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
190
189
  /* TODO: set session->lex->sql_command to SQLCOM_END here */
191
190
 
192
191
  plugin::Logging::preDo(session);
193
 
  if (unlikely(plugin::EventObserver::beforeStatement(*session)))
194
 
  {
195
 
    // We should do something about an error...
196
 
  }
197
192
 
198
193
  session->server_status&=
199
194
           ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
218
213
  }
219
214
  case COM_QUERY:
220
215
  {
221
 
    if (not session->readAndStoreQuery(packet, packet_length))
 
216
    if (! session->readAndStoreQuery(packet, packet_length))
222
217
      break;                                    // fatal error is set
223
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
 
218
    DRIZZLE_QUERY_START(session->query.c_str(),
224
219
                        session->thread_id,
225
 
                        const_cast<const char *>(session->schema()->c_str()));
 
220
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
226
221
 
227
 
    mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
222
    plugin::QueryRewriter::rewriteQuery(session->db, session->query);
 
223
    mysql_parse(session, session->query.c_str(), session->query.length());
228
224
 
229
225
    break;
230
226
  }
269
265
    if (! session->main_da.is_set())
270
266
      session->send_kill_message();
271
267
  }
272
 
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
 
268
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
273
269
  {
274
 
    session->setKilled(Session::NOT_KILLED);
 
270
    session->killed= Session::NOT_KILLED;
275
271
    session->setAbort(false);
276
272
  }
277
273
 
310
306
  session->close_thread_tables();
311
307
 
312
308
  plugin::Logging::postDo(session);
313
 
  if (unlikely(plugin::EventObserver::afterStatement(*session)))
314
 
  {
315
 
    // We should do something about an error...
316
 
  }
317
309
 
318
310
  /* Store temp state for processlist */
319
311
  session->set_proc_info("cleaning up");
320
312
  session->command= COM_SLEEP;
321
 
  session->resetQueryString();
 
313
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
314
  session->query.clear();
322
315
 
323
316
  session->set_proc_info(NULL);
324
317
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
365
358
                           const string& schema_table_name)
366
359
{
367
360
  LEX_STRING db, table;
368
 
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
369
361
  /*
370
362
     We have to make non const db_name & table_name
371
363
     because of lower_case_table_names
374
366
  session->make_lex_string(&table, schema_table_name, false);
375
367
 
376
368
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
377
 
                               NULL, table_options, TL_READ))
 
369
                               NULL, 0, TL_READ))
378
370
  {
379
371
    return true;
380
372
  }
429
421
    true        Error
430
422
*/
431
423
 
432
 
static int mysql_execute_command(Session *session)
 
424
static int
 
425
mysql_execute_command(Session *session)
433
426
{
434
427
  bool res= false;
435
428
  LEX  *lex= session->lex;
437
430
  Select_Lex *select_lex= &lex->select_lex;
438
431
  /* list of all tables in query */
439
432
  TableList *all_tables;
 
433
  /* A peek into the query string */
 
434
  size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
 
435
                        PROCESS_LIST_WIDTH : session->query.length();
 
436
 
 
437
  memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
 
438
  session->process_list_info[proc_info_len]= '\0';
440
439
 
441
440
  /*
442
441
    In many cases first table of main Select_Lex have special meaning =>
477
476
 
478
477
  /* now we are ready to execute the statement */
479
478
  res= lex->statement->execute();
 
479
 
480
480
  session->set_proc_info("query end");
 
481
 
481
482
  /*
482
483
    The return value for ROW_COUNT() is "implementation dependent" if the
483
484
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
491
492
 
492
493
  return (res || session->is_error());
493
494
}
 
495
 
494
496
bool execute_sqlcom_select(Session *session, TableList *all_tables)
495
497
{
496
498
  LEX   *lex= session->lex;
538
540
    {
539
541
      if (!result && !(result= new select_send()))
540
542
        return true;
541
 
 
542
 
      /* Init the Query Cache plugin */
543
 
      plugin::QueryCache::prepareResultset(session); 
544
543
      res= handle_select(session, lex, result, 0);
545
 
      /* Send the Resultset to the cache */
546
 
      plugin::QueryCache::setResultset(session); 
547
 
 
548
544
      if (result != lex->result)
549
545
        delete result;
550
546
    }
716
712
 
717
713
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
718
714
{
719
 
  boost::posix_time::ptime start_time=boost::posix_time::microsec_clock::local_time();
720
 
  session->lex->start(session);
721
 
 
 
715
  uint64_t start_time= my_getsystime();
 
716
  lex_start(session);
722
717
  session->reset_for_next_command();
723
 
  /* Check if the Query is Cached if and return true if yes
724
 
   * TODO the plugin has to make sure that the query is cacheble
725
 
   * by setting the query_safe_cache param to TRUE
726
 
   */
727
 
  bool res= true;
728
 
  if (plugin::QueryCache::isCached(session))
729
 
  {
730
 
    res= plugin::QueryCache::sendCachedResultset(session);
731
 
  }
732
 
  if (not res)
733
 
  {
734
 
    return;
735
 
  }
 
718
 
736
719
  LEX *lex= session->lex;
 
720
 
737
721
  Lex_input_stream lip(session, inBuf, length);
 
722
 
738
723
  bool err= parse_sql(session, &lip);
 
724
 
739
725
  if (!err)
740
726
  {
741
727
    {
742
 
      if (not session->is_error())
 
728
      if (! session->is_error())
743
729
      {
744
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
730
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
745
731
                                 session->thread_id,
746
 
                                 const_cast<const char *>(session->schema()->c_str()));
 
732
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
747
733
        // Implement Views here --Brian
 
734
 
748
735
        /* Actually execute the query */
749
 
        try 
750
 
        {
 
736
        try {
751
737
          mysql_execute_command(session);
752
738
        }
753
739
        catch (...)
754
740
        {
755
741
          // Just try to catch any random failures that could have come
756
742
          // during execution.
757
 
          unireg_abort(1);
758
743
        }
759
744
        DRIZZLE_QUERY_EXEC_DONE(0);
760
745
      }
764
749
  {
765
750
    assert(session->is_error());
766
751
  }
 
752
 
767
753
  lex->unit.cleanup();
768
754
  session->set_proc_info("freeing items");
769
755
  session->end_statement();
770
756
  session->cleanup_after_query();
771
 
  boost::posix_time::ptime end_time=boost::posix_time::microsec_clock::local_time();
772
 
  session->status_var.execution_time_nsec+=(end_time-start_time).total_microseconds();
 
757
  session->status_var.execution_time_nsec+= my_getsystime() - start_time;
773
758
}
774
759
 
775
760
 
897
882
*/
898
883
 
899
884
TableList *Select_Lex::add_table_to_list(Session *session,
900
 
                                                             Table_ident *table,
901
 
                                                             LEX_STRING *alias,
902
 
                                                             const bitset<NUM_OF_TABLE_OPTIONS>& table_options,
903
 
                                                             thr_lock_type lock_type,
904
 
                                                             List<Index_hint> *index_hints_arg,
905
 
                                         LEX_STRING *option)
 
885
                                             Table_ident *table,
 
886
                                             LEX_STRING *alias,
 
887
                                             uint32_t table_options,
 
888
                                             thr_lock_type lock_type,
 
889
                                             List<Index_hint> *index_hints_arg,
 
890
                                             LEX_STRING *option)
906
891
{
907
 
  TableList *ptr;
 
892
  register TableList *ptr;
908
893
  TableList *previous_table_ref; /* The table preceding the current one. */
909
894
  char *alias_str;
910
895
  LEX *lex= session->lex;
912
897
  if (!table)
913
898
    return NULL;                                // End of memory
914
899
  alias_str= alias ? alias->str : table->table.str;
915
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
 
900
  if (!test(table_options & TL_OPTION_ALIAS) &&
916
901
      check_table_name(table->table.str, table->table.length))
917
902
  {
918
903
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
949
934
  if (table->db.str)
950
935
  {
951
936
    ptr->setIsFqtn(true);
952
 
    ptr->setSchemaName(table->db.str);
 
937
    ptr->db= table->db.str;
953
938
    ptr->db_length= table->db.length;
954
939
  }
955
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
 
940
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
956
941
    return NULL;
957
942
  else
958
943
    ptr->setIsFqtn(false);
959
944
 
960
945
  ptr->alias= alias_str;
961
946
  ptr->setIsAlias(alias ? true : false);
962
 
  ptr->setTableName(table->table.str);
 
947
  if (table->table.length)
 
948
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
 
949
  ptr->table_name=table->table.str;
963
950
  ptr->table_name_length=table->table.length;
964
951
  ptr->lock_type=   lock_type;
965
 
  ptr->force_index= table_options.test(TL_OPTION_FORCE_INDEX);
966
 
  ptr->ignore_leaves= table_options.test(TL_OPTION_IGNORE_LEAVES);
 
952
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
 
953
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
967
954
  ptr->derived=     table->sel;
968
955
  ptr->select_lex=  lex->current_select;
969
956
  ptr->index_hints= index_hints_arg;
977
964
         tables=tables->next_local)
978
965
    {
979
966
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
980
 
          !strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
 
967
          !strcasecmp(ptr->db, tables->db))
981
968
      {
982
969
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
983
970
        return NULL;
1415
1402
 
1416
1403
 
1417
1404
/**
 
1405
  kill on thread.
 
1406
 
 
1407
  @param session                        Thread class
 
1408
  @param id                     Thread id
 
1409
  @param only_kill_query        Should it kill the query or the connection
 
1410
 
 
1411
  @note
 
1412
    This is written such that we have a short lock on LOCK_thread_count
 
1413
*/
 
1414
 
 
1415
static unsigned int
 
1416
kill_one_thread(Session *, ulong id, bool only_kill_query)
 
1417
{
 
1418
  Session *tmp= NULL;
 
1419
  uint32_t error= ER_NO_SUCH_THREAD;
 
1420
  LOCK_thread_count.lock(); // For unlink from list
 
1421
  
 
1422
  for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
1423
  {
 
1424
    if ((*it)->thread_id == id)
 
1425
    {
 
1426
      tmp= *it;
 
1427
      tmp->lockForDelete();
 
1428
      break;
 
1429
    }
 
1430
  }
 
1431
  LOCK_thread_count.unlock();
 
1432
  if (tmp)
 
1433
  {
 
1434
 
 
1435
    if (tmp->isViewable())
 
1436
    {
 
1437
      tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1438
      error= 0;
 
1439
    }
 
1440
 
 
1441
    tmp->unlockForDelete();
 
1442
  }
 
1443
  return(error);
 
1444
}
 
1445
 
 
1446
 
 
1447
/*
 
1448
  kills a thread and sends response
 
1449
 
 
1450
  SYNOPSIS
 
1451
    sql_kill()
 
1452
    session                     Thread class
 
1453
    id                  Thread id
 
1454
    only_kill_query     Should it kill the query or the connection
 
1455
*/
 
1456
 
 
1457
void sql_kill(Session *session, ulong id, bool only_kill_query)
 
1458
{
 
1459
  uint32_t error;
 
1460
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
1461
    session->my_ok();
 
1462
  else
 
1463
    my_error(error, MYF(0), id);
 
1464
}
 
1465
 
 
1466
 
 
1467
/**
1418
1468
  Check if the select is a simple select (not an union).
1419
1469
 
1420
1470
  @retval
1542
1592
 
1543
1593
 
1544
1594
/**
 
1595
  CREATE TABLE query pre-check.
 
1596
 
 
1597
  @param session                        Thread handler
 
1598
  @param tables         Global table list
 
1599
  @param create_table           Table which will be created
 
1600
 
 
1601
  @retval
 
1602
    false   OK
 
1603
  @retval
 
1604
    true   Error
 
1605
*/
 
1606
 
 
1607
bool create_table_precheck(TableIdentifier &identifier)
 
1608
{
 
1609
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1610
  {
 
1611
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1612
    return true;
 
1613
  }
 
1614
 
 
1615
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1616
  {
 
1617
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
1618
    return true;
 
1619
  }
 
1620
 
 
1621
  return false;
 
1622
}
 
1623
 
 
1624
 
 
1625
/**
1545
1626
  negate given expression.
1546
1627
 
1547
1628
  @param session  thread handler
1666
1747
{
1667
1748
  assert(session->m_lip == NULL);
1668
1749
 
1669
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1750
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1670
1751
 
1671
1752
  /* Set Lex_input_stream. */
1672
1753