Search in sources :

Example 1 with ViewsMetadata

use of io.crate.metadata.view.ViewsMetadata in project crate by crate.

the class DocSchemaInfo method viewNames.

private Collection<String> viewNames() {
    ViewsMetadata viewMetadata = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
    if (viewMetadata == null) {
        return Collections.emptySet();
    }
    Set<String> views = new HashSet<>();
    extractRelationNamesForSchema(StreamSupport.stream(viewMetadata.names().spliterator(), false), schemaName, views);
    return views;
}
Also used : ViewsMetadata(io.crate.metadata.view.ViewsMetadata) HashSet(java.util.HashSet)

Example 2 with ViewsMetadata

use of io.crate.metadata.view.ViewsMetadata in project crate by crate.

the class Schemas method resolveRelation.

/**
 * Resolves the provided ident relation (table or view) against the search path.
 * @param ident
 * @param searchPath
 * @throws RelationUnknown in case a valid relation cannot be resolved in the search path.
 * @return the corresponding RelationName
 */
public RelationName resolveRelation(QualifiedName ident, SearchPath searchPath) {
    String identSchema = schemaName(ident);
    String relation = relationName(ident);
    ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
    if (identSchema == null) {
        for (String pathSchema : searchPath) {
            RelationName tableOrViewRelation = getTableOrViewRelation(pathSchema, relation, views);
            if (tableOrViewRelation != null) {
                return tableOrViewRelation;
            }
        }
    } else {
        RelationName tableOrViewRelation = getTableOrViewRelation(identSchema, relation, views);
        if (tableOrViewRelation != null) {
            return tableOrViewRelation;
        }
    }
    throw new RelationUnknown(ident.toString());
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) ViewsMetadata(io.crate.metadata.view.ViewsMetadata)

Example 3 with ViewsMetadata

use of io.crate.metadata.view.ViewsMetadata in project crate by crate.

the class Schemas method resolveView.

/**
 * @throws RelationUnknown if the view cannot be resolved against the search path.
 */
public Tuple<ViewMetadata, RelationName> resolveView(QualifiedName ident, SearchPath searchPath) {
    ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
    ViewMetadata view = null;
    RelationName viewRelationName = null;
    String identSchema = schemaName(ident);
    String viewName = relationName(ident);
    if (views != null) {
        if (identSchema == null) {
            for (String pathSchema : searchPath) {
                SchemaInfo schemaInfo = schemas.get(pathSchema);
                if (schemaInfo != null) {
                    viewRelationName = new RelationName(pathSchema, viewName);
                    view = views.getView(viewRelationName);
                    if (view != null) {
                        break;
                    }
                }
            }
        } else {
            viewRelationName = new RelationName(identSchema, viewName);
            view = views.getView(viewRelationName);
        }
    }
    if (view == null) {
        throw new RelationUnknown(viewName);
    }
    return Tuple.tuple(view, viewRelationName);
}
Also used : RelationUnknown(io.crate.exceptions.RelationUnknown) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) ViewMetadata(io.crate.metadata.view.ViewMetadata) BlobSchemaInfo(io.crate.metadata.blob.BlobSchemaInfo) PgCatalogSchemaInfo(io.crate.metadata.pgcatalog.PgCatalogSchemaInfo) SysSchemaInfo(io.crate.metadata.sys.SysSchemaInfo) SchemaInfo(io.crate.metadata.table.SchemaInfo) InformationSchemaInfo(io.crate.metadata.information.InformationSchemaInfo)

Example 4 with ViewsMetadata

use of io.crate.metadata.view.ViewsMetadata in project crate by crate.

the class ViewsITest method testViewCanBeCreatedSelectedAndThenDropped.

@Test
public void testViewCanBeCreatedSelectedAndThenDropped() throws Exception {
    execute("create table t1 (x int)");
    execute("insert into t1 (x) values (1)");
    execute("refresh table t1");
    execute("create view v1 as select * from t1 where x > ?", $(0));
    for (ClusterService clusterService : internalCluster().getInstances(ClusterService.class)) {
        ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
        assertThat(views, Matchers.notNullValue());
        assertThat(views.contains(RelationName.fromIndexName(sqlExecutor.getCurrentSchema() + ".v1")), is(true));
    }
    assertThat(printedTable(execute("select * from v1").rows()), is("1\n"));
    assertThat(printedTable(execute("select view_definition from information_schema.views").rows()), is("SELECT *\nFROM \"t1\"\nWHERE \"x\" > 0\n\n"));
    execute("drop view v1");
    for (ClusterService clusterService : internalCluster().getInstances(ClusterService.class)) {
        ViewsMetadata views = clusterService.state().metadata().custom(ViewsMetadata.TYPE);
        assertThat(views.contains(RelationName.fromIndexName(sqlExecutor.getCurrentSchema() + ".v1")), is(false));
    }
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) Test(org.junit.Test)

Example 5 with ViewsMetadata

use of io.crate.metadata.view.ViewsMetadata in project crate by crate.

the class Schemas method getNewCurrentSchemas.

@VisibleForTesting
static Set<String> getNewCurrentSchemas(Metadata metadata) {
    Set<String> schemas = new HashSet<>();
    // 'doc' schema is always available and has the special property that its indices
    // don't have to be prefixed with the schema name
    schemas.add(DOC_SCHEMA_NAME);
    for (String index : metadata.getConcreteAllIndices()) {
        addIfSchema(schemas, index);
    }
    for (ObjectCursor<String> cursor : metadata.templates().keys()) {
        addIfSchema(schemas, cursor.value);
    }
    UserDefinedFunctionsMetadata udfMetadata = metadata.custom(UserDefinedFunctionsMetadata.TYPE);
    if (udfMetadata != null) {
        udfMetadata.functionsMetadata().stream().map(UserDefinedFunctionMetadata::schema).forEach(schemas::add);
    }
    ViewsMetadata viewsMetadata = metadata.custom(ViewsMetadata.TYPE);
    if (viewsMetadata != null) {
        StreamSupport.stream(viewsMetadata.names().spliterator(), false).map(IndexParts::new).map(IndexParts::getSchema).forEach(schemas::add);
    }
    return schemas;
}
Also used : UserDefinedFunctionsMetadata(io.crate.expression.udf.UserDefinedFunctionsMetadata) ViewsMetadata(io.crate.metadata.view.ViewsMetadata) HashSet(java.util.HashSet) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Aggregations

ViewsMetadata (io.crate.metadata.view.ViewsMetadata)6 RelationUnknown (io.crate.exceptions.RelationUnknown)2 HashSet (java.util.HashSet)2 ClusterService (org.elasticsearch.cluster.service.ClusterService)2 Test (org.junit.Test)2 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)1 UserDefinedFunctionsMetadata (io.crate.expression.udf.UserDefinedFunctionsMetadata)1 BlobSchemaInfo (io.crate.metadata.blob.BlobSchemaInfo)1 InformationSchemaInfo (io.crate.metadata.information.InformationSchemaInfo)1 PgCatalogSchemaInfo (io.crate.metadata.pgcatalog.PgCatalogSchemaInfo)1 SysSchemaInfo (io.crate.metadata.sys.SysSchemaInfo)1 SchemaInfo (io.crate.metadata.table.SchemaInfo)1 ViewMetadata (io.crate.metadata.view.ViewMetadata)1