Search in sources :

Example 11 with ValueLiteralExpression

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();
    }
}
Also used : ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) CollectionDataType(org.apache.flink.table.types.CollectionDataType) LogicalType(org.apache.flink.table.types.logical.LogicalType) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) CallExpression(org.apache.flink.table.expressions.CallExpression)

Example 12 with ValueLiteralExpression

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);
}
Also used : ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ValidationException(org.apache.flink.table.api.ValidationException) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 13 with ValueLiteralExpression

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.");
    }
}
Also used : ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ValidationException(org.apache.flink.table.api.ValidationException) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 14 with ValueLiteralExpression

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);
}
Also used : ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) RexNode(org.apache.calcite.rex.RexNode)

Example 15 with ValueLiteralExpression

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);
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) LinkedList(java.util.LinkedList) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)18 TableException (org.apache.flink.table.api.TableException)9 CallExpression (org.apache.flink.table.expressions.CallExpression)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)6 SessionGroupWindow (org.apache.flink.table.planner.plan.logical.SessionGroupWindow)6 SlidingGroupWindow (org.apache.flink.table.planner.plan.logical.SlidingGroupWindow)6 TumblingGroupWindow (org.apache.flink.table.planner.plan.logical.TumblingGroupWindow)6 RexNode (org.apache.calcite.rex.RexNode)5 ValidationException (org.apache.flink.table.api.ValidationException)4 Expression (org.apache.flink.table.expressions.Expression)3 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)3 LogicalType (org.apache.flink.table.types.logical.LogicalType)3 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 NamedWindowProperty (org.apache.flink.table.runtime.groupwindow.NamedWindowProperty)2 ImmutableList (com.google.common.collect.ImmutableList)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1