Skip to content

Commit 179ffe4

Browse files
committed
Fix getting views for Hive 2.3+ metastore
Hive 2.3 metastore provides more space for table parameter values. On certain databases (e.g. Derby, Oracle) it uses CLOB and these databases disallow `=` predicates over CLOB values. At the same time, they allow `LIKE` predicates over them. This fixes `SHOW TABLES` and queries over `information_schema.tables`.
1 parent ca9611c commit 179ffe4

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

presto-hive/src/main/java/io/prestosql/plugin/hive/metastore/thrift/ThriftHiveMetastore.java

+46-4
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ public class ThriftHiveMetastore
117117
private final Duration maxRetryTime;
118118
private final int maxRetries;
119119

120+
private volatile boolean metastoreKnownToSupportTableParamEqualsPredicate;
121+
private volatile boolean metastoreKnownToSupportTableParamLikePredicate;
122+
120123
@Inject
121124
public ThriftHiveMetastore(MetastoreLocator metastoreLocator, ThriftHiveMetastoreConfig thriftConfig)
122125
{
@@ -722,10 +725,7 @@ public Optional<List<String>> getAllViews(String databaseName)
722725
.stopOn(UnknownDBException.class)
723726
.stopOnIllegalExceptions()
724727
.run("getAllViews", stats.getGetAllViews().wrap(() -> {
725-
try (ThriftMetastoreClient client = clientProvider.createMetastoreClient()) {
726-
String filter = HIVE_FILTER_FIELD_PARAMS + PRESTO_VIEW_FLAG + " = \"true\"";
727-
return Optional.of(client.getTableNamesByFilter(databaseName, filter));
728-
}
728+
return Optional.of(getPrestoViews(databaseName));
729729
}));
730730
}
731731
catch (UnknownDBException e) {
@@ -739,6 +739,48 @@ public Optional<List<String>> getAllViews(String databaseName)
739739
}
740740
}
741741

742+
private List<String> getPrestoViews(String databaseName)
743+
throws TException
744+
{
745+
/*
746+
* Thrift call `get_table_names_by_filter` may be translated by Metastore to a SQL query against Metastore database.
747+
* Hive 2.3 on some databases uses CLOB for table parameter value column and some databases disallow `=` predicate over
748+
* CLOB values. At the same time, they allow `LIKE` predicates over them.
749+
*/
750+
String filterWithEquals = HIVE_FILTER_FIELD_PARAMS + PRESTO_VIEW_FLAG + " = \"true\"";
751+
String filterWithLike = HIVE_FILTER_FIELD_PARAMS + PRESTO_VIEW_FLAG + " LIKE \"true\"";
752+
753+
if (metastoreKnownToSupportTableParamEqualsPredicate) {
754+
try (ThriftMetastoreClient client = clientProvider.createMetastoreClient()) {
755+
return client.getTableNamesByFilter(databaseName, filterWithEquals);
756+
}
757+
}
758+
if (metastoreKnownToSupportTableParamLikePredicate) {
759+
try (ThriftMetastoreClient client = clientProvider.createMetastoreClient()) {
760+
return client.getTableNamesByFilter(databaseName, filterWithLike);
761+
}
762+
}
763+
764+
try (ThriftMetastoreClient client = clientProvider.createMetastoreClient()) {
765+
List<String> views = client.getTableNamesByFilter(databaseName, filterWithEquals);
766+
metastoreKnownToSupportTableParamEqualsPredicate = true;
767+
return views;
768+
}
769+
catch (TException | RuntimeException firstException) {
770+
try (ThriftMetastoreClient client = clientProvider.createMetastoreClient()) {
771+
List<String> views = client.getTableNamesByFilter(databaseName, filterWithLike);
772+
metastoreKnownToSupportTableParamLikePredicate = true;
773+
return views;
774+
}
775+
catch (TException | RuntimeException secondException) {
776+
if (firstException != secondException) {
777+
firstException.addSuppressed(secondException);
778+
}
779+
}
780+
throw firstException;
781+
}
782+
}
783+
742784
@Override
743785
public void createDatabase(Database database)
744786
{

0 commit comments

Comments
 (0)