Abacn commented on code in PR #23014:
URL: https://github.com/apache/beam/pull/23014#discussion_r984083415


##########
sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/logicaltypes/LogicalTypesTest.java:
##########
@@ -130,4 +133,51 @@ public void testSchema() {
         schemaValue,
         new SchemaLogicalType().toInputType(new 
SchemaLogicalType().toBaseType(schemaValue)));
   }
+
+  @Test
+  public void testFixedPrecisionNumeric() {
+    final int precision = 10;
+    final int scale = 2;
+    Random random = new Random();
+
+    Schema argumentSchema =
+        
Schema.builder().addInt32Field("precision").addInt32Field("scale").build();
+
+    // invalid schema
+    final Schema invalidArgumentSchema =
+        
Schema.builder().addInt32Field("invalid").addInt32Field("schema").build();
+    assertThrows(
+        IllegalArgumentException.class,
+        () ->
+            FixedPrecisionNumeric.of(
+                Row.withSchema(invalidArgumentSchema).addValues(precision, 
scale).build()));
+
+    // FixedPrecisionNumeric specified precision and scale
+    FixedPrecisionNumeric numeric =
+        FixedPrecisionNumeric.of(
+            Row.withSchema(argumentSchema).addValues(precision, 
scale).build());
+    Schema schema = Schema.builder().addLogicalTypeField("decimal", 
numeric).build();
+
+    // check argument valid case
+    BigDecimal decimal = BigDecimal.valueOf(random.nextInt(), scale);
+    Row row = Row.withSchema(schema).addValues(decimal).build();
+    assertEquals(decimal, row.getLogicalTypeValue(0, BigDecimal.class));
+
+    // check argument invalid case
+    decimal = BigDecimal.valueOf(random.nextLong() + 100_000_000_000L, scale);
+    assertThrows(IllegalArgumentException.class, 
Row.withSchema(schema).addValues(decimal)::build);
+
+    // FixedPrecisionNumeric without specifying precision
+    numeric = FixedPrecisionNumeric.of(scale);
+    schema = Schema.builder().addLogicalTypeField("decimal", numeric).build();
+
+    // check argument always valid
+    decimal = BigDecimal.valueOf(random.nextInt(), scale);
+    row = Row.withSchema(schema).addValues(decimal).build();
+    assertEquals(decimal, row.getLogicalTypeValue(0, BigDecimal.class));
+
+    decimal = BigDecimal.valueOf(random.nextLong() + 100_000_000_000L, scale);

Review Comment:
   ah I made a typo. These two cases were intended be (random.nextInt() + 
100_000_000_000L) which guarantees to generate a positive number with at least 
11 digits and beyond the precision of DECIMAL(precision=10). Yes we can just 
use 100_000_000_001L here



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to