MartijnVisser commented on code in PR #28437:
URL: https://github.com/apache/flink/pull/28437#discussion_r4786996729


##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/TypeInferenceUtil.java:
##########
@@ -155,11 +158,63 @@ private static CallContext castArgumentsInternal(
                                 "Invalid argument type at position %d. Data 
type %s expected but %s passed.",
                                 pos, expectedType, actualType));
             }
+
+            // Beyond type-level castability, a constant DECIMAL literal must 
also fit the
+            // expected type on the value level.
+            if (throwOnInferInputFailure) {
+                validateDecimalLiteralFitsExpectedType(callContext, pos, 
expectedType);
+            }
         }
 
         return castCallContext;
     }
 
+    /**
+     * Validates that a constant {@code DECIMAL} literal argument fits the 
expected type on the
+     * value level and not only on the type level. For example, {@code 
123.456} is implicitly
+     * castable to a {@code DECIMAL(2, 2)} but would otherwise be silently 
reduced to {@code NULL}
+     * during constant folding instead of raising a type error.
+     *
+     * <p>Only a top-level {@code DECIMAL} argument is covered here; other 
value-level overflows

Review Comment:
   This also changes scalar, table, async and aggregate functions: `f(123.456)` 
for a `DECIMAL(2, 2)` argument returned NULL on master and fails now. Please 
add a non-PTF test and a release note on the Jira.



##########
flink-table/flink-table-planner/src/test/java/org/apache/flink/table/planner/plan/nodes/exec/stream/ProcessTableFunctionTestPrograms.java:
##########
@@ -118,6 +119,19 @@ public class ProcessTableFunctionTestPrograms {
                             "INSERT INTO sink SELECT * FROM f(i => 42, b => 
CAST('TRUE' AS BOOLEAN))")
                     .build();
 
+    public static final TableTestProgram PROCESS_SCALAR_DECIMAL_ARG =
+            TableTestProgram.of(
+                            "process-scalar-decimal-arg",
+                            "a decimal scalar arg that fits the declared 
precision")
+                    .setupTemporarySystemFunction("f", 
ScalarDecimalArgFunction.class)
+                    .setupTableSink(
+                            SinkTestStep.newBuilder("sink")
+                                    .addSchema(BASE_SINK_SCHEMA)
+                                    .consumedValues("+I[{0.4}]")

Review Comment:
   After a rebase onto master this fails with `+I[{0.40}]`, since FLINK-39679 
now casts the argument to `DECIMAL(2, 2)`. The green Azure run predates that.



##########
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/TypeInferenceUtil.java:
##########
@@ -155,11 +158,63 @@ private static CallContext castArgumentsInternal(
                                 "Invalid argument type at position %d. Data 
type %s expected but %s passed.",
                                 pos, expectedType, actualType));
             }
+
+            // Beyond type-level castability, a constant DECIMAL literal must 
also fit the
+            // expected type on the value level.
+            if (throwOnInferInputFailure) {
+                validateDecimalLiteralFitsExpectedType(callContext, pos, 
expectedType);

Review Comment:
   `ProcessTableRunnerGenerator` calls `castArguments` during codegen too, so a 
PTF plan compiled on master with `f(123.456)` now fails on `EXECUTE PLAN`. I 
think the check belongs only in the validation paths, 
`TypeInferenceOperandChecker` and `runTypeInferenceInternal`.



##########
flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/InputTypeStrategiesTest.java:
##########
@@ -117,6 +119,86 @@ protected Stream<TestSpec> testData() {
                         .expectErrorMessage(
                                 "Invalid number of arguments. At least 2 
arguments expected but 1 passed."),
 
+                // a constant literal that overflows the expected DECIMAL 
precision must raise a
+                // type error instead of being silently reduced to NULL
+                TestSpec.forStrategy(
+                                "literal decimal overflowing the expected 
precision",
+                                explicitSequence(DataTypes.DECIMAL(2, 2)))
+                        .calledWithArgumentTypes(DataTypes.DECIMAL(6, 3))
+                        .calledWithLiteralAt(0, new BigDecimal("123.456"))
+                        .expectErrorMessage(
+                                "The value '123.456' does not fit the expected 
data type"),
+
+                // a constant literal that only requires scale rounding still 
fits
+                TestSpec.forStrategy(
+                                "literal decimal rounded within the expected 
precision",

Review Comment:
   Could you add `0.995`, which rounds up to `1.00` and overflows? Replacing 
`fromBigDecimal` with an integer-digit check still passes all tests.



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