Search in sources :

Example 61 with SqlSelect

use of org.apache.calcite.sql.SqlSelect 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)

Example 62 with SqlSelect

use of org.apache.calcite.sql.SqlSelect in project drill by apache.

the class MetastoreAnalyzeTableHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ValidationException, RelConversionException, IOException, ForemanSetupException {
    if (!context.getOptions().getOption(ExecConstants.METASTORE_ENABLED_VALIDATOR)) {
        throw UserException.validationError().message("Running ANALYZE TABLE REFRESH METADATA command when Metastore is disabled (`metastore.enabled` is set to false)").build(logger);
    }
    // disables during analyze to prevent using data about locations from the Metastore
    context.getOptions().setLocalOption(ExecConstants.METASTORE_ENABLED, false);
    SqlMetastoreAnalyzeTable sqlAnalyzeTable = unwrap(sqlNode, SqlMetastoreAnalyzeTable.class);
    SqlNode tableRef = sqlAnalyzeTable.getTableRef();
    DrillTableInfo drillTableInfo = DrillTableInfo.getTableInfoHolder(tableRef, config);
    AnalyzeInfoProvider analyzeInfoProvider = drillTableInfo.drillTable().getGroupScan().getAnalyzeInfoProvider();
    if (analyzeInfoProvider == null) {
        throw UserException.validationError().message("ANALYZE is not supported for group scan [%s]", drillTableInfo.drillTable().getGroupScan()).build(logger);
    }
    ColumnNamesOptions columnNamesOptions = new ColumnNamesOptions(context.getOptions());
    // creates select with DYNAMIC_STAR column and analyze specific columns to obtain corresponding table scan
    SqlSelect scanSql = new SqlSelect(SqlParserPos.ZERO, SqlNodeList.EMPTY, getColumnList(analyzeInfoProvider.getProjectionFields(drillTableInfo.drillTable(), getMetadataType(sqlAnalyzeTable), columnNamesOptions)), tableRef, null, null, null, null, null, null, null);
    ConvertedRelNode convertedRelNode = validateAndConvert(rewrite(scanSql));
    RelDataType validatedRowType = convertedRelNode.getValidatedRowType();
    RelNode relScan = convertedRelNode.getConvertedNode();
    DrillRel drel = convertToDrel(relScan, sqlAnalyzeTable, drillTableInfo);
    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 : PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) AnalyzeInfoProvider(org.apache.drill.exec.metastore.analyze.AnalyzeInfoProvider) RelDataType(org.apache.calcite.rel.type.RelDataType) Prel(org.apache.drill.exec.planner.physical.Prel) SqlSelect(org.apache.calcite.sql.SqlSelect) RelNode(org.apache.calcite.rel.RelNode) SqlMetastoreAnalyzeTable(org.apache.drill.exec.planner.sql.parser.SqlMetastoreAnalyzeTable) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) ColumnNamesOptions(org.apache.drill.exec.metastore.ColumnNamesOptions) DrillRel(org.apache.drill.exec.planner.logical.DrillRel) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

SqlSelect (org.apache.calcite.sql.SqlSelect)62 SqlNode (org.apache.calcite.sql.SqlNode)45 SqlNodeList (org.apache.calcite.sql.SqlNodeList)29 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)20 SqlCall (org.apache.calcite.sql.SqlCall)17 RelDataType (org.apache.calcite.rel.type.RelDataType)15 BitString (org.apache.calcite.util.BitString)12 ArrayList (java.util.ArrayList)9 SqlUpdate (org.apache.calcite.sql.SqlUpdate)8 SqlInsert (org.apache.calcite.sql.SqlInsert)7 SchemaPlus (org.apache.calcite.schema.SchemaPlus)6 SqlMerge (org.apache.calcite.sql.SqlMerge)6 SqlBasicCall (org.apache.calcite.sql.SqlBasicCall)5 SqlDelete (org.apache.calcite.sql.SqlDelete)5 SqlJoin (org.apache.calcite.sql.SqlJoin)5 RelDataTypeField (org.apache.calcite.rel.type.RelDataTypeField)4 SqlOrderBy (org.apache.calcite.sql.SqlOrderBy)4 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)4 RelOptTable (org.apache.calcite.plan.RelOptTable)3 RelNode (org.apache.calcite.rel.RelNode)3