diff --git a/mysql-test/r/derived.result b/mysql-test/r/derived.result index 0290d0755d5..204ca86f306 100644 --- a/mysql-test/r/derived.result +++ b/mysql-test/r/derived.result @@ -40,3 +40,28 @@ a select 1 from (select 1); 1 1 +drop table if exists t1; +create table t1(a int not null, t char(8), index(a)); +SELECT * FROM (SELECT * FROM t1) ORDER BY a ASC LIMIT 0,20; +a t +1 1 +2 2 +3 3 +4 4 +5 5 +6 6 +7 7 +8 8 +9 9 +10 10 +11 11 +12 12 +13 13 +14 14 +15 15 +16 16 +17 17 +18 18 +19 19 +20 20 +drop table if exists t1; diff --git a/mysql-test/r/select_found.result b/mysql-test/r/select_found.result index 1c2cd7da894..6fb9ea0ee0b 100644 --- a/mysql-test/r/select_found.result +++ b/mysql-test/r/select_found.result @@ -168,4 +168,14 @@ test2 2 2 SELECT FOUND_ROWS(); FOUND_ROWS() 2 +SELECT SQL_CALC_FOUND_ROWS 1 FROM (SELECT 1) LIMIT 0; +1 +SELECT FOUND_ROWS(); +FOUND_ROWS() +1 +SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 0; +titre numeropost maxnumrep +SELECT FOUND_ROWS(); +FOUND_ROWS() +3 drop table t1; diff --git a/mysql-test/t/derived.test b/mysql-test/t/derived.test index 501d4db26fa..de765a0e280 100644 --- a/mysql-test/t/derived.test +++ b/mysql-test/t/derived.test @@ -22,3 +22,15 @@ drop table if exists t1.t2,t3; select * from (select 1); select a from (select 1 as a); select 1 from (select 1); +drop table if exists t1; +create table t1(a int not null, t char(8), index(a)); +disable_query_log; +let $1 = 10000; +while ($1) + { + eval insert into t1 values ($1,'$1'); + dec $1; + } +enable_query_log; +SELECT * FROM (SELECT * FROM t1) ORDER BY a ASC LIMIT 0,20; +drop table if exists t1; \ No newline at end of file diff --git a/mysql-test/t/select_found.test b/mysql-test/t/select_found.test index 0a483c860cb..c67c99924c3 100644 --- a/mysql-test/t/select_found.test +++ b/mysql-test/t/select_found.test @@ -84,4 +84,8 @@ INSERT INTO t1 (titre,maxnumrep) VALUES ('test1','1'),('test2','2'),('test3','3'); SELECT SQL_CALC_FOUND_ROWS titre,numeropost,maxnumrep FROM t1 WHERE numeropost IN (1,2) ORDER BY maxnumrep DESC LIMIT 0, 1; SELECT FOUND_ROWS(); +SELECT SQL_CALC_FOUND_ROWS 1 FROM (SELECT 1) LIMIT 0; +SELECT FOUND_ROWS(); +SELECT SQL_CALC_FOUND_ROWS * FROM t1 LIMIT 0; +SELECT FOUND_ROWS(); drop table t1; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 64945fa2d4d..2d869fdda49 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -35,13 +35,13 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, ORDER *order, SQL_SELECT *select=0; READ_RECORD info; bool using_limit=limit != HA_POS_ERROR; - bool using_transactions; + bool using_transactions, safe_update; ha_rows deleted; DBUG_ENTER("mysql_delete"); if (!table_list->db) table_list->db=thd->db; - if ((thd->options & OPTION_SAFE_UPDATES) && !conds) + if (((safe_update=thd->options & OPTION_SAFE_UPDATES)) && !conds) { send_error(thd,ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE); DBUG_RETURN(1); @@ -58,7 +58,7 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, ORDER *order, /* Test if the user wants to delete all rows */ if (!using_limit && (!conds || conds->const_item()) && - !(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE))) + !(specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE)) && !safe_update) { deleted= table->file->records; if (!(error=table->file->delete_all_rows())) @@ -79,9 +79,7 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, ORDER *order, select=make_select(table,0,0,conds,&error); if (error) DBUG_RETURN(-1); - if ((select && select->check_quick(test(thd->options & OPTION_SAFE_UPDATES), - limit)) || - !limit) + if ((select && select->check_quick(safe_update, limit)) || !limit) { delete select; send_ok(thd,0L); @@ -92,7 +90,7 @@ int mysql_delete(THD *thd, TABLE_LIST *table_list, COND *conds, ORDER *order, if (!table->quick_keys) { thd->lex.select_lex.options|=QUERY_NO_INDEX_USED; - if ((thd->options & OPTION_SAFE_UPDATES) && limit == HA_POS_ERROR) + if (safe_update && !using_limit) { delete select; send_error(thd,ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE); diff --git a/sql/sql_derived.cc b/sql/sql_derived.cc index 56636f299a7..b3d2b1602d0 100644 --- a/sql/sql_derived.cc +++ b/sql/sql_derived.cc @@ -119,7 +119,7 @@ int mysql_derived(THD *thd, LEX *lex, SELECT_LEX_UNIT *unit, TABLE_LIST *t) table->tmp_table=TMP_TABLE; if (!lex->describe) sl->exclude(); - t->db=""; + t->db=(char *)""; t->derived=(SELECT_LEX *)0; // just in case ... } } diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 7861d6c5541..9f9e2c67c2d 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -378,7 +378,7 @@ JOIN::optimize() error = 0; DBUG_RETURN(1); } - if (cond_value == Item::COND_FALSE || !unit->select_limit_cnt) + if (cond_value == Item::COND_FALSE || (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS))) { /* Impossible cond */ zero_result_cause= "Impossible WHERE"; DBUG_RETURN(0); @@ -664,10 +664,13 @@ JOIN::exec() result->send_fields(fields_list,1); if (!having || having->val_int()) { - if (do_send_rows && result->send_data(fields_list)) + if (do_send_rows && unit->select_limit_cnt && result->send_data(fields_list)) error= 1; else + { error= (int) result->send_eof(); + send_records=1; + } } else error=(int) result->send_eof(); @@ -5181,7 +5184,7 @@ end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), error=0; if (join->procedure) error=join->procedure->send_row(*join->fields); - else if (join->do_send_rows) + else if (join->do_send_rows && join->unit->select_limit_cnt) error=join->result->send_data(*join->fields); if (error) DBUG_RETURN(-1); /* purecov: inspected */ diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 5187351258e..abb4cf8d3f9 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -523,19 +523,19 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, key_iterator.rewind(); key_number=0; - for (; (key=key_iterator++) ; key_info++, key_number++) + for (; (key=key_iterator++) ; key_number++) { uint key_length=0; key_part_spec *column; switch(key->type){ - case Key::MULTIPLE: + case Key::MULTIPLE: key_info->flags = 0; break; - case Key::FULLTEXT: + case Key::FULLTEXT: key_info->flags = HA_FULLTEXT; break; - case Key::SPATIAL: + case Key::SPATIAL: key_info->flags = HA_SPATIAL; break; case Key::FOREIGN_KEY: @@ -734,6 +734,7 @@ int mysql_create_table(THD *thd,const char *db, const char *table_name, my_error(ER_TOO_LONG_KEY,MYF(0),max_key_length); DBUG_RETURN(-1); } + key_info++; } if (!unique_key && !primary_key && (file->table_flags() & HA_REQUIRE_PRIMARY_KEY)) diff --git a/sql/sql_union.cc b/sql/sql_union.cc index 0e6de306c0d..aeaae92f0c3 100644 --- a/sql/sql_union.cc +++ b/sql/sql_union.cc @@ -86,6 +86,7 @@ bool select_union::send_data(List &values) } else return 1; + thd->net.report_error=0; // donno why does it work, but it does ... } return 0; }