Search in sources :

Example 21 with Expression

use of org.apache.flink.table.expressions.Expression 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)

Example 22 with Expression

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

the class OperationTreeBuilder method tableAggregate.

public QueryOperation tableAggregate(List<Expression> groupingExpressions, 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
    ExpressionResolver resolver = getAggResolver(child, groupingExpressions);
    List<ResolvedExpression> resolvedGroupings = resolver.resolve(newGroupingExpressions);
    Tuple2<ResolvedExpression, List<String>> resolvedFunctionAndAlias = aggregateOperationFactory.extractTableAggFunctionAndAliases(resolveSingleExpression(tableAggFunction, resolver));
    // Step3: create table agg operation
    QueryOperation tableAggOperation = aggregateOperationFactory.createAggregate(resolvedGroupings, Collections.singletonList(resolvedFunctionAndAlias.f0), child);
    // Step4: add a top project to alias the output fields of the table aggregate.
    return aliasBackwardFields(tableAggOperation, resolvedFunctionAndAlias.f1, groupingExpressions.size());
}
Also used : 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 23 with Expression

use of org.apache.flink.table.expressions.Expression 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 24 with Expression

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

the class ResolveSqlCallRule method apply.

@Override
public List<Expression> apply(List<Expression> expression, ResolutionContext context) {
    // only the top-level expressions may access the output data type
    final LogicalType outputType = context.getOutputDataType().map(DataType::getLogicalType).orElse(null);
    final TranslateSqlCallsVisitor visitor = new TranslateSqlCallsVisitor(context, outputType);
    return expression.stream().map(expr -> expr.accept(visitor)).collect(Collectors.toList());
}
Also used : DataType(org.apache.flink.table.types.DataType) SqlCallExpression(org.apache.flink.table.expressions.SqlCallExpression) Expression(org.apache.flink.table.expressions.Expression) RowType(org.apache.flink.table.types.logical.RowType) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) UnresolvedCallExpression(org.apache.flink.table.expressions.UnresolvedCallExpression) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) SqlExpressionResolver(org.apache.flink.table.expressions.resolver.SqlExpressionResolver) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) RowField(org.apache.flink.table.types.logical.RowType.RowField) Internal(org.apache.flink.annotation.Internal) Nullable(javax.annotation.Nullable) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 25 with Expression

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

the class TableImpl method addColumnsOperation.

private Table addColumnsOperation(boolean replaceIfExist, List<Expression> fields) {
    List<Expression> expressionsWithResolvedCalls = preprocessExpressions(fields);
    CategorizedExpressions extracted = OperationExpressionsUtils.extractAggregationsAndProperties(expressionsWithResolvedCalls);
    List<Expression> aggNames = extracted.getAggregations();
    if (!aggNames.isEmpty()) {
        throw new ValidationException("The added field expression cannot be an aggregation, found: " + aggNames.get(0));
    }
    return createTable(operationTreeBuilder.addColumns(replaceIfExist, expressionsWithResolvedCalls, operationTree));
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) Expression(org.apache.flink.table.expressions.Expression) CategorizedExpressions(org.apache.flink.table.operations.utils.OperationExpressionsUtils.CategorizedExpressions)

Aggregations

Expression (org.apache.flink.table.expressions.Expression)33 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)18 ArrayList (java.util.ArrayList)13 CallExpression (org.apache.flink.table.expressions.CallExpression)12 UnresolvedCallExpression (org.apache.flink.table.expressions.UnresolvedCallExpression)12 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)11 UnresolvedReferenceExpression (org.apache.flink.table.expressions.UnresolvedReferenceExpression)9 RexNode (org.apache.calcite.rex.RexNode)8 ValidationException (org.apache.flink.table.api.ValidationException)8 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)8 SqlExpressionResolver (org.apache.flink.table.expressions.resolver.SqlExpressionResolver)8 QueryOperation (org.apache.flink.table.operations.QueryOperation)8 List (java.util.List)7 TableException (org.apache.flink.table.api.TableException)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)6 Collectors (java.util.stream.Collectors)5 DistinctQueryOperation (org.apache.flink.table.operations.DistinctQueryOperation)5 FilterQueryOperation (org.apache.flink.table.operations.FilterQueryOperation)5 ValuesQueryOperation (org.apache.flink.table.operations.ValuesQueryOperation)5 HashSet (java.util.HashSet)3