dawidwys commented on a change in pull request #12411:
URL: https://github.com/apache/flink/pull/12411#discussion_r434513273



##########
File path: 
flink-table/flink-table-common/src/main/java/org/apache/flink/table/functions/BuiltInFunctionDefinitions.java
##########
@@ -919,7 +920,8 @@
                new BuiltInFunctionDefinition.Builder()
                        .name("cast")
                        .kind(SCALAR)
-                       .outputTypeStrategy(TypeStrategies.MISSING)
+                       .inputTypeStrategy(SPECIFIC_FOR_CAST)
+                       
.outputTypeStrategy(nullable(ConstantArgumentCount.to(0), 
TypeStrategies.argument(1)))

Review comment:
       After an offline discussion we said it's okay as it has very limited 
scope. The array approach has the downside that it does not support natively 
open ended ranges.

##########
File path: 
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/strategies/CastInputTypeStrategy.java
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.types.inference.strategies;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.functions.BuiltInFunctionDefinitions;
+import org.apache.flink.table.functions.FunctionDefinition;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.inference.ArgumentCount;
+import org.apache.flink.table.types.inference.CallContext;
+import org.apache.flink.table.types.inference.ConstantArgumentCount;
+import org.apache.flink.table.types.inference.InputTypeStrategy;
+import org.apache.flink.table.types.inference.Signature;
+import org.apache.flink.table.types.logical.LegacyTypeInformationType;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import static 
org.apache.flink.table.types.logical.utils.LogicalTypeCasts.supportsExplicitCast;
+
+/**
+ * {@link InputTypeStrategy} specific for {@link 
BuiltInFunctionDefinitions#CAST}.
+ *
+ * <p>It expects two arguments where the type of first one must be castable to 
the type of the second
+ * one. The second one must be a type literal.
+ */
+@Internal
+public final class CastInputTypeStrategy implements InputTypeStrategy {
+
+       @Override
+       public ArgumentCount getArgumentCount() {
+               return ConstantArgumentCount.of(2);
+       }
+
+       @Override
+       public Optional<List<DataType>> inferInputTypes(CallContext 
callContext, boolean throwOnFailure) {
+               // check for type literal
+               if (!callContext.isArgumentLiteral(1) || 
!callContext.getArgumentValue(1, DataType.class).isPresent()) {
+                       return Optional.empty();

Review comment:
       nit: throw an exception?

##########
File path: 
flink-table/flink-table-common/src/test/java/org/apache/flink/table/types/inference/TypeStrategiesTest.java
##########
@@ -113,37 +113,60 @@
                                .inputTypes()
                                .expectErrorMessage("Could not infer an output 
type for the given arguments. Untyped NULL received."),
 
-                       TestSpec.forStrategy(
-                               "Infer a row type",
-                               TypeStrategies.ROW)
+                       TestSpec
+                               .forStrategy(
+                                       "Infer a row type",
+                                       TypeStrategies.ROW)
                                .inputTypes(DataTypes.BIGINT(), 
DataTypes.STRING())
                                .expectDataType(DataTypes.ROW(
                                        DataTypes.FIELD("f0", 
DataTypes.BIGINT()),
                                        DataTypes.FIELD("f1", 
DataTypes.STRING())).notNull()
                                ),
 
-                       TestSpec.forStrategy(
-                               "Infer an array type",
-                               TypeStrategies.ARRAY)
+                       TestSpec
+                               .forStrategy(
+                                       "Infer an array type",
+                                       TypeStrategies.ARRAY)
                                .inputTypes(DataTypes.BIGINT(), 
DataTypes.BIGINT())
                                
.expectDataType(DataTypes.ARRAY(DataTypes.BIGINT()).notNull()),
 
-                       TestSpec.forStrategy(
-                               "Infer a map type",
-                               TypeStrategies.MAP)
+                       TestSpec.
+                               forStrategy(
+                                       "Infer a map type",
+                                       TypeStrategies.MAP)
                                .inputTypes(DataTypes.BIGINT(), 
DataTypes.STRING().notNull())
                                
.expectDataType(DataTypes.MAP(DataTypes.BIGINT(), 
DataTypes.STRING().notNull()).notNull()),
 
+                       TestSpec
+                               .forStrategy(
+                                       "Cascading to nullable type",
+                                       
nullable(explicit(DataTypes.BOOLEAN().notNull())))
+                               .inputTypes(DataTypes.BIGINT().notNull(), 
DataTypes.VARCHAR(2).nullable())
+                               .expectDataType(DataTypes.BOOLEAN().nullable()),
+
+                       TestSpec
+                               .forStrategy(
+                                       "Cascading to not null type",
+                                       
nullable(explicit(DataTypes.BOOLEAN().nullable())))
+                               .inputTypes(DataTypes.BIGINT().notNull(), 
DataTypes.VARCHAR(2).notNull())
+                               .expectDataType(DataTypes.BOOLEAN().notNull()),
+
                        TestSpec.forStrategy(
-                               "Cascading to nullable type",
-                               
nullable(explicit(DataTypes.BOOLEAN().notNull())))
+                                       "Cascading to not null type but only 
consider first argument",
+                                       nullable(ConstantArgumentCount.to(0), 
explicit(DataTypes.BOOLEAN().nullable())))
+                               .inputTypes(DataTypes.BIGINT().notNull(), 
DataTypes.VARCHAR(2).nullable())
+                               .expectDataType(DataTypes.BOOLEAN().notNull()),
+
+                       TestSpec.forStrategy(
+                                       "Cascading to null type but only 
consider first two argument",
+                                       nullable(ConstantArgumentCount.to(1), 
explicit(DataTypes.BOOLEAN().nullable())))
                                .inputTypes(DataTypes.BIGINT().notNull(), 
DataTypes.VARCHAR(2).nullable())
                                .expectDataType(DataTypes.BOOLEAN().nullable()),
 
                        TestSpec.forStrategy(
-                               "Cascading to not null type",
-                               
nullable(explicit(DataTypes.BOOLEAN().nullable())))
-                               .inputTypes(DataTypes.BIGINT().notNull(), 
DataTypes.VARCHAR(2).notNull())
+                                       "Cascading to not null type but only 
consider first two argument",
+                                       
nullable(ConstantArgumentCount.between(1, 2), 
explicit(DataTypes.BOOLEAN().nullable())))

Review comment:
       bump




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

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


Reply via email to