Skip to content

Commit

Permalink
Optimize materialized_views with schema IN predicate
Browse files Browse the repository at this point in the history
Before the changes, `system.metadata.materialized_views` queried with
`schema_name IN ...` predicate would first read all materialized views
from all schemas within catalog, and only then filter by schema. This
commit introduces filtering by schema first. For now this is done on the
engine side, but with something like generified `Constraint` class we
could give the connector the opportunity to process the information even
better.
  • Loading branch information
findepi committed Oct 8, 2023
1 parent 2d163d5 commit 08ef2ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package io.trino.connector.system;

import com.google.inject.Inject;
import io.airlift.slice.Slice;
import io.trino.FullConnectorSession;
import io.trino.Session;
import io.trino.metadata.Metadata;
Expand Down Expand Up @@ -107,13 +108,23 @@ public RecordCursor cursor(
return displayTable.build().cursor();
}

Optional<String> schemaFilter = tryGetSingleVarcharValue(schemaDomain);
Optional<String> tableFilter = tryGetSingleVarcharValue(tableDomain);

listCatalogNames(session, metadata, accessControl, catalogDomain).forEach(catalogName -> {
QualifiedTablePrefix tablePrefix = tablePrefix(catalogName, schemaFilter, tableFilter);

addMaterializedViewForCatalog(session, displayTable, tablePrefix);
// TODO A connector may be able to pull information from multiple schemas at once, so pass the schema filter to the connector instead.
// TODO Support LIKE predicates on schema name (or any other functional predicates), so pass the schema filter as Constraint-like to the connector.
if (schemaDomain.isNullableDiscreteSet()) {
for (Object slice : schemaDomain.getNullableDiscreteSet().getNonNullValues()) {
String schemaName = ((Slice) slice).toStringUtf8();
if (isImpossibleObjectName(schemaName)) {
continue;
}
addMaterializedViewForCatalog(session, displayTable, tablePrefix(catalogName, Optional.of(schemaName), tableFilter));
}
}
else {
addMaterializedViewForCatalog(session, displayTable, tablePrefix(catalogName, Optional.empty(), tableFilter));
}
});

return displayTable.build().cursor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ public void testMaterializedViewsMetadataCalls()
// TODO introduce materialized views in CountingMockConnector
"VALUES (0, 0, 0, 0)",
ImmutableMultiset.<String>builder()
.add("ConnectorMetadata.getMaterializedViews")
.add("ConnectorMetadata.getMaterializedViews(schema=test_schema1)")
.add("ConnectorMetadata.getMaterializedViews(schema=test_schema2)")
.build());

// Multiple schemas
Expand All @@ -320,7 +321,11 @@ public void testMaterializedViewsMetadataCalls()
// TODO introduce materialized views in CountingMockConnector
"VALUES (0, 0, 0, 0)",
ImmutableMultiset.<String>builder()
.add("ConnectorMetadata.getMaterializedViews")
.add("ConnectorMetadata.getMaterializedViews(schema=test_schema1)")
.add("ConnectorMetadata.getMaterializedViews(schema=test_schema2)")
.addAll(IntStream.range(1, MAX_PREFIXES_COUNT + 1)
.mapToObj("ConnectorMetadata.getMaterializedViews(schema=bogus_schema%s)"::formatted)
.toList())
.build());

// Small LIMIT
Expand Down

0 comments on commit 08ef2ee

Please sign in to comment.