Search in sources :

Example 1 with UnresolvedReferenceExpression

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

the class AggregateOperationFactory method createResolvedWindow.

/**
 * Converts an API class to a resolved window for planning with expressions already resolved. It
 * performs following validations:
 *
 * <ul>
 *   <li>The alias is represented with an unresolved reference
 *   <li>The time attribute is a single field reference of a {@link
 *       TimeIndicatorTypeInfo}(stream), {@link SqlTimeTypeInfo}(batch), or {@link
 *       BasicTypeInfo#LONG_TYPE_INFO}(batch) type
 *   <li>The size & slide are value literals of either {@link BasicTypeInfo#LONG_TYPE_INFO}, or
 *       {@link TimeIntervalTypeInfo} type
 *   <li>The size & slide are of the same type
 *   <li>The gap is a value literal of a {@link TimeIntervalTypeInfo} type
 * </ul>
 *
 * @param window window to resolve
 * @param resolver resolver to resolve potential unresolved field references
 * @return window with expressions resolved
 */
ResolvedGroupWindow createResolvedWindow(GroupWindow window, ExpressionResolver resolver) {
    Expression alias = window.getAlias();
    if (!(alias instanceof UnresolvedReferenceExpression)) {
        throw new ValidationException("Only unresolved reference supported for alias of a group window.");
    }
    final String windowName = ((UnresolvedReferenceExpression) alias).getName();
    FieldReferenceExpression timeField = getValidatedTimeAttribute(window, resolver);
    if (window instanceof TumbleWithSizeOnTimeWithAlias) {
        return validateAndCreateTumbleWindow((TumbleWithSizeOnTimeWithAlias) window, windowName, timeField);
    } else if (window instanceof SlideWithSizeAndSlideOnTimeWithAlias) {
        return validateAndCreateSlideWindow((SlideWithSizeAndSlideOnTimeWithAlias) window, windowName, timeField);
    } else if (window instanceof SessionWithGapOnTimeWithAlias) {
        return validateAndCreateSessionWindow((SessionWithGapOnTimeWithAlias) window, windowName, timeField);
    } else {
        throw new TableException("Unknown window type: " + window);
    }
}
Also used : TableException(org.apache.flink.table.api.TableException) 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) TumbleWithSizeOnTimeWithAlias(org.apache.flink.table.api.TumbleWithSizeOnTimeWithAlias) SlideWithSizeAndSlideOnTimeWithAlias(org.apache.flink.table.api.SlideWithSizeAndSlideOnTimeWithAlias) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) SessionWithGapOnTimeWithAlias(org.apache.flink.table.api.SessionWithGapOnTimeWithAlias)

Example 2 with UnresolvedReferenceExpression

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

the class FieldInfoUtils method extractFieldInfoFromAtomicType.

private static List<FieldInfo> extractFieldInfoFromAtomicType(TypeInformation<?> atomicType, Expression[] exprs) {
    List<FieldInfo> fields = new ArrayList<>(exprs.length);
    boolean alreadyReferenced = false;
    for (int i = 0; i < exprs.length; i++) {
        Expression expr = exprs[i];
        if (expr instanceof UnresolvedReferenceExpression) {
            if (alreadyReferenced) {
                throw new ValidationException("Too many fields referenced from an atomic type.");
            }
            alreadyReferenced = true;
            String name = ((UnresolvedReferenceExpression) expr).getName();
            fields.add(new FieldInfo(name, i, fromLegacyInfoToDataType(atomicType)));
        } else if (isRowTimeExpression(expr)) {
            UnresolvedReferenceExpression reference = getChildAsReference(expr);
            fields.add(createTimeAttributeField(reference, TimestampKind.ROWTIME, null));
        } else if (isProcTimeExpression(expr)) {
            UnresolvedReferenceExpression reference = getChildAsReference(expr);
            fields.add(createTimeAttributeField(reference, TimestampKind.PROCTIME, null));
        } else {
            throw new ValidationException("Field reference expression expected.");
        }
    }
    return fields;
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) Expression(org.apache.flink.table.expressions.Expression) UnresolvedCallExpression(org.apache.flink.table.expressions.UnresolvedCallExpression) ArrayList(java.util.ArrayList) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression)

Example 3 with UnresolvedReferenceExpression

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

the class AliasOperationUtils method createAliasList.

/**
 * Creates a list of valid alias expressions. Resulting expression might still contain {@link
 * UnresolvedReferenceExpression}.
 *
 * @param aliases aliases to validate
 * @param child relational operation on top of which to apply the aliases
 * @return validated list of aliases
 */
