Search in sources :

Example 71 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class DynamicTableSourceSpec method getTableSource.

private DynamicTableSource getTableSource(FlinkContext flinkContext) {
    if (tableSource == null) {
        final DynamicTableSourceFactory factory = flinkContext.getModuleManager().getFactory(Module::getTableSourceFactory).orElse(null);
        tableSource = FactoryUtil.createDynamicTableSource(factory, contextResolvedTable.getIdentifier(), contextResolvedTable.getResolvedTable(), loadOptionsFromCatalogTable(contextResolvedTable, flinkContext), flinkContext.getTableConfig().getConfiguration(), flinkContext.getClassLoader(), contextResolvedTable.isTemporary());
        if (sourceAbilities != null) {
            RowType newProducedType = (RowType) contextResolvedTable.getResolvedSchema().toSourceRowDataType().getLogicalType();
            for (SourceAbilitySpec spec : sourceAbilities) {
                SourceAbilityContext context = new SourceAbilityContext(flinkContext, newProducedType);
                spec.apply(tableSource, context);
                if (spec.getProducedType().isPresent()) {
                    newProducedType = spec.getProducedType().get();
                }
            }
        }
    }
    return tableSource;
}
Also used : DynamicTableSourceFactory(org.apache.flink.table.factories.DynamicTableSourceFactory) SourceAbilityContext(org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext) SourceAbilitySpec(org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec) RowType(org.apache.flink.table.types.logical.RowType)

Example 72 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class StreamExecSort method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    if (!config.get(InternalConfigOptions.TABLE_EXEC_NON_TEMPORAL_SORT_ENABLED)) {
        throw new TableException("Sort on a non-time-attribute field is not supported.");
    }
    ExecEdge inputEdge = getInputEdges().get(0);
    RowType inputType = (RowType) inputEdge.getOutputType();
    // sort code gen
    GeneratedRecordComparator rowComparator = ComparatorCodeGenerator.gen(config.getTableConfig(), "StreamExecSortComparator", inputType, sortSpec);
    StreamSortOperator sortOperator = new StreamSortOperator(InternalTypeInfo.of(inputType), rowComparator);
    Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    return ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationMeta(SORT_TRANSFORMATION, config), sortOperator, 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) StreamSortOperator(org.apache.flink.table.runtime.operators.sort.StreamSortOperator) GeneratedRecordComparator(org.apache.flink.table.runtime.generated.GeneratedRecordComparator)

Example 73 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class DataTypeUtils method stripRowPrefix.

/**
 * Removes a string prefix from the fields of the given row data type.
 */
