Search in sources :

Example 1 with MapRDBStatisticsPayload

use of org.apache.drill.exec.planner.index.MapRDBStatisticsPayload in project drill by apache.

the class JsonTableGroupScan method getFirstKeyEstimatedStatsInternal.

/**
 * Get the estimated statistics after applying the {@link QueryCondition} condition
 * @param condition filter to apply
 * @param index to use for generating the estimate
 * @param scanRel the current scan rel
 * @return {@link MapRDBStatisticsPayload} statistics
 */
private MapRDBStatisticsPayload getFirstKeyEstimatedStatsInternal(QueryCondition condition, IndexDesc index, RelNode scanRel) {
    // If no index is specified, get it from the primary table
    if (index == null && scanSpec.isSecondaryIndex()) {
        // table = MapRDB.getTable(scanSpec.getPrimaryTablePath());
        throw new UnsupportedOperationException("getFirstKeyEstimatedStats should be invoked on primary table");
    }
    // Get the index table or primary table and use the DB API to get the estimated number of rows. For size estimates,
    // we assume that all the columns would be read from the disk.
    final Table table = this.formatPlugin.getJsonTableCache().getTable(scanSpec.getTableName(), index, getUserName());
    if (table != null) {
        // Factor reflecting confidence in the DB estimates. If a table has few tablets, the tablet-level stats
        // might be off. The decay scalingFactor will reduce estimates when one tablet represents a significant percentage
        // of the entire table.
        double scalingFactor = 1.0;
        boolean isFullScan = false;
        final MetaTable metaTable = table.getMetaTable();
        com.mapr.db.scan.ScanStats stats = (condition == null) ? metaTable.getScanStats() : metaTable.getScanStats(condition);
        if (index == null && condition != null) {
            // Given table condition might not be on leading column. Check if the rowcount matches full table rows.
            // In that case no leading key present or does not prune enough. Treat it like so.
            com.mapr.db.scan.ScanStats noConditionPTabStats = metaTable.getScanStats();
            if (stats.getEstimatedNumRows() == noConditionPTabStats.getEstimatedNumRows()) {
                isFullScan = true;
            }
        }
        // should be selected. So the scalingFactor should not reduce the returned rows
        if (condition != null && !isFullScan) {
            double forcedScalingFactor = PrelUtil.getSettings(scanRel.getCluster()).getIndexStatsRowCountScalingFactor();
            // For 2 or less matching tablets, the error is assumed to be 50%. The Sqrt gives the decaying scalingFactor
            if (stats.getTabletCount() > 2) {
                double accuracy = 1.0 - (2.0 / stats.getTabletCount());
                scalingFactor = Math.min(1.0, 1.0 / Math.sqrt(1.0 / accuracy));
            } else {
                scalingFactor = 0.5;
            }
            if (forcedScalingFactor < 1.0 && metaTable.getScanStats().getTabletCount() < PluginConstants.JSON_TABLE_NUM_TABLETS_PER_INDEX_DEFAULT) {
                // User forced confidence scalingFactor for small tables (assumed as less than 32 tablets (~512 MB))
                scalingFactor = forcedScalingFactor;
            }
        }
        logger.info("index_plan_info: getEstimatedRowCount obtained from DB Client for {}: indexName: {}, indexInfo: {}, " + "condition: {} rowCount: {}, avgRowSize: {}, estimatedSize {}, tabletCount {}, totalTabletCount {}, " + "scalingFactor {}", this, (index == null ? "null" : index.getIndexName()), (index == null ? "null" : index.getIndexInfo()), (condition == null ? "null" : condition.toString()), stats.getEstimatedNumRows(), (stats.getEstimatedNumRows() == 0 ? 0 : stats.getEstimatedSize() / stats.getEstimatedNumRows()), stats.getEstimatedSize(), stats.getTabletCount(), metaTable.getScanStats().getTabletCount(), scalingFactor);
        return new MapRDBStatisticsPayload(scalingFactor * stats.getEstimatedNumRows(), scalingFactor * stats.getEstimatedNumRows(), ((stats.getEstimatedNumRows() == 0 ? 0 : (double) stats.getEstimatedSize() / stats.getEstimatedNumRows())));
    } else {
        logger.info("index_plan_info: getEstimatedRowCount: {} indexName: {}, indexInfo: {}, " + "condition: {} rowCount: UNKNOWN, avgRowSize: UNKNOWN", this, (index == null ? "null" : index.getIndexName()), (index == null ? "null" : index.getIndexInfo()), (condition == null ? "null" : condition.toString()));
        return new MapRDBStatisticsPayload(ROWCOUNT_UNKNOWN, ROWCOUNT_UNKNOWN, AVG_ROWSIZE_UNKNOWN);
    }
}
Also used : Table(com.mapr.db.Table) MetaTable(com.mapr.db.MetaTable) MapRDBStatisticsPayload(org.apache.drill.exec.planner.index.MapRDBStatisticsPayload) MetaTable(com.mapr.db.MetaTable)

