From 9d95bfff1ebf5788f4ac162f4480ac17748f25d3 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Fri, 1 Sep 2023 21:57:08 +0200 Subject: [PATCH] Implement schemaExists for Hive Same logic as implemented in Iceberg. This should make certain operations faster. --- .../java/io/trino/plugin/hive/HiveMetadata.java | 15 +++++++++++++++ .../thrift/TestHiveMetastoreAccessOperations.java | 5 ++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java index d628d4e82070..dd64f4ada383 100644 --- a/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java +++ b/plugin/trino-hive/src/main/java/io/trino/plugin/hive/HiveMetadata.java @@ -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; @@ -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 listSchemaNames(ConnectorSession session) { diff --git a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/thrift/TestHiveMetastoreAccessOperations.java b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/thrift/TestHiveMetastoreAccessOperations.java index 427b3f8d193d..d02ef6dbb83b 100644 --- a/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/thrift/TestHiveMetastoreAccessOperations.java +++ b/plugin/trino-hive/src/test/java/io/trino/plugin/hive/metastore/thrift/TestHiveMetastoreAccessOperations.java @@ -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; @@ -73,7 +72,7 @@ public void testUse() { assertMetastoreInvocations("USE " + getSession().getSchema().orElseThrow(), ImmutableMultiset.builder() - .add(GET_ALL_DATABASES) + .add(GET_DATABASE) .build()); } @@ -220,7 +219,7 @@ public void testDescribe() assertMetastoreInvocations("DESCRIBE test_describe", ImmutableMultiset.builder() - .add(GET_ALL_DATABASES) + .add(GET_DATABASE) .add(GET_TABLE) .build()); }