Search in sources :

Example 36 with Transformation

use of org.apache.flink.api.dag.Transformation in project flink by apache.

the class BatchExecSortLimit method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    if (limitEnd == Long.MAX_VALUE) {
        throw new TableException("Not support limitEnd is max value now!");
    }
    ExecEdge inputEdge = getInputEdges().get(0);
    Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    RowType inputType = (RowType) inputEdge.getOutputType();
    // generate comparator
    GeneratedRecordComparator genComparator = ComparatorCodeGenerator.gen(config.getTableConfig(), "SortLimitComparator", inputType, sortSpec);
    // TODO If input is ordered, there is no need to use the heap.
    SortLimitOperator operator = new SortLimitOperator(isGlobal, limitStart, limitEnd, genComparator);
    return ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationName(config), createTransformationDescription(config), SimpleOperatorFactory.of(operator), InternalTypeInfo.of(inputType), inputTransform.getParallelism());
}
Also used : TableException(org.apache.flink.table.api.TableException) RowData(org.apache.flink.table.data.RowData) Transformation(org.apache.flink.api.dag.Transformation) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) RowType(org.apache.flink.table.types.logical.RowType) GeneratedRecordComparator(org.apache.flink.table.runtime.generated.GeneratedRecordComparator) SortLimitOperator(org.apache.flink.table.runtime.operators.sort.SortLimitOperator)

Example 37 with Transformation

use of org.apache.flink.api.dag.Transformation in project flink by apache.

the class BatchExecHashJoin method translateToPlanInternal.

@Override
@SuppressWarnings("unchecked")
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    ExecEdge leftInputEdge = getInputEdges().get(0);
    ExecEdge rightInputEdge = getInputEdges().get(1);
    Transformation<RowData> leftInputTransform = (Transformation<RowData>) leftInputEdge.translateToPlan(planner);
    Transformation<RowData> rightInputTransform = (Transformation<RowData>) rightInputEdge.translateToPlan(planner);
    // get input types
    RowType leftType = (RowType) leftInputEdge.getOutputType();
    RowType rightType = (RowType) rightInputEdge.getOutputType();
    JoinUtil.validateJoinSpec(joinSpec, leftType, rightType, false);
    int[] leftKeys = joinSpec.getLeftKeys();
    int[] rightKeys = joinSpec.getRightKeys();
    LogicalType[] keyFieldTypes = IntStream.of(leftKeys).mapToObj(leftType::getTypeAt).toArray(LogicalType[]::new);
    RowType keyType = RowType.of(keyFieldTypes);
    GeneratedJoinCondition condFunc = JoinUtil.generateConditionFunction(config.getTableConfig(), joinSpec.getNonEquiCondition().orElse(null), leftType, rightType);
    // projection for equals
    GeneratedProjection leftProj = ProjectionCodeGenerator.generateProjection(new CodeGeneratorContext(config.getTableConfig()), "HashJoinLeftProjection", leftType, keyType, leftKeys);
    GeneratedProjection rightProj = ProjectionCodeGenerator.generateProjection(new CodeGeneratorContext(config.getTableConfig()), "HashJoinRightProjection", rightType, keyType, rightKeys);
    Transformation<RowData> buildTransform;
    Transformation<RowData> probeTransform;
    GeneratedProjection buildProj;
    GeneratedProjection probeProj;
    int[] buildKeys;
    int[] probeKeys;
    RowType buildType;
    RowType probeType;
    int buildRowSize;
    long buildRowCount;
    long probeRowCount;
    boolean reverseJoin = !leftIsBuild;
    if (leftIsBuild) {
        buildTransform = leftInputTransform;
        buildProj = leftProj;
        buildType = leftType;
        buildRowSize = estimatedLeftAvgRowSize;
        buildRowCount = estimatedLeftRowCount;
        buildKeys = leftKeys;
        probeTransform = rightInputTransform;
        probeProj = rightProj;
        probeType = rightType;
        probeRowCount = estimatedLeftRowCount;
        probeKeys = rightKeys;
    } else {
        buildTransform = rightInputTransform;
        buildProj = rightProj;
        buildType = rightType;
        buildRowSize = estimatedRightAvgRowSize;
        buildRowCount = estimatedRightRowCount;
        buildKeys = rightKeys;
        probeTransform = leftInputTransform;
        probeProj = leftProj;
        probeType = leftType;
        probeRowCount = estimatedLeftRowCount;
        probeKeys = leftKeys;
    }
    // operator
    StreamOperatorFactory<RowData> operator;
    FlinkJoinType joinType = joinSpec.getJoinType();
    HashJoinType hashJoinType = HashJoinType.of(leftIsBuild, joinType.isLeftOuter(), joinType.isRightOuter(), joinType == FlinkJoinType.SEMI, joinType == FlinkJoinType.ANTI);
    if (LongHashJoinGenerator.support(hashJoinType, keyType, joinSpec.getFilterNulls())) {
        operator = LongHashJoinGenerator.gen(config.getTableConfig(), hashJoinType, keyType, buildType, probeType, buildKeys, probeKeys, buildRowSize, buildRowCount, reverseJoin, condFunc);
    } else {
        operator = SimpleOperatorFactory.of(HashJoinOperator.newHashJoinOperator(hashJoinType, condFunc, reverseJoin, joinSpec.getFilterNulls(), buildProj, probeProj, tryDistinctBuildRow, buildRowSize, buildRowCount, probeRowCount, keyType));
    }
    long managedMemory = config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_HASH_JOIN_MEMORY).getBytes();
    return ExecNodeUtil.createTwoInputTransformation(buildTransform, probeTransform, createTransformationName(config), createTransformationDescription(config), operator, InternalTypeInfo.of(getOutputType()), probeTransform.getParallelism(), managedMemory);
}
Also used : Transformation(org.apache.flink.api.dag.Transformation) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) HashJoinType(org.apache.flink.table.runtime.operators.join.HashJoinType) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) FlinkJoinType(org.apache.flink.table.runtime.operators.join.FlinkJoinType) RowData(org.apache.flink.table.data.RowData) GeneratedJoinCondition(org.apache.flink.table.runtime.generated.GeneratedJoinCondition) GeneratedProjection(org.apache.flink.table.runtime.generated.GeneratedProjection)

