This is an automated email from the ASF dual-hosted git repository. mblow pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/asterixdb.git
commit d28c0a7669f1d4a877524011517aea0fe2ff2505 Author: Ali Alsuliman <[email protected]> AuthorDate: Thu Jun 3 20:43:22 2021 +0300 [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 Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/11763 Integration-Tests: Jenkins <[email protected]> Tested-by: Jenkins <[email protected]> Reviewed-by: Ali Alsuliman <[email protected]> Reviewed-by: Dmitry Lychagin <[email protected]> --- .../queries_sqlpp/misc/random/random.3.query.sqlpp | 22 +++++++++++ .../runtimets/results/misc/random/random.3.adm | 3 ++ .../functions/RandomWithSeedDescriptor.java | 5 ++- .../evaluators/functions/utils/RandomHelper.java | 46 +++++++++++----------- 4 files changed, 51 insertions(+), 25 deletions(-) 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..5b9e73c --- /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.BuiltinFunctions; 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 @@ public class RandomWithSeedDescriptor extends AbstractScalarFunctionDynamicDescr 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..bc905ba 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,7 +20,7 @@ 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.formats.nontagged.SerializerDeserializerProvider; @@ -30,44 +30,42 @@ import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer; 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 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) { + public RandomHelper(boolean withSeed) throws HyracksDataException { + if (withSeed) { try { - seed.reset(); - seed.append(bytes, offset, length); - random.setSeed(seed.getByteArray()); - } catch (IOException e) { - throw HyracksDataException.create(e); + random = SecureRandom.getInstance("SHA1PRNG"); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("random()"); } + } else { + random = new SecureRandom(); + } + isFirst = true; + } + + public void setSeed(double seedVal) throws HyracksDataException { + if (isFirst || seedVal != seed) { + seed = seedVal; + isFirst = false; + random.setSeed(Double.doubleToLongBits(seedVal)); } }
