drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
rand_function.cc
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 * Copyright (C) 2010 Stewart Smith
6 *
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.
10 *
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.
15 *
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
19 */
20
21#include <config.h>
22#include <drizzled/plugin/function.h>
23#include <drizzled/item/func.h>
24#include <drizzled/function/math/real.h>
25#include <drizzled/session.h>
26
27using namespace std;
28using namespace drizzled;
29
30class RandFunction :public Item_real_func
31{
32 uint64_t seed1;
33 uint64_t seed2;
34 uint64_t max_value;
35 double max_value_dbl;
36 void _seed_random_int(uint64_t new_seed1, uint64_t new_seed2);
37
38public:
39 RandFunction() :Item_real_func() {}
40 double val_real();
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);
45
47 {
48 return (n == 0 || n == 1);
49 }
50
51private:
52 void seed_random (Item * val);
53};
54
55static uint32_t sql_rnd()
56{
57 return (uint32_t) (rand() * 0xffffffff); /* make all bits random */
58}
59
60
61void RandFunction::seed_random(Item *arg)
62{
63 /*
64 TODO: do not do reinit 'rand' for every execute of PS/SP if
65 args[0] is a constant.
66 */
67 uint64_t tmp= (uint64_t) arg->val_int();
68 _seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
69}
70
71void RandFunction::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
72{
73 max_value= 0x3FFFFFFFL;
74 max_value_dbl=(double) max_value;
75 seed1= new_seed1 % max_value;
76 seed2= new_seed2 % max_value;
77}
78
79bool RandFunction::fix_fields(Session *session,Item **ref)
80{
81 if (Item_real_func::fix_fields(session, ref))
82 return true;
83
84 used_tables_cache|= RAND_TABLE_BIT;
85 if (arg_count)
86 { // Only use argument once in query
87 /*
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.
90 */
91 if (args[0]->const_item())
92 seed_random(args[0]);
93 }
94 else
95 {
96 uint64_t tmp= sql_rnd();
97 _seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
98 }
99
100 return false;
101}
102
103void RandFunction::update_used_tables()
104{
105 Item_real_func::update_used_tables();
106 used_tables_cache|= RAND_TABLE_BIT;
107}
108
110{
111 assert(fixed == 1);
112 if (arg_count && !args[0]->const_item())
113 seed_random(args[0]);
114
115 seed1= (seed1 * 3 + seed2) % max_value;
116 seed2= (seed1 + seed2 + 33) % max_value;
117 return (((double) seed1) / max_value_dbl);
118}
119
120plugin::Create_function<RandFunction> *rand_function= NULL;
121
122static int initialize(module::Context &registry)
123{
124 rand_function= new plugin::Create_function<RandFunction>("rand");
125 registry.add(rand_function);
126 return 0;
127}
128
129DRIZZLE_DECLARE_PLUGIN
130{
131 DRIZZLE_VERSION_ID,
132 "rand",
133 "1.0",
134 "Stewart Smith",
135 N_("RAND function"),
136 PLUGIN_LICENSE_GPL,
137 initialize,
138 NULL,
139 NULL,
140}
141DRIZZLE_DECLARE_PLUGIN_END;
double val_real()
bool const_item() const
bool check_argument_count(int n)
virtual int64_t val_int()=0
bool fixed
Definition item.h:122
TODO: Rename this file - func.h is stupid.
STL namespace.