Drizzled Public API Documentation

rand_function.cc

00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2008 Sun Microsystems, Inc.
00005  *  Copyright (C) 2010 Stewart Smith
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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); /* make all bits random */
00058 }
00059 
00060 
00061 void RandFunction::seed_random(Item *arg)
00062 {
00063   /*
00064     TODO: do not do reinit 'rand' for every execute of PS/SP if
00065     args[0] is a constant.
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   {                                     // Only use argument once in query
00087     /*
00088       No need to send a Rand log event if seed was given eg: RAND(seed),
00089       as it will be replicated in the query as such.
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 &registry)
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);