twalthr commented on a change in pull request #17800:
URL: https://github.com/apache/flink/pull/17800#discussion_r751278021



##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/CastRule.java
##########
@@ -45,6 +45,9 @@
     CastExecutor<IN, OUT> create(
             Context context, LogicalType inputLogicalType, LogicalType 
targetLogicalType);
 
+    /** @return true if the cast operation can fail. */
+    boolean isFallible();

Review comment:
       sometimes the JavaDoc should be the source for a name: rename to 
`canFail` and no JavaDoc is required.

##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToNumericPrimitiveCastRule.java
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.flink.table.planner.functions.casting;
+
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeFamily;
+
+import static 
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_BYTE;
+import static 
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_DOUBLE;
+import static 
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_FLOAT;
+import static 
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_INT;
+import static 
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_LONG;
+import static 
org.apache.flink.table.planner.codegen.calls.BuiltInMethods.STRING_DATA_TO_SHORT;
+import static 
org.apache.flink.table.planner.functions.casting.CastRuleUtils.staticCall;
+
+/**
+ * {@link LogicalTypeFamily#CHARACTER_STRING} to {@link 
LogicalTypeFamily#INTEGER_NUMERIC} and
+ * {@link LogicalTypeFamily#APPROXIMATE_NUMERIC} cast rule.
+ */
+class StringToNumericPrimitiveCastRule
+        extends AbstractExpressionCodeGeneratorCastRule<StringData, Number> {
+
+    static final StringToNumericPrimitiveCastRule INSTANCE = new 
StringToNumericPrimitiveCastRule();
+
+    private StringToNumericPrimitiveCastRule() {
+        super(
+                CastRulePredicate.builder()
+                        .input(LogicalTypeFamily.CHARACTER_STRING)
+                        .target(LogicalTypeFamily.INTEGER_NUMERIC)
+                        .target(LogicalTypeFamily.APPROXIMATE_NUMERIC)
+                        .build());
+    }
+
+    @Override
+    public String generateExpression(
+            CodeGeneratorCastRule.Context context,
+            String inputTerm,
+            LogicalType inputLogicalType,
+            LogicalType targetLogicalType) {
+        switch (targetLogicalType.getTypeRoot()) {
+            case TINYINT:
+                return staticCall(STRING_DATA_TO_BYTE(), inputTerm);
+            case SMALLINT:
+                return staticCall(STRING_DATA_TO_SHORT(), inputTerm);
+            case INTEGER:
+                return staticCall(STRING_DATA_TO_INT(), inputTerm);
+            case BIGINT:
+                return staticCall(STRING_DATA_TO_LONG(), inputTerm);
+            case FLOAT:
+                return staticCall(STRING_DATA_TO_FLOAT(), inputTerm);
+            case DOUBLE:
+                return staticCall(STRING_DATA_TO_DOUBLE(), inputTerm);
+        }
+        throw new IllegalArgumentException("This is a rule bug, contact the 
developers");

Review comment:
       usually we use `This is a bug. Please file an issue.`

##########
File path: 
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/data/binary/BinaryStringDataUtil.java
##########
@@ -144,12 +151,16 @@
         return substrings.toArray(new BinaryStringData[0]);
     }
 
-    /** Decide boolean representation of a string. */
-    public static Boolean toBooleanSQL(BinaryStringData str) {
+    /** Parse a {@link StringData} to boolean. */
+    public static boolean toBoolean(BinaryStringData str) throws 
TableException {

Review comment:
       move the changes in `BinaryStringDataUtil` in a separate commit to be 
visible in the log

##########
File path: 
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/data/binary/BinaryStringDataUtil.java
##########
@@ -168,21 +179,35 @@ public static BinaryStringData hash(BinaryStringData str, 
String algorithm)
     }
 
     /**
-     * Parses this BinaryStringData to DecimalData.
+     * Parses a {@link BinaryStringData} to {@link DecimalData}.
      *
-     * @return DecimalData value if the parsing was successful, or null if 
overflow
-     * @throws NumberFormatException if the parsing failed.
+     * @return DecimalData value if the parsing was successful.
+     * @throws TableException if the parsing failed or if overflow happened.
      */
-    public static DecimalData toDecimal(BinaryStringData str, int precision, 
int scale) {
+    public static DecimalData toDecimal(BinaryStringData str, int precision, 
int scale)
+            throws TableException {
         str.ensureMaterialized();
 
-        if (DecimalDataUtils.isByteArrayDecimal(precision)
-                || DecimalDataUtils.isByteArrayDecimal(str.getSizeInBytes())) {
-            return toBigPrecisionDecimal(str, precision, scale);
+        DecimalData data;
+
+        try {
+            if (DecimalDataUtils.isByteArrayDecimal(precision)
+                    || 
DecimalDataUtils.isByteArrayDecimal(str.getSizeInBytes())) {
+                data = toBigPrecisionDecimal(str, precision, scale);
+            } else {
+                int sizeInBytes = str.getSizeInBytes();
+                data =
+                        toDecimalFromBytes(
+                                precision, scale, getTmpBytes(str, 
sizeInBytes), 0, sizeInBytes);
+            }
+        } catch (NumberFormatException e) {
+            throw new TableException("Cannot parse STRING to DECIMAL", e);

Review comment:
       very nit: `.` at the end of all exception messages.




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