Search in sources :

Example 1 with SupportsSourceWatermark

use of org.apache.flink.table.connector.source.abilities.SupportsSourceWatermark in project flink by apache.

the class PushWatermarkIntoTableSourceScanRuleBase method getNewScan.

/**
 * It uses the input watermark expression to generate the {@link WatermarkGeneratorSupplier}.
 * After the {@link WatermarkStrategy} is pushed into the scan, it will build a new scan.
 * However, when {@link FlinkLogicalWatermarkAssigner} is the parent of the {@link
 * FlinkLogicalTableSourceScan} it should modify the rowtime type to keep the type of plan is
 * consistent. In other cases, it just keep the data type of the scan as same as before and
 * leave the work when rewriting the projection.
 *
 * <p>NOTES: the row type of the scan is not always as same as the watermark assigner. Because
 * the scan will not add the rowtime column into the row when pushing the watermark assigner
 * into the scan. In some cases, query may have computed columns defined on rowtime column. If
 * modifying the type of the rowtime(with time attribute), it will also influence the type of
 * the computed column. Therefore, if the watermark assigner is not the parent of the scan, set
 * the type of the scan as before and leave the work to projection.
 */
protected FlinkLogicalTableSourceScan getNewScan(FlinkLogicalWatermarkAssigner watermarkAssigner, RexNode watermarkExpr, FlinkLogicalTableSourceScan scan, TableConfig tableConfig, boolean useWatermarkAssignerRowType) {
    final TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class);
    final DynamicTableSource newDynamicTableSource = tableSourceTable.tableSource().copy();
    final boolean isSourceWatermark = newDynamicTableSource instanceof SupportsSourceWatermark && hasSourceWatermarkDeclaration(watermarkExpr);
    final RelDataType newType;
    if (useWatermarkAssignerRowType) {
        // project is trivial and set rowtime type in scan
        newType = watermarkAssigner.getRowType();
    } else {
        // project add/delete columns and set the rowtime column type in project
        newType = scan.getRowType();
    }
    final RowType producedType = (RowType) FlinkTypeFactory.toLogicalType(newType);
    final SourceAbilityContext abilityContext = SourceAbilityContext.from(scan);
    final SourceAbilitySpec abilitySpec;
    if (isSourceWatermark) {
        final SourceWatermarkSpec sourceWatermarkSpec = new SourceWatermarkSpec(true, producedType);
        sourceWatermarkSpec.apply(newDynamicTableSource, abilityContext);
        abilitySpec = sourceWatermarkSpec;
    } else {
        final Duration idleTimeout = tableConfig.getConfiguration().get(ExecutionConfigOptions.TABLE_EXEC_SOURCE_IDLE_TIMEOUT);
        final long idleTimeoutMillis;
        if (!idleTimeout.isZero() && !idleTimeout.isNegative()) {
            idleTimeoutMillis = idleTimeout.toMillis();
        } else {
            idleTimeoutMillis = -1L;
        }
        final WatermarkPushDownSpec watermarkPushDownSpec = new WatermarkPushDownSpec(watermarkExpr, idleTimeoutMillis, producedType);
        watermarkPushDownSpec.apply(newDynamicTableSource, abilityContext);
        abilitySpec = watermarkPushDownSpec;
    }
    TableSourceTable newTableSourceTable = tableSourceTable.copy(newDynamicTableSource, newType, new SourceAbilitySpec[] { abilitySpec });
    return FlinkLogicalTableSourceScan.create(scan.getCluster(), scan.getHints(), newTableSourceTable);
}
Also used : WatermarkPushDownSpec(org.apache.flink.table.planner.plan.abilities.source.WatermarkPushDownSpec) SourceAbilityContext(org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext) SourceAbilitySpec(org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec) SupportsSourceWatermark(org.apache.flink.table.connector.source.abilities.SupportsSourceWatermark) RowType(org.apache.flink.table.types.logical.RowType) RelDataType(org.apache.calcite.rel.type.RelDataType) Duration(java.time.Duration) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) SourceWatermarkSpec(org.apache.flink.table.planner.plan.abilities.source.SourceWatermarkSpec)

Example 2 with SupportsSourceWatermark

use of org.apache.flink.table.connector.source.abilities.SupportsSourceWatermark in project flink by apache.

the class PushWatermarkIntoTableSourceScanRuleBase method supportsWatermarkPushDown.

protected boolean supportsWatermarkPushDown(FlinkLogicalTableSourceScan scan) {
    TableSourceTable tableSourceTable = scan.getTable().unwrap(TableSourceTable.class);
    if (tableSourceTable == null) {
        return false;
    }
    final DynamicTableSource tableSource = tableSourceTable.tableSource();
    return (tableSource instanceof SupportsWatermarkPushDown) || (tableSource instanceof SupportsSourceWatermark && hasSourceWatermarkDeclaration(tableSourceTable));
}
Also used : SupportsSourceWatermark(org.apache.flink.table.connector.source.abilities.SupportsSourceWatermark) TableSourceTable(org.apache.flink.table.planner.plan.schema.TableSourceTable) DynamicTableSource(org.apache.flink.table.connector.source.DynamicTableSource) SupportsWatermarkPushDown(org.apache.flink.table.connector.source.abilities.SupportsWatermarkPushDown)

Aggregations

DynamicTableSource (org.apache.flink.table.connector.source.DynamicTableSource)2 SupportsSourceWatermark (org.apache.flink.table.connector.source.abilities.SupportsSourceWatermark)2 TableSourceTable (org.apache.flink.table.planner.plan.schema.TableSourceTable)2 Duration (java.time.Duration)1 RelDataType (org.apache.calcite.rel.type.RelDataType)1 SupportsWatermarkPushDown (org.apache.flink.table.connector.source.abilities.SupportsWatermarkPushDown)1 SourceAbilityContext (org.apache.flink.table.planner.plan.abilities.source.SourceAbilityContext)1 SourceAbilitySpec (org.apache.flink.table.planner.plan.abilities.source.SourceAbilitySpec)1 SourceWatermarkSpec (org.apache.flink.table.planner.plan.abilities.source.SourceWatermarkSpec)1 WatermarkPushDownSpec (org.apache.flink.table.planner.plan.abilities.source.WatermarkPushDownSpec)1 RowType (org.apache.flink.table.types.logical.RowType)1