Skip to content

Commit

Permalink
Implement schemaExists for Hive
Browse files Browse the repository at this point in the history
Same logic as implemented in Iceberg. This should make certain
operations faster.
  • Loading branch information
findepi committed Sep 2, 2023
1 parent 1b2b8c0 commit 9d95bff
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@
import static io.trino.spi.type.VarcharType.createUnboundedVarcharType;
import static java.lang.Boolean.parseBoolean;
import static java.lang.String.format;
import static java.util.Locale.ENGLISH;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -460,6 +461,20 @@ public DirectoryLister getDirectoryLister()
return directoryLister;
}

@Override
public boolean schemaExists(ConnectorSession session, String schemaName)
{
if (!schemaName.equals(schemaName.toLowerCase(ENGLISH))) {
// Currently, Trino schemas are always lowercase, so this one cannot exist (https://github.com/trinodb/trino/issues/17)
// In fact, some metastores (e.g. Glue) store database names lowercase only, but accepted mixed case on lookup, so we need to filter out here.
return false;
}
if (isHiveSystemSchema(schemaName)) {
return false;
}
return metastore.getDatabase(schemaName).isPresent();
}

@Override
public List<String> listSchemaNames(ConnectorSession session)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.io.File;

import static io.trino.plugin.hive.metastore.CountingAccessHiveMetastore.Method.CREATE_TABLE;
import static io.trino.plugin.hive.metastore.CountingAccessHiveMetastore.Method.GET_ALL_DATABASES;
import static io.trino.plugin.hive.metastore.CountingAccessHiveMetastore.Method.GET_DATABASE;
import static io.trino.plugin.hive.metastore.CountingAccessHiveMetastore.Method.GET_PARTITIONS_BY_NAMES;
import static io.trino.plugin.hive.metastore.CountingAccessHiveMetastore.Method.GET_PARTITION_NAMES_BY_FILTER;
Expand Down Expand Up @@ -73,7 +72,7 @@ public void testUse()
{
assertMetastoreInvocations("USE " + getSession().getSchema().orElseThrow(),
ImmutableMultiset.builder()
.add(GET_ALL_DATABASES)
.add(GET_DATABASE)
.build());
}

Expand Down Expand Up @@ -220,7 +219,7 @@ public void testDescribe()

assertMetastoreInvocations("DESCRIBE test_describe",
ImmutableMultiset.builder()
.add(GET_ALL_DATABASES)
.add(GET_DATABASE)
.add(GET_TABLE)
.build());
}
Expand Down

0 comments on commit 9d95bff

Please sign in to comment.