Search in sources :

Example 81 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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)

Example 82 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class SchemaUtilites method findSchema.

/**
 * Search and return schema with given schemaPath. First search in schema tree starting from defaultSchema,
 * if not found search starting from rootSchema. Root schema tree is derived from the defaultSchema reference.
 *
 * @param defaultSchema Reference to the default schema in complete schema tree.
 * @param schemaPath Schema path to search.
 * @return SchemaPlus object from default or root schema, or null if not found.
 */
public static SchemaPlus findSchema(final SchemaPlus defaultSchema, final List<String> schemaPath) {
    if (schemaPath.size() == 0) {
        return defaultSchema;
    }
    SchemaPlus schema = searchSchemaTree(defaultSchema, schemaPath);
    SchemaPlus rootSchema;
    if (schema == null && (rootSchema = rootSchema(defaultSchema)) != defaultSchema) {
        schema = searchSchemaTree(rootSchema, schemaPath);
    }
    return schema;
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 83 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class SchemaUtilites method resolveToDrillSchemaInternal.

private static AbstractSchema resolveToDrillSchemaInternal(SchemaPlus defaultSchema, List<String> schemaPath, boolean checkMutable) {
    final SchemaPlus schema = findSchema(defaultSchema, schemaPath);
    if (schema == null) {
        throwSchemaNotFoundException(defaultSchema, SCHEMA_PATH_JOINER.join(schemaPath));
    }
    if (checkMutable && isRootSchema(schema)) {
        throw UserException.validationError().message("Root schema is immutable. Drill does not allow creating or deleting tables or views in the root schema. " + "Select a schema using 'USE schema' command.").build(logger);
    }
    final AbstractSchema drillSchema = unwrapAsDrillSchemaInstance(schema);
    if (checkMutable && !drillSchema.isMutable()) {
        throw UserException.validationError().message("Unable to create or drop objects. Schema [%s] is immutable.", getSchemaPath(schema)).build(logger);
    }
    return drillSchema;
}
Also used : AbstractSchema(org.apache.drill.exec.store.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 84 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project drill by apache.

the class CapitalizingJdbcSchema method setHolder.

void setHolder(SchemaPlus plusOfThis) {
    for (String s : getSubSchemaNames()) {
        CapitalizingJdbcSchema inner = getSubSchema(s);
        SchemaPlus holder = plusOfThis.add(s, inner);
        inner.setHolder(holder);
    }
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus)

Example 85 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project samza by apache.

the class QueryPlanner method registerSourceSchemas.

private void registerSourceSchemas(SchemaPlus rootSchema) {
    RelSchemaConverter relSchemaConverter = new RelSchemaConverter();
    for (SqlIOConfig ssc : systemStreamConfigBySource.values()) {
        SchemaPlus previousLevelSchema = rootSchema;
        List<String> sourceParts = ssc.getSourceParts();
        RelSchemaProvider relSchemaProvider = relSchemaProviders.get(ssc.getSource());
        for (int sourcePartIndex = 0; sourcePartIndex < sourceParts.size(); sourcePartIndex++) {
            String sourcePart = sourceParts.get(sourcePartIndex);
            if (sourcePartIndex < sourceParts.size() - 1) {
                SchemaPlus sourcePartSchema = previousLevelSchema.getSubSchema(sourcePart);
                if (sourcePartSchema == null) {
                    sourcePartSchema = previousLevelSchema.add(sourcePart, new AbstractSchema());
                }
                previousLevelSchema = sourcePartSchema;
            } else {
                // If the source part is the last one, then fetch the schema corresponding to the stream and register.
                RelDataType relationalSchema = getSourceRelSchema(relSchemaProvider, relSchemaConverter);
                previousLevelSchema.add(sourcePart, createTableFromRelSchema(relationalSchema));
                break;
            }
        }
    }
}
Also used : SqlIOConfig(org.apache.samza.sql.interfaces.SqlIOConfig) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus) RelSchemaProvider(org.apache.samza.sql.interfaces.RelSchemaProvider) RelDataType(org.apache.calcite.rel.type.RelDataType)

Aggregations

SchemaPlus (org.apache.calcite.schema.SchemaPlus)180 Test (org.junit.Test)56 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)42 Connection (java.sql.Connection)39 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 ResultSet (java.sql.ResultSet)26 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)24 Table (org.apache.calcite.schema.Table)20 RelNode (org.apache.calcite.rel.RelNode)19 IOException (java.io.IOException)17 Statement (java.sql.Statement)17 SqlNode (org.apache.calcite.sql.SqlNode)17 PreparedStatement (java.sql.PreparedStatement)16 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)15 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)15 ArrayList (java.util.ArrayList)14 SQLException (java.sql.SQLException)13 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)13 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)13 Properties (java.util.Properties)12