Search in sources :

Example 1 with ResolvedExpression

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

the class DefaultSchemaResolver method resolveWatermarkSpecs.

private List<WatermarkSpec> resolveWatermarkSpecs(List<UnresolvedWatermarkSpec> unresolvedWatermarkSpecs, List<Column> inputColumns) {
    if (unresolvedWatermarkSpecs.size() == 0) {
        return Collections.emptyList();
    }
    if (unresolvedWatermarkSpecs.size() > 1) {
        throw new ValidationException("Multiple watermark definitions are not supported yet.");
    }
    final UnresolvedWatermarkSpec watermarkSpec = unresolvedWatermarkSpecs.get(0);
    // validate time attribute
    final String timeColumn = watermarkSpec.getColumnName();
    final Column validatedTimeColumn = validateTimeColumn(timeColumn, inputColumns);
    // resolve watermark expression
    final ResolvedExpression watermarkExpression;
    try {
        watermarkExpression = resolveExpression(inputColumns, watermarkSpec.getWatermarkExpression(), validatedTimeColumn.getDataType());
    } catch (Exception e) {
        throw new ValidationException(String.format("Invalid expression for watermark '%s'.", watermarkSpec.toString()), e);
    }
    final LogicalType outputType = watermarkExpression.getOutputDataType().getLogicalType();
    final LogicalType timeColumnType = validatedTimeColumn.getDataType().getLogicalType();
    validateWatermarkExpression(outputType);
    if (outputType.getTypeRoot() != timeColumnType.getTypeRoot()) {
        throw new ValidationException(String.format("The watermark declaration's output data type '%s' is different " + "from the time field's data type '%s'.", outputType, timeColumnType));
    }
    return Collections.singletonList(WatermarkSpec.of(watermarkSpec.getColumnName(), watermarkExpression));
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) PhysicalColumn(org.apache.flink.table.catalog.Column.PhysicalColumn) UnresolvedComputedColumn(org.apache.flink.table.api.Schema.UnresolvedComputedColumn) MetadataColumn(org.apache.flink.table.catalog.Column.MetadataColumn) ComputedColumn(org.apache.flink.table.catalog.Column.ComputedColumn) UnresolvedMetadataColumn(org.apache.flink.table.api.Schema.UnresolvedMetadataColumn) UnresolvedPhysicalColumn(org.apache.flink.table.api.Schema.UnresolvedPhysicalColumn) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) DataTypeUtils.replaceLogicalType(org.apache.flink.table.types.utils.DataTypeUtils.replaceLogicalType) LogicalType(org.apache.flink.table.types.logical.LogicalType) UnresolvedWatermarkSpec(org.apache.flink.table.api.Schema.UnresolvedWatermarkSpec) ValidationException(org.apache.flink.table.api.ValidationException)

Example 2 with ResolvedExpression

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

Example 3 with ResolvedExpression

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

the class ProjectionOperationFactory method validateAndGetUniqueNames.

private String[] validateAndGetUniqueNames(List<ResolvedExpression> namedExpressions) {
    // we need to maintain field names order to match with types
    final Set<String> names = new LinkedHashSet<>();
    extractNames(namedExpressions).stream().map(name -> name.orElseThrow(() -> new TableException("Could not name a field in a projection."))).forEach(name -> {
        if (!names.add(name)) {
            throw new ValidationException("Ambiguous column name: " + name);
        }
    });
    return names.toArray(new String[0]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IntStream(java.util.stream.IntStream) DataType(org.apache.flink.table.types.DataType) QueryOperation(org.apache.flink.table.operations.QueryOperation) CallExpression(org.apache.flink.table.expressions.CallExpression) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) Expression(org.apache.flink.table.expressions.Expression) LocalReferenceExpression(org.apache.flink.table.expressions.LocalReferenceExpression) ResolvedExpressionDefaultVisitor(org.apache.flink.table.expressions.utils.ResolvedExpressionDefaultVisitor) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) ExpressionResolver(org.apache.flink.table.expressions.resolver.ExpressionResolver) TableReferenceExpression(org.apache.flink.table.expressions.TableReferenceExpression) GET(org.apache.flink.table.functions.BuiltInFunctionDefinitions.GET) FieldReferenceExpression(org.apache.flink.table.expressions.FieldReferenceExpression) LinkedHashSet(java.util.LinkedHashSet) CAST(org.apache.flink.table.functions.BuiltInFunctionDefinitions.CAST) AS(org.apache.flink.table.functions.BuiltInFunctionDefinitions.AS) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) BuiltInFunctionDefinitions(org.apache.flink.table.functions.BuiltInFunctionDefinitions) ProjectQueryOperation(org.apache.flink.table.operations.ProjectQueryOperation) TableException(org.apache.flink.table.api.TableException) Set(java.util.Set) INTEGER(org.apache.flink.table.types.logical.LogicalTypeRoot.INTEGER) OperationExpressionsUtils.extractName(org.apache.flink.table.operations.utils.OperationExpressionsUtils.extractName) ValueLiteralExpression(org.apache.flink.table.expressions.ValueLiteralExpression) Collectors(java.util.stream.Collectors) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) ValidationException(org.apache.flink.table.api.ValidationException) OperationExpressionsUtils.extractNames(org.apache.flink.table.operations.utils.OperationExpressionsUtils.extractNames) Optional(java.util.Optional) Internal(org.apache.flink.annotation.Internal) TableException(org.apache.flink.table.api.TableException) ValidationException(org.apache.flink.table.api.ValidationException)

