Search in sources :

Example 16 with Expression

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

the class JsonValueConverter method getBehaviorOperands.

private List<RexNode> getBehaviorOperands(CallExpression call, CallExpressionConvertRule.ConvertContext context, SqlJsonEmptyOrError mode) {
    final int idx = getArgumentIndexForBehavior(mode);
    final SqlJsonValueEmptyOrErrorBehavior behavior = getBehavior(call, idx);
    final Expression defaultExpression = call.getChildren().get(idx + 1);
    final List<RexNode> operands = new ArrayList<>();
    operands.add(context.getRelBuilder().getRexBuilder().makeFlag(behavior));
    if (behavior == SqlJsonValueEmptyOrErrorBehavior.DEFAULT) {
        operands.add(context.toRexNode(defaultExpression));
    }
    operands.add(context.getRelBuilder().getRexBuilder().makeFlag(mode));
    return operands;
}
Also used : CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) ArrayList(java.util.ArrayList) SqlJsonValueEmptyOrErrorBehavior(org.apache.calcite.sql.SqlJsonValueEmptyOrErrorBehavior) RexNode(org.apache.calcite.rex.RexNode)

Example 17 with Expression

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

the class TrimConverter method convert.

@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
    checkArgumentNumber(call, 4);
    List<Expression> children = call.getChildren();
    ValueLiteralExpression removeLeadingExpr = (ValueLiteralExpression) children.get(0);
    Boolean removeLeading = extractValue(removeLeadingExpr, Boolean.class);
    ValueLiteralExpression removeTrailingExpr = (ValueLiteralExpression) children.get(1);
    Boolean removeTrailing = extractValue(removeTrailingExpr, Boolean.class);
    RexNode trimString = context.toRexNode(children.get(2));
    RexNode str = context.toRexNode(children.get(3));
    Enum trimMode;
    if (removeLeading && removeTrailing) {
        trimMode = SqlTrimFunction.Flag.BOTH;
    } else if (removeLeading) {
        trimMode = SqlTrimFunction.Flag.LEADING;
    } else if (removeTrailing) {
        trimMode = SqlTrimFunction.Flag.TRAILING;
    } else {
        throw new IllegalArgumentException("Unsupported trim mode.");
    }
    return context.getRelBuilder().call(FlinkSqlOperatorTable.TRIM, context.getRelBuilder().getRexBuilder().makeFlag(trimMode), trimString, str);
}
Also used : 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) RexNode(org.apache.calcite.rex.RexNode)

Example 18 with Expression

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

the class OperationTreeBuilder method aggregate.

public QueryOperation aggregate(List<Expression> groupingExpressions, Expression aggregate, QueryOperation child) {
    Expression resolvedAggregate = aggregate.accept(lookupResolver);
    ExpressionResolver resolver = getAggResolver(child, groupingExpressions);
    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
    // aggregate function in Step5.
    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: get agg table
    QueryOperation aggregateOperation = this.aggregate(newGroupingExpressions, Collections.singletonList(aggregateRenamed), child);
    // Step4: flatten the aggregate function
    List<String> aggNames = aggregateOperation.getResolvedSchema().getColumnNames();
    List<Expression> flattenedExpressions = aggNames.subList(0, groupingExpressions.size()).stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toCollection(ArrayList::new));
    flattenedExpressions.add(unresolvedCall(BuiltInFunctionDefinitions.FLATTEN, unresolvedRef(aggNames.get(aggNames.size() - 1))));
    QueryOperation flattenedProjection = this.project(flattenedExpressions, aggregateOperation);
    // Step5: add alias
    return aliasBackwardFields(flattenedProjection, aggregateWithAlias.aliases, 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) 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 19 with Expression

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

the class OperationTreeBuilder method addAliasToTheCallInAggregate.

/**
 * Add a default name to the call in the grouping expressions, e.g., groupBy(a % 5) to groupBy(a
 * % 5 as TMP_0) or make aggregate a named aggregate.
 */
private List<Expression> addAliasToTheCallInAggregate(List<String> inputFieldNames, List<Expression> expressions) {
    int attrNameCntr = 0;
    Set<String> usedFieldNames = new HashSet<>(inputFieldNames);
    List<Expression> result = new ArrayList<>();
    for (Expression groupingExpression : expressions) {
        if (groupingExpression instanceof UnresolvedCallExpression && !ApiExpressionUtils.isFunction(groupingExpression, BuiltInFunctionDefinitions.AS)) {
            String tempName = getUniqueName("TMP_" + attrNameCntr, usedFieldNames);
            attrNameCntr += 1;
            usedFieldNames.add(tempName);
            result.add(unresolvedCall(BuiltInFunctionDefinitions.AS, groupingExpression, valueLiteral(tempName)));
        } else {
            result.add(groupingExpression);
        }
    }
    return result;
}
Also used : UnresolvedCallExpression(org.apache.flink.table.expressions.UnresolvedCallExpression) 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) HashSet(java.util.HashSet)

Example 20 with Expression

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

the class OperationTreeBuilder method flatMap.

public QueryOperation flatMap(Expression tableFunctionCall, QueryOperation child) {
    final ExpressionResolver resolver = getResolverBuilder(child).build();
    final ResolvedExpression resolvedCall = resolveSingleExpression(tableFunctionCall, resolver);
    if (!isFunctionOfKind(resolvedCall, FunctionKind.TABLE)) {
        throw new ValidationException("Only a table function can be used in the flatMap operator.");
    }
    final List<String> originFieldNames = DataTypeUtils.flattenToNames(resolvedCall.getOutputDataType());
    List<String> childFields = child.getResolvedSchema().getColumnNames();
    Set<String> usedFieldNames = new HashSet<>(childFields);
    List<Expression> args = new ArrayList<>();
    for (String originFieldName : originFieldNames) {
        String resultName = getUniqueName(originFieldName, usedFieldNames);
        usedFieldNames.add(resultName);
        args.add(valueLiteral(resultName));
    }
    args.add(0, tableFunctionCall);
    Expression renamedTableFunction = unresolvedCall(BuiltInFunctionDefinitions.AS, args.toArray(new Expression[0]));
    QueryOperation joinNode = joinLateral(child, renamedTableFunction, JoinType.INNER, Optional.empty());
    QueryOperation rightNode = dropColumns(childFields.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toList()), joinNode);
    return alias(originFieldNames.stream().map(ApiExpressionUtils::unresolvedRef).collect(Collectors.toList()), rightNode);
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) 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) ArrayList(java.util.ArrayList) ApiExpressionUtils(org.apache.flink.table.expressions.ApiExpressionUtils) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) SqlExpressionResolver(org.apache.flink.table.expressions.resolver.SqlExpressionResolver) HashSet(java.util.HashSet) 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

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