Skip to content

Commit

Permalink
Extends flush_metadata_cache procedure
Browse files Browse the repository at this point in the history
Now it is possible to flush all caches related to a given table via a procedure
  • Loading branch information
skrzypo987 authored and Praveen2112 committed Sep 27, 2022
1 parent 339452c commit 0ece498
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/src/main/sphinx/connector/hive.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,11 @@ The following procedures are available:

Flush all Hive metadata caches.

* ``system.flush_metadata_cache(schema_name => ..., table_name => ...)``

Flush Hive metadata caches entries connected with selected table.
Procedure requires named parameters to be passed

* ``system.flush_metadata_cache(schema_name => ..., table_name => ..., partition_column => ARRAY[...], partition_value => ARRAY[...])``

Flush Hive metadata cache entries connected with selected partition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class FlushHiveMetastoreCacheProcedure
private static final String PROCEDURE_USAGE_EXAMPLES = format(
"Valid usages:%n" +
" - '%1$s()'%n" +
" - %1$s(%2$s => ..., %3$s => ...)" +
" - %1$s(%2$s => ..., %3$s => ..., %4$s => ARRAY['...'], %5$s => ARRAY['...'])",
PROCEDURE_NAME,
// Use lowercase parameter names per convention. In the usage example the names are not delimited.
Expand Down Expand Up @@ -113,8 +114,13 @@ private void doFlushMetadataCache(Optional<String> schemaName, Optional<String>
if (schemaName.isEmpty() && tableName.isEmpty() && partitionColumns.isEmpty()) {
cachingHiveMetastore.flushCache();
}
else if (schemaName.isPresent() && tableName.isPresent() && !partitionColumns.isEmpty()) {
cachingHiveMetastore.flushPartitionCache(schemaName.get(), tableName.get(), partitionColumns, partitionValues);
else if (schemaName.isPresent() && tableName.isPresent()) {
if (!partitionColumns.isEmpty()) {
cachingHiveMetastore.flushPartitionCache(schemaName.get(), tableName.get(), partitionColumns, partitionValues);
}
else {
cachingHiveMetastore.invalidateTable(schemaName.get(), tableName.get());
}
}
else {
throw new TrinoException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ public void testFlushPartitionCache()
// Should return 0 rows as we left cache untouched
assertQueryReturnsEmptyResult(queryUsingPartitionCacheForValue2);

// Refresh cache for schema_name => 'dummy_schema', table_name => 'dummy_table'
getQueryRunner().execute(format(
"CALL system.flush_metadata_cache(schema_name => '%s', table_name => '%s')",
HIVE_TEST_SCHEMA,
tableName));

// Should return expected rows for all partitions
assertQuery(queryUsingPartitionCacheForValue1, expectedQueryResultForValue1);
assertQuery(queryUsingPartitionCacheForValue2, expectedQueryResultForValue2);

computeActual(format("DROP TABLE %s", fullyQualifiedTestTableName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,16 @@ public void testFlushHiveMetastoreCacheProcedureCallable()
@Test
public void testIllegalFlushHiveMetastoreCacheProcedureCalls()
{
String illegalParameterMessage = "Illegal parameter set passed. Valid usages:\n - 'flush_metadata_cache()'\n - flush_metadata_cache(schema_name => ..., table_name => ..., partition_column => ARRAY['...'], partition_value => ARRAY['...'])";
String illegalParameterMessage = "Illegal parameter set passed. Valid usages:\n" +
" - 'flush_metadata_cache()'\n" +
" - flush_metadata_cache(schema_name => ..., table_name => ...)" +
" - flush_metadata_cache(schema_name => ..., table_name => ..., partition_column => ARRAY['...'], partition_value => ARRAY['...'])";

assertThatThrownBy(() -> getQueryRunner().execute("CALL system.flush_metadata_cache('dummy_schema')"))
.hasMessageContaining("Only named arguments are allowed for this procedure");

assertThatThrownBy(() -> getQueryRunner().execute("CALL system.flush_metadata_cache(schema_name => 'dummy_schema')"))
.hasMessage(illegalParameterMessage);
assertThatThrownBy(() -> getQueryRunner().execute("CALL system.flush_metadata_cache(schema_name => 'dummy_schema', table_name => 'dummy_table')"))
.hasMessage(illegalParameterMessage);

assertThatThrownBy(() -> getQueryRunner().execute("CALL system.flush_metadata_cache(schema_name => 'dummy_schema', table_name => 'dummy_table', partition_column => ARRAY['dummy_partition'])"))
.hasMessage("Parameters partition_column and partition_value should have same length");
Expand Down

0 comments on commit 0ece498

Please sign in to comment.