Search in sources :

Example 26 with Expression

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

the class RankLikeAggFunctionBase method orderKeyEqualsExpression.

protected Expression orderKeyEqualsExpression() {
    Expression[] orderKeyEquals = new Expression[orderKeyTypes.length];
    for (int i = 0; i < orderKeyTypes.length; ++i) {
        // pseudo code:
        // if (lastValue_i is null) {
        // if (operand(i) is null) true else false
        // } else {
        // lastValue_i equalTo orderKey(i)
        // }
        Expression lasValue = lastValues[i];
        orderKeyEquals[i] = ifThenElse(isNull(lasValue), ifThenElse(isNull(operand(i)), literal(true), literal(false)), equalTo(lasValue, operand(i)));
    }
    Optional<Expression> ret = Arrays.stream(orderKeyEquals).reduce(ExpressionBuilder::and);
    return ret.orElseGet(() -> literal(true));
}
Also used : UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) Expression(org.apache.flink.table.expressions.Expression) ExpressionBuilder(org.apache.flink.table.planner.expressions.ExpressionBuilder)

Example 27 with Expression

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

Example 28 with Expression

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

the class InConverter method convert.

@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
    checkArgument(call, call.getChildren().size() > 1);
    Expression headExpr = call.getChildren().get(1);
    if (headExpr instanceof TableReferenceExpression) {
        QueryOperation tableOperation = ((TableReferenceExpression) headExpr).getQueryOperation();
        RexNode child = context.toRexNode(call.getChildren().get(0));
        return RexSubQuery.in(((FlinkRelBuilder) context.getRelBuilder()).queryOperation(tableOperation).build(), ImmutableList.of(child));
    } else {
        List<RexNode> child = toRexNodes(context, call.getChildren());
        return context.getRelBuilder().getRexBuilder().makeIn(child.get(0), child.subList(1, child.size()));
    }
}
Also used : CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) TableReferenceExpression(org.apache.flink.table.expressions.TableReferenceExpression) FlinkRelBuilder(org.apache.flink.table.planner.calcite.FlinkRelBuilder) TableReferenceExpression(org.apache.flink.table.expressions.TableReferenceExpression) QueryOperation(org.apache.flink.table.operations.QueryOperation) RexNode(org.apache.calcite.rex.RexNode)

Example 29 with Expression

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

the class JsonObjectConverter method convert.

@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
    checkArgument(call, (call.getChildren().size() - 1) % 2 == 0);
    final List<RexNode> operands = new LinkedList<>();
    final SqlJsonConstructorNullClause onNull = JsonConverterUtil.getOnNullArgument(call, 0);
    operands.add(context.getRelBuilder().getRexBuilder().makeFlag(onNull));
    for (int i = 1; i < call.getChildren().size(); i++) {
        final Expression operand = call.getChildren().get(i);
        if (i % 2 == 1 && !(operand instanceof ValueLiteralExpression)) {
            throw new TableException(String.format("Argument at position %s must be a string literal.", i));
        }
        operands.add(context.toRexNode(operand));
    }
    return context.getRelBuilder().getRexBuilder().makeCall(FlinkSqlOperatorTable.JSON_OBJECT, operands);
}
Also used : TableException(org.apache.flink.table.api.TableException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) SqlJsonConstructorNullClause(org.apache.calcite.sql.SqlJsonConstructorNullClause) LinkedList(java.util.LinkedList) RexNode(org.apache.calcite.rex.RexNode)

Example 30 with Expression

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

the class FilterUtils method isRetainedAfterApplyingFilterPredicates.

public static boolean isRetainedAfterApplyingFilterPredicates(List<ResolvedExpression> predicates, Function<String, Comparable<?>> getter) {
    for (ResolvedExpression predicate : predicates) {
        if (predicate instanceof CallExpression) {
            FunctionDefinition definition = ((CallExpression) predicate).getFunctionDefinition();
            boolean result = false;
            if (definition.equals(BuiltInFunctionDefinitions.OR)) {
                // nested filter, such as (key1 > 2 or key2 > 3)
                for (Expression expr : predicate.getChildren()) {
                    if (!(expr instanceof CallExpression && expr.getChildren().size() == 2)) {
                        throw new TableException(expr + " not supported!");
                    }
                    result = binaryFilterApplies((CallExpression) expr, getter);
                    if (result) {
                        break;
                    }
                }
            } else if (predicate.getChildren().size() == 2) {
                result = binaryFilterApplies((CallExpression) predicate, getter);
            } else {
                throw new UnsupportedOperationException(String.format("Unsupported expr: %s.", predicate));
            }
            if (!result) {
                return false;
            }
        } else {
            throw new UnsupportedOperationException(String.format("Unsupported expr: %s.", predicate));
        }
    }
    return true;
}
Also used : TableException(org.apache.flink.table.api.TableException) CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) CallExpression(org.apache.flink.table.expressions.CallExpression)

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