Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,23 @@ public List<DataType> getArgumentDataTypes() {
.collect(Collectors.toList());
}

@Override
public Optional<String> getArgumentName(int pos) {
final ResolvedExpression arg = getArgument(pos);

if (arg instanceof CallExpression) {
final CallExpression call = (CallExpression) arg;
if (call.getFunctionDefinition() == BuiltInFunctionDefinitions.AS) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice here there is a way of aliasing using = as well as AS. Does Flink support this? If so can we add a unit test to ensure this works in the same way as AS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe Flink does support it, but I'll verify

final List<ResolvedExpression> children = call.getResolvedChildren();
if (children.size() >= 2 && children.get(1) instanceof ValueLiteralExpression) {
return ((ValueLiteralExpression) children.get(1)).getValueAs(String.class);
}
}
}

return Optional.empty();
}

@Override
public Optional<DataType> getOutputDataType() {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ default Optional<ChangelogMode> getOutputChangelogMode() {
*/
List<DataType> getArgumentDataTypes();

/** Returns the name/alias of the argument at the given position if one is available. */
default Optional<String> getArgumentName(int pos) {
return Optional.empty();
}

/**
* Returns the inferred output data type of the function call.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public Optional<DataType> inferType(CallContext callContext) {
List<DataType> argumentDataTypes = callContext.getArgumentDataTypes();
DataTypes.Field[] fields =
IntStream.range(0, argumentDataTypes.size())
.mapToObj(idx -> DataTypes.FIELD("f" + idx, argumentDataTypes.get(idx)))
.mapToObj(
idx -> {
String fieldName =
callContext.getArgumentName(idx).orElse("f" + idx);
return DataTypes.FIELD(fieldName, argumentDataTypes.get(idx));
})
.toArray(DataTypes.Field[]::new);

return Optional.of(DataTypes.ROW(fields).notNull());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ public List<DataType> getArgumentDataTypes() {
return expectedArguments;
}

@Override
public Optional<String> getArgumentName(int pos) {
// argument names remain regardless of casting
return originalContext.getArgumentName(pos);
}

@Override
public Optional<DataType> getOutputDataType() {
return Optional.ofNullable(outputDataType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,23 @@ Stream<TestSetSpec> getTestSetSpecs() {
DataTypes.FIELD("b", DataTypes.TINYINT()),
DataTypes.FIELD("c", DataTypes.BIGINT()),
DataTypes.FIELD("d", DataTypes.BOOLEAN()))
.notNull()));
.notNull()),
TestSetSpec.forFunction(
BuiltInFunctionDefinitions.ROW, "with aliased fields using .as()")
.onFieldsWithData(100, "abc", 75.50)
.andDataTypes(DataTypes.INT(), DataTypes.STRING(), DataTypes.DOUBLE())
.testTableApiResult(
row($("f0").as("a"), $("f1").as("b"), $("f2").as("c")),
Row.of(100, "abc", 75.50),
DataTypes.ROW(
DataTypes.FIELD("a", DataTypes.INT()),
DataTypes.FIELD("b", DataTypes.STRING()),
DataTypes.FIELD("c", DataTypes.DOUBLE()))
.notNull())
.testTableApiResult(
row($("f0").as("a"), $("f1").as("b"), $("f2").as("c")).get("a"),
100,
DataTypes.INT()));
}

// --------------------------------------------------------------------------------------------
Expand Down