Search in sources :

Example 6 with MaterializedViewDefinition

use of io.trino.metadata.MaterializedViewDefinition in project trino by trinodb.

the class TestMaterializedViews method createLocalQueryRunner.

@Override
protected LocalQueryRunner createLocalQueryRunner() {
    Session.SessionBuilder sessionBuilder = testSessionBuilder().setCatalog(CATALOG).setSchema(SCHEMA).setSystemProperty("task_concurrency", // these tests don't handle exchanges from local parallel
    "1");
    TestingMetadata testingConnectorMetadata = new TestingMetadata();
    LocalQueryRunner queryRunner = LocalQueryRunner.create(sessionBuilder.build());
    queryRunner.createCatalog(CATALOG, new StaticConnectorFactory("test", new TestMaterializedViewConnector(testingConnectorMetadata)), ImmutableMap.of());
    Metadata metadata = queryRunner.getMetadata();
    SchemaTableName testTable = new SchemaTableName(SCHEMA, "test_table");
    queryRunner.inTransaction(session -> {
        metadata.createTable(session, CATALOG, new ConnectorTableMetadata(testTable, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT))), false);
        return null;
    });
    SchemaTableName storageTable = new SchemaTableName(SCHEMA, "storage_table");
    queryRunner.inTransaction(session -> {
        metadata.createTable(session, CATALOG, new ConnectorTableMetadata(storageTable, ImmutableList.of(new ColumnMetadata("a", BIGINT), new ColumnMetadata("b", BIGINT))), false);
        return null;
    });
    SchemaTableName storageTableWithCasts = new SchemaTableName(SCHEMA, "storage_table_with_casts");
    queryRunner.inTransaction(session -> {
        metadata.createTable(session, CATALOG, new ConnectorTableMetadata(storageTableWithCasts, ImmutableList.of(new ColumnMetadata("a", TINYINT), new ColumnMetadata("b", VARCHAR))), false);
        return null;
    });
    QualifiedObjectName freshMaterializedView = new QualifiedObjectName(CATALOG, SCHEMA, "fresh_materialized_view");
    MaterializedViewDefinition materializedViewDefinition = new MaterializedViewDefinition("SELECT a, b FROM test_table", Optional.of(CATALOG), Optional.of(SCHEMA), ImmutableList.of(new ViewColumn("a", BIGINT.getTypeId()), new ViewColumn("b", BIGINT.getTypeId())), Optional.empty(), Identity.ofUser("some user"), Optional.of(new CatalogSchemaTableName(CATALOG, SCHEMA, "storage_table")), ImmutableMap.of());
    queryRunner.inTransaction(session -> {
        metadata.createMaterializedView(session, freshMaterializedView, materializedViewDefinition, false, false);
        return null;
    });
    testingConnectorMetadata.markMaterializedViewIsFresh(freshMaterializedView.asSchemaTableName());
    QualifiedObjectName notFreshMaterializedView = new QualifiedObjectName(CATALOG, SCHEMA, "not_fresh_materialized_view");
    queryRunner.inTransaction(session -> {
        metadata.createMaterializedView(session, notFreshMaterializedView, materializedViewDefinition, false, false);
        return null;
    });
    MaterializedViewDefinition materializedViewDefinitionWithCasts = new MaterializedViewDefinition("SELECT a, b FROM test_table", Optional.of(CATALOG), Optional.of(SCHEMA), ImmutableList.of(new ViewColumn("a", BIGINT.getTypeId()), new ViewColumn("b", BIGINT.getTypeId())), Optional.empty(), Identity.ofUser("some user"), Optional.of(new CatalogSchemaTableName(CATALOG, SCHEMA, "storage_table_with_casts")), ImmutableMap.of());
    QualifiedObjectName materializedViewWithCasts = new QualifiedObjectName(CATALOG, SCHEMA, "materialized_view_with_casts");
    queryRunner.inTransaction(session -> {
        metadata.createMaterializedView(session, materializedViewWithCasts, materializedViewDefinitionWithCasts, false, false);
        return null;
    });
    testingConnectorMetadata.markMaterializedViewIsFresh(materializedViewWithCasts.asSchemaTableName());
    return queryRunner;
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) ViewColumn(io.trino.metadata.ViewColumn) ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TestingMetadata(io.trino.testing.TestingMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) Metadata(io.trino.metadata.Metadata) ConnectorMetadata(io.trino.spi.connector.ConnectorMetadata) SchemaTableName(io.trino.spi.connector.SchemaTableName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) LocalQueryRunner(io.trino.testing.LocalQueryRunner) StaticConnectorFactory(io.trino.connector.StaticConnectorFactory) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) CatalogSchemaTableName(io.trino.spi.connector.CatalogSchemaTableName) MaterializedViewDefinition(io.trino.metadata.MaterializedViewDefinition) TestingMetadata(io.trino.testing.TestingMetadata) ConnectorTableMetadata(io.trino.spi.connector.ConnectorTableMetadata) ConnectorSession(io.trino.spi.connector.ConnectorSession) Session(io.trino.Session)

Example 7 with MaterializedViewDefinition

