Search in sources :

Example 11 with Expression

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

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

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

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

the class AvgAggFunction method getValueExpression.

/**
 * If all input are nulls, count will be 0 and we will get null after the division.
 */
@Override
public Expression getValueExpression() {
    Expression ifTrue = nullOf(getResultType());
    Expression ifFalse = cast(div(sum, count), typeLiteral(getResultType()));
    return ifThenElse(equalTo(count, literal(0L)), ifTrue, ifFalse);
}
Also used : UnresolvedReferenceExpression(org.apache.flink.table.expressions.UnresolvedReferenceExpression) Expression(org.apache.flink.table.expressions.Expression) UnresolvedCallExpression(org.apache.flink.table.expressions.UnresolvedCallExpression)

Example 15 with Expression

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

the class MapConverter method convert.

@Override
public RexNode convert(CallExpression call, CallExpressionConvertRule.ConvertContext context) {
    List<Expression> children = call.getChildren();
    checkArgument(call, !children.isEmpty() && children.size() % 2 == 0);
    List<RexNode> childrenRexNode = toRexNodes(context, children);
    RelDataType mapType = context.getTypeFactory().createFieldTypeFromLogicalType(call.getOutputDataType().getLogicalType());
    return context.getRelBuilder().getRexBuilder().makeCall(mapType, FlinkSqlOperatorTable.MAP_VALUE_CONSTRUCTOR, childrenRexNode);
}
Also used : CallExpression(org.apache.flink.table.expressions.CallExpression) Expression(org.apache.flink.table.expressions.Expression) RelDataType(org.apache.calcite.rel.type.RelDataType) 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