~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/rand_function/rand_function.cc

  • Committer: Stewart Smith
  • Date: 2010-03-18 12:01:34 UTC
  • mto: (1666.2.3 build)
  • mto: This revision was merged to the branch mainline in revision 1596.
  • Revision ID: stewart@flamingspork.com-20100318120134-45fdnsw8g3j6c7oy
move RAND() into a plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
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
 
 
27
using namespace std;
 
28
using namespace drizzled;
 
29
 
 
30
class 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
 
 
38
public:
 
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
 
 
46
  bool check_argument_count(int n)
 
47
  {
 
48
    return (n == 0 || n == 1);
 
49
  }
 
50
 
 
51
private:
 
52
  void seed_random (Item * val);
 
53
};
 
54
 
 
55
static uint32_t sql_rnd()
 
56
{
 
57
  return (uint32_t) (rand() * 0xffffffff); /* make all bits random */
 
58
}
 
59
 
 
60
 
 
61
void 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
 
 
71
void 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
 
 
79
bool 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
 
 
103
void RandFunction::update_used_tables()
 
104
{
 
105
  Item_real_func::update_used_tables();
 
106
  used_tables_cache|= RAND_TABLE_BIT;
 
107
}
 
108
 
 
109
double RandFunction::val_real()
 
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
 
 
120
plugin::Create_function<RandFunction> *rand_function= NULL;
 
121
 
 
122
static int initialize(plugin::Registry &registry)
 
123
{
 
124
  rand_function= new plugin::Create_function<RandFunction>("rand");
 
125
  registry.add(rand_function);
 
126
  return 0;
 
127
}
 
128
 
 
129
static int finalize(plugin::Registry &registry)
 
130
{
 
131
  registry.remove(rand_function);
 
132
  delete rand_function;
 
133
  return 0;
 
134
}
 
135
 
 
136
DRIZZLE_PLUGIN(initialize, finalize, NULL);