Search in sources :

Example 1 with Expression

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

the class HiveTableUtil method makePartitionFilter.

/**
 * Generates a filter string for partition columns from the given filter expressions.
 *
 * @param partColOffset The number of non-partition columns -- used to shift field reference
 *     index
 * @param partColNames The names of all partition columns
 * @param expressions The filter expressions in CNF form
 * @return an Optional filter string equivalent to the expressions, which is empty if the
 *     expressions can't be handled
 */
public static Optional<String> makePartitionFilter(int partColOffset, List<String> partColNames, List<Expression> expressions, HiveShim hiveShim) {
    List<String> filters = new ArrayList<>(expressions.size());
    ExpressionExtractor extractor = new ExpressionExtractor(partColOffset, partColNames, hiveShim);
    for (Expression expression : expressions) {
        String str = expression.accept(extractor);
        if (str == null) {
            return Optional.empty();
        }
        filters.add(str);
    }
    return Optional.of(String.join(" and ", filters));
}
Also used : TypeLiteralExpression(org.apache.flink.table.expressions.TypeLiteralExpression) 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) ArrayList(java.util.ArrayList)

Example 2 with Expression

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

the class OrcFilters method convertOr.

private static Predicate convertOr(CallExpression callExp) {
    if (callExp.getChildren().size() < 2) {
        return null;
    }
    Expression left = callExp.getChildren().get(0);
    Expression right = callExp.getChildren().get(1);
    Predicate c1 = toOrcPredicate(left);
    Predicate c2 = toOrcPredicate(right);
    if (c1 == null || c2 == null) {
        return null;
    } else {
        return new Or(c1, c2);
    }
}
Also used : 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)

Example 3 with Expression

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

the class TableImpl method createTemporalTableFunction.

@Override
public TemporalTableFunction createTemporalTableFunction(Expression timeAttribute, Expression primaryKey) {
    Expression resolvedTimeAttribute = operationTreeBuilder.resolveExpression(timeAttribute, operationTree);
    Expression resolvedPrimaryKey = operationTreeBuilder.resolveExpression(primaryKey, operationTree);
    return TemporalTableFunctionImpl.create(operationTree, resolvedTimeAttribute, resolvedPrimaryKey);
}
Also used : UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) Expression(org.apache.flink.table.expressions.Expression)

Example 4 with Expression

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

the class TableImpl method select.

@Override
public Table select(Expression... fields) {
    List<Expression> expressionsWithResolvedCalls = preprocessExpressions(fields);
    CategorizedExpressions extracted = OperationExpressionsUtils.extractAggregationsAndProperties(expressionsWithResolvedCalls);
    if (!extracted.getWindowProperties().isEmpty()) {
        throw new ValidationException("Window properties can only be used on windowed tables.");
    }
    if (!extracted.getAggregations().isEmpty()) {
        QueryOperation aggregate = operationTreeBuilder.aggregate(Collections.emptyList(), extracted.getAggregations(), operationTree);
        return createTable(operationTreeBuilder.project(extracted.getProjections(), aggregate, false));
    } else {
        return createTable(operationTreeBuilder.project(expressionsWithResolvedCalls, operationTree, false));
    }
}
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) QueryOperation(org.apache.flink.table.operations.QueryOperation)

Example 5 with Expression

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

the class PushPartitionIntoTableSourceScanRule method readPartitionFromCatalogAndPrune.

private List<Map<String, String>> readPartitionFromCatalogAndPrune(RexBuilder rexBuilder, FlinkContext context, Catalog catalog, ObjectIdentifier tableIdentifier, List<String> allFieldNames, Seq<RexNode> partitionPredicate, Function<List<Map<String, String>>, List<Map<String, String>>> pruner) throws TableNotExistException, TableNotPartitionedException {
    ObjectPath tablePath = tableIdentifier.toObjectPath();
    // build filters
    RexNodeToExpressionConverter converter = new RexNodeToExpressionConverter(rexBuilder, allFieldNames.toArray(new String[0]), context.getFunctionCatalog(), context.getCatalogManager(), TimeZone.getTimeZone(context.getTableConfig().getLocalTimeZone()));
    ArrayList<Expression> partitionFilters = new ArrayList<>();
    Option<ResolvedExpression> subExpr;
    for (RexNode node : JavaConversions.seqAsJavaList(partitionPredicate)) {
        subExpr = node.accept(converter);
        if (!subExpr.isEmpty()) {
            partitionFilters.add(subExpr.get());
        } else {
            // if part of expr is unresolved, we read all partitions and prune.
            return readPartitionFromCatalogWithoutFilterAndPrune(catalog, tablePath, pruner);
        }
    }
    try {
        return catalog.listPartitionsByFilter(tablePath, partitionFilters).stream().map(CatalogPartitionSpec::getPartitionSpec).collect(Collectors.toList());
    } catch (UnsupportedOperationException e) {
        return readPartitionFromCatalogWithoutFilterAndPrune(catalog, tablePath, pruner);
    }
}
Also used : ObjectPath(org.apache.flink.table.catalog.ObjectPath) RexNodeToExpressionConverter(org.apache.flink.table.planner.plan.utils.RexNodeToExpressionConverter) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) Expression(org.apache.flink.table.expressions.Expression) ArrayList(java.util.ArrayList) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) RexNode(org.apache.calcite.rex.RexNode)

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