public static DataType stripRowPrefix(DataType dataType, String prefix) {
    Preconditions.checkArgument(dataType.getLogicalType().is(ROW), "Row data type expected.");
    final RowType rowType = (RowType) dataType.getLogicalType();
    final List<String> newFieldNames = rowType.getFieldNames().stream().map(s -> {
        if (s.startsWith(prefix)) {
            return s.substring(prefix.length());
        }
        return s;
    }).collect(Collectors.toList());
    final LogicalType newRowType = LogicalTypeUtils.renameRowFields(rowType, newFieldNames);
    return new FieldsDataType(newRowType, dataType.getConversionClass(), dataType.getChildren());
}
Also used : DataType(org.apache.flink.table.types.DataType) AtomicDataType(org.apache.flink.table.types.AtomicDataType) Arrays(java.util.Arrays) ResolvedSchema(org.apache.flink.table.catalog.ResolvedSchema) StructuredType(org.apache.flink.table.types.logical.StructuredType) MapType(org.apache.flink.table.types.logical.MapType) RowField(org.apache.flink.table.types.logical.RowType.RowField) StructuredAttribute(org.apache.flink.table.types.logical.StructuredType.StructuredAttribute) Projection(org.apache.flink.table.connector.Projection) LocalZonedTimestampType(org.apache.flink.table.types.logical.LocalZonedTimestampType) Preconditions(org.apache.flink.util.Preconditions) Collectors(java.util.stream.Collectors) LegacyTypeInformationType(org.apache.flink.table.types.logical.LegacyTypeInformationType) List(java.util.List) Stream(java.util.stream.Stream) CollectionDataType(org.apache.flink.table.types.CollectionDataType) DistinctType(org.apache.flink.table.types.logical.DistinctType) LogicalType(org.apache.flink.table.types.logical.LogicalType) ValidationException(org.apache.flink.table.api.ValidationException) Optional(java.util.Optional) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory) IntStream(java.util.stream.IntStream) LogicalTypeUtils.toInternalConversionClass(org.apache.flink.table.types.logical.utils.LogicalTypeUtils.toInternalConversionClass) ObjectIdentifier(org.apache.flink.table.catalog.ObjectIdentifier) KeyValueDataType(org.apache.flink.table.types.KeyValueDataType) Column(org.apache.flink.table.catalog.Column) RowType(org.apache.flink.table.types.logical.RowType) TimestampKind(org.apache.flink.table.types.logical.TimestampKind) LogicalTypeFamily(org.apache.flink.table.types.logical.LogicalTypeFamily) FieldsDataType(org.apache.flink.table.types.FieldsDataType) DISTINCT_TYPE(org.apache.flink.table.types.logical.LogicalTypeRoot.DISTINCT_TYPE) LogicalTypeUtils.removeTimeAttributes(org.apache.flink.table.types.logical.utils.LogicalTypeUtils.removeTimeAttributes) LogicalTypeUtils(org.apache.flink.table.types.logical.utils.LogicalTypeUtils) Nullable(javax.annotation.Nullable) ROW(org.apache.flink.table.types.logical.LogicalTypeRoot.ROW) MultisetType(org.apache.flink.table.types.logical.MultisetType) LogicalTypeChecks.getFieldNames(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getFieldNames) LogicalTypeUtils.getAtomicName(org.apache.flink.table.types.logical.utils.LogicalTypeUtils.getAtomicName) CompositeType(org.apache.flink.api.common.typeutils.CompositeType) LogicalTypeChecks.isCompositeType(org.apache.flink.table.types.logical.utils.LogicalTypeChecks.isCompositeType) LogicalTypeDefaultVisitor(org.apache.flink.table.types.logical.utils.LogicalTypeDefaultVisitor) DataTypes(org.apache.flink.table.api.DataTypes) TypeTransformation(org.apache.flink.table.types.inference.TypeTransformation) ArrayType(org.apache.flink.table.types.logical.ArrayType) STRUCTURED_TYPE(org.apache.flink.table.types.logical.LogicalTypeRoot.STRUCTURED_TYPE) DataTypeVisitor(org.apache.flink.table.types.DataTypeVisitor) ExtractionUtils.primitiveToWrapper(org.apache.flink.table.types.extraction.ExtractionUtils.primitiveToWrapper) Internal(org.apache.flink.annotation.Internal) Collections(java.util.Collections) LogicalTypeChecks(org.apache.flink.table.types.logical.utils.LogicalTypeChecks) FieldsDataType(org.apache.flink.table.types.FieldsDataType) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 74 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class BatchExecHashWindowAggregate method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    final ExecEdge inputEdge = getInputEdges().get(0);
    final Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    final AggregateInfoList aggInfos = AggregateUtil.transformToBatchAggregateInfoList(aggInputRowType, JavaScalaConversionUtil.toScala(Arrays.asList(aggCalls)), // aggCallNeedRetractions
    null, // orderKeyIndexes
    null);
    final RowType inputRowType = (RowType) inputEdge.getOutputType();
    final HashWindowCodeGenerator hashWindowCodeGenerator = new HashWindowCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), planner.getRelBuilder(), window, inputTimeFieldIndex, inputTimeIsDate, JavaScalaConversionUtil.toScala(Arrays.asList(namedWindowProperties)), aggInfos, inputRowType, grouping, auxGrouping, enableAssignPane, isMerge, isFinal);
    final int groupBufferLimitSize = config.get(ExecutionConfigOptions.TABLE_EXEC_WINDOW_AGG_BUFFER_SIZE_LIMIT);
    final Tuple2<Long, Long> windowSizeAndSlideSize = WindowCodeGenerator.getWindowDef(window);
    final GeneratedOperator<OneInputStreamOperator<RowData, RowData>> generatedOperator = hashWindowCodeGenerator.gen(inputRowType, (RowType) getOutputType(), groupBufferLimitSize, // windowStart
    0, windowSizeAndSlideSize.f0, windowSizeAndSlideSize.f1);
    final long managedMemory = config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_HASH_AGG_MEMORY).getBytes();
    return ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationName(config), createTransformationDescription(config), new CodeGenOperatorFactory<>(generatedOperator), InternalTypeInfo.of(getOutputType()), inputTransform.getParallelism(), managedMemory);
}
Also used : Transformation(org.apache.flink.api.dag.Transformation) AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) ExecEdge(org.apache.flink.table.planner.plan.nodes.exec.ExecEdge) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) RowType(org.apache.flink.table.types.logical.RowType) RowData(org.apache.flink.table.data.RowData) OneInputStreamOperator(org.apache.flink.streaming.api.operators.OneInputStreamOperator) HashWindowCodeGenerator(org.apache.flink.table.planner.codegen.agg.batch.HashWindowCodeGenerator)

Example 75 with RowType

use of org.apache.flink.table.types.logical.RowType in project flink by apache.

the class BatchExecOverAggregate method createOverWindowFrames.

