Search in sources :

Example 41 with LogicalType

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

the class StreamExecGlobalWindowAggregate 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 RowType inputRowType = (RowType) inputEdge.getOutputType();
    final ZoneId shiftTimeZone = TimeWindowUtil.getShiftTimeZone(windowing.getTimeAttributeType(), config.getLocalTimeZone());
    final SliceAssigner sliceAssigner = createSliceAssigner(windowing, shiftTimeZone);
    final AggregateInfoList localAggInfoList = AggregateUtil.deriveStreamWindowAggregateInfoList(// should use original input here
    localAggInputRowType, JavaScalaConversionUtil.toScala(Arrays.asList(aggCalls)), windowing.getWindow(), // isStateBackendDataViews
    false);
    final AggregateInfoList globalAggInfoList = AggregateUtil.deriveStreamWindowAggregateInfoList(// should use original input here
    localAggInputRowType, JavaScalaConversionUtil.toScala(Arrays.asList(aggCalls)), windowing.getWindow(), // isStateBackendDataViews
    true);
    // handler used to merge multiple local accumulators into one accumulator,
    // where the accumulators are all on memory
    final GeneratedNamespaceAggsHandleFunction<Long> localAggsHandler = createAggsHandler("LocalWindowAggsHandler", sliceAssigner, localAggInfoList, grouping.length, true, localAggInfoList.getAccTypes(), config, planner.getRelBuilder(), shiftTimeZone);
    // handler used to merge the single local accumulator (on memory) into state accumulator
    final GeneratedNamespaceAggsHandleFunction<Long> globalAggsHandler = createAggsHandler("GlobalWindowAggsHandler", sliceAssigner, globalAggInfoList, 0, true, localAggInfoList.getAccTypes(), config, planner.getRelBuilder(), shiftTimeZone);
    // handler used to merge state accumulators for merging slices into window,
    // e.g. Hop and Cumulate
    final GeneratedNamespaceAggsHandleFunction<Long> stateAggsHandler = createAggsHandler("StateWindowAggsHandler", sliceAssigner, globalAggInfoList, 0, false, globalAggInfoList.getAccTypes(), config, planner.getRelBuilder(), shiftTimeZone);
    final RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(grouping, InternalTypeInfo.of(inputRowType));
    final LogicalType[] accTypes = convertToLogicalTypes(globalAggInfoList.getAccTypes());
    final OneInputStreamOperator<RowData, RowData> windowOperator = SlicingWindowAggOperatorBuilder.builder().inputSerializer(new RowDataSerializer(inputRowType)).shiftTimeZone(shiftTimeZone).keySerializer((PagedTypeSerializer<RowData>) selector.getProducedType().toSerializer()).assigner(sliceAssigner).countStarIndex(globalAggInfoList.getIndexOfCountStar()).globalAggregate(localAggsHandler, globalAggsHandler, stateAggsHandler, new RowDataSerializer(accTypes)).build();
    final OneInputTransformation<RowData, RowData> transform = ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationMeta(GLOBAL_WINDOW_AGGREGATE_TRANSFORMATION, config), SimpleOperatorFactory.of(windowOperator), InternalTypeInfo.of(getOutputType()), inputTransform.getParallelism(), WINDOW_AGG_MEMORY_RATIO);
    // set KeyType and Selector for state
    transform.setStateKeySelector(selector);
    transform.setStateKeyType(selector.getProducedType());
    return transform;
}
Also used : SliceAssigner(org.apache.flink.table.runtime.operators.window.slicing.SliceAssigner) OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) 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) ZoneId(java.time.ZoneId) RowType(org.apache.flink.table.types.logical.RowType) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowData(org.apache.flink.table.data.RowData) RowDataKeySelector(org.apache.flink.table.runtime.keyselector.RowDataKeySelector) RowDataSerializer(org.apache.flink.table.runtime.typeutils.RowDataSerializer)

Example 42 with LogicalType

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

the class StreamExecGroupTableAggregate method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    if (grouping.length > 0 && config.getStateRetentionTime() < 0) {
        LOG.warn("No state retention interval configured for a query which accumulates state. " + "Please provide a query configuration with valid retention interval to prevent excessive " + "state size. You may specify a retention time of 0 to not clean up the state.");
    }
    final ExecEdge inputEdge = getInputEdges().get(0);
    final Transformation<RowData> inputTransform = (Transformation<RowData>) inputEdge.translateToPlan(planner);
    final RowType inputRowType = (RowType) inputEdge.getOutputType();
    final AggsHandlerCodeGenerator generator = new AggsHandlerCodeGenerator(new CodeGeneratorContext(config.getTableConfig()), planner.getRelBuilder(), JavaScalaConversionUtil.toScala(inputRowType.getChildren()), // TODO: but other operators do not copy this input field.....
    true).needAccumulate();
    if (needRetraction) {
        generator.needRetract();
    }
    final AggregateInfoList aggInfoList = AggregateUtil.transformToStreamAggregateInfoList(inputRowType, JavaScalaConversionUtil.toScala(Arrays.asList(aggCalls)), aggCallNeedRetractions, needRetraction, // isStateBackendDataViews
    true, // needDistinctInfo
    true);
    final GeneratedTableAggsHandleFunction aggsHandler = generator.generateTableAggsHandler("GroupTableAggHandler", aggInfoList);
    final LogicalType[] accTypes = Arrays.stream(aggInfoList.getAccTypes()).map(LogicalTypeDataTypeConverter::fromDataTypeToLogicalType).toArray(LogicalType[]::new);
    final int inputCountIndex = aggInfoList.getIndexOfCountStar();
    final GroupTableAggFunction aggFunction = new GroupTableAggFunction(aggsHandler, accTypes, inputCountIndex, generateUpdateBefore, config.getStateRetentionTime());
    final OneInputStreamOperator<RowData, RowData> operator = new KeyedProcessOperator<>(aggFunction);
    // partitioned aggregation
    final OneInputTransformation<RowData, RowData> transform = ExecNodeUtil.createOneInputTransformation(inputTransform, createTransformationMeta(GROUP_TABLE_AGGREGATE_TRANSFORMATION, config), operator, InternalTypeInfo.of(getOutputType()), inputTransform.getParallelism());
    // set KeyType and Selector for state
    final RowDataKeySelector selector = KeySelectorUtil.getRowDataSelector(grouping, InternalTypeInfo.of(inputRowType));
    transform.setStateKeySelector(selector);
    transform.setStateKeyType(selector.getProducedType());
    return transform;
}
Also used : OneInputTransformation(org.apache.flink.streaming.api.transformations.OneInputTransformation) 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) GeneratedTableAggsHandleFunction(org.apache.flink.table.runtime.generated.GeneratedTableAggsHandleFunction) RowType(org.apache.flink.table.types.logical.RowType) AggsHandlerCodeGenerator(org.apache.flink.table.planner.codegen.agg.AggsHandlerCodeGenerator) LogicalType(org.apache.flink.table.types.logical.LogicalType) RowData(org.apache.flink.table.data.RowData) RowDataKeySelector(org.apache.flink.table.runtime.keyselector.RowDataKeySelector) GroupTableAggFunction(org.apache.flink.table.runtime.operators.aggregate.GroupTableAggFunction) KeyedProcessOperator(org.apache.flink.streaming.api.operators.KeyedProcessOperator)

Example 43 with LogicalType

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

the class AbstractTopNFunction method open.

@Override
public void open(Configuration parameters) throws Exception {
    super.open(parameters);
    outputRow = new JoinedRowData();
    if (!isConstantRankEnd) {
        ValueStateDescriptor<Long> rankStateDesc = new ValueStateDescriptor<>("rankEnd", Types.LONG);
        if (ttlConfig.isEnabled()) {
            rankStateDesc.enableTimeToLive(ttlConfig);
        }
        rankEndState = getRuntimeContext().getState(rankStateDesc);
    }
    // compile comparator
    sortKeyComparator = generatedSortKeyComparator.newInstance(getRuntimeContext().getUserCodeClassLoader());
    generatedSortKeyComparator = null;
    invalidCounter = getRuntimeContext().getMetricGroup().counter("topn.invalidTopSize");
    // initialize rankEndFetcher
    if (!isConstantRankEnd) {
        LogicalType rankEndIdxType = inputRowType.toRowFieldTypes()[rankEndIndex];
        switch(rankEndIdxType.getTypeRoot()) {
            case BIGINT:
                rankEndFetcher = (RowData row) -> row.getLong(rankEndIndex);
                break;
            case INTEGER:
                rankEndFetcher = (RowData row) -> (long) row.getInt(rankEndIndex);
                break;
            case SMALLINT:
                rankEndFetcher = (RowData row) -> (long) row.getShort(rankEndIndex);
                break;
            default:
                LOG.error("variable rank index column must be long, short or int type, while input type is {}", rankEndIdxType.getClass().getName());
                throw new UnsupportedOperationException("variable rank index column must be long type, while input type is " + rankEndIdxType.getClass().getName());
        }
    }
}
Also used : ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) RowData(org.apache.flink.table.data.RowData) GenericRowData(org.apache.flink.table.data.GenericRowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) JoinedRowData(org.apache.flink.table.data.utils.JoinedRowData) LogicalType(org.apache.flink.table.types.logical.LogicalType)

Example 44 with LogicalType

use of org.apache.flink.table.types.logical.LogicalType 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 45 with LogicalType

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

the class BatchExecSortMergeJoin method translateToPlanInternal.

@Override
@SuppressWarnings("unchecked")
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    ExecEdge leftInputEdge = getInputEdges().get(0);
    ExecEdge rightInputEdge = getInputEdges().get(1);
    // get input types
    RowType leftType = (RowType) leftInputEdge.getOutputType();
    RowType rightType = (RowType) rightInputEdge.getOutputType();
    LogicalType[] keyFieldTypes = IntStream.of(leftKeys).mapToObj(leftType::getTypeAt).toArray(LogicalType[]::new);
    RowType keyType = RowType.of(keyFieldTypes);
    GeneratedJoinCondition condFunc = JoinUtil.generateConditionFunction(config.getTableConfig(), nonEquiCondition, leftType, rightType);
    long externalBufferMemory = config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_EXTERNAL_BUFFER_MEMORY).getBytes();
    long sortMemory = config.get(ExecutionConfigOptions.TABLE_EXEC_RESOURCE_SORT_MEMORY).getBytes();
    int externalBufferNum = 1;
    if (joinType == FlinkJoinType.FULL) {
        externalBufferNum = 2;
    }
    long managedMemory = externalBufferMemory * externalBufferNum + sortMemory * 2;
    SortCodeGenerator leftSortGen = newSortGen(config, leftKeys, leftType);
    SortCodeGenerator rightSortGen = newSortGen(config, rightKeys, rightType);
    int[] keyPositions = IntStream.range(0, leftKeys.length).toArray();
    SortMergeJoinOperator operator = new SortMergeJoinOperator(1.0 * externalBufferMemory / managedMemory, joinType, leftIsSmaller, condFunc, ProjectionCodeGenerator.generateProjection(new CodeGeneratorContext(config.getTableConfig()), "SMJProjection", leftType, keyType, leftKeys), ProjectionCodeGenerator.generateProjection(new CodeGeneratorContext(config.getTableConfig()), "SMJProjection", rightType, keyType, rightKeys), leftSortGen.generateNormalizedKeyComputer("LeftComputer"), leftSortGen.generateRecordComparator("LeftComparator"), rightSortGen.generateNormalizedKeyComputer("RightComputer"), rightSortGen.generateRecordComparator("RightComparator"), newSortGen(config, keyPositions, keyType).generateRecordComparator("KeyComparator"), filterNulls);
    Transformation<RowData> leftInputTransform = (Transformation<RowData>) leftInputEdge.translateToPlan(planner);
    Transformation<RowData> rightInputTransform = (Transformation<RowData>) rightInputEdge.translateToPlan(planner);
    return ExecNodeUtil.createTwoInputTransformation(leftInputTransform, rightInputTransform, createTransformationName(config), createTransformationDescription(config), SimpleOperatorFactory.of(operator), InternalTypeInfo.of(getOutputType()), rightInputTransform.getParallelism(), managedMemory);
}
Also used : Transformation(org.apache.flink.api.dag.Transformation) 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) LogicalType(org.apache.flink.table.types.logical.LogicalType) GeneratedJoinCondition(org.apache.flink.table.runtime.generated.GeneratedJoinCondition) RowData(org.apache.flink.table.data.RowData) SortMergeJoinOperator(org.apache.flink.table.runtime.operators.join.SortMergeJoinOperator) SortCodeGenerator(org.apache.flink.table.planner.codegen.sort.SortCodeGenerator)

Aggregations

LogicalType (org.apache.flink.table.types.logical.LogicalType)192 DataType (org.apache.flink.table.types.DataType)53 RowType (org.apache.flink.table.types.logical.RowType)53 RowData (org.apache.flink.table.data.RowData)45 List (java.util.List)29 ArrayList (java.util.ArrayList)28 TableException (org.apache.flink.table.api.TableException)25 TimestampType (org.apache.flink.table.types.logical.TimestampType)25 Internal (org.apache.flink.annotation.Internal)21 IntType (org.apache.flink.table.types.logical.IntType)21 Map (java.util.Map)20 ValidationException (org.apache.flink.table.api.ValidationException)20 ArrayType (org.apache.flink.table.types.logical.ArrayType)19 DecimalType (org.apache.flink.table.types.logical.DecimalType)19 LocalZonedTimestampType (org.apache.flink.table.types.logical.LocalZonedTimestampType)17 Test (org.junit.Test)17 BigIntType (org.apache.flink.table.types.logical.BigIntType)16 LegacyTypeInformationType (org.apache.flink.table.types.logical.LegacyTypeInformationType)16 GenericRowData (org.apache.flink.table.data.GenericRowData)15 Arrays (java.util.Arrays)14