...
There are two approaches to enhance statistics for the planner, one is to introduce the "ANALYZE TABLE" syntax which will write the analyzed analyze result to the catalog, another is to introduce a new connector interface which allows the connector itself to report statistics directly to the planner. The second one is a supplement to the catalog statisticsfirst one.
The main purpose of this FLIP is to discuss disucss the second approachapproache. Compared to the first approachapproache, the second one is to get statistics in real time, no need to run an analysis analyze job for each table. This could help improve the user experience. The disadvantage is, in most cases, the statistics reported by connector is not as complete as the results of analyzed analyze job. We will also introduce the "ANALYZE TABLE" syntax in other FLIP.
Public Interfaces
Currently, table statistics and column statistics are described via two classes. We introduce CatalogStatistics to combine table statistics and column statistics, which describes all statistics for a table or partition.
/**
* All statistics for a table or partition, including {@link CatalogTableStatistics} and {@link
* CatalogColumnStatistics}.
*/
@PublicEvolving
public class CatalogStatistics {
public static final CatalogStatistics UNKNOWN =
new CatalogStatistics(CatalogTableStatistics.UNKNOWN, CatalogColumnStatistics.UNKNOWN);
private final CatalogTableStatistics tableStatistics;
private final CatalogColumnStatistics columnStatistics;
public CatalogStatistics(
CatalogTableStatistics tableStatistics, CatalogColumnStatistics columnStatistics) {
this.tableStatistics = tableStatistics;
this.columnStatistics = columnStatistics;
}
public CatalogTableStatistics getTableStatistics() {
return tableStatistics;
}
public CatalogColumnStatistics getColumnStatistics() {
return columnStatistics;
}
}
SupportStatisticReport is an interface that allows the Connector to report statistics to the planner. The statistics reported by Connector have a high priority and could override the statistics from CatalogSupportStatisticReport is an interface that allows the Connector to report statistics to the planner. The statistics reported by Connector will be used when the statistics from Catalog is unknown.
/** Enables to report the estimated statistics provided by the {@link DynamicTableSource}. */ @PublicEvolving public interface SupportStatisticReport { /** * Returns the estimated statistics of this {@link DynamicTableSource}, else {@link * CatalogStatistics#UNKNOWNTableStats#UNKNOWN} if some situations are not supported or cannot be handled. */ CatalogStatisticsTableStats reportStatistics(); }
We introduce a new config option as following to whether to call the reportStatistics method or not. Because it's a heavy operation to collect the statistics for some source in same cases.
public static final ConfigOption<Boolean> TABLE_OPTIMIZER_SOURCE_CONNECT_STATISTICS_ENABLED = key("table.optimizer.source.connect-statistics-enabled") .booleanType() .defaultValue(true) .withDescription( "When it is true, the optimizer will connect and use the statistics from source connector" + " if the source extends from SupportStatisticReport and the connected statistics is not UNKNOWN." + "Default value is true.");
The FileSystem connector is a FileSystemTableSource and HiveTableSource are commonly used connectorconnectors, especially for batch jobs. FileSystem supports multple They support multiple kinds of format, such as: csv, parquet, orc, etc. [1] Different formats have different ways of getting statistics. For parquet[2] and orc[3], they both have metadata information stored in the file footer, which including row count, max/min, null count, etc. For csv, we can get file size and estimated row count (file_size / simpled_lines_length).
Currently, the statistical dimensions used by the optimizer include row count, ndv(number fo disitinct distinct value), null count, max length, min length, max value and min value.[4] The file count, file size (which can be easily get from file system) is not used in the planner now, we can improve this later.We introduce FileBasedStatisticsReportableDecodingFormat interface to get the estimated statistics for the format in FileSystem connector.
/** * Extension of {@link DecodingFormat} which is able to report estimated statistics for FileSystem * connector. */ @PublicEvolving public interface FileBasedStatisticsReportableDecodingFormat<I> extends DecodingFormat<I> { /** * Returns the estimated statistics of this {@link DecodingFormat}. * * @param files The files to be estimated. * @param producedDataType the final output type of the format. */ CatalogStatisticsTableStats reportStatistics(List<Path> files, DataType producedDataType); }
It's a heavy operation if there are thousands of file to list or to read footer, so we also introduce a config option as following to allow the users to choose which kind of statistics is neededneeded. Once we introduce file size, FileStatisticsType.SIZE can be added.
public static final ConfigOption<FileStatisticsType> SOURCE_STATISTICS_TYPE = key("source.statistics-type") .enumType(FileStatisticsType.class) .defaultValue(FileStatisticsType.ALL) .withDescription("The file statistics type which the source could provide. " + "The statistics collecting is a heavy operation in some cases," + "this config allows users to choose the statistics type according to different situations."); public enum FileStatisticsType implements DescribedEnum { NONE("NONE", text("Do not collect any file statistics.")), ALL("ALL", text("Collect all file statistics that the format can provide.")); private final String value; private final InlineElement description; FileStatisticsType(String value, InlineElement description) { this.value = value; this.description = description; } @Override public String toString() { return value; } @Override public InlineElement getDescription() { return description; } }
...
- How the planner use the statistics reported by connector?
The statistics for a table needs to be re-computed when:
- the statistics from catalog is unknown
- the partitions are pruned
- the filter predicates are pushed down
In order to avoid multiple recalculations for each of the above operations, we introduced a new optimzation program after the predicate pushdown program to collect the statistics one-time.
The pseudocode is as follows:
public class FlinkCollectStatisticsProgram implements FlinkOptimizeProgram<BatchOptimizeContext> {
@Override
public RelNode optimize(RelNode root, BatchOptimizeContext context) {
// create a visitor to find all LogicalTableScan nodes
// call collectStatistics method for each LogicalTableScan
}
private LogicalTableScan collectStatistics(LogicalTableScan scan) {
final RelOptTable scanTable = scan.getTable();
if (!(scanTable instanceof TableSourceTable)) {
return scan;
}
boolean collectStatEnabled =
ShortcutUtils.unwrapContext(scan)
.getTableConfig()
.get(TABLE_OPTIMIZER_SOURCE_
...
COLLECT_STATISTICS_ENABLED); TableSourceTable table = (TableSourceTable) scanTable; DynamicTableSource tableSource = table.tableSource(); SourceAbilitySpec[] specs = table.abilitySpecs(); PartitionPushDownSpec partitionPushDownSpec = // find the PartitionPushDownSpec FilterPushDownSpec filterPushDownSpec = // find the FilterPushDownSpec TableStats newTableStat = null; if (partitionPushDownSpec != null && filterPushDownSpec == null) { // do partition pruning while no filter push down if (table.contextResolvedTable().isPermanent()) { // collect the statistics from catalog } if (collectStatEnabled && (newTableStat == null || newTableStat == TableStats.UNKNOWN) && tableSource instanceof SupportStatisticReport) {
...
newTableStat = ((SupportStatisticReport) tableSource).reportStatistics
...
(
...
);
}
} else if (filterPushDownSpec != null) {
// only filter push down
// the catalog do not support get statistics with filters,
// so only call reportStatistics method if needed
if (collectStatEnabled && tableSource instanceof SupportStatisticReport) {
...
newTableStat = ((SupportStatisticReport
...
)
...
tableSource).reportStatistics(); } } else if (collectStatEnabled && (table.getStatistic().getTableStats() == TableStats.UNKNOWN) && tableSource instanceof SupportStatisticReport) { // no partition pruning and no filter push down // call reportStatistics method if needed
...
newTableStat
...
=
...
((SupportStatisticReport) tableSource).reportStatistics();
...
...
...
} FlinkStatistic newStatistic = FlinkStatistic.builder() .statistic(table.getStatistic()) .tableStats(newTableStat) .build(); return new LogicalTableScan( scan.getCluster(), scan.getTraitSet(), scan.getHints(), table.copy(newStatistic)); } }
- Which connectors and formats will be supported by default?
FileSystem collector, Hive connector, Csv format, Parquet format, Orc format will be supported by default.
...