Example 2 with MapRDBStatisticsPayload

use of org.apache.drill.exec.planner.index.MapRDBStatisticsPayload in project drill by apache.

the class JsonTableGroupScan method getAverageRowSizeStats.

/**
 * Get the estimated average rowsize. DO NOT call this API directly.
 * Call the stats API instead which modifies the counts based on preference options.
 * @param index to use for generating the estimate
 * @return row count post filtering
 */
public MapRDBStatisticsPayload getAverageRowSizeStats(IndexDescriptor index) {
    IndexDesc indexDesc = null;
    double avgRowSize = AVG_ROWSIZE_UNKNOWN;
    if (index != null) {
        indexDesc = (IndexDesc) ((MapRDBIndexDescriptor) index).getOriginalDesc();
    }
    // If no index is specified, get it from the primary table
    if (indexDesc == null && scanSpec.isSecondaryIndex()) {
        throw new UnsupportedOperationException("getAverageRowSizeStats should be invoked on primary table");
    }
    // Get the index table or primary table and use the DB API to get the estimated number of rows. For size estimates,
    // we assume that all the columns would be read from the disk.
    final Table table = this.formatPlugin.getJsonTableCache().getTable(scanSpec.getTableName(), indexDesc, getUserName());
    if (table != null) {
        final MetaTable metaTable = table.getMetaTable();
        if (metaTable != null) {
            avgRowSize = metaTable.getAverageRowSize();
        }
    }
    logger.debug("index_plan_info: getEstimatedRowCount obtained from DB Client for {}: indexName: {}, indexInfo: {}, " + "avgRowSize: {}, estimatedSize {}", this, (indexDesc == null ? "null" : indexDesc.getIndexName()), (indexDesc == null ? "null" : indexDesc.getIndexInfo()), avgRowSize, fullTableEstimatedSize);
    return new MapRDBStatisticsPayload(ROWCOUNT_UNKNOWN, ROWCOUNT_UNKNOWN, avgRowSize);
}
Also used : Table(com.mapr.db.Table) MetaTable(com.mapr.db.MetaTable) MapRDBStatisticsPayload(org.apache.drill.exec.planner.index.MapRDBStatisticsPayload) IndexDesc(com.mapr.db.index.IndexDesc) MetaTable(com.mapr.db.MetaTable) MapRDBIndexDescriptor(org.apache.drill.exec.planner.index.MapRDBIndexDescriptor)

Aggregations

MetaTable (com.mapr.db.MetaTable)2 Table (com.mapr.db.Table)2 MapRDBStatisticsPayload (org.apache.drill.exec.planner.index.MapRDBStatisticsPayload)2 IndexDesc (com.mapr.db.index.IndexDesc)1 MapRDBIndexDescriptor (org.apache.drill.exec.planner.index.MapRDBIndexDescriptor)1