Search in sources :

Example 16 with DrillTable

use of org.apache.drill.exec.planner.logical.DrillTable in project drill by apache.

the class DrillRelMdRowCount method getRowCount.

@Override
public Double getRowCount(TableScan rel, RelMetadataQuery mq) {
    DrillTable table = Utilities.getDrillTable(rel.getTable());
    PlannerSettings settings = PrelUtil.getSettings(rel.getCluster());
    // If guessing, return selectivity from RelMDRowCount
    if (DrillRelOptUtil.guessRows(rel)) {
        return super.getRowCount(rel, mq);
    }
    // Return rowcount from statistics, if available. Otherwise, delegate to parent.
    try {
        if (table != null && table.getGroupScan().getTableMetadata() != null && TableStatisticsKind.HAS_DESCRIPTIVE_STATISTICS.getValue(table.getGroupScan().getTableMetadata())) {
            /* For GroupScan rely on accurate count from the scan, if available, instead of
           * statistics since partition pruning/filter pushdown might have occurred.
           * e.g. ParquetGroupScan returns accurate rowcount. The other way would be to
           * iterate over the rowgroups present in the GroupScan to compute the rowcount.
           */
            if (!table.getGroupScan().getScanStats(settings).getGroupScanProperty().hasExactRowCount()) {
                return TableStatisticsKind.EST_ROW_COUNT.getValue(table.getGroupScan().getTableMetadata());
            } else {
                if (!(rel instanceof DrillScanRelBase)) {
                    return table.getGroupScan().getScanStats(settings).getRecordCount();
                }
            }
        }
    } catch (IOException ex) {
        return super.getRowCount(rel, mq);
    }
    return super.getRowCount(rel, mq);
}
Also used : DrillTable(org.apache.drill.exec.planner.logical.DrillTable) PlannerSettings(org.apache.drill.exec.planner.physical.PlannerSettings) DrillScanRelBase(org.apache.drill.exec.planner.common.DrillScanRelBase) IOException(java.io.IOException)

Example 17 with DrillTable

use of org.apache.drill.exec.planner.logical.DrillTable in project drill by apache.