Example 4 with ResolvedExpression

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

the class ValuesOperationFactory method convertArrayToExpectedType.

private Optional<ResolvedExpression> convertArrayToExpectedType(ResolvedExpression sourceExpression, CollectionDataType targetDataType, ExpressionResolver.PostResolverFactory postResolverFactory) {
    DataType elementTargetDataType = targetDataType.getElementDataType();
    List<ResolvedExpression> resolvedChildren = sourceExpression.getResolvedChildren();
    ResolvedExpression[] castedChildren = new ResolvedExpression[resolvedChildren.size()];
    for (int i = 0; i < resolvedChildren.size(); i++) {
        Optional<ResolvedExpression> castedChild = convertToExpectedType(resolvedChildren.get(i), elementTargetDataType, postResolverFactory);
        if (castedChild.isPresent()) {
            castedChildren[i] = castedChild.get();
        } else {
            return Optional.empty();
        }
    }
    return Optional.of(postResolverFactory.array(targetDataType, castedChildren));
}
Also used : ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) DataType(org.apache.flink.table.types.DataType) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) FieldsDataType(org.apache.flink.table.types.FieldsDataType) CollectionDataType(org.apache.flink.table.types.CollectionDataType)

Example 5 with ResolvedExpression

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

the class ValuesOperationFactory method extractLogicalTypesAtPosition.

private List<LogicalType> extractLogicalTypesAtPosition(List<List<ResolvedExpression>> resolvedRows, int rowPosition) {
    List<LogicalType> typesAtIPosition = new ArrayList<>();
    for (List<ResolvedExpression> resolvedExpression : resolvedRows) {
        LogicalType outputLogicalType = resolvedExpression.get(rowPosition).getOutputDataType().getLogicalType();
        typesAtIPosition.add(outputLogicalType);
    }
    return typesAtIPosition;
}
Also used : ArrayList(java.util.ArrayList) ResolvedExpression(org.apache.flink.table.expressions.ResolvedExpression) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Aggregations

ResolvedExpression (org.apache.flink.table.expressions.ResolvedExpression)29 ExpressionResolver (org.apache.flink.table.expressions.resolver.ExpressionResolver)15 Expression (org.apache.flink.table.expressions.Expression)13 ArrayList (java.util.ArrayList)11 SqlExpressionResolver (org.apache.flink.table.expressions.resolver.SqlExpressionResolver)11 List (java.util.List)8 QueryOperation (org.apache.flink.table.operations.QueryOperation)8 DataType (org.apache.flink.table.types.DataType)8 TableException (org.apache.flink.table.api.TableException)7 ValidationException (org.apache.flink.table.api.ValidationException)7 CallExpression (org.apache.flink.table.expressions.CallExpression)7 ValueLiteralExpression (org.apache.flink.table.expressions.ValueLiteralExpression)7 LogicalType (org.apache.flink.table.types.logical.LogicalType)7 Optional (java.util.Optional)6 Collectors (java.util.stream.Collectors)6 FilterQueryOperation (org.apache.flink.table.operations.FilterQueryOperation)6 ValuesQueryOperation (org.apache.flink.table.operations.ValuesQueryOperation)6 FieldReferenceExpression (org.apache.flink.table.expressions.FieldReferenceExpression)5 UnresolvedCallExpression (org.apache.flink.table.expressions.UnresolvedCallExpression)5 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)5