Skip to content

Commit 1176ffd

Browse files
imsayari404tdcmeehan
authored andcommitted
Fix getting views for Hive metastore
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. Cherry-pick of prestodb@179ffe4 Co-authored-by: Piotr Findeisen <piotr.findeisen@gmail.com>
1 parent 928fdb3 commit 1176ffd

File tree

1 file changed

+46
-5
lines changed

1 file changed

+46
-5
lines changed

presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/thrift/ThriftHiveMetastore.java

+46-5
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ public class ThriftHiveMetastore
183183
private final boolean isMetastoreAuthenticationEnabled;
184184
private final boolean deleteFilesOnTableDrop;
185185

186+
private volatile boolean metastoreKnownToSupportTableParamEqualsPredicate;
187+
private volatile boolean metastoreKnownToSupportTableParamLikePredicate;
188+
186189
@Inject
187190
public ThriftHiveMetastore(HiveCluster hiveCluster, MetastoreClientConfig config, HdfsEnvironment hdfsEnvironment)
188191
{
@@ -896,11 +899,9 @@ public Optional<List<String>> getAllViews(MetastoreContext metastoreContext, Str
896899
return retry()
897900
.stopOn(UnknownDBException.class)
898901
.stopOnIllegalExceptions()
899-
.run("getAllViews", stats.getGetAllViews().wrap(() ->
900-
getMetastoreClientThenCall(metastoreContext, client -> {
901-
String filter = HIVE_FILTER_FIELD_PARAMS + PRESTO_VIEW_FLAG + " = \"true\"";
902-
return Optional.of(client.getTableNamesByFilter(databaseName, filter));
903-
})));
902+
.run("getAllViews", stats.getGetAllViews().wrap(() -> {
903+
return Optional.of(getPrestoViews(databaseName));
904+
}));
904905
}
905906
catch (UnknownDBException e) {
906907
return Optional.empty();
@@ -1209,6 +1210,46 @@ public MetastoreOperationResult addPartitions(
12091210
return EMPTY_RESULT;
12101211
}
12111212

1213+
private List<String> getPrestoViews(String databaseName)
1214+
throws TException
1215+
{
1216+
/*
1217+
* Thrift call `get_table_names_by_filter` may be translated by Metastore to a SQL query against Metastore database.
1218+
* Hive 2.3 on some databases uses CLOB for table parameter value column and some databases disallow `=` predicate over
1219+
* CLOB values. At the same time, they allow `LIKE` predicates over them.
1220+
*/
1221+
String filterWithEquals = HIVE_FILTER_FIELD_PARAMS + PRESTO_VIEW_FLAG + " = \"true\"";
1222+
String filterWithLike = HIVE_FILTER_FIELD_PARAMS + PRESTO_VIEW_FLAG + " LIKE \"true\"";
1223+
if (metastoreKnownToSupportTableParamEqualsPredicate) {
1224+
try (HiveMetastoreClient client = clientProvider.createMetastoreClient(Optional.empty())) {
1225+
return client.getTableNamesByFilter(databaseName, filterWithEquals);
1226+
}
1227+
}
1228+
if (metastoreKnownToSupportTableParamLikePredicate) {
1229+
try (HiveMetastoreClient client = clientProvider.createMetastoreClient(Optional.empty())) {
1230+
return client.getTableNamesByFilter(databaseName, filterWithLike);
1231+
}
1232+
}
1233+
try (HiveMetastoreClient client = clientProvider.createMetastoreClient(Optional.empty())) {
1234+
List<String> views = client.getTableNamesByFilter(databaseName, filterWithEquals);
1235+
metastoreKnownToSupportTableParamEqualsPredicate = true;
1236+
return views;
1237+
}
1238+
catch (TException | RuntimeException firstException) {
1239+
try (HiveMetastoreClient client = clientProvider.createMetastoreClient(Optional.empty())) {
1240+
List<String> views = client.getTableNamesByFilter(databaseName, filterWithLike);
1241+
metastoreKnownToSupportTableParamLikePredicate = true;
1242+
return views;
1243+
}
1244+
catch (TException | RuntimeException secondException) {
1245+
if (firstException != secondException) {
1246+
firstException.addSuppressed(secondException);
1247+
}
1248+
}
1249+
throw firstException;
1250+
}
1251+
}
1252+
12121253
private void addPartitionsWithoutStatistics(MetastoreContext metastoreContext, String databaseName, String tableName, List<Partition> partitions)
12131254
{
12141255
if (partitions.isEmpty()) {

0 commit comments

Comments
 (0)