snuyanzin commented on code in PR #29507:
URL: https://github.com/apache/flink/pull/29507#discussion_r4306237247


##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/data/DecimalDataTest.java:
##########
@@ -45,10 +49,77 @@
 import static org.apache.flink.table.data.DecimalDataUtils.sround;
 import static org.apache.flink.table.data.DecimalDataUtils.subtract;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
 
 /** Test for {@link DecimalData}. */
 class DecimalDataTest {
 
+    private static Stream<Arguments> compactCastCases() {
+        return Stream.of(
+                // unscaled, srcPrecision, srcScale, targetPrecision, 
targetScale
+                arguments(0L, 5, 2, 3, 1), // zero
+                arguments(123L, 5, 2, 6, 2), // equal scale
+                arguments(12345L, 5, 0, 10, 2), // exact scale-up
+                arguments(24L, 2, 1, 2, 0), // scale-down, round half down
+                arguments(25L, 2, 1, 2, 0), // scale-down, round half up (+)
+                arguments(-25L, 2, 1, 2, 0), // scale-down, round half up (-)
+                arguments(995L, 3, 2, 2, 1), // rounds into precision overflow 
-> null
+                arguments(-995L, 3, 2, 2, 1), // rounds into precision 
overflow -> null (-)
+                arguments(999_999_999_999_999_999L, 18, 0, 18, 2), // 
long-overflow scale-up -> null
+                arguments(999_999_999_999_999_999L, 18, 0, 18, 0), // max 
18-digit boundary
+                arguments(-999_999_999_999_999_999L, 18, 0, 18, 0), // max 
18-digit boundary (-)
+                arguments(12345L, 5, 2, 3, 2)); // value exceeds target 
precision -> null
+    }
+
+    private static Stream<Arguments> longCastCases() {
+        return Stream.of(
+                // val, targetPrecision, targetScale
+                arguments(0L, 3, 1),
+                arguments(123L, 5, 2), // exact scale-up
+                arguments(-123L, 5, 2),
+                arguments(Long.MAX_VALUE, 18, 0), // 19 digits, exceeds 
precision -> null
+                arguments(Long.MIN_VALUE, 18, 0), // 19 digits, exceeds 
precision -> null
+                arguments(999_999_999_999_999_999L, 18, 0), // max 18-digit 
boundary
+                arguments(99L, 2, 1)); // scale-up overflow of precision -> 
null
+    }
+
+    @ParameterizedTest(name = "{0} -> DECIMAL({1},{2})")
+    @MethodSource("longCastCases")
+    void testCastFromLongCompactMatchesBigDecimal(long val, int 
targetPrecision, int targetScale) {
+        final DecimalData expected =
+                DecimalData.fromBigDecimal(BigDecimal.valueOf(val), 
targetPrecision, targetScale);
+        final DecimalData actual = castFrom(val, targetPrecision, targetScale);
+
+        if (expected == null) {
+            assertThat(actual).isNull();
+        } else {
+            assertThat(actual).isNotNull();
+            assertThat(actual.precision()).isEqualTo(expected.precision());
+            assertThat(actual.scale()).isEqualTo(expected.scale());
+            
assertThat(actual.toBigDecimal()).isEqualTo(expected.toBigDecimal());
+        }
+    }
+
+    @ParameterizedTest(name = "unscaled={0} DECIMAL({1},{2}) -> 
DECIMAL({3},{4})")
+    @MethodSource("compactCastCases")
+    void testCastToDecimalCompactMatchesBigDecimal(
+            long unscaled, int srcPrecision, int srcScale, int 
targetPrecision, int targetScale) {
+        final DecimalData src = DecimalData.fromUnscaledLong(unscaled, 
srcPrecision, srcScale);
+        final DecimalData expected =
+                DecimalData.fromBigDecimal(src.toBigDecimal(), 
targetPrecision, targetScale);
+        final DecimalData actual = castToDecimal(src, targetPrecision, 
targetScale);
+
+        if (expected == null) {
+            assertThat(actual).isNull();
+        } else {
+            assertThat(actual).isNotNull();
+            assertThat(actual.precision()).isEqualTo(expected.precision());
+            assertThat(actual.scale()).isEqualTo(expected.scale());
+            // BigDecimal.equals is scale-sensitive, so this pins value *and* 
scale
+            
assertThat(actual.toBigDecimal()).isEqualTo(expected.toBigDecimal());
+        }

Review Comment:
   extracted to one method



##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/data/DecimalDataTest.java:
##########
@@ -45,10 +49,77 @@
 import static org.apache.flink.table.data.DecimalDataUtils.sround;
 import static org.apache.flink.table.data.DecimalDataUtils.subtract;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.params.provider.Arguments.arguments;
 
 /** Test for {@link DecimalData}. */
 class DecimalDataTest {
 
+    private static Stream<Arguments> compactCastCases() {
+        return Stream.of(
+                // unscaled, srcPrecision, srcScale, targetPrecision, 
targetScale
+                arguments(0L, 5, 2, 3, 1), // zero
+                arguments(123L, 5, 2, 6, 2), // equal scale
+                arguments(12345L, 5, 0, 10, 2), // exact scale-up
+                arguments(24L, 2, 1, 2, 0), // scale-down, round half down
+                arguments(25L, 2, 1, 2, 0), // scale-down, round half up (+)
+                arguments(-25L, 2, 1, 2, 0), // scale-down, round half up (-)
+                arguments(995L, 3, 2, 2, 1), // rounds into precision overflow 
-> null
+                arguments(-995L, 3, 2, 2, 1), // rounds into precision 
overflow -> null (-)
+                arguments(999_999_999_999_999_999L, 18, 0, 18, 2), // 
long-overflow scale-up -> null
+                arguments(999_999_999_999_999_999L, 18, 0, 18, 0), // max 
18-digit boundary
+                arguments(-999_999_999_999_999_999L, 18, 0, 18, 0), // max 
18-digit boundary (-)
+                arguments(12345L, 5, 2, 3, 2)); // value exceeds target 
precision -> null
+    }
+
+    private static Stream<Arguments> longCastCases() {
+        return Stream.of(
+                // val, targetPrecision, targetScale
+                arguments(0L, 3, 1),
+                arguments(123L, 5, 2), // exact scale-up
+                arguments(-123L, 5, 2),
+                arguments(Long.MAX_VALUE, 18, 0), // 19 digits, exceeds 
precision -> null
+                arguments(Long.MIN_VALUE, 18, 0), // 19 digits, exceeds 
precision -> null
+                arguments(999_999_999_999_999_999L, 18, 0), // max 18-digit 
boundary
+                arguments(99L, 2, 1)); // scale-up overflow of precision -> 
null
+    }
+
+    @ParameterizedTest(name = "{0} -> DECIMAL({1},{2})")
+    @MethodSource("longCastCases")
+    void testCastFromLongCompactMatchesBigDecimal(long val, int 
targetPrecision, int targetScale) {
+        final DecimalData expected =
+                DecimalData.fromBigDecimal(BigDecimal.valueOf(val), 
targetPrecision, targetScale);
+        final DecimalData actual = castFrom(val, targetPrecision, targetScale);
+
+        if (expected == null) {
+            assertThat(actual).isNull();
+        } else {
+            assertThat(actual).isNotNull();
+            assertThat(actual.precision()).isEqualTo(expected.precision());
+            assertThat(actual.scale()).isEqualTo(expected.scale());
+            
assertThat(actual.toBigDecimal()).isEqualTo(expected.toBigDecimal());
+        }

Review Comment:
   reduced amount of comments



-- 
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