00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <config.h>
00022 #include <drizzled/plugin/function.h>
00023 #include <drizzled/item/func.h>
00024 #include <drizzled/function/math/real.h>
00025 #include <drizzled/session.h>
00026
00027 using namespace std;
00028 using namespace drizzled;
00029
00030 class RandFunction :public Item_real_func
00031 {
00032 uint64_t seed1;
00033 uint64_t seed2;
00034 uint64_t max_value;
00035 double max_value_dbl;
00036 void _seed_random_int(uint64_t new_seed1, uint64_t new_seed2);
00037
00038 public:
00039 RandFunction() :Item_real_func() {}
00040 double val_real();
00041 const char *func_name() const { return "rand"; }
00042 bool const_item() const { return 0; }
00043 void update_used_tables();
00044 bool fix_fields(Session *session, Item **ref);
00045
00046 bool check_argument_count(int n)
00047 {
00048 return (n == 0 || n == 1);
00049 }
00050
00051 private:
00052 void seed_random (Item * val);
00053 };
00054
00055 static uint32_t sql_rnd()
00056 {
00057 return (uint32_t) (rand() * 0xffffffff);
00058 }
00059
00060
00061 void RandFunction::seed_random(Item *arg)
00062 {
00063
00064
00065
00066
00067 uint64_t tmp= (uint64_t) arg->val_int();
00068 _seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
00069 }
00070
00071 void RandFunction::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
00072 {
00073 max_value= 0x3FFFFFFFL;
00074 max_value_dbl=(double) max_value;
00075 seed1= new_seed1 % max_value;
00076 seed2= new_seed2 % max_value;
00077 }
00078
00079 bool RandFunction::fix_fields(Session *session,Item **ref)
00080 {
00081 if (Item_real_func::fix_fields(session, ref))
00082 return true;
00083
00084 used_tables_cache|= RAND_TABLE_BIT;
00085 if (arg_count)
00086 {
00087
00088
00089
00090
00091 if (args[0]->const_item())
00092 seed_random(args[0]);
00093 }
00094 else
00095 {
00096 uint64_t tmp= sql_rnd();
00097 _seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
00098 }
00099
00100 return false;
00101 }
00102
00103 void RandFunction::update_used_tables()
00104 {
00105 Item_real_func::update_used_tables();
00106 used_tables_cache|= RAND_TABLE_BIT;
00107 }
00108
00109 double RandFunction::val_real()
00110 {
00111 assert(fixed == 1);
00112 if (arg_count && !args[0]->const_item())
00113 seed_random(args[0]);
00114
00115 seed1= (seed1 * 3 + seed2) % max_value;
00116 seed2= (seed1 + seed2 + 33) % max_value;
00117 return (((double) seed1) / max_value_dbl);
00118 }
00119
00120 plugin::Create_function<RandFunction> *rand_function= NULL;
00121
00122 static int initialize(module::Context ®istry)
00123 {
00124 rand_function= new plugin::Create_function<RandFunction>("rand");
00125 registry.add(rand_function);
00126 return 0;
00127 }
00128
00129 DRIZZLE_PLUGIN(initialize, NULL, NULL);