Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 28 additions & 21 deletions core/trino-main/src/test/java/io/trino/block/BlockAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ public final class BlockAssertions
private static final int MAX_STRING_SIZE = 50;
private static final int RANDOM_SEED = 633969769;

private static final Random RANDOM = new Random(RANDOM_SEED);

private BlockAssertions() {}

public static Object getOnlyValue(Type type, Block block)
Expand Down Expand Up @@ -129,15 +127,15 @@ public static DictionaryBlock createRandomDictionaryBlock(Block dictionary, int
checkArgument(dictionary.getPositionCount() > 0, "dictionary position count %s is less than or equal to 0", dictionary.getPositionCount());

int[] ids = IntStream.range(0, positionCount)
.map(i -> RANDOM.nextInt(dictionary.getPositionCount()))
.map(i -> random().nextInt(dictionary.getPositionCount()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, this actually uses a fresh Random instance per iteration. Each instance has the same seed, so each iteration will produce the same result. So this is kind of more deterministic than you wanted, I think :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 #13029 cc @sopel39

.toArray();
return new DictionaryBlock(0, positionCount, dictionary, ids, false, randomDictionaryId());
}

public static RunLengthEncodedBlock createRandomRleBlock(Block block, int positionCount)
{
checkArgument(block.getPositionCount() > 0, format("block positions %d is less than or equal to 0", block.getPositionCount()));
return new RunLengthEncodedBlock(block.getSingleValueBlock(RANDOM.nextInt(block.getPositionCount())), positionCount);
return new RunLengthEncodedBlock(block.getSingleValueBlock(random().nextInt(block.getPositionCount())), positionCount);
}

public static Block createRandomBlockForType(Type type, int positionCount, float nullRate)
Expand Down Expand Up @@ -214,7 +212,7 @@ private static Block createRandomBlockForNestedType(Type type, int positionCount
else {
// RowType doesn't need offsets, so we just use 1,
// for ArrayType and MapType we choose randomly either array length or map size at the current position
offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : RANDOM.nextInt(ENTRY_SIZE) + 1);
offsets[position + 1] = offsets[position] + (type instanceof RowType ? 1 : random().nextInt(ENTRY_SIZE) + 1);
}
}

Expand Down Expand Up @@ -246,20 +244,22 @@ private static Block createRandomBlockForNestedType(Type type, int positionCount

public static Block createRandomBooleansBlock(int positionCount, float nullRate)
{
return createBooleansBlock(generateListWithNulls(positionCount, nullRate, RANDOM::nextBoolean));
Random random = random();
return createBooleansBlock(generateListWithNulls(positionCount, nullRate, random::nextBoolean));
}

public static Block createRandomIntsBlock(int positionCount, float nullRate)
{
return createIntsBlock(generateListWithNulls(positionCount, nullRate, RANDOM::nextInt));
Random random = random();
return createIntsBlock(generateListWithNulls(positionCount, nullRate, random::nextInt));
}

public static Block createRandomLongDecimalsBlock(int positionCount, float nullRate)
{
return createLongDecimalsBlock(generateListWithNulls(
positionCount,
nullRate,
() -> String.valueOf(RANDOM.nextLong())));
() -> String.valueOf(random().nextLong())));
}

public static Block createRandomShortTimestampBlock(TimestampType type, int positionCount, float nullRate)
Expand All @@ -268,7 +268,7 @@ public static Block createRandomShortTimestampBlock(TimestampType type, int posi
generateListWithNulls(
positionCount,
nullRate,
() -> SqlTimestamp.fromMillis(type.getPrecision(), RANDOM.nextLong()).getEpochMicros()));
() -> SqlTimestamp.fromMillis(type.getPrecision(), random().nextLong()).getEpochMicros()));
}

public static Block createRandomLongTimestampBlock(TimestampType type, int positionCount, float nullRate)
Expand All @@ -279,7 +279,7 @@ public static Block createRandomLongTimestampBlock(TimestampType type, int posit
positionCount,
nullRate,
() -> {
SqlTimestamp sqlTimestamp = SqlTimestamp.fromMillis(type.getPrecision(), RANDOM.nextLong());
SqlTimestamp sqlTimestamp = SqlTimestamp.fromMillis(type.getPrecision(), random().nextLong());
return new LongTimestamp(sqlTimestamp.getEpochMicros(), sqlTimestamp.getPicosOfMicros());
}));
}
Expand All @@ -291,21 +291,22 @@ public static Block createRandomLongsBlock(int positionCount, int numberOfUnique
.mapToInt(Integer::intValue)
.toArray();
return createLongsBlock(IntStream.range(0, positionCount)
.mapToLong(position -> uniqueValues[RANDOM.nextInt(numberOfUniqueValues)])
.mapToLong(position -> uniqueValues[random().nextInt(numberOfUniqueValues)])
.boxed()
.collect(toImmutableList()));
}

