Search in sources :

Example 6 with ResolvedExpression

use of org.apache.flink.table.expressions.ResolvedExpression in project flink by apache.

the class ValuesOperationFactory method convertRowToExpectedType.

private Optional<ResolvedExpression> convertRowToExpectedType(ResolvedExpression sourceExpression, FieldsDataType targetDataType, ExpressionResolver.PostResolverFactory postResolverFactory) {
    List<DataType> targetDataTypes = targetDataType.getChildren();
    List<ResolvedExpression> resolvedChildren = sourceExpression.getResolvedChildren();
    if (resolvedChildren.size() != targetDataTypes.size()) {
        return Optional.empty();
    }
    ResolvedExpression[] castedChildren = new ResolvedExpression[resolvedChildren.size()];
    for (int i = 0; i < resolvedChildren.size(); i++) {
        boolean typesMatch = resolvedChildren.get(i).getOutputDataType().getLogicalType().equals(targetDataTypes.get(i).getLogicalType());
        if (typesMatch) {
            castedChildren[i] = resolvedChildren.get(i);
        }
        ResolvedExpression child = resolvedChildren.get(i);
        DataType targetChildDataType = targetDataTypes.get(i);
        Optional<ResolvedExpression> castedChild = convertToExpectedType(child, targetChildDataType, postResolverFactory);
        if (!castedChild.isPresent()) {
            return Optional.empty();
        } else {
            castedChildren[i] = castedChild.get();
        }
    }
    return Optional.of(postResolverFactory.row(targetDataType, castedChildren));
}
Also used : ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType)

Example 7 with ResolvedExpression

use of org.apache.flink.table.expressions.ResolvedExpression in project flink by apache.

the class ValuesOperationFactory method convertMapToExpectedType.

private Optional<ResolvedExpression> convertMapToExpectedType(ResolvedExpression sourceExpression, KeyValueDataType targetDataType, ExpressionResolver.PostResolverFactory postResolverFactory) {
    DataType keyTargetDataType = targetDataType.getKeyDataType();
    DataType valueTargetDataType = targetDataType.getValueDataType();
    List<ResolvedExpression> resolvedChildren = sourceExpression.getResolvedChildren();
    ResolvedExpression[] castedChildren = new ResolvedExpression[resolvedChildren.size()];
    for (int i = 0; i < resolvedChildren.size(); i++) {
        Optional<ResolvedExpression> castedChild = convertToExpectedType(resolvedChildren.get(i), i % 2 == 0 ? keyTargetDataType : valueTargetDataType, postResolverFactory);
        if (castedChild.isPresent()) {
            castedChildren[i] = castedChild.get();
        } else {
            return Optional.empty();
        }
    }
    return Optional.of(postResolverFactory.map(targetDataType, castedChildren));
}
Also used : ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType)

Example 8 with ResolvedExpression

use of org.apache.flink.table.expressions.ResolvedExpression in project flink by apache.

the class OperationTreeBuilder method filter.

public QueryOperation filter(Expression condition, QueryOperation child) {
    ExpressionResolver resolver = getResolver(child);
    ResolvedExpression resolvedExpression = resolveSingleExpression(condition, resolver);
    DataType conditionType = resolvedExpression.getOutputDataType();
    if (!conditionType.getLogicalType().is(BOOLEAN)) {
        throw new ValidationException("Filter operator requires a boolean expression as input," + " but $condition is of type " + conditionType);
    }
    return new FilterQueryOperation(resolvedExpression, child);
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) DataType(org.apache.flink.table.types.DataType) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) SqlExpressionResolver(org.apache.flink.table.expressions.resolver.SqlExpressionResolver) FilterQueryOperation(org.apache.flink.table.operations.FilterQueryOperation)

Example 9 with ResolvedExpression

use of org.apache.flink.table.expressions.ResolvedExpression in project flink by apache.

the class OperationTreeBuilder method join.

public QueryOperation join(QueryOperation left, QueryOperation right, JoinType joinType, Optional<Expression> condition, boolean correlated) {
    ExpressionResolver resolver = getResolver(left, right);
    Optional<ResolvedExpression> resolvedCondition = condition.map(expr -> resolveSingleExpression(expr, resolver));
    return joinOperationFactory.create(left, right, joinType, resolvedCondition.orElse(valueLiteral(true)), correlated);
}
Also used : ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) SqlExpressionResolver(org.apache.flink.table.expressions.resolver.SqlExpressionResolver)

