You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafodion.apache.org by hz...@apache.org on 2018/04/25 16:27:11 UTC

[1/4] trafodion git commit: [TRAFODION-3042] RAND() function is not always random

Repository: trafodion
Updated Branches:
  refs/heads/master dd051ea60 -> 06d38d5d3


[TRAFODION-3042] RAND() function is not always random

When called without a seed, we use a seed based on the
system timestamp. The random generator we use generates similar
output values for seeds that are close together. Adding another
randomization step to avoid that.


Project: http://git-wip-us.apache.org/repos/asf/trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafodion/commit/36e21b2c
Tree: http://git-wip-us.apache.org/repos/asf/trafodion/tree/36e21b2c
Diff: http://git-wip-us.apache.org/repos/asf/trafodion/diff/36e21b2c

Branch: refs/heads/master
Commit: 36e21b2c9c722bbcb551c35a761f069811051ea0
Parents: 81fadbc
Author: Hans Zeller <hz...@apache.org>
Authored: Tue Apr 24 21:39:16 2018 +0000
Committer: Hans Zeller <hz...@apache.org>
Committed: Tue Apr 24 21:39:16 2018 +0000

----------------------------------------------------------------------
 core/sql/exp/exp_function.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafodion/blob/36e21b2c/core/sql/exp/exp_function.cpp
----------------------------------------------------------------------
diff --git a/core/sql/exp/exp_function.cpp b/core/sql/exp/exp_function.cpp
index a65c120..5ece196 100644
--- a/core/sql/exp/exp_function.cpp
+++ b/core/sql/exp/exp_function.cpp
@@ -5977,6 +5977,14 @@ void ExFunctionRandomNum::initSeed(char *op_data[])
 
       seed_ = x + fraction;
 
+      // Go through one step of a linear congruential random generator.
+      // (https://en.wikipedia.org/wiki/Linear_congruential_generator).
+      // This is to avoid seed values that are close to each other when
+      // we call this method again within a short time. The eval() method
+      // below doesn't handle seed values that are close to each other
+      // very well.
+      seed_ = (((Int64) seed_) * 1664525L + 1664525L) % 2147483648;
+
       if (seed_<0)
         seed_ += 2147483647;
       if ( seed_ < 1 ) seed_ = 1;


[3/4] trafodion git commit: [TRAFODION-3042] Addressed review comment

Posted by hz...@apache.org.
[TRAFODION-3042] Addressed review comment


Project: http://git-wip-us.apache.org/repos/asf/trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafodion/commit/80a0113f
Tree: http://git-wip-us.apache.org/repos/asf/trafodion/tree/80a0113f
Diff: http://git-wip-us.apache.org/repos/asf/trafodion/diff/80a0113f

Branch: refs/heads/master
Commit: 80a0113f4fd267a367d58a589f95dd33400cbd8a
Parents: 4f83472
Author: Hans Zeller <hz...@apache.org>
Authored: Tue Apr 24 23:22:26 2018 +0000
Committer: Hans Zeller <hz...@apache.org>
Committed: Tue Apr 24 23:22:26 2018 +0000

----------------------------------------------------------------------
 core/sql/exp/exp_function.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafodion/blob/80a0113f/core/sql/exp/exp_function.cpp
----------------------------------------------------------------------
diff --git a/core/sql/exp/exp_function.cpp b/core/sql/exp/exp_function.cpp
index 109e06d..70eb59c 100644
--- a/core/sql/exp/exp_function.cpp
+++ b/core/sql/exp/exp_function.cpp
@@ -5953,7 +5953,7 @@ void ExFunctionRandomNum::initSeed(char *op_data[])
       // we call this method again within a short time. The eval() method
       // below doesn't handle seed values that are close to each other
       // very well.
-      seed_ = (((Int64) seed_) * 1664525L + 1664525L) % 2147483648;
+      seed_ = (((Int64) seed_) * 1664525L + 1013904223L) % 2147483648;
 
       if (seed_<0)
         seed_ += 2147483647;


[2/4] trafodion git commit: [TRAFODION-3042] Switch to nanosecond-resolution time

Posted by hz...@apache.org.
[TRAFODION-3042] Switch to nanosecond-resolution time

Getting ready for the day where we can do two RAND() calls
in a microsecond - hopefully soon :-)


