Search in sources :

Example 11 with ExpressionResolver

use of org.apache.flink.table.expressions.resolver.ExpressionResolver 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 12 with ExpressionResolver

use of org.apache.flink.table.expressions.resolver.ExpressionResolver 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 13 with ExpressionResolver

use of org.apache.flink.table.expressions.resolver.ExpressionResolver 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 14 with ExpressionResolver

use of org.apache.flink.table.expressions.resolver.ExpressionResolver 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 15 with ExpressionResolver

use of org.apache.flink.table.expressions.resolver.ExpressionResolver in project flink by apache.

the class FilterPushDownSpec method apply.

public static SupportsFilterPushDown.Result apply(List<RexNode> predicates, DynamicTableSource tableSource, SourceAbilityContext context) {
    if (tableSource instanceof SupportsFilterPushDown) {
        RexNodeToExpressionConverter converter = new RexNodeToExpressionConverter(new RexBuilder(FlinkTypeFactory.INSTANCE()), context.getSourceRowType().getFieldNames().toArray(new String[0]), context.getFunctionCatalog(), context.getCatalogManager(), TimeZone.getTimeZone(context.getTableConfig().getLocalTimeZone()));
        List<Expression> filters = predicates.stream().map(p -> {
            scala.Option<ResolvedExpression> expr = p.accept(converter);
            if (expr.isDefined()) {
                return expr.get();
            } else {
                throw new TableException(String.format("%s can not be converted to Expression, please make sure %s can accept %s.", p.toString(), tableSource.getClass().getSimpleName(), p.toString()));
            }
        }).collect(Collectors.toList());
        ExpressionResolver resolver = ExpressionResolver.resolverFor(context.getTableConfig(), name -> Optional.empty(), context.getFunctionCatalog().asLookup(str -> {
            throw new TableException("We should not need to lookup any expressions at this point");
        }), context.getCatalogManager().getDataTypeFactory(), (sqlExpression, inputRowType, outputType) -> {
            throw new TableException("SQL expression parsing is not supported at this location.");
        }).build();
        return ((SupportsFilterPushDown) tableSource).applyFilters(resolver.resolve(filters));
    } else {
        throw new TableException(String.format("%s does not support SupportsFilterPushDown.", tableSource.getClass().getName()));
    }
}
Also used : DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) FlinkRexUtil(org.apache.flink.table.planner.plan.utils.FlinkRexUtil) JsonCreator(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCreator) RexBuilder(org.apache.calcite.rex.RexBuilder) TimeZone(java.util.TimeZone) TableException(org.apache.flink.table.api.TableException) Expression(org.apache.flink.table.expressions.Expression) FlinkTypeFactory(org.apache.flink.table.planner.calcite.FlinkTypeFactory) RowType(org.apache.flink.table.types.logical.RowType) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) Collectors(java.util.stream.Collectors) JsonProperty(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty) ArrayList(java.util.ArrayList) Objects(java.util.Objects) List(java.util.List) RexNode(org.apache.calcite.rex.RexNode) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) JavaScalaConversionUtil(org.apache.flink.table.planner.utils.JavaScalaConversionUtil) Optional(java.util.Optional) RexNodeToExpressionConverter(org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter) Preconditions.checkNotNull(org.apache.flink.util.Preconditions.checkNotNull) JsonTypeName(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonTypeName) TableException(org.apache.flink.table.api.TableException) RexNodeToExpressionConverter(org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter) Expression(org.apache.flink.table.expressions.Expression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) RexBuilder(org.apache.calcite.rex.RexBuilder) SupportsFilterPushDown(org.apache.flink.table.connector.source.abilities.SupportsFilterPushDown) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver)

Aggregations

ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)15 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)15 SqlExpressionResolver (org.apache.flink.table.expressions.resolver.SqlExpressionResolver)14 Expression (org.apache.flink.table.expressions.Expression)8 UnresolvedCallExpression (org.apache.flink.table.expressions.UnresolvedCallExpression)7 FilterQueryOperation (org.apache.flink.table.operations.FilterQueryOperation)7 ArrayList (java.util.ArrayList)6 DistinctQueryOperation (org.apache.flink.table.operations.DistinctQueryOperation)6 QueryOperation (org.apache.flink.table.operations.QueryOperation)6 ValuesQueryOperation (org.apache.flink.table.operations.ValuesQueryOperation)6 List (java.util.List)3 ResolvedGroupWindow (org.apache.flink.table.operations.WindowAggregateQueryOperation.ResolvedGroupWindow)3 ValidationException (org.apache.flink.table.api.ValidationException)2 HashSet (java.util.HashSet)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 TimeZone (java.util.TimeZone)1 Collectors (java.util.stream.Collectors)1 RexBuilder (org.apache.calcite.rex.RexBuilder)1 RexNode (org.apache.calcite.rex.RexNode)1