the class AnalyzeTableHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
    final SqlAnalyzeTable sqlAnalyzeTable = unwrap(sqlNode, SqlAnalyzeTable.class);
    verifyNoUnsupportedFunctions(sqlAnalyzeTable);
    SqlNode tableRef = sqlAnalyzeTable.getTableRef();
    SqlSelect scanSql = new SqlSelect(SqlParserPos.ZERO, /* position */
    SqlNodeList.EMPTY, /* keyword list */
    getColumnList(sqlAnalyzeTable), /* select list */
    tableRef, /* from */
    null, /* where */
    null, /* group by */
    null, /* having */
    null, /* windowDecls */
    null, /* orderBy */
    null, /* offset */
    null);
    ConvertedRelNode convertedRelNode = validateAndConvert(rewrite(scanSql));
    RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
    RelNode relScan = convertedRelNode.getConvertedNode();
    DrillTableInfo drillTableInfo = DrillTableInfo.getTableInfoHolder(sqlAnalyzeTable.getTableRef(), config);
    String tableName = drillTableInfo.tableName();
    AbstractSchema drillSchema = SchemaUtilites.resolveToDrillSchema(config.getConverter().getDefaultSchema(), drillTableInfo.schemaPath());
    Table table = SqlHandlerUtil.getTableFromSchema(drillSchema, tableName);
    if (table == null) {
        throw UserException.validationError().message("No table with given name [%s] exists in schema [%s]", tableName, drillSchema.getFullSchemaName()).build(logger);
    } else if (!(table instanceof DrillTable)) {
        return DrillStatsTable.notSupported(context, tableName);
    }
    DrillTable drillTable = (DrillTable) table;
    final Object selection = drillTable.getSelection();
    if (!(selection instanceof FormatSelection)) {
        return DrillStatsTable.notSupported(context, tableName);
    }
    // Do not support non-parquet tables
    FormatSelection formatSelection = (FormatSelection) selection;
    FormatPluginConfig formatConfig = formatSelection.getFormat();
    if (!((formatConfig instanceof ParquetFormatConfig) || ((formatConfig instanceof NamedFormatPluginConfig) && ((NamedFormatPluginConfig) formatConfig).getName().equals("parquet")))) {
        return DrillStatsTable.notSupported(context, tableName);
    }
    FileSystemPlugin plugin = (FileSystemPlugin) drillTable.getPlugin();
    DrillFileSystem fs = new DrillFileSystem(plugin.getFormatPlugin(formatSelection.getFormat()).getFsConf());
    Path selectionRoot = formatSelection.getSelection().getSelectionRoot();
    if (!selectionRoot.toUri().getPath().endsWith(tableName) || !fs.getFileStatus(selectionRoot).isDirectory()) {
        return DrillStatsTable.notSupported(context, tableName);
    }
    // Do not recompute statistics, if stale
    Path statsFilePath = new Path(selectionRoot, DotDrillType.STATS.getEnding());
    if (fs.exists(statsFilePath) && !isStatsStale(fs, statsFilePath)) {
        return DrillStatsTable.notRequired(context, tableName);
    }
    // Convert the query to Drill Logical plan and insert a writer operator on top.
    DrillRel drel = convertToDrel(relScan, drillSchema, tableName, sqlAnalyzeTable.getSamplePercent());
    Prel prel = convertToPrel(drel, validatedRowType);
    logAndSetTextPlan("Drill Physical", prel, logger);
    PhysicalOperator pop = convertToPop(prel);
    PhysicalPlan plan = convertToPlan(pop);
    log("Drill Plan", plan, logger);
    return plan;
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaPath(org.apache.drill.common.expression.SchemaPath) FileSystemPlugin(org.apache.drill.exec.store.dfs.FileSystemPlugin) PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) DrillTable(org.apache.drill.exec.planner.logical.DrillTable) DrillStatsTable(org.apache.drill.exec.planner.common.DrillStatsTable) SqlAnalyzeTable(org.apache.drill.exec.planner.sql.parser.SqlAnalyzeTable) Table(org.apache.calcite.schema.Table) DrillTable(org.apache.drill.exec.planner.logical.DrillTable) RelDataType(org.apache.calcite.rel.type.RelDataType) FormatSelection(org.apache.drill.exec.store.dfs.FormatSelection) NamedFormatPluginConfig(org.apache.drill.exec.store.dfs.NamedFormatPluginConfig) Prel(org.apache.drill.exec.planner.physical.Prel) SqlSelect(org.apache.calcite.sql.SqlSelect) RelNode(org.apache.calcite.rel.RelNode) DrillFileSystem(org.apache.drill.exec.store.dfs.DrillFileSystem) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) FormatPluginConfig(org.apache.drill.common.logical.FormatPluginConfig) NamedFormatPluginConfig(org.apache.drill.exec.store.dfs.NamedFormatPluginConfig) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) DrillRel(org.apache.drill.exec.planner.logical.DrillRel) ParquetFormatConfig(org.apache.drill.exec.store.parquet.ParquetFormatConfig) SqlAnalyzeTable(org.apache.drill.exec.planner.sql.parser.SqlAnalyzeTable) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

DrillTable (org.apache.drill.exec.planner.logical.DrillTable)17 IOException (java.io.IOException)8 FormatSelection (org.apache.drill.exec.store.dfs.FormatSelection)6 SchemaPath (org.apache.drill.common.expression.SchemaPath)5 Table (org.apache.calcite.schema.Table)4 PlannerSettings (org.apache.drill.exec.planner.physical.PlannerSettings)4 TableScan (org.apache.calcite.rel.core.TableScan)3 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)3 FormatPluginConfig (org.apache.drill.common.logical.FormatPluginConfig)3 DynamicDrillTable (org.apache.drill.exec.planner.logical.DynamicDrillTable)3 DrillFileSystem (org.apache.drill.exec.store.dfs.DrillFileSystem)3 Path (org.apache.hadoop.fs.Path)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 RelNode (org.apache.calcite.rel.RelNode)2 RelDataType (org.apache.calcite.rel.type.RelDataType)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2