use of io.trino.metadata.MaterializedViewDefinition in project trino by trinodb.

the class CreateMaterializedViewTask method execute.

@Override
public ListenableFuture<Void> execute(CreateMaterializedView statement, QueryStateMachine stateMachine, List<Expression> parameters, WarningCollector warningCollector) {
    Session session = stateMachine.getSession();
    QualifiedObjectName name = createQualifiedObjectName(session, statement, statement.getName());
    Map<NodeRef<Parameter>, Expression> parameterLookup = parameterExtractor(statement, parameters);
    String sql = getFormattedSql(statement.getQuery(), sqlParser);
    Analysis analysis = analyzerFactory.createAnalyzer(session, parameters, parameterLookup, stateMachine.getWarningCollector()).analyze(statement);
    List<ViewColumn> columns = analysis.getOutputDescriptor(statement.getQuery()).getVisibleFields().stream().map(field -> new ViewColumn(field.getName().get(), field.getType().getTypeId())).collect(toImmutableList());
    CatalogName catalogName = getRequiredCatalogHandle(plannerContext.getMetadata(), session, statement, name.getCatalogName());
    Map<String, Object> properties = materializedViewPropertyManager.getProperties(catalogName, statement.getProperties(), session, plannerContext, accessControl, parameterLookup, true);
    MaterializedViewDefinition definition = new MaterializedViewDefinition(sql, session.getCatalog(), session.getSchema(), columns, statement.getComment(), session.getIdentity(), Optional.empty(), properties);
    accessControl.checkCanCreateMaterializedView(session.toSecurityContext(), name, properties);
    plannerContext.getMetadata().createMaterializedView(session, name, definition, statement.isReplace(), statement.isNotExists());
    stateMachine.setOutput(analysis.getTarget());
    stateMachine.setReferencedTables(analysis.getReferencedTables());
    return immediateVoidFuture();
}
Also used : ViewColumn(io.trino.metadata.ViewColumn) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) AnalyzerFactory(io.trino.sql.analyzer.AnalyzerFactory) ParameterUtils.parameterExtractor(io.trino.sql.ParameterUtils.parameterExtractor) Inject(javax.inject.Inject) CatalogName(io.trino.connector.CatalogName) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) NodeRef(io.trino.sql.tree.NodeRef) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) SqlParser(io.trino.sql.parser.SqlParser) Futures.immediateVoidFuture(com.google.common.util.concurrent.Futures.immediateVoidFuture) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) MetadataUtil.getRequiredCatalogHandle(io.trino.metadata.MetadataUtil.getRequiredCatalogHandle) CreateMaterializedView(io.trino.sql.tree.CreateMaterializedView) List(java.util.List) FeaturesConfig(io.trino.FeaturesConfig) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) AccessControl(io.trino.security.AccessControl) Parameter(io.trino.sql.tree.Parameter) SqlFormatterUtil.getFormattedSql(io.trino.sql.SqlFormatterUtil.getFormattedSql) WarningCollector(io.trino.execution.warnings.WarningCollector) MaterializedViewDefinition(io.trino.metadata.MaterializedViewDefinition) MaterializedViewPropertyManager(io.trino.metadata.MaterializedViewPropertyManager) Optional(java.util.Optional) Expression(io.trino.sql.tree.Expression) Session(io.trino.Session) PlannerContext(io.trino.sql.PlannerContext) Analysis(io.trino.sql.analyzer.Analysis) ViewColumn(io.trino.metadata.ViewColumn) MetadataUtil.createQualifiedObjectName(io.trino.metadata.MetadataUtil.createQualifiedObjectName) QualifiedObjectName(io.trino.metadata.QualifiedObjectName) NodeRef(io.trino.sql.tree.NodeRef) Expression(io.trino.sql.tree.Expression) MaterializedViewDefinition(io.trino.metadata.MaterializedViewDefinition) Analysis(io.trino.sql.analyzer.Analysis) CatalogName(io.trino.connector.CatalogName) Session(io.trino.Session)

Aggregations

MaterializedViewDefinition (io.trino.metadata.MaterializedViewDefinition)7 QualifiedObjectName (io.trino.metadata.QualifiedObjectName)6 Session (io.trino.Session)5 ViewColumn (io.trino.metadata.ViewColumn)3 FeaturesConfig (io.trino.FeaturesConfig)2 StaticConnectorFactory (io.trino.connector.StaticConnectorFactory)2 Metadata (io.trino.metadata.Metadata)2 ViewDefinition (io.trino.metadata.ViewDefinition)2 CatalogSchemaTableName (io.trino.spi.connector.CatalogSchemaTableName)2 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)2 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)2 ConnectorSession (io.trino.spi.connector.ConnectorSession)2 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)2 SchemaTableName (io.trino.spi.connector.SchemaTableName)2 CreateMaterializedView (io.trino.sql.tree.CreateMaterializedView)2 LocalQueryRunner (io.trino.testing.LocalQueryRunner)2 TestingMetadata (io.trino.testing.TestingMetadata)2 TransactionId (io.trino.transaction.TransactionId)2 TransactionManager (io.trino.transaction.TransactionManager)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1