private List<OverWindowFrame> createOverWindowFrames(FlinkRelBuilder relBuilder, ExecNodeConfig config, RowType inputType, SortSpec sortSpec, RowType inputTypeWithConstants) {
    final List<OverWindowFrame> windowFrames = new ArrayList<>();
    for (GroupSpec group : overSpec.getGroups()) {
        OverWindowMode mode = inferGroupMode(group);
        if (mode == OverWindowMode.OFFSET) {
            for (AggregateCall aggCall : group.getAggCalls()) {
                AggregateInfoList aggInfoList = AggregateUtil.transformToBatchAggregateInfoList(inputTypeWithConstants, JavaScalaConversionUtil.toScala(Collections.singletonList(aggCall)), new boolean[] { true }, /* needRetraction = true, See LeadLagAggFunction */
                sortSpec.getFieldIndices());
                AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), relBuilder, JavaScalaConversionUtil.toScala(inputType.getChildren()), // copyInputField
                false);
                // over agg code gen must pass the constants
                GeneratedAggsHandleFunction genAggsHandler = generator.needAccumulate().needRetract().withConstants(JavaScalaConversionUtil.toScala(getConstants())).generateAggsHandler("BoundedOverAggregateHelper", aggInfoList);
                // LEAD is behind the currentRow, so we need plus offset.
                // LAG is in front of the currentRow, so we need minus offset.
                long flag = aggCall.getAggregation().kind == SqlKind.LEAD ? 1L : -1L;
                final Long offset;
                final OffsetOverFrame.CalcOffsetFunc calcOffsetFunc;
                // The second arg mean the offset arg index for leag/lag function, default is 1.
                if (aggCall.getArgList().size() >= 2) {
                    int constantIndex = aggCall.getArgList().get(1) - overSpec.getOriginalInputFields();
                    if (constantIndex < 0) {
                        offset = null;
                        int rowIndex = aggCall.getArgList().get(1);
                        switch(inputType.getTypeAt(rowIndex).getTypeRoot()) {
                            case BIGINT:
                                calcOffsetFunc = row -> row.getLong(rowIndex) * flag;
                                break;
                            case INTEGER:
                                calcOffsetFunc = row -> (long) row.getInt(rowIndex) * flag;
                                break;
                            case SMALLINT:
                                calcOffsetFunc = row -> (long) row.getShort(rowIndex) * flag;
                                break;
                            default:
                                throw new RuntimeException("The column type must be in long/int/short.");
                        }
                    } else {
                        long constantOffset = getConstants().get(constantIndex).getValueAs(Long.class);
                        offset = constantOffset * flag;
                        calcOffsetFunc = null;
                    }
                } else {
                    offset = flag;
                    calcOffsetFunc = null;
                }
                windowFrames.add(new OffsetOverFrame(genAggsHandler, offset, calcOffsetFunc));
            }
        } else {
            AggregateInfoList aggInfoList = AggregateUtil.transformToBatchAggregateInfoList(// inputSchema.relDataType
            inputTypeWithConstants, JavaScalaConversionUtil.toScala(group.getAggCalls()), // aggCallNeedRetractions
            null, sortSpec.getFieldIndices());
            AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), relBuilder, JavaScalaConversionUtil.toScala(inputType.getChildren()), // copyInputField
            false);
            // over agg code gen must pass the constants
            GeneratedAggsHandleFunction genAggsHandler = generator.needAccumulate().withConstants(JavaScalaConversionUtil.toScala(getConstants())).generateAggsHandler("BoundedOverAggregateHelper", aggInfoList);
            RowType valueType = generator.valueType();
            final OverWindowFrame frame;
            switch(mode) {
                case RANGE:
                    if (isUnboundedWindow(group)) {
                        frame = new UnboundedOverWindowFrame(genAggsHandler, valueType);
                    } else if (isUnboundedPrecedingWindow(group)) {
                        GeneratedRecordComparator genBoundComparator = createBoundComparator(relBuilder, config, sortSpec, group.getUpperBound(), false, inputType);
                        frame = new RangeUnboundedPrecedingOverFrame(genAggsHandler, genBoundComparator);
                    } else if (isUnboundedFollowingWindow(group)) {
                        GeneratedRecordComparator genBoundComparator = createBoundComparator(relBuilder, config, sortSpec, group.getLowerBound(), true, inputType);
                        frame = new RangeUnboundedFollowingOverFrame(valueType, genAggsHandler, genBoundComparator);
                    } else if (isSlidingWindow(group)) {
                        GeneratedRecordComparator genLeftBoundComparator = createBoundComparator(relBuilder, config, sortSpec, group.getLowerBound(), true, inputType);
                        GeneratedRecordComparator genRightBoundComparator = createBoundComparator(relBuilder, config, sortSpec, group.getUpperBound(), false, inputType);
                        frame = new RangeSlidingOverFrame(inputType, valueType, genAggsHandler, genLeftBoundComparator, genRightBoundComparator);
                    } else {
                        throw new TableException("This should not happen.");
                    }
                    break;
                case ROW:
                    if (isUnboundedWindow(group)) {
                        frame = new UnboundedOverWindowFrame(genAggsHandler, valueType);
                    } else if (isUnboundedPrecedingWindow(group)) {
                        frame = new RowUnboundedPrecedingOverFrame(genAggsHandler, OverAggregateUtil.getLongBoundary(overSpec, group.getUpperBound()));
                    } else if (isUnboundedFollowingWindow(group)) {
                        frame = new RowUnboundedFollowingOverFrame(valueType, genAggsHandler, OverAggregateUtil.getLongBoundary(overSpec, group.getLowerBound()));
                    } else if (isSlidingWindow(group)) {
                        frame = new RowSlidingOverFrame(inputType, valueType, genAggsHandler, OverAggregateUtil.getLongBoundary(overSpec, group.getLowerBound()), OverAggregateUtil.getLongBoundary(overSpec, group.getUpperBound()));
                    } else {
                        throw new TableException("This should not happen.");
                    }
                    break;
                case INSENSITIVE:
                    frame = new InsensitiveOverFrame(genAggsHandler);
                    break;
                default:
                    throw new TableException("This should not happen.");
            }
            windowFrames.add(frame);
        }
    }
    return windowFrames;
}
Also used : AggregateInfoList(org.apache.flink.table.planner.plan.utils.AggregateInfoList) ArrayList(java.util.ArrayList) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) RowType(org.apache.flink.table.types.logical.RowType) RowUnboundedFollowingOverFrame(org.apache.flink.table.runtime.operators.over.frame.RowUnboundedFollowingOverFrame) RangeUnboundedPrecedingOverFrame(org.apache.flink.table.runtime.operators.over.frame.RangeUnboundedPrecedingOverFrame) RowUnboundedPrecedingOverFrame(org.apache.flink.table.runtime.operators.over.frame.RowUnboundedPrecedingOverFrame) RowSlidingOverFrame(org.apache.flink.table.runtime.operators.over.frame.RowSlidingOverFrame) InsensitiveOverFrame(org.apache.flink.table.runtime.operators.over.frame.InsensitiveOverFrame) TableException(org.apache.flink.table.api.TableException) CodeGeneratorContext(org.apache.flink.table.planner.codegen.CodeGeneratorContext) OffsetOverFrame(org.apache.flink.table.runtime.operators.over.frame.OffsetOverFrame) GeneratedAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedAggsHandleFunction) RangeSlidingOverFrame(org.apache.flink.table.runtime.operators.over.frame.RangeSlidingOverFrame) UnboundedOverWindowFrame(org.apache.flink.table.runtime.operators.over.frame.UnboundedOverWindowFrame) UnboundedOverWindowFrame(org.apache.flink.table.runtime.operators.over.frame.UnboundedOverWindowFrame) OverWindowFrame(org.apache.flink.table.runtime.operators.over.frame.OverWindowFrame) AggregateCall(org.apache.calcite.rel.core.AggregateCall) GroupSpec(org.apache.flink.table.planner.plan.nodes.exec.spec.OverSpec.GroupSpec) RangeUnboundedFollowingOverFrame(org.apache.flink.table.runtime.operators.over.frame.RangeUnboundedFollowingOverFrame) GeneratedRecordComparator(org.apache.flink.table.runtime.generated.GeneratedRecordComparator)

