1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
5
* Copyright (C) 2010 Stewart Smith
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; version 2 of the License.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <drizzled/plugin/function.h>
23
#include <drizzled/item/func.h>
24
#include <drizzled/function/math/real.h>
25
#include <drizzled/session.h>
28
using namespace drizzled;
30
class RandFunction :public Item_real_func
36
void _seed_random_int(uint64_t new_seed1, uint64_t new_seed2);
39
RandFunction() :Item_real_func() {}
41
const char *func_name() const { return "rand"; }
42
bool const_item() const { return 0; }
43
void update_used_tables();
44
bool fix_fields(Session *session, Item **ref);
46
bool check_argument_count(int n)
48
return (n == 0 || n == 1);
52
void seed_random (Item * val);
55
static uint32_t sql_rnd()
57
return (uint32_t) (rand() * 0xffffffff); /* make all bits random */
61
void RandFunction::seed_random(Item *arg)
64
TODO: do not do reinit 'rand' for every execute of PS/SP if
65
args[0] is a constant.
67
uint64_t tmp= (uint64_t) arg->val_int();
68
_seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
71
void RandFunction::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
73
max_value= 0x3FFFFFFFL;
74
max_value_dbl=(double) max_value;
75
seed1= new_seed1 % max_value;
76
seed2= new_seed2 % max_value;
79
bool RandFunction::fix_fields(Session *session,Item **ref)
81
if (Item_real_func::fix_fields(session, ref))
84
used_tables_cache|= RAND_TABLE_BIT;
86
{ // Only use argument once in query
88
No need to send a Rand log event if seed was given eg: RAND(seed),
89
as it will be replicated in the query as such.
91
if (args[0]->const_item())
96
uint64_t tmp= sql_rnd();
97
_seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
103
void RandFunction::update_used_tables()
105
Item_real_func::update_used_tables();
106
used_tables_cache|= RAND_TABLE_BIT;
109
double RandFunction::val_real()
112
if (arg_count && !args[0]->const_item())
113
seed_random(args[0]);
115
seed1= (seed1 * 3 + seed2) % max_value;
116
seed2= (seed1 + seed2 + 33) % max_value;
117
return (((double) seed1) / max_value_dbl);
120
plugin::Create_function<RandFunction> *rand_function= NULL;
122
static int initialize(module::Context ®istry)
124
rand_function= new plugin::Create_function<RandFunction>("rand");
125
registry.add(rand_function);
129
DRIZZLE_PLUGIN(initialize, NULL, NULL);