Search in sources :

Example 21 with ResolvedExpression

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

the class OperationTreeBuilder method projectInternal.

private QueryOperation projectInternal(List<Expression> projectList, QueryOperation child, boolean explicitAlias, List<OverWindow> overWindows) {
    ExpressionResolver resolver = getResolverBuilder(child).withOverWindows(overWindows).build();
    List<ResolvedExpression> projections = resolver.resolve(projectList);
    return projectionOperationFactory.create(projections, child, explicitAlias, resolver.postResolverFactory());
}
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 22 with ResolvedExpression

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

the class OperationTreeBuilder method sort.

public QueryOperation sort(List<Expression> fields, QueryOperation child) {
    ExpressionResolver resolver = getResolver(child);
    List<ResolvedExpression> resolvedFields = resolver.resolve(fields);
    return sortOperationFactory.createSort(resolvedFields, child, resolver.postResolverFactory());
}
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 23 with ResolvedExpression

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

the class OperationTreeBuilder method aggregate.

public QueryOperation aggregate(List<Expression> groupingExpressions, List<Expression> aggregates, QueryOperation child) {
    ExpressionResolver resolver = getAggResolver(child, groupingExpressions);
    List<ResolvedExpression> resolvedGroupings = resolver.resolve(groupingExpressions);
    List<ResolvedExpression> resolvedAggregates = resolver.resolve(aggregates);
    return aggregateOperationFactory.createAggregate(resolvedGroupings, resolvedAggregates, child);
}
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 24 with ResolvedExpression

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

the class AggregateOperationFactory method getValidatedTimeAttribute.

private FieldReferenceExpression getValidatedTimeAttribute(GroupWindow window, ExpressionResolver resolver) {
    List<ResolvedExpression> timeFieldExprs = resolver.resolve(singletonList(window.getTimeField()));
    if (timeFieldExprs.size() != 1) {
        throw new ValidationException("A group window only supports a single time field column.");
    }
    Expression timeFieldExpr = timeFieldExprs.get(0);
    if (!(timeFieldExpr instanceof FieldReferenceExpression)) {
        throw new ValidationException("A group window expects a time attribute for grouping.");
    }
    FieldReferenceExpression timeField = (FieldReferenceExpression) timeFieldExpr;
    final LogicalType timeFieldType = timeField.getOutputDataType().getLogicalType();
    validateTimeAttributeType(timeFieldType);
    return timeField;
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 25 with ResolvedExpression

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

the class OverConvertRule method convert.

@Override
public Optional<RexNode> convert(CallExpression call, ConvertContext context) {
    List<Expression> children = call.getChildren();
    if (call.getFunctionDefinition() == BuiltInFunctionDefinitions.OVER) {
        FlinkTypeFactory typeFactory = context.getTypeFactory();
        Expression agg = children.get(0);
        FunctionDefinition def = ((CallExpression) agg).getFunctionDefinition();
        boolean isDistinct = BuiltInFunctionDefinitions.DISTINCT == def;
        SqlAggFunction aggFunc = agg.accept(new SqlAggFunctionVisitor(context.getRelBuilder()));
        RelDataType aggResultType = typeFactory.createFieldTypeFromLogicalType(fromDataTypeToLogicalType(((ResolvedExpression) agg).getOutputDataType()));
        // assemble exprs by agg children
        List<RexNode> aggExprs = agg.getChildren().stream().map(child -> {
            if (isDistinct) {
                return context.toRexNode(child.getChildren().get(0));
            } else {
                return context.toRexNode(child);
            }
        }).collect(Collectors.toList());
        // assemble order by key
        Expression orderKeyExpr = children.get(1);
        Set<SqlKind> kinds = new HashSet<>();
        RexNode collationRexNode = createCollation(context.toRexNode(orderKeyExpr), RelFieldCollation.Direction.ASCENDING, null, kinds);
        ImmutableList<RexFieldCollation> orderKey = ImmutableList.of(new RexFieldCollation(collationRexNode, kinds));
        // assemble partition by keys
        List<RexNode> partitionKeys = children.subList(4, children.size()).stream().map(context::toRexNode).collect(Collectors.toList());
        // assemble bounds
        Expression preceding = children.get(2);
        boolean isPhysical = fromDataTypeToLogicalType(((ResolvedExpression) preceding).getOutputDataType()).is(LogicalTypeRoot.BIGINT);
        Expression following = children.get(3);
        RexWindowBound lowerBound = createBound(context, preceding, SqlKind.PRECEDING);
        RexWindowBound upperBound = createBound(context, following, SqlKind.FOLLOWING);
        // build RexOver
        return Optional.of(context.getRelBuilder().getRexBuilder().makeOver(aggResultType, aggFunc, aggExprs, partitionKeys, orderKey, lowerBound, upperBound, isPhysical, true, false, isDistinct));
    }
    return Optional.empty();
}
Also used : SqlPostfixOperator(org.apache.calcite.sql.SqlPostfixOperator) CallExpression(org.apache.flink.table.expressions.CallExpression) FlinkPlannerImpl(org.apache.flink.table.planner.calcite.FlinkPlannerImpl) OrdinalReturnTypeInference(org.apache.calcite.sql.type.OrdinalReturnTypeInference) Expression(org.apache.flink.table.expressions.Expression) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) BigDecimal(java.math.BigDecimal) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) SqlLiteral(org.apache.calcite.sql.SqlLiteral) SqlNode(org.apache.calcite.sql.SqlNode) DecimalType(org.apache.flink.table.types.logical.DecimalType) ImmutableList(com.google.common.collect.ImmutableList) RexNode(org.apache.calcite.rex.RexNode) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) SqlWindow(org.apache.calcite.sql.SqlWindow) SqlOperator(org.apache.calcite.sql.SqlOperator) ExpressionConverter.extractValue(org.apache.flink.table.planner.expressions.converter.ExpressionConverter.extractValue) RexWindowBound(org.apache.calcite.rex.RexWindowBound) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlParserPos(org.apache.calcite.sql.parser.SqlParserPos) SqlKind(org.apache.calcite.sql.SqlKind) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) BuiltInFunctionDefinitions(org.apache.flink.table.functions.BuiltInFunctionDefinitions) TableException(org.apache.flink.table.api.TableException) SqlBasicCall(org.apache.calcite.sql.SqlBasicCall) Set(java.util.Set) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) RelFieldCollation(org.apache.calcite.rel.RelFieldCollation) Collectors(java.util.stream.Collectors) List(java.util.List) LogicalTypeDataTypeConverter.fromDataTypeToLogicalType(org.apache.flink.table.runtime.types.LogicalTypeDataTypeConverter.fromDataTypeToLogicalType) Optional(java.util.Optional) SqlAggFunctionVisitor(org.apache.flink.table.planner.expressions.SqlAggFunctionVisitor) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) LogicalTypeRoot(org.apache.flink.table.types.logical.LogicalTypeRoot) RexCall(org.apache.calcite.rex.RexCall) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) SqlAggFunction(org.apache.calcite.sql.SqlAggFunction) SqlKind(org.apache.calcite.sql.SqlKind) RexFieldCollation(org.apache.calcite.rex.RexFieldCollation) SqlAggFunctionVisitor(org.apache.flink.table.planner.expressions.SqlAggFunctionVisitor) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) RexWindowBound(org.apache.calcite.rex.RexWindowBound) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) CallExpression(org.apache.flink.table.expressions.CallExpression) RexNode(org.apache.calcite.rex.RexNode) HashSet(java.util.HashSet)

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