Search in sources :

Example 1 with ResolvedGroupWindow

use of org.apache.flink.table.operations.WindowAggregateQueryOperation.ResolvedGroupWindow 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)

Example 2 with ResolvedGroupWindow

use of org.apache.flink.table.operations.WindowAggregateQueryOperation.ResolvedGroupWindow in project flink by apache.

the class OperationTreeBuilder method windowAggregate.

public QueryOperation windowAggregate(List<Expression> groupingExpressions, GroupWindow window, List<Expression> windowProperties, List<Expression> aggregates, QueryOperation child) {
    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(groupingExpressions);
    List<ResolvedExpression> convertedAggregates = resolverWithWindowReferences.resolve(aggregates);
    List<ResolvedExpression> convertedProperties = resolverWithWindowReferences.resolve(windowProperties);
    return aggregateOperationFactory.createWindowAggregate(convertedGroupings, convertedAggregates, convertedProperties, resolvedWindow, child);
}
Also used : ResolvedGroupWindow(org.apache.flink.table.operations.WindowAggregateQueryOperation.ResolvedGroupWindow) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) SqlExpressionResolver(org.apache.flink.table.expressions.resolver.SqlExpressionResolver)

Example 3 with ResolvedGroupWindow

use of org.apache.flink.table.operations.WindowAggregateQueryOperation.ResolvedGroupWindow in project flink by apache.

the class OperationTreeBuilder method windowAggregate.

public QueryOperation windowAggregate(List<Expression> groupingExpressions, GroupWindow window, List<Expression> windowProperties, Expression aggregateFunction, QueryOperation child) {
    ExpressionResolver resolver = getAggResolver(child, groupingExpressions);
    Expression resolvedAggregate = aggregateFunction.accept(lookupResolver);
    AggregateWithAlias aggregateWithAlias = resolvedAggregate.accept(new ExtractAliasAndAggregate(true, resolver));
    List<Expression> groupsAndAggregate = new ArrayList<>(groupingExpressions);
    groupsAndAggregate.add(aggregateWithAlias.aggregate);
    List<Expression> namedGroupsAndAggregate = addAliasToTheCallInAggregate(child.getResolvedSchema().getColumnNames(), groupsAndAggregate);
    // 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 Step6.
    List<Expression> newGroupingExpressions = namedGroupsAndAggregate.subList(0, groupingExpressions.size());
    // Step2: turn agg to a named agg, because it will be verified later.
    Expression aggregateRenamed = namedGroupsAndAggregate.get(groupingExpressions.size());
    // Step3: resolve expressions, including grouping, aggregates and window properties.
    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(aggregateRenamed));
    List<ResolvedExpression> convertedProperties = resolverWithWindowReferences.resolve(windowProperties);
    // Step4: create window agg operation
    QueryOperation aggregateOperation = aggregateOperationFactory.createWindowAggregate(convertedGroupings, Collections.singletonList(convertedAggregates.get(0)), convertedProperties, resolvedWindow, child);
    // Step5: flatten the aggregate function
    List<String> aggNames = aggregateOperation.getResolvedSchema().getColumnNames();
    List<Expression> flattenedExpressions = aggNames.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toCollection(ArrayList::new));
    flattenedExpressions.set(groupingExpressions.size(), unresolvedCall(BuiltInFunctionDefinitions.FLATTEN, unresolvedRef(aggNames.get(groupingExpressions.size()))));
    QueryOperation flattenedProjection = this.project(flattenedExpressions, aggregateOperation);
    // window attribute.
    return aliasBackwardFields(flattenedProjection, aggregateWithAlias.aliases, 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) ArrayList(java.util.ArrayList) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) 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)3 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)3 SqlExpressionResolver (org.apache.flink.table.expressions.resolver.SqlExpressionResolver)3 ResolvedGroupWindow (org.apache.flink.table.operations.WindowAggregateQueryOperation.ResolvedGroupWindow)3 ArrayList (java.util.ArrayList)2 Expression (org.apache.flink.table.expressions.Expression)2 UnresolvedCallExpression (org.apache.flink.table.expressions.UnresolvedCallExpression)2 DistinctQueryOperation (org.apache.flink.table.operations.DistinctQueryOperation)2 FilterQueryOperation (org.apache.flink.table.operations.FilterQueryOperation)2 QueryOperation (org.apache.flink.table.operations.QueryOperation)2 ValuesQueryOperation (org.apache.flink.table.operations.ValuesQueryOperation)2 List (java.util.List)1