Commit 417dd728 authored by monty@donna.mysql.fi's avatar monty@donna.mysql.fi

Added SQL_CALC_FOUND_ROWS and FOUND_ROWS()

parent 8f62579c
monty@work.mysql.com
monty@donna.mysql.fi
......@@ -90,8 +90,9 @@ static double _nwghts[11]={
-3.796875000000000};
static double *nwghts=_nwghts+5; // nwghts[i] = -0.5*1.5**i
int do_boolean(ALL_IN_ONE *aio, uint nested,
int yesno, int plusminus, bool pmsign)
int do_boolean(ALL_IN_ONE *aio, uint nested __attribute__((unused)),
int yesno __attribute__((unused)),
int plusminus, bool pmsign)
{
int r, res;
uint keylen, wno;
......@@ -106,7 +107,7 @@ int do_boolean(ALL_IN_ONE *aio, uint nested,
param.prev=' ';
for(wno=1; res=ft_get_word(&aio->start,aio->end,&w,&param); wno++)
for(wno=1; (res=ft_get_word(&aio->start,aio->end,&w,&param)); wno++)
{
r=plusminus+param.plusminus;
if (param.pmsign^pmsign)
......
......@@ -161,7 +161,7 @@ byte ft_get_word(byte **start, byte *end, FT_WORD *word, FTB_PARAM *param)
param->prev='A'; // be sure *prev is true_word_char
word->len= (uint)(doc-word->pos) - mwc;
if (param->trunc=(doc<end && *doc == FTB_TRUNC))
if ((param->trunc=(doc<end && *doc == FTB_TRUNC)))
doc++;
if (word->len >= MIN_WORD_LEN && word->len < MAX_WORD_LEN &&
......@@ -221,7 +221,6 @@ int is_boolean(byte *q, uint len)
TREE * ft_parse(TREE *wtree, byte *doc, int doclen)
{
byte *end=doc+doclen;
int res;
FT_WORD w;
if (!is_tree_inited(wtree))
......@@ -229,7 +228,7 @@ TREE * ft_parse(TREE *wtree, byte *doc, int doclen)
init_tree(wtree,0,sizeof(FT_WORD),(qsort_cmp)&FT_WORD_cmp,0,NULL);
}
while (res=ft_simple_get_word(&doc,end,&w))
while (ft_simple_get_word(&doc,end,&w))
{
if (!tree_insert(wtree, &w, 0))
goto err;
......
......@@ -160,7 +160,7 @@ int _mi_ft_update(MI_INFO *info, uint keynr, byte *keybuf,
int error= -1;
FT_WORD *oldlist,*newlist, *old_word, *new_word;
uint key_length;
uint cmp;
int cmp;
if (!(old_word=oldlist=_mi_ft_parserecord(info, keynr, keybuf, oldrec)))
goto err0;
......@@ -187,7 +187,7 @@ int _mi_ft_update(MI_INFO *info, uint keynr, byte *keybuf,
switch (cmp) {
case -1:
key_length=_ft_make_key(info,keynr,keybuf,old_word,pos);
if (error=_mi_ck_delete(info,keynr,(uchar*) keybuf,key_length))
if ((error=_mi_ck_delete(info,keynr,(uchar*) keybuf,key_length)))
goto err2;
old_word++;
break;
......@@ -197,7 +197,7 @@ int _mi_ft_update(MI_INFO *info, uint keynr, byte *keybuf,
break;
case 1:
key_length=_ft_make_key(info,keynr,keybuf,new_word,pos);
if (error=_mi_ck_write(info,keynr,(uchar*) keybuf,key_length))
if ((error=_mi_ck_write(info,keynr,(uchar*) keybuf,key_length)))
goto err2;
new_word++;
break;
......
......@@ -116,9 +116,11 @@ int _create_index_by_sort(MI_SORT_PARAM *info,my_bool no_messages,
}
while ((maxbuffer= (int) (records/(keys-1)+1)) != skr);
if (sort_keys=(uchar **)my_malloc(keys*(sort_length+sizeof(char*))+HA_FT_MAXLEN, MYF(0)))
if ((sort_keys=(uchar **)my_malloc(keys*(sort_length+sizeof(char*))+
HA_FT_MAXLEN, MYF(0))))
{
if (init_dynamic_array(&buffpek, sizeof(BUFFPEK), maxbuffer, maxbuffer/2))
if (init_dynamic_array(&buffpek, sizeof(BUFFPEK), maxbuffer,
maxbuffer/2))
my_free((gptr) sort_keys,MYF(0));
else
break;
......
......@@ -129,6 +129,11 @@ Item *create_func_floor(Item* a)
return new Item_func_floor(a);
}
Item *create_func_found_rows(void)
{
return new Item_int("FOUND_ROWS()",(longlong) current_thd->found_rows(),21);
}
Item *create_func_from_days(Item* a)
{
return new Item_func_from_days(a);
......
......@@ -37,6 +37,7 @@ Item *create_func_degrees(Item *);
Item *create_func_exp(Item* a);
Item *create_func_find_in_set(Item* a, Item *b);
Item *create_func_floor(Item* a);
Item *create_func_found_rows(void);
Item *create_func_from_days(Item* a);
Item *create_func_get_lock(Item* a, Item *b);
Item *create_func_hex(Item *a);
......
......@@ -287,6 +287,7 @@ static SYMBOL symbols[] = {
{ "SQL_BIG_SELECTS", SYM(SQL_BIG_SELECTS),0,0},
{ "SQL_BIG_TABLES", SYM(SQL_BIG_TABLES),0,0},
{ "SQL_BUFFER_RESULT", SYM(SQL_BUFFER_RESULT),0,0},
{ "SQL_CALC_FOUND_ROWS", SYM(SQL_CALC_FOUND_ROWS),0,0},
{ "SQL_LOG_BIN", SYM(SQL_LOG_BIN),0,0},
{ "SQL_LOG_OFF", SYM(SQL_LOG_OFF),0,0},
{ "SQL_LOG_UPDATE", SYM(SQL_LOG_UPDATE),0,0},
......@@ -392,6 +393,7 @@ static SYMBOL sql_functions[] = {
{ "FIND_IN_SET", SYM(FUNC_ARG2),0,CREATE_FUNC(create_func_find_in_set)},
{ "FLOOR", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_floor)},
{ "FORMAT", SYM(FORMAT_SYM),0,0},
{ "FOUND_ROWS", SYM(FUNC_ARG0),0,CREATE_FUNC(create_func_found_rows)},
{ "FROM_DAYS", SYM(FUNC_ARG1),0,CREATE_FUNC(create_func_from_days)},
{ "FROM_UNIXTIME", SYM(FROM_UNIXTIME),0,0},
{ "GET_LOCK", SYM(FUNC_ARG2),0,CREATE_FUNC(create_func_get_lock)},
......
......@@ -145,9 +145,9 @@ void kill_one_thread(THD *thd, ulong id);
#define SELECT_DESCRIBE 4
#define SELECT_SMALL_RESULT 8
#define SELECT_BIG_RESULT 16
#define OPTION_FOUND_ROWS 32
#define SELECT_HIGH_PRIORITY 64 /* Intern */
#define SELECT_USE_CACHE 256 /* Intern */
#define SELECT_COUNT_DISTINCT 512 /* Intern */
#define OPTION_BIG_TABLES 512 /* for SQL OPTION */
#define OPTION_BIG_SELECTS 1024 /* for SQL OPTION */
......
......@@ -263,7 +263,7 @@ public:
Vio* active_vio;
pthread_mutex_t active_vio_lock;
#endif
ulonglong next_insert_id,last_insert_id,current_insert_id;
ulonglong next_insert_id,last_insert_id,current_insert_id, limit_found_rows;
ha_rows select_limit,offset_limit,default_select_limit,cuted_fields,
max_join_size,sent_row_count;
table_map used_tables;
......@@ -353,6 +353,10 @@ public:
}
return last_insert_id;
}
inline ulonglong found_rows(void)
{
return limit_found_rows;
}
inline bool active_transaction()
{
#ifdef USING_TRANSACTIONS
......
......@@ -1092,6 +1092,8 @@ mysql_execute_command(void)
thd->select_limit=lex->select_limit+lex->offset_limit;
if (thd->select_limit < lex->select_limit)
thd->select_limit= HA_POS_ERROR; // no limit
if (thd->select_limit == HA_POS_ERROR)
lex->options&= ~OPTION_FOUND_ROWS;
if (lex->exchange)
{
......
......@@ -272,6 +272,7 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds,
count_field_types(&join.tmp_table_param,all_fields,0);
join.const_tables=0;
join.having=0;
join.do_send_rows = 1;
join.group= group != 0;
#ifdef RESTRICTED_GROUP
......@@ -354,7 +355,7 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds,
result->send_fields(fields,1);
if (!having || having->val_int())
{
if (result->send_data(fields))
if (join.do_send_rows && result->send_data(fields))
{
result->send_error(0,NullS); /* purecov: inspected */
error=1;
......@@ -429,7 +430,8 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds,
select_distinct=0;
}
else if (select_distinct && join.tables - join.const_tables == 1 &&
(order || thd->select_limit == HA_POS_ERROR))
(order || thd->select_limit == HA_POS_ERROR ||
(join.select_options & OPTION_FOUND_ROWS)))
{
if ((group=create_distinct_group(order,fields)))
{
......@@ -528,7 +530,8 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds,
(join.const_tables == join.tables ||
test_if_skip_sort_order(&join.join_tab[join.const_tables], order,
(having || group ||
join.const_tables != join.tables - 1) ?
join.const_tables != join.tables - 1 ||
(join.select_options & OPTION_FOUND_ROWS)) ?
HA_POS_ERROR : thd->select_limit)))
order=0;
select_describe(&join,need_tmp,
......@@ -565,7 +568,8 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds,
group : (ORDER*) 0),
group ? 0 : select_distinct,
group && simple_group,
order == 0,
order == 0 &&
!(join.select_options & OPTION_FOUND_ROWS),
join.select_options)))
goto err; /* purecov: inspected */
......@@ -776,7 +780,8 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds,
if (create_sort_index(&join.join_tab[join.const_tables],
group ? group : order,
(having || group ||
join.const_tables != join.tables - 1) ?
join.const_tables != join.tables - 1 ||
(join.select_options & OPTION_FOUND_ROWS)) ?
HA_POS_ERROR : thd->select_limit))
goto err; /* purecov: inspected */
}
......@@ -785,6 +790,7 @@ mysql_select(THD *thd,TABLE_LIST *tables,List<Item> &fields,COND *conds,
error=do_select(&join,&fields,NULL,procedure);
err:
thd->limit_found_rows = join.send_records;
thd->proc_info="end";
join.lock=0; // It's faster to unlock later
join_free(&join);
......@@ -2207,6 +2213,7 @@ make_simple_join(JOIN *join,TABLE *tmp_table)
join->sum_funcs=0;
join->send_records=(ha_rows) 0;
join->group=0;
join->do_send_rows = 1;
join_tab->cache.buff=0; /* No cacheing */
join_tab->table=tmp_table;
......@@ -2307,15 +2314,19 @@ make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
*/
if ((tab->keys & ~ tab->const_keys && i > 0) ||
tab->const_keys && i == join->const_tables &&
join->thd->select_limit < join->best_positions[i].records_read)
(tab->const_keys && i == join->const_tables &&
join->thd->select_limit < join->best_positions[i].records_read &&
!(join->select_options & OPTION_FOUND_ROWS)))
{
/* Join with outer join condition */
COND *orig_cond=sel->cond;
sel->cond=and_conds(sel->cond,tab->on_expr);
if (sel->test_quick_select(tab->keys,
used_tables & ~ current_map,
join->thd->select_limit) < 0)
(join->select_options &
OPTION_FOUND_ROWS ?
HA_POS_ERROR :
join->thd->select_limit)) < 0)
DBUG_RETURN(1); // Impossible range
sel->cond=orig_cond;
}
......@@ -4594,15 +4605,24 @@ end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
int error;
if (join->having && join->having->val_int() == 0)
DBUG_RETURN(0); // Didn't match having
error=0;
if (join->procedure)
error=join->procedure->send_row(*join->fields);
else
else if (join->do_send_rows)
error=join->result->send_data(*join->fields);
if (error)
DBUG_RETURN(-1); /* purecov: inspected */
if (++join->send_records >= join->thd->select_limit)
if (++join->send_records >= join->thd->select_limit && join->do_send_rows)
{
if (join->select_options & OPTION_FOUND_ROWS)
{
join->do_send_rows=0;
join->thd->select_limit = HA_POS_ERROR;
DBUG_RETURN(0);
}
DBUG_RETURN(-3); // Abort nicely
}
}
else
{
if (join->procedure && join->procedure->end_of_records())
......@@ -4632,9 +4652,10 @@ end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
int error;
if (join->procedure)
{
error=0;
if (join->having && join->having->val_int() == 0)
error= -1; // Didn't satisfy having
else
else if (join->do_send_rows)
error=join->procedure->send_row(*join->fields) ? 1 : 0;
if (end_of_records && join->procedure->end_of_records())
error= 1; // Fatal error
......@@ -4652,8 +4673,14 @@ end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
DBUG_RETURN(-1); /* purecov: inspected */
if (end_of_records)
DBUG_RETURN(0);
if (!error && ++join->send_records >= join->thd->select_limit)
DBUG_RETURN(-3); /* Abort nicely */
if (!error && ++join->send_records >= join->thd->select_limit &&
join->do_send_rows)
{
if (!(join->select_options & OPTION_FOUND_ROWS))
DBUG_RETURN(-3); // Abort nicely
join->do_send_rows=0;
join->thd->select_limit = HA_POS_ERROR;
}
}
}
else
......@@ -4724,8 +4751,15 @@ end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
if (create_myisam_from_heap(table, &join->tmp_table_param, error,1))
DBUG_RETURN(1); // Not a table_is_full error
table->uniques=0; // To ensure rows are the same
if (++join->send_records >= join->tmp_table_param.end_write_records)
if (++join->send_records >= join->tmp_table_param.end_write_records &
join->do_send_rows)
{
if (!(join->select_options & OPTION_FOUND_ROWS))
DBUG_RETURN(-3);
join->do_send_rows=0;
join->thd->select_limit = HA_POS_ERROR;
DBUG_RETURN(0);
}
}
}
}
......
......@@ -150,6 +150,7 @@ class JOIN {
uint tables,const_tables;
uint send_group_parts;
bool sort_and_group,first_record,full_join,group, no_field_update;
bool do_send_rows;
table_map const_table_map,outer_join;
ha_rows send_records,found_records;
POSITION positions[MAX_TABLES+1],best_positions[MAX_TABLES+1];
......
......@@ -431,6 +431,7 @@ bool my_yyoverflow(short **a, YYSTYPE **b,int *yystacksize);
%token SQL_BUFFER_RESULT
%token SQL_WARNINGS
%token SQL_AUTO_IS_NULL
%token SQL_CALC_FOUND_ROWS
%token SQL_SAFE_UPDATES
%token SQL_QUOTE_SHOW_CREATE
%token SQL_SLAVE_SKIP_COUNTER
......@@ -1274,6 +1275,7 @@ select_option:
| SQL_SMALL_RESULT { Lex->options|= SELECT_SMALL_RESULT; }
| SQL_BIG_RESULT { Lex->options|= SELECT_BIG_RESULT; }
| SQL_BUFFER_RESULT { Lex->options|= OPTION_BUFFER_RESULT; }
| SQL_CALC_FOUND_ROWS { Lex->options|= OPTION_FOUND_ROWS; }
| ALL {}
select_lock_type:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment