~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_parse.cc

  • Committer: Monty Taylor
  • Date: 2010-04-14 17:24:09 UTC
  • mto: This revision was merged to the branch mainline in revision 1470.
  • Revision ID: mordred@inaugust.com-20100414172409-msncde88yewjrlhx
Moved gcc-specific optoins to their own thing.

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 */
183
182
    break;
184
183
  /* Increase id and count all other statements. */
185
184
  default:
186
 
    session->status_var.questions++;
 
185
    statistic_increment(session->status_var.questions, &LOCK_status);
187
186
    query_id.next();
188
187
  }
189
188
 
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);
200
195
  switch (command) {
201
196
  case COM_INIT_DB:
202
197
  {
 
198
    status_var_increment(session->status_var.com_stat[SQLCOM_CHANGE_DB]);
203
199
    if (packet_length == 0)
204
200
    {
205
201
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
218
214
  }
219
215
  case COM_QUERY:
220
216
  {
221
 
    if (not session->readAndStoreQuery(packet, packet_length))
 
217
    if (! session->readAndStoreQuery(packet, packet_length))
222
218
      break;                                    // fatal error is set
223
 
    DRIZZLE_QUERY_START(session->getQueryString()->c_str(),
 
219
    DRIZZLE_QUERY_START(session->query.c_str(),
224
220
                        session->thread_id,
225
 
                        const_cast<const char *>(session->schema()->c_str()));
 
221
                        const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
226
222
 
227
 
    mysql_parse(session, session->getQueryString()->c_str(), session->getQueryString()->length());
 
223
    plugin::QueryRewriter::rewriteQuery(session->db, session->query);
 
224
    mysql_parse(session, session->query.c_str(), session->query.length());
228
225
 
229
226
    break;
230
227
  }
235
232
    break;
236
233
  case COM_SHUTDOWN:
237
234
  {
238
 
    session->status_var.com_other++;
 
235
    status_var_increment(session->status_var.com_other);
239
236
    session->my_eof();
240
237
    session->close_thread_tables();                     // Free before kill
241
238
    kill_drizzle();
243
240
    break;
244
241
  }
245
242
  case COM_PING:
246
 
    session->status_var.com_other++;
 
243
    status_var_increment(session->status_var.com_other);
247
244
    session->my_ok();                           // Tell client we are alive
248
245
    break;
249
246
  case COM_SLEEP:
269
266
    if (! session->main_da.is_set())
270
267
      session->send_kill_message();
271
268
  }
272
 
  if (session->getKilled() == Session::KILL_QUERY || session->getKilled() == Session::KILL_BAD_DATA)
 
269
  if (session->killed == Session::KILL_QUERY || session->killed == Session::KILL_BAD_DATA)
273
270
  {
274
 
    session->setKilled(Session::NOT_KILLED);
275
 
    session->setAbort(false);
 
271
    session->killed= Session::NOT_KILLED;
 
272
    session->mysys_var->abort= 0;
276
273
  }
277
274
 
278
275
  /* Can not be true, but do not take chances in production. */
310
307
  session->close_thread_tables();
311
308
 
312
309
  plugin::Logging::postDo(session);
313
 
  if (unlikely(plugin::EventObserver::afterStatement(*session)))
314
 
  {
315
 
    // We should do something about an error...
316
 
  }
317
310
 
318
311
  /* Store temp state for processlist */
319
312
  session->set_proc_info("cleaning up");
320
313
  session->command= COM_SLEEP;
321
 
  session->resetQueryString();
 
314
  memset(session->process_list_info, 0, PROCESS_LIST_WIDTH);
 
315
  session->query.clear();
322
316
 
323
317
  session->set_proc_info(NULL);
324
 
  session->mem_root->free_root(MYF(memory::KEEP_PREALLOC));
 
318
  free_root(session->mem_root,MYF(memory::KEEP_PREALLOC));
325
319
 
326
320
  if (DRIZZLE_QUERY_DONE_ENABLED() || DRIZZLE_COMMAND_DONE_ENABLED())
327
321
  {
365
359
                           const string& schema_table_name)
366
360
{
367
361
  LEX_STRING db, table;
368
 
  bitset<NUM_OF_TABLE_OPTIONS> table_options;
369
362
  /*
370
363
     We have to make non const db_name & table_name
371
364
     because of lower_case_table_names
374
367
  session->make_lex_string(&table, schema_table_name, false);
375
368
 
376
369
  if (! sel->add_table_to_list(session, new Table_ident(db, table),
377
 
                               NULL, table_options, TL_READ))
 
370
                               NULL, 0, TL_READ))
378
371
  {
379
372
    return true;
380
373
  }
429
422
    true        Error
430
423
*/
431
424
 
432
 
static int mysql_execute_command(Session *session)
 
425
static int
 
426
mysql_execute_command(Session *session)
433
427
{
434
428
  bool res= false;
435
429
  LEX  *lex= session->lex;
437
431
  Select_Lex *select_lex= &lex->select_lex;
438
432
  /* list of all tables in query */
439
433
  TableList *all_tables;
 
434
  /* A peek into the query string */
 
435
  size_t proc_info_len= session->query.length() > PROCESS_LIST_WIDTH ?
 
436
                        PROCESS_LIST_WIDTH : session->query.length();
 
437
 
 
438
  memcpy(session->process_list_info, session->query.c_str(), proc_info_len);
 
439
  session->process_list_info[proc_info_len]= '\0';
440
440
 
441
441
  /*
442
442
    In many cases first table of main Select_Lex have special meaning =>
473
473
    drizzle_reset_errors(session, 0);
474
474
  }
475
475
 
 
476
  status_var_increment(session->status_var.com_stat[lex->sql_command]);
 
477
 
476
478
  assert(session->transaction.stmt.hasModifiedNonTransData() == false);
477
479
 
478
480
  /* now we are ready to execute the statement */
479
481
  res= lex->statement->execute();
 
482
 
480
483
  session->set_proc_info("query end");
 
484
 
481
485
  /*
482
486
    The return value for ROW_COUNT() is "implementation dependent" if the
483
487
    statement is not DELETE, INSERT or UPDATE, but -1 is what JDBC and ODBC
491
495
 
492
496
  return (res || session->is_error());
493
497
}
 
498
 
494
499
bool execute_sqlcom_select(Session *session, TableList *all_tables)
495
500
{
496
501
  LEX   *lex= session->lex;
538
543
    {
539
544
      if (!result && !(result= new select_send()))
540
545
        return true;
541
 
 
542
 
      /* Init the Query Cache plugin */
543
 
      plugin::QueryCache::prepareResultset(session); 
544
546
      res= handle_select(session, lex, result, 0);
545
 
      /* Send the Resultset to the cache */
546
 
      plugin::QueryCache::setResultset(session); 
547
 
 
548
547
      if (result != lex->result)
549
548
        delete result;
550
549
    }
716
715
 
717
716
void mysql_parse(Session *session, const char *inBuf, uint32_t length)
718
717
{
719
 
  boost::posix_time::ptime start_time=boost::posix_time::microsec_clock::local_time();
720
 
  session->lex->start(session);
721
 
 
 
718
  lex_start(session);
722
719
  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
 
  }
 
720
 
736
721
  LEX *lex= session->lex;
 
722
 
737
723
  Lex_input_stream lip(session, inBuf, length);
 
724
 
738
725
  bool err= parse_sql(session, &lip);
 
726
 
739
727
  if (!err)
740
728
  {
741
729
    {
742
 
      if (not session->is_error())
 
730
      if (! session->is_error())
743
731
      {
744
 
        DRIZZLE_QUERY_EXEC_START(session->getQueryString()->c_str(),
 
732
        DRIZZLE_QUERY_EXEC_START(session->query.c_str(),
745
733
                                 session->thread_id,
746
 
                                 const_cast<const char *>(session->schema()->c_str()));
747
 
        // Implement Views here --Brian
 
734
                                 const_cast<const char *>(session->db.empty() ? "" : session->db.c_str()));
748
735
        /* Actually execute the query */
749
 
        try 
750
 
        {
751
 
          mysql_execute_command(session);
752
 
        }
753
 
        catch (...)
754
 
        {
755
 
          // Just try to catch any random failures that could have come
756
 
          // during execution.
757
 
          unireg_abort(1);
758
 
        }
 
736
        mysql_execute_command(session);
759
737
        DRIZZLE_QUERY_EXEC_DONE(0);
760
738
      }
761
739
    }
764
742
  {
765
743
    assert(session->is_error());
766
744
  }
 
745
 
767
746
  lex->unit.cleanup();
768
747
  session->set_proc_info("freeing items");
769
748
  session->end_statement();
770
749
  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();
 
750
 
 
751
  return;
773
752
}
774
753
 
775
754
 
897
876
*/
898
877
 
899
878
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)
 
879
                                             Table_ident *table,
 
880
                                             LEX_STRING *alias,
 
881
                                             uint32_t table_options,
 
882
                                             thr_lock_type lock_type,
 
883
                                             List<Index_hint> *index_hints_arg,
 
884
                                             LEX_STRING *option)
906
885
{
907
 
  TableList *ptr;
 
886
  register TableList *ptr;
908
887
  TableList *previous_table_ref; /* The table preceding the current one. */
909
888
  char *alias_str;
910
889
  LEX *lex= session->lex;
912
891
  if (!table)
913
892
    return NULL;                                // End of memory
914
893
  alias_str= alias ? alias->str : table->table.str;
915
 
  if (! table_options.test(TL_OPTION_ALIAS) &&
 
894
  if (!test(table_options & TL_OPTION_ALIAS) &&
916
895
      check_table_name(table->table.str, table->table.length))
917
896
  {
918
897
    my_error(ER_WRONG_TABLE_NAME, MYF(0), table->table.str);
923
902
  {
924
903
    my_casedn_str(files_charset_info, table->db.str);
925
904
 
926
 
    SchemaIdentifier schema_identifier(string(table->db.str));
927
 
    if (not check_db_name(session, schema_identifier))
 
905
    SchemaIdentifier schema_identifier(string(table->db.str, table->db.length));
 
906
    if (not check_db_name(schema_identifier))
928
907
    {
929
908
 
930
909
      my_error(ER_WRONG_DB_NAME, MYF(0), table->db.str);
945
924
  }
946
925
  if (!(ptr = (TableList *) session->calloc(sizeof(TableList))))
947
926
    return NULL;
948
 
 
949
927
  if (table->db.str)
950
928
  {
951
 
    ptr->setIsFqtn(true);
952
 
    ptr->setSchemaName(table->db.str);
 
929
    ptr->is_fqtn= true;
 
930
    ptr->db= table->db.str;
953
931
    ptr->db_length= table->db.length;
954
932
  }
955
 
  else if (lex->copy_db_to(ptr->getSchemaNamePtr(), &ptr->db_length))
 
933
  else if (lex->copy_db_to(&ptr->db, &ptr->db_length))
956
934
    return NULL;
957
935
  else
958
 
    ptr->setIsFqtn(false);
 
936
    ptr->is_fqtn= false;
959
937
 
960
938
  ptr->alias= alias_str;
961
 
  ptr->setIsAlias(alias ? true : false);
962
 
  ptr->setTableName(table->table.str);
 
939
  ptr->is_alias= alias ? true : false;
 
940
  if (table->table.length)
 
941
    table->table.length= my_casedn_str(files_charset_info, table->table.str);
 
942
  ptr->table_name=table->table.str;
963
943
  ptr->table_name_length=table->table.length;
964
944
  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);
 
945
  ptr->force_index= test(table_options & TL_OPTION_FORCE_INDEX);
 
946
  ptr->ignore_leaves= test(table_options & TL_OPTION_IGNORE_LEAVES);
967
947
  ptr->derived=     table->sel;
968
948
  ptr->select_lex=  lex->current_select;
969
949
  ptr->index_hints= index_hints_arg;
977
957
         tables=tables->next_local)
978
958
    {
979
959
      if (!my_strcasecmp(table_alias_charset, alias_str, tables->alias) &&
980
 
          !strcasecmp(ptr->getSchemaName(), tables->getSchemaName()))
 
960
          !strcasecmp(ptr->db, tables->db))
981
961
      {
982
962
        my_error(ER_NONUNIQ_TABLE, MYF(0), alias_str);
983
963
        return NULL;
1046
1026
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1047
1027
                                       sizeof(nested_join_st))))
1048
1028
    return true;
1049
 
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1050
 
  nested_join= ptr->getNestedJoin();
 
1029
  nested_join= ptr->nested_join=
 
1030
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1031
 
1051
1032
  join_list->push_front(ptr);
1052
 
  ptr->setEmbedding(embedding);
1053
 
  ptr->setJoinList(join_list);
 
1033
  ptr->embedding= embedding;
 
1034
  ptr->join_list= join_list;
1054
1035
  ptr->alias= (char*) "(nested_join)";
1055
1036
  embedding= ptr;
1056
1037
  join_list= &nested_join->join_list;
1080
1061
 
1081
1062
  assert(embedding);
1082
1063
  ptr= embedding;
1083
 
  join_list= ptr->getJoinList();
1084
 
  embedding= ptr->getEmbedding();
1085
 
  nested_join= ptr->getNestedJoin();
 
1064
  join_list= ptr->join_list;
 
1065
  embedding= ptr->embedding;
 
1066
  nested_join= ptr->nested_join;
1086
1067
  if (nested_join->join_list.elements == 1)
1087
1068
  {
1088
1069
    TableList *embedded= nested_join->join_list.head();
1089
1070
    join_list->pop();
1090
 
    embedded->setJoinList(join_list);
1091
 
    embedded->setEmbedding(embedding);
 
1071
    embedded->join_list= join_list;
 
1072
    embedded->embedding= embedding;
1092
1073
    join_list->push_front(embedded);
1093
1074
    ptr= embedded;
1094
1075
  }
1121
1102
  List<TableList> *embedded_list;
1122
1103
 
1123
1104
  if (!(ptr= (TableList*) session->calloc(ALIGN_SIZE(sizeof(TableList))+
1124
 
                                          sizeof(nested_join_st))))
 
1105
                                       sizeof(nested_join_st))))
