You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu> on 2021/06/03 15:36:37 UTC

Change in asterixdb[mad-hatter]: [ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)

From Ali Alsuliman <al...@gmail.com>:

Ali Alsuliman has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11763 )


Change subject: [ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)
......................................................................

[ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
Use pseudo-random for random(seed)

Change-Id: I14e81b24933744b136f85bd1218987401830fad9
---
A asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
A asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
M asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
M asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
4 files changed, 53 insertions(+), 25 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/63/11763/1

diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
new file mode 100644
index 0000000..8b0aa23
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+FROM range(1, 3) AS r
+SELECT random(int8("12")) AS i8, random(int16("12")) AS i16, random(int32("12")) AS i32, random(int64("12")) AS i64,
+random(float("12")) AS float, random(double("12")) AS double;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
new file mode 100644
index 0000000..5b9e73c4
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
@@ -0,0 +1,3 @@
+{ "i8": 0.26795443606823943, "i16": 0.26795443606823943, "i32": 0.26795443606823943, "i64": 0.26795443606823943, "float": 0.26795443606823943, "double": 0.26795443606823943 }
+{ "i8": 0.4533526797678967, "i16": 0.4533526797678967, "i32": 0.4533526797678967, "i64": 0.4533526797678967, "float": 0.4533526797678967, "double": 0.4533526797678967 }
+{ "i8": 0.38508513586474447, "i16": 0.38508513586474447, "i32": 0.38508513586474447, "i64": 0.38508513586474447, "float": 0.38508513586474447, "double": 0.38508513586474447 }
\ No newline at end of file
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
index aa49010..bd74f24 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
@@ -24,6 +24,7 @@
 import org.apache.asterix.om.functions.IFunctionDescriptor;
 import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.om.types.hierachy.ATypeHierarchy;
 import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.utils.RandomHelper;
 import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
@@ -78,7 +79,9 @@
                             case BIGINT:
                             case FLOAT:
                             case DOUBLE:
-                                randomHelper.setSeed(bytes, offset + 1, arg0.getLength() - 1);
+                                double seed =
+                                        ATypeHierarchy.getDoubleValue(getIdentifier().getName(), 0, bytes, offset);
+                                randomHelper.setSeed(seed);
                                 randomHelper.nextDouble(resultPointable);
                                 break;
                             default:
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
index 7ed1ce5..db5b774 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
@@ -20,9 +20,11 @@
 package org.apache.asterix.runtime.evaluators.functions.utils;
 
 import java.io.DataOutput;
-import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
 import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider;
 import org.apache.asterix.om.base.AMutableDouble;
 import org.apache.asterix.om.types.BuiltinType;
@@ -30,44 +32,42 @@
 import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.api.IPointable;
 import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
-import org.apache.hyracks.data.std.util.DataUtils;
-import org.apache.hyracks.data.std.util.GrowableArray;
 
 public final class RandomHelper {
 
-    private final SecureRandom random = new SecureRandom();
+    private final SecureRandom random;
 
-    private final GrowableArray seed;
+    private double seed;
+
+    private boolean isFirst;
 
     private final AMutableDouble aDouble = new AMutableDouble(0);
 
     @SuppressWarnings("rawtypes")
-    private ISerializerDeserializer doubleSerde =
+    private final ISerializerDeserializer doubleSerde =
             SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
 
     private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
     private final DataOutput dataOutput = resultStorage.getDataOutput();
 
-    public RandomHelper(boolean withSeed) {
-        seed = withSeed ? new GrowableArray(8) : null;
+    public RandomHelper(boolean withSeed) throws HyracksDataException {
+        if (withSeed) {
+            try {
+                random = SecureRandom.getInstance("SHA1PRNG");
+            } catch (NoSuchAlgorithmException e) {
+                throw new IllegalStateException("random()");
+            }
+        } else {
+            random = new SecureRandom();
+        }
+        isFirst = true;
     }
 
-    public void setSeed(byte[] bytes, int offset, int length) throws HyracksDataException {
-        if (seed == null) {
-            throw new IllegalStateException();
-        }
-
-        boolean sameSeed =
-                seed.getLength() == length && DataUtils.equalsInRange(seed.getByteArray(), 0, bytes, offset, length);
-
-        if (!sameSeed) {
-            try {
-                seed.reset();
-                seed.append(bytes, offset, length);
-                random.setSeed(seed.getByteArray());
-            } catch (IOException e) {
-                throw HyracksDataException.create(e);
-            }
+    public void setSeed(double seedVal) throws HyracksDataException {
+        if (isFirst || seedVal != seed) {
+            seed = seedVal;
+            isFirst = false;
+            random.setSeed(Double.doubleToLongBits(seedVal));
         }
     }
 

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11763
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: mad-hatter
Gerrit-Change-Id: I14e81b24933744b136f85bd1218987401830fad9
Gerrit-Change-Number: 11763
Gerrit-PatchSet: 1
Gerrit-Owner: Ali Alsuliman <al...@gmail.com>
Gerrit-MessageType: newchange

Change in asterixdb[mad-hatter]: [ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)

Posted by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu>.
From Ali Alsuliman <al...@gmail.com>:

Ali Alsuliman has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11763 )


Change subject: [ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)
......................................................................

[ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
Use pseudo-random for random(seed)

Change-Id: I14e81b24933744b136f85bd1218987401830fad9
---
A asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
A asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
M asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
M asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
4 files changed, 53 insertions(+), 25 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/63/11763/1

diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
new file mode 100644
index 0000000..8b0aa23
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/queries_sqlpp/misc/random/random.3.query.sqlpp
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+FROM range(1, 3) AS r
+SELECT random(int8("12")) AS i8, random(int16("12")) AS i16, random(int32("12")) AS i32, random(int64("12")) AS i64,
+random(float("12")) AS float, random(double("12")) AS double;
diff --git a/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
new file mode 100644
index 0000000..5b9e73c4
--- /dev/null
+++ b/asterixdb/asterix-app/src/test/resources/runtimets/results/misc/random/random.3.adm
@@ -0,0 +1,3 @@
+{ "i8": 0.26795443606823943, "i16": 0.26795443606823943, "i32": 0.26795443606823943, "i64": 0.26795443606823943, "float": 0.26795443606823943, "double": 0.26795443606823943 }
+{ "i8": 0.4533526797678967, "i16": 0.4533526797678967, "i32": 0.4533526797678967, "i64": 0.4533526797678967, "float": 0.4533526797678967, "double": 0.4533526797678967 }
+{ "i8": 0.38508513586474447, "i16": 0.38508513586474447, "i32": 0.38508513586474447, "i64": 0.38508513586474447, "float": 0.38508513586474447, "double": 0.38508513586474447 }
\ No newline at end of file
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
index aa49010..bd74f24 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/RandomWithSeedDescriptor.java
@@ -24,6 +24,7 @@
 import org.apache.asterix.om.functions.IFunctionDescriptor;
 import org.apache.asterix.om.functions.IFunctionDescriptorFactory;
 import org.apache.asterix.om.types.ATypeTag;
+import org.apache.asterix.om.types.hierachy.ATypeHierarchy;
 import org.apache.asterix.runtime.evaluators.base.AbstractScalarFunctionDynamicDescriptor;
 import org.apache.asterix.runtime.evaluators.functions.utils.RandomHelper;
 import org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier;
@@ -78,7 +79,9 @@
                             case BIGINT:
                             case FLOAT:
                             case DOUBLE:
-                                randomHelper.setSeed(bytes, offset + 1, arg0.getLength() - 1);
+                                double seed =
+                                        ATypeHierarchy.getDoubleValue(getIdentifier().getName(), 0, bytes, offset);
+                                randomHelper.setSeed(seed);
                                 randomHelper.nextDouble(resultPointable);
                                 break;
                             default:
diff --git a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
index 7ed1ce5..db5b774 100644
--- a/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
+++ b/asterixdb/asterix-runtime/src/main/java/org/apache/asterix/runtime/evaluators/functions/utils/RandomHelper.java
@@ -20,9 +20,11 @@
 package org.apache.asterix.runtime.evaluators.functions.utils;
 
 import java.io.DataOutput;
-import java.io.IOException;
+import java.security.NoSuchAlgorithmException;
 import java.security.SecureRandom;
 
+import org.apache.asterix.common.exceptions.ErrorCode;
+import org.apache.asterix.common.exceptions.RuntimeDataException;
 import org.apache.asterix.formats.nontagged.SerializerDeserializerProvider;
 import org.apache.asterix.om.base.AMutableDouble;
 import org.apache.asterix.om.types.BuiltinType;
@@ -30,44 +32,42 @@
 import org.apache.hyracks.api.exceptions.HyracksDataException;
 import org.apache.hyracks.data.std.api.IPointable;
 import org.apache.hyracks.data.std.util.ArrayBackedValueStorage;
-import org.apache.hyracks.data.std.util.DataUtils;
-import org.apache.hyracks.data.std.util.GrowableArray;
 
 public final class RandomHelper {
 
-    private final SecureRandom random = new SecureRandom();
+    private final SecureRandom random;
 
-    private final GrowableArray seed;
+    private double seed;
+
+    private boolean isFirst;
 
     private final AMutableDouble aDouble = new AMutableDouble(0);
 
     @SuppressWarnings("rawtypes")
-    private ISerializerDeserializer doubleSerde =
+    private final ISerializerDeserializer doubleSerde =
             SerializerDeserializerProvider.INSTANCE.getSerializerDeserializer(BuiltinType.ADOUBLE);
 
     private final ArrayBackedValueStorage resultStorage = new ArrayBackedValueStorage();
     private final DataOutput dataOutput = resultStorage.getDataOutput();
 
-    public RandomHelper(boolean withSeed) {
-        seed = withSeed ? new GrowableArray(8) : null;
+    public RandomHelper(boolean withSeed) throws HyracksDataException {
+        if (withSeed) {
+            try {
+                random = SecureRandom.getInstance("SHA1PRNG");
+            } catch (NoSuchAlgorithmException e) {
+                throw new IllegalStateException("random()");
+            }
+        } else {
+            random = new SecureRandom();
+        }
+        isFirst = true;
     }
 
-    public void setSeed(byte[] bytes, int offset, int length) throws HyracksDataException {
-        if (seed == null) {
-            throw new IllegalStateException();
-        }
-
-        boolean sameSeed =
-                seed.getLength() == length && DataUtils.equalsInRange(seed.getByteArray(), 0, bytes, offset, length);
-
-        if (!sameSeed) {
-            try {
-                seed.reset();
-                seed.append(bytes, offset, length);
-                random.setSeed(seed.getByteArray());
-            } catch (IOException e) {
-                throw HyracksDataException.create(e);
-            }
+    public void setSeed(double seedVal) throws HyracksDataException {
+        if (isFirst || seedVal != seed) {
+            seed = seedVal;
+            isFirst = false;
+            random.setSeed(Double.doubleToLongBits(seedVal));
         }
     }
 

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11763
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: mad-hatter
Gerrit-Change-Id: I14e81b24933744b136f85bd1218987401830fad9
Gerrit-Change-Number: 11763
Gerrit-PatchSet: 1
Gerrit-Owner: Ali Alsuliman <al...@gmail.com>
Gerrit-MessageType: newchange

Change in asterixdb[mad-hatter]: [ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)

Posted by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu>.
From Jenkins <je...@fulliautomatix.ics.uci.edu>:

Jenkins has posted comments on this change. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11763 )

Change subject: [ASTERIXDB-2804][FUN] Use pseudo-random for random(seed)
......................................................................


Patch Set 1: Integration-Tests+1

Integration Tests Successful

https://asterix-jenkins.ics.uci.edu/job/asterix-gerrit-integration-tests/12040/ : SUCCESS


-- 
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11763
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: mad-hatter
Gerrit-Change-Id: I14e81b24933744b136f85bd1218987401830fad9
Gerrit-Change-Number: 11763
Gerrit-PatchSet: 1
Gerrit-Owner: Ali Alsuliman <al...@gmail.com>
Gerrit-Reviewer: Jenkins <je...@fulliautomatix.ics.uci.edu>
Gerrit-CC: Anon. E. Moose #1000171
Gerrit-Comment-Date: Thu, 03 Jun 2021 17:09:06 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment