Search in sources :

Example 1 with InputFormatTableSource

use of org.apache.flink.table.sources.InputFormatTableSource in project flink by apache.

the class CommonExecLegacyTableSourceScan method translateToPlanInternal.

@SuppressWarnings("unchecked")
@Override
protected Transformation<RowData> translateToPlanInternal(PlannerBase planner, ExecNodeConfig config) {
    final Transformation<?> sourceTransform;
    final StreamExecutionEnvironment env = planner.getExecEnv();
    if (tableSource instanceof InputFormatTableSource) {
        InputFormatTableSource<Object> inputFormat = (InputFormatTableSource<Object>) tableSource;
        TypeInformation<Object> typeInfo = (TypeInformation<Object>) fromDataTypeToTypeInfo(inputFormat.getProducedDataType());
        InputFormat<Object, ?> format = inputFormat.getInputFormat();
        sourceTransform = createInput(env, format, typeInfo);
    } else if (tableSource instanceof StreamTableSource) {
        sourceTransform = ((StreamTableSource<?>) tableSource).getDataStream(env).getTransformation();
    } else {
        throw new UnsupportedOperationException(tableSource.getClass().getSimpleName() + " is unsupported.");
    }
    final TypeInformation<?> inputType = sourceTransform.getOutputType();
    final DataType producedDataType = tableSource.getProducedDataType();
    // check that declared and actual type of table source DataStream are identical
    if (!inputType.equals(TypeInfoDataTypeConverter.fromDataTypeToTypeInfo(producedDataType))) {
        throw new TableException(String.format("TableSource of type %s " + "returned a DataStream of data type %s that does not match with the " + "data type %s declared by the TableSource.getProducedDataType() method. " + "Please validate the implementation of the TableSource.", tableSource.getClass().getCanonicalName(), inputType, producedDataType));
    }
    final RowType outputType = (RowType) getOutputType();
    final RelDataType relDataType = FlinkTypeFactory.INSTANCE().buildRelNodeRowType(outputType);
    // get expression to extract rowtime attribute
    final Optional<RexNode> rowtimeExpression = JavaScalaConversionUtil.toJava(TableSourceUtil.getRowtimeAttributeDescriptor(tableSource, relDataType)).map(desc -> TableSourceUtil.getRowtimeExtractionExpression(desc.getTimestampExtractor(), producedDataType, planner.getRelBuilder(), getNameRemapping()));
    return createConversionTransformationIfNeeded(planner.getExecEnv(), config, sourceTransform, rowtimeExpression.orElse(null));
}
Also used : TableException(org.apache.flink.table.api.TableException) RowType(org.apache.flink.table.types.logical.RowType) RelDataType(org.apache.calcite.rel.type.RelDataType) StreamTableSource(org.apache.flink.table.sources.StreamTableSource) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) DataType(org.apache.flink.table.types.DataType) RelDataType(org.apache.calcite.rel.type.RelDataType) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) InputFormatTableSource(org.apache.flink.table.sources.InputFormatTableSource) RexNode(org.apache.calcite.rex.RexNode)

Aggregations

RelDataType (org.apache.calcite.rel.type.RelDataType)1 RexNode (org.apache.calcite.rex.RexNode)1 TypeInformation (org.apache.flink.api.common.typeinfo.TypeInformation)1 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)1 TableException (org.apache.flink.table.api.TableException)1 InputFormatTableSource (org.apache.flink.table.sources.InputFormatTableSource)1 StreamTableSource (org.apache.flink.table.sources.StreamTableSource)1 DataType (org.apache.flink.table.types.DataType)1 RowType (org.apache.flink.table.types.logical.RowType)1