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



##########
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:
       I don't know, this branch should never be took anyway given that if the 
type is not one of those in the various case branches, the rule will never be 
matched... I can add the type root, but i don't think it helps anyway




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