1125
1106
    return NULL;
1126
 
  ptr->setNestedJoin(((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList)))));
1127
 
  nested_join= ptr->getNestedJoin();
1128
 
  ptr->setEmbedding(embedding);
1129
 
  ptr->setJoinList(join_list);
 
1107
  nested_join= ptr->nested_join=
 
1108
    ((nested_join_st*) ((unsigned char*) ptr + ALIGN_SIZE(sizeof(TableList))));
 
1109
 
 
1110
  ptr->embedding= embedding;
 
1111
  ptr->join_list= join_list;
1130
1112
  ptr->alias= (char*) "(nest_last_join)";
1131
1113
  embedded_list= &nested_join->join_list;
1132
1114
  embedded_list->empty();
1134
1116
  for (uint32_t i=0; i < 2; i++)
1135
1117
  {
1136
1118
    TableList *table= join_list->pop();
1137
 
    table->setJoinList(embedded_list);
1138
 
    table->setEmbedding(ptr);
 
1119
    table->join_list= embedded_list;
 
1120
    table->embedding= ptr;
1139
1121
    embedded_list->push_back(table);
1140
1122
    if (table->natural_join)
1141
1123
    {
1171
1153
void Select_Lex::add_joined_table(TableList *table)
1172
1154
{
1173
1155
  join_list->push_front(table);
1174
 
  table->setJoinList(join_list);
1175
 
  table->setEmbedding(embedding);
 
1156
  table->join_list= join_list;
 
1157
  table->embedding= embedding;
1176
1158
}
1177
1159
 
1178
1160
 
1415
1397
 
1416
1398
 
1417
1399
/**
 
1400
  kill on thread.
 
1401
 
 
1402
  @param session                        Thread class
 
1403
  @param id                     Thread id
 
1404
  @param only_kill_query        Should it kill the query or the connection
 
1405
 
 
1406
  @note
 
1407
    This is written such that we have a short lock on LOCK_thread_count
 
1408
*/
 
1409
 
 
1410
static unsigned int
 
1411
kill_one_thread(Session *, ulong id, bool only_kill_query)
 
1412
{
 
1413
  Session *tmp= NULL;
 
1414
  uint32_t error= ER_NO_SUCH_THREAD;
 
1415
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
 
1416
  
 
1417
  for (SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
 
1418
  {
 
1419
    if ((*it)->thread_id == id)
 
1420
    {
 
1421
      tmp= *it;
 
1422
      pthread_mutex_lock(&tmp->LOCK_delete);    // Lock from delete
 
1423
      break;
 
1424
    }
 
1425
  }
 
1426
  pthread_mutex_unlock(&LOCK_thread_count);
 
1427
  if (tmp)
 
1428
  {
 
1429
 
 
1430
    if (tmp->isViewable())
 
1431
    {
 
1432
      tmp->awake(only_kill_query ? Session::KILL_QUERY : Session::KILL_CONNECTION);
 
1433
      error= 0;
 
1434
    }
 
1435
 
 
1436
    pthread_mutex_unlock(&tmp->LOCK_delete);
 
1437
  }
 
1438
  return(error);
 
1439
}
 
1440
 
 
1441
 
 
1442
/*
 
1443
  kills a thread and sends response
 
1444
 
 
1445
  SYNOPSIS
 
1446
    sql_kill()
 
1447
    session                     Thread class
 
1448
    id                  Thread id
 
1449
    only_kill_query     Should it kill the query or the connection
 
1450
*/
 
1451
 
 
1452
void sql_kill(Session *session, ulong id, bool only_kill_query)
 
1453
{
 
1454
  uint32_t error;
 
1455
  if (!(error= kill_one_thread(session, id, only_kill_query)))
 
1456
    session->my_ok();
 
1457
  else
 
1458
    my_error(error, MYF(0), id);
 
1459
}
 
1460
 
 
1461
 
 
1462
/**
1418
1463
  Check if the select is a simple select (not an union).
1419
1464
 
1420
1465
  @retval
1542
1587
 
1543
1588
 
1544
1589
/**
 
1590
  CREATE TABLE query pre-check.
 
1591
 
 
1592
  @param session                        Thread handler
 
1593
  @param tables         Global table list
 
1594
  @param create_table           Table which will be created
 
1595
 
 
1596
  @retval
 
1597
    false   OK
 
1598
  @retval
 
1599
    true   Error
 
1600
*/
 
1601
 
 
1602
bool create_table_precheck(TableIdentifier &identifier)
 
1603
{
 
1604
  if (not plugin::StorageEngine::canCreateTable(identifier))
 
1605
  {
 
1606
    my_error(ER_DBACCESS_DENIED_ERROR, MYF(0), "", "", identifier.getSchemaName().c_str());
 
1607
    return true;
 
1608
  }
 
1609
 
 
1610
  if (not plugin::StorageEngine::doesSchemaExist(identifier))
 
1611
  {
 
1612
    my_error(ER_BAD_DB_ERROR, MYF(0), identifier.getSchemaName().c_str());
 
1613
    return true;
 
1614
  }
 
1615
 
 
1616
  return false;
 
1617
}
 
1618
 
 
1619
 
 
1620
/**
1545
1621
  negate given expression.
1546
1622
 
1547
1623
  @param session  thread handler
1666
1742
{
1667
1743
  assert(session->m_lip == NULL);
1668
1744
 
1669
 
  DRIZZLE_QUERY_PARSE_START(session->getQueryString()->c_str());
 
1745
  DRIZZLE_QUERY_PARSE_START(session->query.c_str());
1670
1746
 
1671
1747
  /* Set Lex_input_stream. */
1672
1748