Example 38 with Transformation

use of org.apache.flink.api.dag.Transformation in project flink by apache.

the class StreamGraphGenerator method transform.

/**
 * Transforms one {@code Transformation}.
 *
 * <p>This checks whether we already transformed it and exits early in that case. If not it
 * delegates to one of the transformation specific methods.
 */
private Collection<Integer> transform(Transformation<?> transform) {
    if (alreadyTransformed.containsKey(transform)) {
        return alreadyTransformed.get(transform);
    }
    LOG.debug("Transforming " + transform);
    if (transform.getMaxParallelism() <= 0) {
        // if the max parallelism hasn't been set, then first use the job wide max parallelism
        // from the ExecutionConfig.
        int globalMaxParallelismFromConfig = executionConfig.getMaxParallelism();
        if (globalMaxParallelismFromConfig > 0) {
            transform.setMaxParallelism(globalMaxParallelismFromConfig);
        }
    }
    transform.getSlotSharingGroup().ifPresent(slotSharingGroup -> {
        final ResourceSpec resourceSpec = SlotSharingGroupUtils.extractResourceSpec(slotSharingGroup);
        if (!resourceSpec.equals(ResourceSpec.UNKNOWN)) {
            slotSharingGroupResources.compute(slotSharingGroup.getName(), (name, profile) -> {
                if (profile == null) {
                    return ResourceProfile.fromResourceSpec(resourceSpec, MemorySize.ZERO);
                } else if (!ResourceProfile.fromResourceSpec(resourceSpec, MemorySize.ZERO).equals(profile)) {
                    throw new IllegalArgumentException("The slot sharing group " + slotSharingGroup.getName() + " has been configured with two different resource spec.");
                } else {
                    return profile;
                }
            });
        }
    });
    // call at least once to trigger exceptions about MissingTypeInfo
    transform.getOutputType();
    @SuppressWarnings("unchecked") final TransformationTranslator<?, Transformation<?>> translator = (TransformationTranslator<?, Transformation<?>>) translatorMap.get(transform.getClass());
    Collection<Integer> transformedIds;
    if (translator != null) {
        transformedIds = translate(translator, transform);
    } else {
        transformedIds = legacyTransform(transform);
    }
    // transforming the feedback edges
    if (!alreadyTransformed.containsKey(transform)) {
        alreadyTransformed.put(transform, transformedIds);
    }
    return transformedIds;
}
Also used : FeedbackTransformation(org.apache.flink.streaming.api.transformations.FeedbackTransformation) SourceTransformation(org.apache.flink.streaming.api.transformations.SourceTransformation) ReduceTransformation(org.apache.flink.streaming.api.transformations.ReduceTransformation) PartitionTransformation(org.apache.flink.streaming.api.transformations.PartitionTransformation) TwoInputTransformation(org.apache.flink.streaming.api.transformations.TwoInputTransformation) UnionTransformation(org.apache.flink.streaming.api.transformations.UnionTransformation) LegacySourceTransformation(org.apache.flink.streaming.api.transformations.LegacySourceTransformation) BroadcastStateTransformation(org.apache.flink.streaming.api.transformations.BroadcastStateTransformation) LegacySinkTransformation(org.apache.flink.streaming.api.transformations.LegacySinkTransformation) MultipleInputTransformation(org.apache.flink.streaming.api.transformations.MultipleInputTransformation) TimestampsAndWatermarksTransformation(org.apache.flink.streaming.api.transformations.TimestampsAndWatermarksTransformation) CoFeedbackTransformation(org.apache.flink.streaming.api.transformations.CoFeedbackTransformation) PhysicalTransformation(org.apache.flink.streaming.api.transformations.PhysicalTransformation) SinkTransformation(org.apache.flink.streaming.api.transformations.SinkTransformation) KeyedMultipleInputTransformation(org.apache.flink.streaming.api.transformations.KeyedMultipleInputTransformation) KeyedBroadcastStateTransformation(org.apache.flink.streaming.api.transformations.KeyedBroadcastStateTransformation) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) SideOutputTransformation(org.apache.flink.streaming.api.transformations.SideOutputTransformation) Transformation(org.apache.flink.api.dag.Transformation) LegacySourceTransformationTranslator(org.apache.flink.streaming.runtime.translators.LegacySourceTransformationTranslator) KeyedBroadcastStateTransformationTranslator(org.apache.flink.streaming.runtime.translators.KeyedBroadcastStateTransformationTranslator) TimestampsAndWatermarksTransformationTranslator(org.apache.flink.streaming.runtime.translators.TimestampsAndWatermarksTransformationTranslator) ReduceTransformationTranslator(org.apache.flink.streaming.runtime.translators.ReduceTransformationTranslator) LegacySinkTransformationTranslator(org.apache.flink.streaming.runtime.translators.LegacySinkTransformationTranslator) OneInputTransformationTranslator(org.apache.flink.streaming.runtime.translators.OneInputTransformationTranslator) SourceTransformationTranslator(org.apache.flink.streaming.runtime.translators.SourceTransformationTranslator) MultiInputTransformationTranslator(org.apache.flink.streaming.runtime.translators.MultiInputTransformationTranslator) BroadcastStateTransformationTranslator(org.apache.flink.streaming.runtime.translators.BroadcastStateTransformationTranslator) PartitionTransformationTranslator(org.apache.flink.streaming.runtime.translators.PartitionTransformationTranslator) TwoInputTransformationTranslator(org.apache.flink.streaming.runtime.translators.TwoInputTransformationTranslator) SinkTransformationTranslator(org.apache.flink.streaming.runtime.translators.SinkTransformationTranslator) UnionTransformationTranslator(org.apache.flink.streaming.runtime.translators.UnionTransformationTranslator) SideOutputTransformationTranslator(org.apache.flink.streaming.runtime.translators.SideOutputTransformationTranslator) ResourceSpec(org.apache.flink.api.common.operators.ResourceSpec)

