Search in sources :

Example 1 with Records

use of org.apache.drill.exec.store.ischema.Records in project drill by apache.

the class ShowFilesHandler method getPlan.

@Override
public PhysicalPlan getPlan(SqlNode sqlNode) throws ForemanSetupException {
    SchemaPlus defaultSchema = config.getConverter().getDefaultSchema();
    SchemaPlus drillSchema = defaultSchema;
    SqlShowFiles showFiles = unwrap(sqlNode, SqlShowFiles.class);
    SqlIdentifier from = showFiles.getDb();
    String fromDir = null;
    // Show files can be used without from clause, in which case we display the files in the default schema
    if (from != null) {
        // We are not sure if the full from clause is just the schema or includes table name,
        // first try to see if the full path specified is a schema
        drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names);
        if (drillSchema == null) {
            // Entire from clause is not a schema, try to obtain the schema without the last part of the specified clause.
            drillSchema = SchemaUtilites.findSchema(defaultSchema, from.names.subList(0, from.names.size() - 1));
            // Listing for specific directory: show files in dfs.tmp.specific_directory
            fromDir = from.names.get((from.names.size() - 1));
        }
        if (drillSchema == null) {
            throw UserException.validationError().message("Invalid FROM/IN clause [%s]", from.toString()).build(logger);
        }
    }
    WorkspaceSchema wsSchema;
    try {
        wsSchema = (WorkspaceSchema) drillSchema.unwrap(AbstractSchema.class).getDefaultSchema();
    } catch (ClassCastException e) {
        throw UserException.validationError().message("SHOW FILES is supported in workspace type schema only. Schema [%s] is not a workspace schema.", SchemaUtilites.getSchemaPath(drillSchema)).build(logger);
    }
    Path endPath = fromDir == null ? new Path(wsSchema.getDefaultLocation()) : new Path(wsSchema.getDefaultLocation(), fromDir);
    // add URI to the path to ensure that directory objects are skipped (see S3AFileSystem.listStatus method)
    Path path = new Path(wsSchema.getFS().getUri().toString(), endPath);
    List<ShowFilesCommandResult> records = FileSystemUtil.listAllSafe(wsSchema.getFS(), path, false).stream().map(fileStatus -> new ShowFilesCommandResult(new Records.File(wsSchema.getFullSchemaName(), wsSchema, fileStatus))).collect(Collectors.toList());
    return DirectPlan.createDirectPlan(context.getCurrentEndpoint(), records, ShowFilesCommandResult.class);
}
Also used : Path(org.apache.hadoop.fs.Path) SchemaUtilites(org.apache.drill.exec.planner.sql.SchemaUtilites) SqlShowFiles(org.apache.drill.exec.planner.sql.parser.SqlShowFiles) SchemaPlus(org.apache.calcite.schema.SchemaPlus) Records(org.apache.drill.exec.store.ischema.Records) Logger(org.slf4j.Logger) UserException(org.apache.drill.common.exceptions.UserException) Timestamp(java.sql.Timestamp) WorkspaceSchema(org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.WorkspaceSchema) Collectors(java.util.stream.Collectors) FileSystemUtil(org.apache.drill.exec.util.FileSystemUtil) List(java.util.List) SqlNode(org.apache.calcite.sql.SqlNode) ForemanSetupException(org.apache.drill.exec.work.foreman.ForemanSetupException) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) DirectPlan(org.apache.drill.exec.planner.sql.DirectPlan) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) PhysicalPlan(org.apache.drill.exec.physical.PhysicalPlan) Path(org.apache.hadoop.fs.Path) WorkspaceSchema(org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.WorkspaceSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SqlIdentifier(org.apache.calcite.sql.SqlIdentifier) SqlShowFiles(org.apache.drill.exec.planner.sql.parser.SqlShowFiles) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) Records(org.apache.drill.exec.store.ischema.Records)

Aggregations

Timestamp (java.sql.Timestamp)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 SqlIdentifier (org.apache.calcite.sql.SqlIdentifier)1 SqlNode (org.apache.calcite.sql.SqlNode)1 UserException (org.apache.drill.common.exceptions.UserException)1 PhysicalPlan (org.apache.drill.exec.physical.PhysicalPlan)1 DirectPlan (org.apache.drill.exec.planner.sql.DirectPlan)1 SchemaUtilites (org.apache.drill.exec.planner.sql.SchemaUtilites)1 SqlShowFiles (org.apache.drill.exec.planner.sql.parser.SqlShowFiles)1 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)1 WorkspaceSchema (org.apache.drill.exec.store.dfs.WorkspaceSchemaFactory.WorkspaceSchema)1 Records (org.apache.drill.exec.store.ischema.Records)1 FileSystemUtil (org.apache.drill.exec.util.FileSystemUtil)1 ForemanSetupException (org.apache.drill.exec.work.foreman.ForemanSetupException)1 Path (org.apache.hadoop.fs.Path)1 Logger (org.slf4j.Logger)1 LoggerFactory.getLogger (org.slf4j.LoggerFactory.getLogger)1