public static Block createRandomLongsBlock(int positionCount, float nullRate)
{
return createLongsBlock(generateListWithNulls(positionCount, nullRate, RANDOM::nextLong));
Random random = random();
return createLongsBlock(generateListWithNulls(positionCount, nullRate, random::nextLong));
}

public static Block createRandomSmallintsBlock(int positionCount, float nullRate)
{
return createTypedLongsBlock(
SMALLINT,
generateListWithNulls(positionCount, nullRate, () -> (long) (short) RANDOM.nextLong()));
generateListWithNulls(positionCount, nullRate, () -> (long) (short) random().nextLong()));
}

public static Block createRandomStringBlock(int positionCount, float nullRate, int maxStringLength)
Expand All @@ -316,27 +317,28 @@ public static Block createRandomStringBlock(int positionCount, float nullRate, i

private static Block createRandomVarbinariesBlock(int positionCount, float nullRate)
{
return createSlicesBlock(VARBINARY, generateListWithNulls(positionCount, nullRate, () -> Slices.wrappedLongArray(RANDOM.nextLong(), RANDOM.nextLong())));
return createSlicesBlock(VARBINARY, generateListWithNulls(positionCount, nullRate, () -> Slices.wrappedLongArray(random().nextLong(), random().nextLong())));
}

private static Block createRandomUUIDsBlock(int positionCount, float nullRate)
{
return createSlicesBlock(UUID, generateListWithNulls(positionCount, nullRate, () -> Slices.wrappedLongArray(RANDOM.nextLong(), RANDOM.nextLong())));
return createSlicesBlock(UUID, generateListWithNulls(positionCount, nullRate, () -> Slices.wrappedLongArray(random().nextLong(), random().nextLong())));
}

private static Block createRandomIpAddressesBlock(int positionCount, float nullRate)
{
return createSlicesBlock(IPADDRESS, generateListWithNulls(positionCount, nullRate, () -> Slices.wrappedLongArray(RANDOM.nextLong(), RANDOM.nextLong())));
return createSlicesBlock(IPADDRESS, generateListWithNulls(positionCount, nullRate, () -> Slices.wrappedLongArray(random().nextLong(), random().nextLong())));
}

private static Block createRandomTinyintsBlock(int positionCount, float nullRate)
{
return createTypedLongsBlock(TINYINT, generateListWithNulls(positionCount, nullRate, () -> (long) (byte) RANDOM.nextLong()));
return createTypedLongsBlock(TINYINT, generateListWithNulls(positionCount, nullRate, () -> (long) (byte) random().nextLong()));
}

public static Block createRandomDoublesBlock(int positionCount, float nullRate)
{
return createDoublesBlock(generateListWithNulls(positionCount, nullRate, RANDOM::nextDouble));
Random random = random();
return createDoublesBlock(generateListWithNulls(positionCount, nullRate, random::nextDouble));
}

public static Block createRandomCharsBlock(CharType charType, int positionCount, float nullRate)
Expand Down Expand Up @@ -917,13 +919,13 @@ private static Set<Integer> chooseRandomUnique(int bound, int count)
// it's an order of bound/count faster to use this method for small enough count/bound ratio
Set<Integer> values = new HashSet<>(count);
while (values.size() < count) {
values.add(RANDOM.nextInt(bound));
values.add(random().nextInt(bound));
}
return ImmutableSet.copyOf(values);
}

List<Integer> allNumbers = IntStream.range(0, bound).boxed().collect(toList());
Collections.shuffle(allNumbers, RANDOM);
Collections.shuffle(allNumbers, random());
return allNumbers.stream().limit(count).collect(toImmutableSet());
}

Expand All @@ -932,7 +934,7 @@ private static String generateRandomStringWithLength(int length)
String symbols = "abcdefghijklmnopqrstuvwxyz";
char[] chars = new char[length];
for (int i = 0; i < length; i++) {
chars[i] = symbols.charAt(RANDOM.nextInt(symbols.length()));
chars[i] = symbols.charAt(random().nextInt(symbols.length()));
}
return new String(chars);
}
Expand All @@ -941,4 +943,9 @@ private static void verifyNullRate(float nullRate)
{
verify(nullRate >= 0 && nullRate <= 1, "nullRate %s is not valid", nullRate);
}

private static Random random()
{
return new Random(RANDOM_SEED);
}
}