dawidwys commented on a change in pull request #12411:
URL: https://github.com/apache/flink/pull/12411#discussion_r434029476
##########
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:
is it 0 or 1 indexed? If 0 then those are not the first two.
##########
File path:
flink-table/flink-table-common/src/main/java/org/apache/flink/table/types/inference/InputTypeStrategies.java
##########
@@ -249,18 +265,39 @@ public static OrArgumentTypeStrategy
or(ArgumentTypeStrategy... strategies) {
//
--------------------------------------------------------------------------------------------
- // Specific type strategies
+ // Specific input type strategies
//
--------------------------------------------------------------------------------------------
/**
- * Strategy specific for {@link
org.apache.flink.table.functions.BuiltInFunctionDefinitions#ARRAY}.
+ * Strategy specific for {@link BuiltInFunctionDefinitions#CAST}.
+ */
+ public static final InputTypeStrategy SPECIFIC_FOR_CAST = sequence(
+ InputTypeStrategies.ANY,
+ and(
+ InputTypeStrategies.ANY,
Review comment:
Does it mean you can pass any expression as the second argument?
Shouldn't we check that it is a `TypeLiteralExpression` somehow?
##########
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:
I don't like using the `ArgumentCount` here. It is used for completely
different purpose than it was originally intended. It serves as a range here. I
know Java lacks a proper range support, but in this case I think we could use
`int[]` here.
And then we could write it like:
```
nullable(IntStream.rangeClosed(0, 1).toArray(), TypeStrategies.argument(1))
// if we need to select multiple consecutive arguments
nullable(new int[] {0, 3}, TypeStrategies.argument(1)) // if we need to
select non consecutive arguments
```
----------------------------------------------------------------
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]