Example 39 with Transformation

use of org.apache.flink.api.dag.Transformation in project flink by apache.

the class TableEnvironmentImpl method executeQueryOperation.

private TableResultInternal executeQueryOperation(QueryOperation operation) {
    CollectModifyOperation sinkOperation = new CollectModifyOperation(operation);
    List<Transformation<?>> transformations = translate(Collections.singletonList(sinkOperation));
    final String defaultJobName = "collect";
    Pipeline pipeline = execEnv.createPipeline(transformations, tableConfig.getConfiguration(), defaultJobName);
    try {
        JobClient jobClient = execEnv.executeAsync(pipeline);
        ResultProvider resultProvider = sinkOperation.getSelectResultProvider();
        resultProvider.setJobClient(jobClient);
        return TableResultImpl.builder().jobClient(jobClient).resultKind(ResultKind.SUCCESS_WITH_CONTENT).schema(operation.getResolvedSchema()).resultProvider(resultProvider).setPrintStyle(PrintStyle.tableauWithTypeInferredColumnWidths(// sinkOperation.getConsumedDataType() handles legacy types
        DataTypeUtils.expandCompositeTypeToSchema(sinkOperation.getConsumedDataType()), resultProvider.getRowDataStringConverter(), PrintStyle.DEFAULT_MAX_COLUMN_WIDTH, false, isStreamingMode)).build();
    } catch (Exception e) {
        throw new TableException("Failed to execute sql", e);
    }
}
Also used : Transformation(org.apache.flink.api.dag.Transformation) TableException(org.apache.flink.table.api.TableException) CollectModifyOperation(org.apache.flink.table.operations.CollectModifyOperation) JobClient(org.apache.flink.core.execution.JobClient) FunctionAlreadyExistException(org.apache.flink.table.catalog.exceptions.FunctionAlreadyExistException) DatabaseNotExistException(org.apache.flink.table.catalog.exceptions.DatabaseNotExistException) TableAlreadyExistException(org.apache.flink.table.catalog.exceptions.TableAlreadyExistException) TableException(org.apache.flink.table.api.TableException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CatalogException(org.apache.flink.table.catalog.exceptions.CatalogException) FunctionNotExistException(org.apache.flink.table.catalog.exceptions.FunctionNotExistException) DatabaseNotEmptyException(org.apache.flink.table.catalog.exceptions.DatabaseNotEmptyException) DatabaseAlreadyExistException(org.apache.flink.table.catalog.exceptions.DatabaseAlreadyExistException) SqlParserException(org.apache.flink.table.api.SqlParserException) ValidationException(org.apache.flink.table.api.ValidationException) TableNotExistException(org.apache.flink.table.catalog.exceptions.TableNotExistException) Pipeline(org.apache.flink.api.dag.Pipeline)

Example 40 with Transformation

use of org.apache.flink.api.dag.Transformation in project flink by apache.

the class ExternalDynamicSink method getSinkRuntimeProvider.

@Override
public SinkRuntimeProvider getSinkRuntimeProvider(Context context) {
    final DynamicTableSink.DataStructureConverter physicalConverter = context.createDataStructureConverter(physicalDataType);
    return (TransformationSinkProvider) transformationContext -> {
        final Transformation<RowData> input = transformationContext.getInputTransformation();
        final LogicalType physicalType = physicalDataType.getLogicalType();
        final RowData.FieldGetter atomicFieldGetter;
        if (LogicalTypeChecks.isCompositeType(physicalType)) {
            atomicFieldGetter = null;
        } else {
            atomicFieldGetter = RowData.createFieldGetter(physicalType, 0);
        }
        TransformationMetadata transformationMeta = transformationContext.generateUid(EXTERNAL_DATASTREAM_TRANSFORMATION).map(uid -> new TransformationMetadata(uid, generateOperatorName(), generateOperatorDesc())).orElseGet(() -> new TransformationMetadata(generateOperatorName(), generateOperatorDesc()));
        return ExecNodeUtil.createOneInputTransformation(input, transformationMeta, new OutputConversionOperator(atomicFieldGetter, physicalConverter, transformationContext.getRowtimeIndex(), consumeRowtimeMetadata), ExternalTypeInfo.of(physicalDataType), input.getParallelism());
    };
}
Also used : TransformationMetadata(org.apache.flink.table.planner.plan.nodes.exec.utils.TransformationMetadata) DataType(org.apache.flink.table.types.DataType) OutputConversionOperator(org.apache.flink.table.runtime.operators.sink.OutputConversionOperator) RowData(org.apache.flink.table.data.RowData) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink) ChangelogMode(org.apache.flink.table.connector.ChangelogMode) SupportsWritingMetadata(org.apache.flink.table.connector.sink.abilities.SupportsWritingMetadata) DataTypes(org.apache.flink.table.api.DataTypes) ExternalTypeInfo(org.apache.flink.table.runtime.typeutils.ExternalTypeInfo) DataStream(org.apache.flink.streaming.api.datastream.DataStream) ExecNodeUtil(org.apache.flink.table.planner.plan.nodes.exec.utils.ExecNodeUtil) List(java.util.List) LogicalType(org.apache.flink.table.types.logical.LogicalType) Map(java.util.Map) Internal(org.apache.flink.annotation.Internal) Transformation(org.apache.flink.api.dag.Transformation) Collections(java.util.Collections) Nullable(javax.annotation.Nullable) LogicalTypeChecks(org.apache.flink.table.types.logical.utils.LogicalTypeChecks) RowData(org.apache.flink.table.data.RowData) TransformationMetadata(org.apache.flink.table.planner.plan.nodes.exec.utils.TransformationMetadata) OutputConversionOperator(org.apache.flink.table.runtime.operators.sink.OutputConversionOperator) LogicalType(org.apache.flink.table.types.logical.LogicalType) DynamicTableSink(org.apache.flink.table.connector.sink.DynamicTableSink)

Aggregations

Transformation (org.apache.flink.api.dag.Transformation)98 RowData (org.apache.flink.table.data.RowData)69 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)53 RowType (org.apache.flink.table.types.logical.RowType)50 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)45 TableException (org.apache.flink.table.api.TableException)28 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)28 ArrayList (java.util.ArrayList)25 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)21 Configuration (org.apache.flink.configuration.Configuration)19 TwoInputTransformation (org.apache.flink.streaming.api.transformations.TwoInputTransformation)18 List (java.util.List)17 PartitionTransformation (org.apache.flink.streaming.api.transformations.PartitionTransformation)17 AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)17 LogicalType (org.apache.flink.table.types.logical.LogicalType)16 Test (org.junit.Test)16 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)13 SourceTransformation (org.apache.flink.streaming.api.transformations.SourceTransformation)13 Arrays (java.util.Arrays)11 Collections (java.util.Collections)10