Skip to content

Commit d60f1b4

Browse files
chenjian2664ebyhr
authored andcommitted
Filter views by schemaName in listViews and getViews in BlackHole
1 parent fa51cd1 commit d60f1b4

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

plugin/trino-blackhole/src/main/java/io/trino/plugin/blackhole/BlackHoleMetadata.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,13 +438,19 @@ public void dropView(ConnectorSession session, SchemaTableName viewName)
438438
@Override
439439
public List<SchemaTableName> listViews(ConnectorSession session, Optional<String> schemaName)
440440
{
441-
return ImmutableList.copyOf(views.keySet());
441+
return schemaName.map(schema -> views.keySet().stream()
442+
.filter(view -> view.getSchemaName().equals(schema))
443+
.collect(toImmutableList()))
444+
.orElseGet(() -> ImmutableList.copyOf(views.keySet()));
442445
}
443446

444447
@Override
445448
public Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession session, Optional<String> schemaName)
446449
{
447-
return ImmutableMap.copyOf(views);
450+
return schemaName.map(schema -> views.entrySet().stream()
451+
.filter(view -> view.getKey().getSchemaName().equals(schema))
452+
.collect(toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)))
453+
.orElseGet(() -> ImmutableMap.copyOf(views));
448454
}
449455

450456
@Override

plugin/trino-blackhole/src/test/java/io/trino/plugin/blackhole/TestBlackHoleMetadata.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.airlift.units.Duration;
1919
import io.trino.spi.connector.ConnectorOutputTableHandle;
2020
import io.trino.spi.connector.ConnectorTableMetadata;
21+
import io.trino.spi.connector.ConnectorViewDefinition;
2122
import io.trino.spi.connector.SchemaTableName;
2223
import io.trino.spi.security.TrinoPrincipal;
2324
import org.junit.jupiter.api.Test;
@@ -29,8 +30,10 @@
2930
import static io.trino.spi.StandardErrorCode.NOT_FOUND;
3031
import static io.trino.spi.connector.RetryMode.NO_RETRIES;
3132
import static io.trino.spi.security.PrincipalType.USER;
33+
import static io.trino.spi.type.BigintType.BIGINT;
3234
import static io.trino.testing.TestingConnectorSession.SESSION;
3335
import static io.trino.testing.assertions.TrinoExceptionAssert.assertTrinoExceptionThrownBy;
36+
import static java.util.Map.entry;
3437
import static java.util.concurrent.TimeUnit.SECONDS;
3538
import static org.assertj.core.api.Assertions.assertThat;
3639

@@ -84,6 +87,44 @@ void testCreateTableInNotExistSchema()
8487
.hasMessage("Schema schema1 not found");
8588
}
8689

90+
@Test
91+
void testGetView()
92+
{
93+
ConnectorViewDefinition viewDefinition = createViewDefinition();
94+
Map<SchemaTableName, ConnectorViewDefinition> views = ImmutableMap.<SchemaTableName, ConnectorViewDefinition>builder()
95+
.put(new SchemaTableName("default", "test_view"), viewDefinition)
96+
.put(new SchemaTableName("default", "test_view2"), viewDefinition)
97+
.put(new SchemaTableName("default2", "test_view2"), viewDefinition)
98+
.buildOrThrow();
99+
100+
for (Map.Entry<SchemaTableName, ConnectorViewDefinition> entry : views.entrySet()) {
101+
metadata.createView(SESSION, entry.getKey(), entry.getValue(), ImmutableMap.of(), false);
102+
}
103+
104+
assertThat(metadata.listViews(SESSION, Optional.empty()))
105+
.containsExactlyElementsOf(views.keySet());
106+
assertThat(metadata.listViews(SESSION, Optional.of("default")))
107+
.containsExactly(new SchemaTableName("default", "test_view"), new SchemaTableName("default", "test_view2"));
108+
109+
assertThat(metadata.getViews(SESSION, Optional.empty()))
110+
.containsExactlyEntriesOf(views);
111+
assertThat(metadata.getViews(SESSION, Optional.of("default2")))
112+
.containsExactly(entry(new SchemaTableName("default2", "test_view2"), viewDefinition));
113+
}
114+
115+
private static ConnectorViewDefinition createViewDefinition()
116+
{
117+
return new ConnectorViewDefinition(
118+
"SELECT * FROM test_table",
119+
Optional.of("blackhole"),
120+
Optional.of("default"),
121+
ImmutableList.of(new ConnectorViewDefinition.ViewColumn("col1", BIGINT.getTypeId(), Optional.empty())),
122+
Optional.empty(),
123+
Optional.empty(),
124+
true,
125+
ImmutableList.of());
126+
}
127+
87128
private void assertThatNoTableIsCreated()
88129
{
89130
assertThat(metadata.listTables(SESSION, Optional.empty())).isEmpty();

0 commit comments

Comments
 (0)