Project: http://git-wip-us.apache.org/repos/asf/trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafodion/commit/4f834729
Tree: http://git-wip-us.apache.org/repos/asf/trafodion/tree/4f834729
Diff: http://git-wip-us.apache.org/repos/asf/trafodion/diff/4f834729

Branch: refs/heads/master
Commit: 4f834729efa2c9ae74922f1f32226a29b6f8d7d0
Parents: 36e21b2
Author: Hans Zeller <hz...@apache.org>
Authored: Tue Apr 24 22:01:37 2018 +0000
Committer: Hans Zeller <hz...@apache.org>
Committed: Tue Apr 24 22:01:37 2018 +0000

----------------------------------------------------------------------
 core/sql/exp/exp_function.cpp | 42 ++++++--------------------------------
 1 file changed, 6 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafodion/blob/4f834729/core/sql/exp/exp_function.cpp
----------------------------------------------------------------------
diff --git a/core/sql/exp/exp_function.cpp b/core/sql/exp/exp_function.cpp
index 5ece196..109e06d 100644
--- a/core/sql/exp/exp_function.cpp
+++ b/core/sql/exp/exp_function.cpp
@@ -51,6 +51,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <uuid/uuid.h>
+#include <time.h>
 
 #include "NLSConversion.h"
 #include "nawstring.h"
@@ -5937,45 +5938,14 @@ void ExFunctionRandomNum::initSeed(char *op_data[])
 
       // Pick an initial seed.  According to the reference given below
       // (in the eval function), all initial seeds between 1 and
-      // 2147483646 are equally valid.  So, we just need to pick one
+      // 2147483647 are equally valid.  So, we just need to pick one
       // in this range.  Do this based on a timestamp.
+      struct timespec seedTime;
 
-      // Use ex_function_current to get timestamp.
-      //
-      char currBuff[32];
-      char *opData[1];
-      opData[0] = currBuff;
-      ex_function_current currentFun;
-      currentFun.eval(&opData[0], 0, 0);
+      clock_gettime(CLOCK_REALTIME, &seedTime);
 
-      // Extract year, month, etc.
-      //
-      char *p = currBuff;
-      short year = *((short*) p);
-      p += sizeof(short);
-      char month = *p++;
-      char day = *p++;
-      char hour = *p++;
-      char minute = *p++;
-      char second = *p++;
-      Lng32 fraction = *((Lng32*) p);
-
-      // Local variables year, ..., fraction are now initialized.
-      // From the values of these variables, generate a seed in the
-      // desired range.
-
-      Lng32 x = year * month * day;
-      if (hour) x *= hour;
-      p = (char*) &x;
-
-      assert(sizeof(Lng32)==4);
-
-      p[0] |= (second<<1);
-      p[1] |= (minute<<1);
-      p[2] |= (minute<<2);
-      p[3] |= second;
-
-      seed_ = x + fraction;
+      seed_  = (Int32) (seedTime.tv_sec  % 2147483648);
+      seed_ ^= (Int32) (seedTime.tv_nsec % 2147483648L);
 
       // Go through one step of a linear congruential random generator.
       // (https://en.wikipedia.org/wiki/Linear_congruential_generator).


[4/4] trafodion git commit: Merge [TRAFODION-3042] RAND() function is not always random PR-1539

Posted by hz...@apache.org.
Merge [TRAFODION-3042] RAND() function is not always random PR-1539


Project: http://git-wip-us.apache.org/repos/asf/trafodion/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafodion/commit/06d38d5d
Tree: http://git-wip-us.apache.org/repos/asf/trafodion/tree/06d38d5d
Diff: http://git-wip-us.apache.org/repos/asf/trafodion/diff/06d38d5d

Branch: refs/heads/master
Commit: 06d38d5d3d2eedf542b5fc4b231575c7c32a698d
Parents: dd051ea 80a0113
Author: Hans Zeller <hz...@apache.org>
Authored: Wed Apr 25 16:07:16 2018 +0000
Committer: Hans Zeller <hz...@apache.org>
Committed: Wed Apr 25 16:07:16 2018 +0000

----------------------------------------------------------------------
 core/sql/exp/exp_function.cpp | 50 +++++++++++---------------------------
 1 file changed, 14 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafodion/blob/06d38d5d/core/sql/exp/exp_function.cpp
----------------------------------------------------------------------