Example 10 with ResolvedExpression

use of org.apache.flink.table.expressions.ResolvedExpression in project flink by apache.

the class OperationTreeBuilder method windowTableAggregate.

public QueryOperation windowTableAggregate(List<Expression> groupingExpressions, GroupWindow window, List<Expression> windowProperties, Expression tableAggFunction, QueryOperation child) {
    // Step1: add a default name to the call in the grouping expressions, e.g., groupBy(a % 5)
    // to
    // groupBy(a % 5 as TMP_0). We need a name for every column so that to perform alias for the
    // table aggregate function in Step4.
    List<Expression> newGroupingExpressions = addAliasToTheCallInAggregate(child.getResolvedSchema().getColumnNames(), groupingExpressions);
    // Step2: resolve expressions, including grouping, aggregates and window properties.
    ExpressionResolver resolver = getAggResolver(child, groupingExpressions);
    ResolvedGroupWindow resolvedWindow = aggregateOperationFactory.createResolvedWindow(window, resolver);
    ExpressionResolver resolverWithWindowReferences = getResolverBuilder(child).withLocalReferences(localRef(resolvedWindow.getAlias(), resolvedWindow.getTimeAttribute().getOutputDataType())).build();
    List<ResolvedExpression> convertedGroupings = resolverWithWindowReferences.resolve(newGroupingExpressions);
    List<ResolvedExpression> convertedAggregates = resolverWithWindowReferences.resolve(Collections.singletonList(tableAggFunction));
    List<ResolvedExpression> convertedProperties = resolverWithWindowReferences.resolve(windowProperties);
    Tuple2<ResolvedExpression, List<String>> resolvedFunctionAndAlias = aggregateOperationFactory.extractTableAggFunctionAndAliases(convertedAggregates.get(0));
    // Step3: create window table agg operation
    QueryOperation tableAggOperation = aggregateOperationFactory.createWindowAggregate(convertedGroupings, Collections.singletonList(resolvedFunctionAndAlias.f0), convertedProperties, resolvedWindow, child);
    // window attribute.
    return aliasBackwardFields(tableAggOperation, resolvedFunctionAndAlias.f1, groupingExpressions.size());
}
Also used : ResolvedGroupWindow(org.apache.flink.table.operations.WindowAggregateQueryOperation.ResolvedGroupWindow) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) Expression(org.apache.flink.table.expressions.Expression) UnresolvedCallExpression(org.apache.flink.table.expressions.UnresolvedCallExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) List(java.util.List) ArrayList(java.util.ArrayList) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) SqlExpressionResolver(org.apache.flink.table.expressions.resolver.SqlExpressionResolver) ValuesQueryOperation(org.apache.flink.table.operations.ValuesQueryOperation) QueryOperation(org.apache.flink.table.operations.QueryOperation) DistinctQueryOperation(org.apache.flink.table.operations.DistinctQueryOperation) FilterQueryOperation(org.apache.flink.table.operations.FilterQueryOperation)

Aggregations

ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)29 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)15 Expression (org.apache.flink.table.expressions.Expression)13 ArrayList (java.util.ArrayList)11 SqlExpressionResolver (org.apache.flink.table.expressions.resolver.SqlExpressionResolver)11 List (java.util.List)8 QueryOperation (org.apache.flink.table.operations.QueryOperation)8 DataType (org.apache.flink.table.types.DataType)8 TableException (org.apache.flink.table.api.TableException)7 ValidationException (org.apache.flink.table.api.ValidationException)7 CallExpression (org.apache.flink.table.expressions.CallExpression)7 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)7 LogicalType (org.apache.flink.table.types.logical.LogicalType)7 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 FilterQueryOperation (org.apache.flink.table.operations.FilterQueryOperation)6 ValuesQueryOperation (org.apache.flink.table.operations.ValuesQueryOperation)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)5 UnresolvedCallExpression (org.apache.flink.table.expressions.UnresolvedCallExpression)5 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)5