-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Reuse the common methods in serveral RandomData classes. #1102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
core/src/main/java/org/apache/iceberg/util/RandomUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.iceberg.util; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.util.Random; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.Types; | ||
|
|
||
| public class RandomUtil { | ||
|
|
||
| private RandomUtil() { | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no need for a blank line here. |
||
| } | ||
|
|
||
| @SuppressWarnings("RandomModInteger") | ||
| public static Object generatePrimitive(Type.PrimitiveType primitive, | ||
| Random random) { | ||
| int choice = random.nextInt(20); | ||
|
|
||
| switch (primitive.typeId()) { | ||
| case BOOLEAN: | ||
| return choice < 10; | ||
|
|
||
| case INTEGER: | ||
| switch (choice) { | ||
| case 1: | ||
| return Integer.MIN_VALUE; | ||
| case 2: | ||
| return Integer.MAX_VALUE; | ||
| case 3: | ||
| return 0; | ||
| default: | ||
| return random.nextInt(); | ||
| } | ||
|
|
||
| case LONG: | ||
| switch (choice) { | ||
| case 1: | ||
| return Long.MIN_VALUE; | ||
| case 2: | ||
| return Long.MAX_VALUE; | ||
| case 3: | ||
| return 0L; | ||
| default: | ||
| return random.nextLong(); | ||
| } | ||
|
|
||
| case FLOAT: | ||
| switch (choice) { | ||
| case 1: | ||
| return Float.MIN_VALUE; | ||
| case 2: | ||
| return -Float.MIN_VALUE; | ||
| case 3: | ||
| return Float.MAX_VALUE; | ||
| case 4: | ||
| return -Float.MAX_VALUE; | ||
| case 5: | ||
| return Float.NEGATIVE_INFINITY; | ||
| case 6: | ||
| return Float.POSITIVE_INFINITY; | ||
| case 7: | ||
| return 0.0F; | ||
| case 8: | ||
| return Float.NaN; | ||
| default: | ||
| return random.nextFloat(); | ||
| } | ||
|
|
||
| case DOUBLE: | ||
| switch (choice) { | ||
| case 1: | ||
| return Double.MIN_VALUE; | ||
| case 2: | ||
| return -Double.MIN_VALUE; | ||
| case 3: | ||
| return Double.MAX_VALUE; | ||
| case 4: | ||
| return -Double.MAX_VALUE; | ||
| case 5: | ||
| return Double.NEGATIVE_INFINITY; | ||
| case 6: | ||
| return Double.POSITIVE_INFINITY; | ||
| case 7: | ||
| return 0.0D; | ||
| case 8: | ||
| return Double.NaN; | ||
| default: | ||
| return random.nextDouble(); | ||
| } | ||
|
|
||
| case DATE: | ||
| // this will include negative values (dates before 1970-01-01) | ||
| return random.nextInt() % ABOUT_380_YEARS_IN_DAYS; | ||
|
|
||
| case TIME: | ||
| return (random.nextLong() & Integer.MAX_VALUE) % ONE_DAY_IN_MICROS; | ||
|
|
||
| case TIMESTAMP: | ||
| return random.nextLong() % FIFTY_YEARS_IN_MICROS; | ||
|
|
||
| case STRING: | ||
| return randomString(random); | ||
|
|
||
| case UUID: | ||
| byte[] uuidBytes = new byte[16]; | ||
| random.nextBytes(uuidBytes); | ||
| // this will hash the uuidBytes | ||
| return uuidBytes; | ||
|
|
||
| case FIXED: | ||
| byte[] fixed = new byte[((Types.FixedType) primitive).length()]; | ||
| random.nextBytes(fixed); | ||
| return fixed; | ||
|
|
||
| case BINARY: | ||
| byte[] binary = new byte[random.nextInt(50)]; | ||
| random.nextBytes(binary); | ||
| return binary; | ||
|
|
||
| case DECIMAL: | ||
| Types.DecimalType type = (Types.DecimalType) primitive; | ||
| BigInteger unscaled = randomUnscaled(type.precision(), random); | ||
| return new BigDecimal(unscaled, type.scale()); | ||
|
|
||
| default: | ||
| throw new IllegalArgumentException( | ||
| "Cannot generate random value for unknown type: " + primitive); | ||
| } | ||
| } | ||
|
|
||
| private static final long FIFTY_YEARS_IN_MICROS = | ||
| (50L * (365 * 3 + 366) * 24 * 60 * 60 * 1_000_000) / 4; | ||
| private static final int ABOUT_380_YEARS_IN_DAYS = 380 * 365; | ||
| private static final long ONE_DAY_IN_MICROS = 24 * 60 * 60 * 1_000_000L; | ||
| private static final String CHARS = | ||
| "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.!?"; | ||
|
|
||
| private static String randomString(Random random) { | ||
| int length = random.nextInt(50); | ||
| byte[] buffer = new byte[length]; | ||
|
|
||
| for (int i = 0; i < length; i += 1) { | ||
| buffer[i] = (byte) CHARS.charAt(random.nextInt(CHARS.length())); | ||
| } | ||
|
|
||
| return new String(buffer); | ||
| } | ||
|
|
||
| private static final String DIGITS = "0123456789"; | ||
|
|
||
| private static BigInteger randomUnscaled(int precision, Random random) { | ||
| int length = random.nextInt(precision); | ||
| if (length == 0) { | ||
| return BigInteger.ZERO; | ||
| } | ||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
| for (int i = 0; i < length; i += 1) { | ||
| sb.append(DIGITS.charAt(random.nextInt(DIGITS.length()))); | ||
| } | ||
|
|
||
| return new BigInteger(sb.toString()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this class should be in src/main. Can you move it to src/test?