static List<Expression> createAliasList(List<Expression> aliases, QueryOperation child) {
    ResolvedSchema childSchema = child.getResolvedSchema();
    if (aliases.size() > childSchema.getColumnCount()) {
        throw new ValidationException("Aliasing more fields than we actually have.");
    }
    List<ValueLiteralExpression> fieldAliases = aliases.stream().map(f -> f.accept(aliasLiteralValidator)).collect(Collectors.toList());
    List<String> childNames = childSchema.getColumnNames();
    return IntStream.range(0, childNames.size()).mapToObj(idx -> {
        UnresolvedReferenceExpression oldField = unresolvedRef(childNames.get(idx));
        if (idx < fieldAliases.size()) {
            ValueLiteralExpression alias = fieldAliases.get(idx);
            return unresolvedCall(BuiltInFunctionDefinitions.AS, oldField, alias);
        } else {
            return oldField;
        }
    }).collect(Collectors.toList());
}
Also used : IntStream(java.util.stream.IntStream) QueryOperation(org.apache.flink.table.operations.QueryOperation) ApiExpressionUtils.valueLiteral(org.apache.flink.table.expressions.ApiExpressionUtils.valueLiteral) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) ExpressionUtils(org.apache.flink.table.expressions.ExpressionUtils) ApiExpressionDefaultVisitor(org.apache.flink.table.expressions.utils.ApiExpressionDefaultVisitor) BuiltInFunctionDefinitions(org.apache.flink.table.functions.BuiltInFunctionDefinitions) ApiExpressionUtils.unresolvedCall(org.apache.flink.table.expressions.ApiExpressionUtils.unresolvedCall) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Expression(org.apache.flink.table.expressions.Expression) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) Collectors(java.util.stream.Collectors) ApiExpressionUtils.unresolvedRef(org.apache.flink.table.expressions.ApiExpressionUtils.unresolvedRef) List(java.util.List) ValidationException(org.apache.flink.table.api.ValidationException) Internal(org.apache.flink.annotation.Internal) ValidationException(org.apache.flink.table.api.ValidationException) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema)

Example 4 with UnresolvedReferenceExpression

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

the class DeclarativeExpressionResolver method defaultMethod.

@Override
protected ResolvedExpression defaultMethod(Expression expression) {
    if (expression instanceof UnresolvedReferenceExpression) {
        UnresolvedReferenceExpression expr = (UnresolvedReferenceExpression) expression;
        String name = expr.getName();
        int localIndex = ArrayUtils.indexOf(function.aggBufferAttributes(), expr);
        if (localIndex == -1) {
            // case, the input is aggregate buffers which sent by local aggregate.
            if (isMerge) {
                return toMergeInputExpr(name, ArrayUtils.indexOf(function.mergeOperands(), expr));
            } else {
                return toAccInputExpr(name, ArrayUtils.indexOf(function.operands(), expr));
            }
        } else {
            return toAggBufferExpr(name, localIndex);
        }
    } else if (expression instanceof UnresolvedCallExpression) {
        UnresolvedCallExpression unresolvedCall = (UnresolvedCallExpression) expression;
        return resolver.resolve(ApiExpressionUtils.unresolvedCall(unresolvedCall.getFunctionDefinition(), unresolvedCall.getChildren().stream().map(c -> c.accept(DeclarativeExpressionResolver.this)).collect(Collectors.toList())));
    } else if (expression instanceof ResolvedExpression) {
        return (ResolvedExpression) expression;
    } else {
        return resolver.resolve(expression);
    }
}
Also used : UnresolvedCallExpression(org.apache.flink.table.expressions.UnresolvedCallExpression) UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression)

Aggregations

UnresolvedReferenceExpression (org.apache.flink.table.expressions.UnresolvedReferenceExpression)4 ValidationException (org.apache.flink.table.api.ValidationException)3 Expression (org.apache.flink.table.expressions.Expression)3 ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)2 UnresolvedCallExpression (org.apache.flink.table.expressions.UnresolvedCallExpression)2 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Internal (org.apache.flink.annotation.Internal)1 SessionWithGapOnTimeWithAlias (org.apache.flink.table.api.SessionWithGapOnTimeWithAlias)1 SlideWithSizeAndSlideOnTimeWithAlias (org.apache.flink.table.api.SlideWithSizeAndSlideOnTimeWithAlias)1 TableException (org.apache.flink.table.api.TableException)1 TumbleWithSizeOnTimeWithAlias (org.apache.flink.table.api.TumbleWithSizeOnTimeWithAlias)1 ResolvedSchema (org.apache.flink.table.catalog.ResolvedSchema)1 ApiExpressionUtils.unresolvedCall (org.apache.flink.table.expressions.ApiExpressionUtils.unresolvedCall)1 ApiExpressionUtils.unresolvedRef (org.apache.flink.table.expressions.ApiExpressionUtils.unresolvedRef)1 ApiExpressionUtils.valueLiteral (org.apache.flink.table.expressions.ApiExpressionUtils.valueLiteral)1 CallExpression (org.apache.flink.table.expressions.CallExpression)1