Aggregations

RowType (org.apache.flink.table.types.logical.RowType)212 RowData (org.apache.flink.table.data.RowData)108 LogicalType (org.apache.flink.table.types.logical.LogicalType)59 DataType (org.apache.flink.table.types.DataType)57 Transformation (org.apache.flink.api.dag.Transformation)50 ExecEdge (org.apache.flink.table.planner.plan.nodes.exec.ExecEdge)46 TableException (org.apache.flink.table.api.TableException)37 Test (org.junit.Test)36 GenericRowData (org.apache.flink.table.data.GenericRowData)33 ArrayList (java.util.ArrayList)28 List (java.util.List)28 OneInputTransformation (org.apache.flink.streaming.api.transformations.OneInputTransformation)26 RowDataKeySelector (org.apache.flink.table.runtime.keyselector.RowDataKeySelector)25 CodeGeneratorContext (org.apache.flink.table.planner.codegen.CodeGeneratorContext)22 TableConfig (org.apache.flink.table.api.TableConfig)19 ArrayType (org.apache.flink.table.types.logical.ArrayType)19 TimestampType (org.apache.flink.table.types.logical.TimestampType)19 DecimalType (org.apache.flink.table.types.logical.DecimalType)17 Collections (java.util.Collections)16 AggregateInfoList (org.apache.flink.table.planner.plan.utils.AggregateInfoList)16