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;
}
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();
}
Aggregations