You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@avro.apache.org by GitBox <gi...@apache.org> on 2022/09/19 20:32:33 UTC

[GitHub] [avro] martin-g commented on a diff in pull request #1867: AVRO-3611: fix generator

martin-g commented on code in PR #1867:
URL: https://github.com/apache/avro/pull/1867#discussion_r974662031


##########
lang/java/avro/src/main/java/org/apache/avro/util/RandomData.java:
##########
@@ -146,6 +150,23 @@ private Object generate(Schema schema, Random random, int d) {
 
   private static final Charset UTF8 = StandardCharsets.UTF_8;
 
+  private int randomInt(Random random, LogicalType type) {
+    if (type instanceof LogicalTypes.TimeMillis) {
+      return random.nextInt((int) Duration.ofDays(1).toMillis() - 1);
+    }
+    // LogicalTypes.Date LocalDate.MAX.toEpochDay() > Integer.MAX;
+    return random.nextInt();
+  }
+
+  private long randomLong(Random random, LogicalType type) {
+    if (type instanceof LogicalTypes.TimeMicros) {
+      return ThreadLocalRandom.current().nextLong(Duration.ofDays(1).toMillis() * 1000L);

Review Comment:
   `Duration.ofDays(1).toMillis() * 1000L` is `86400000000`. IMO it would be good to make it a proper Java constant (`private static final int`).



##########
lang/java/avro/src/main/java/org/apache/avro/util/RandomData.java:
##########
@@ -146,6 +150,23 @@ private Object generate(Schema schema, Random random, int d) {
 
   private static final Charset UTF8 = StandardCharsets.UTF_8;
 
+  private int randomInt(Random random, LogicalType type) {
+    if (type instanceof LogicalTypes.TimeMillis) {
+      return random.nextInt((int) Duration.ofDays(1).toMillis() - 1);
+    }
+    // LogicalTypes.Date LocalDate.MAX.toEpochDay() > Integer.MAX;
+    return random.nextInt();
+  }
+
+  private long randomLong(Random random, LogicalType type) {
+    if (type instanceof LogicalTypes.TimeMicros) {
+      return ThreadLocalRandom.current().nextLong(Duration.ofDays(1).toMillis() * 1000L);

Review Comment:
   You could still use `Random#nextInt(86400000000)` and cast it to `long`.



##########
lang/java/avro/src/main/java/org/apache/avro/util/RandomData.java:
##########
@@ -146,6 +150,23 @@ private Object generate(Schema schema, Random random, int d) {
 
   private static final Charset UTF8 = StandardCharsets.UTF_8;
 
+  private int randomInt(Random random, LogicalType type) {
+    if (type instanceof LogicalTypes.TimeMillis) {
+      return random.nextInt((int) Duration.ofDays(1).toMillis() - 1);

Review Comment:
   Make `Duration.ofDays(1).toMillis() - 1` a constant.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@avro.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org