use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class ValuesOperationFactory method convertToExpectedType.
private Optional<ResolvedExpression> convertToExpectedType(ResolvedExpression sourceExpression, DataType targetDataType, ExpressionResolver.PostResolverFactory postResolverFactory) {
LogicalType sourceLogicalType = sourceExpression.getOutputDataType().getLogicalType();
LogicalType targetLogicalType = targetDataType.getLogicalType();
// if the expression is a literal try converting the literal in place instead of casting
if (sourceExpression instanceof ValueLiteralExpression) {
// Assign a type to a null literal
if (sourceLogicalType.is(NULL)) {
return Optional.of(valueLiteral(null, targetDataType));
}
// Check if the source value class is a valid input conversion class of the target type
// It may happen that a user wanted to use a secondary input conversion class as a value
// for
// a different type than what we derived.
//
// Example: we interpreted 1L as BIGINT, but user wanted to interpret it as a TIMESTAMP
// In this case long is a valid conversion class for TIMESTAMP, but a
// cast from BIGINT to TIMESTAMP is an invalid operation.
Optional<Object> value = ((ValueLiteralExpression) sourceExpression).getValueAs(Object.class);
if (value.isPresent() && targetLogicalType.supportsInputConversion(value.get().getClass())) {
ValueLiteralExpression convertedLiteral = valueLiteral(value.get(), targetDataType.notNull().bridgedTo(value.get().getClass()));
if (targetLogicalType.isNullable()) {
return Optional.of(postResolverFactory.cast(convertedLiteral, targetDataType));
} else {
return Optional.of(convertedLiteral);
}
}
}
if (sourceExpression instanceof CallExpression) {
FunctionDefinition functionDefinition = ((CallExpression) sourceExpression).getFunctionDefinition();
if (functionDefinition == BuiltInFunctionDefinitions.ROW && targetLogicalType.is(ROW)) {
return convertRowToExpectedType(sourceExpression, (FieldsDataType) targetDataType, postResolverFactory);
} else if (functionDefinition == BuiltInFunctionDefinitions.ARRAY && targetLogicalType.is(ARRAY)) {
return convertArrayToExpectedType(sourceExpression, (CollectionDataType) targetDataType, postResolverFactory);
} else if (functionDefinition == BuiltInFunctionDefinitions.MAP && targetLogicalType.is(MAP)) {
return convertMapToExpectedType(sourceExpression, (KeyValueDataType) targetDataType, postResolverFactory);
}
}
// might know that a certain function will not produce nullable values for a given input
if (supportsExplicitCast(sourceLogicalType.copy(true), targetLogicalType.copy(true))) {
return Optional.of(postResolverFactory.cast(sourceExpression, targetDataType));
} else {
return Optional.empty();
}
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class AggregateOperationFactory method validateAndCreateSlideWindow.
private ResolvedGroupWindow validateAndCreateSlideWindow(SlideWithSizeAndSlideOnTimeWithAlias window, String windowName, FieldReferenceExpression timeField) {
ValueLiteralExpression windowSize = getAsValueLiteral(window.getSize(), "A sliding window expects a size value literal.");
ValueLiteralExpression windowSlide = getAsValueLiteral(window.getSlide(), "A sliding window expects a slide value literal.");
final LogicalType timeFieldType = timeField.getOutputDataType().getLogicalType();
final LogicalType windowSizeType = windowSize.getOutputDataType().getLogicalType();
final LogicalType windowSlideType = windowSlide.getOutputDataType().getLogicalType();
if (!windowSizeType.is(BIGINT) && !windowSizeType.is(INTERVAL_DAY_TIME)) {
throw new ValidationException("A sliding window expects a size literal of a day-time interval or BIGINT type.");
}
if (!windowSizeType.equals(windowSlideType)) {
throw new ValidationException("A sliding window expects the same type of size and slide.");
}
validateWindowIntervalType(timeFieldType, windowSizeType);
return ResolvedGroupWindow.slidingWindow(windowName, timeField, windowSize, windowSlide);
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class AggregateOperationFactory method validateAndCreateTumbleWindow.
private ResolvedGroupWindow validateAndCreateTumbleWindow(TumbleWithSizeOnTimeWithAlias window, String windowName, FieldReferenceExpression timeField) {
ValueLiteralExpression windowSize = getAsValueLiteral(window.getSize(), "A tumble window expects a size value literal.");
final LogicalType timeFieldType = timeField.getOutputDataType().getLogicalType();
final LogicalType windowSizeType = windowSize.getOutputDataType().getLogicalType();
if (windowSizeType.isAnyOf(BIGINT, INTERVAL_DAY_TIME)) {
validateWindowIntervalType(timeFieldType, windowSizeType);
return ResolvedGroupWindow.tumblingWindow(windowName, timeField, windowSize);
} else {
throw new ValidationException("Tumbling window expects a size literal of a day-time interval or BIGINT type.");
}
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class GetConverter method convert.
@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
checkArgumentNumber(call, 2);
RexNode child = context.toRexNode(call.getChildren().get(0));
ValueLiteralExpression keyLiteral = (ValueLiteralExpression) call.getChildren().get(1);
Optional<Integer> indexOptional = ExpressionUtils.extractValue(keyLiteral, String.class).map(child.getType().getFieldNames()::indexOf);
int index = indexOptional.orElseGet(() -> extractValue(keyLiteral, Integer.class));
return context.getRelBuilder().getRexBuilder().makeFieldAccess(child, index);
}
use of org.apache.flink.table.expressions.ValueLiteralExpression in project flink by apache.
the class JsonQueryConverter method convert.
@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
checkArgumentNumber(call, 5);
final List<RexNode> operands = new LinkedList<>();
operands.add(context.toRexNode(call.getChildren().get(0)));
operands.add(context.toRexNode(call.getChildren().get(1)));
final Enum<?> wrappingBehavior = ((ValueLiteralExpression) call.getChildren().get(2)).getValueAs(JsonQueryWrapper.class).map(SymbolUtil::commonToCalcite).orElseThrow(() -> new TableException("Missing argument for wrapping behavior."));
final Enum<?> onEmpty = ((ValueLiteralExpression) call.getChildren().get(3)).getValueAs(JsonQueryOnEmptyOrError.class).map(SymbolUtil::commonToCalcite).orElseThrow(() -> new TableException("Missing argument for ON EMPTY behavior."));
final Enum<?> onError = ((ValueLiteralExpression) call.getChildren().get(4)).getValueAs(JsonQueryOnEmptyOrError.class).map(SymbolUtil::commonToCalcite).orElseThrow(() -> new TableException("Missing argument for ON ERROR behavior."));
operands.add(context.getRelBuilder().getRexBuilder().makeFlag(wrappingBehavior));
operands.add(context.getRelBuilder().getRexBuilder().makeFlag(onEmpty));
operands.add(context.getRelBuilder().getRexBuilder().makeFlag(onError));
return context.getRelBuilder().getRexBuilder().makeCall(FlinkSqlOperatorTable.JSON_QUERY, operands);
}
Aggregations