Skip to content

Commit e24d787

Browse files
committed
[iceberg] Reduce redundant getTable calls in IcebergHiveMetadata
1 parent 1f3425b commit e24d787

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

presto-iceberg/src/main/java/com/facebook/presto/iceberg/IcebergHiveMetadata.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
import java.util.Optional;
8686
import java.util.Set;
8787
import java.util.TimeZone;
88+
import java.util.concurrent.ConcurrentHashMap;
89+
import java.util.concurrent.ConcurrentMap;
8890
import java.util.stream.Stream;
8991

9092
import static com.facebook.presto.hive.HiveStatisticsUtil.createPartitionStatistics;
@@ -152,6 +154,7 @@ public class IcebergHiveMetadata
152154
private final HdfsEnvironment hdfsEnvironment;
153155
private final DateTimeZone timeZone = DateTimeZone.forTimeZone(TimeZone.getTimeZone(ZoneId.of(TimeZone.getDefault().getID())));
154156
private final IcebergHiveTableOperationsConfig hiveTableOeprationsConfig;
157+
private final ConcurrentMap<SchemaTableName, Optional<Table>> tablesCache = new ConcurrentHashMap<>();
155158

156159
public IcebergHiveMetadata(
157160
ExtendedHiveMetastore metastore,
@@ -191,8 +194,7 @@ protected View getIcebergView(ConnectorSession session, SchemaTableName schemaTa
191194
@Override
192195
protected boolean tableExists(ConnectorSession session, SchemaTableName schemaTableName)
193196
{
194-
IcebergTableName name = IcebergTableName.from(schemaTableName.getTableName());
195-
Optional<Table> hiveTable = metastore.getTable(getMetastoreContext(session), schemaTableName.getSchemaName(), name.getTableName());
197+
Optional<Table> hiveTable = getHiveTable(session, schemaTableName);
196198
if (!hiveTable.isPresent()) {
197199
return false;
198200
}
@@ -202,6 +204,14 @@ protected boolean tableExists(ConnectorSession session, SchemaTableName schemaTa
202204
return true;
203205
}
204206

207+
private Optional<Table> getHiveTable(ConnectorSession session, SchemaTableName schemaTableName)
208+
{
209+
IcebergTableName name = IcebergTableName.from(schemaTableName.getTableName());
210+
return tablesCache.computeIfAbsent(
211+
schemaTableName,
212+
ignored -> metastore.getTable(getMetastoreContext(session), schemaTableName.getSchemaName(), name.getTableName()));
213+
}
214+
205215
@Override
206216
public List<String> listSchemaNames(ConnectorSession session)
207217
{
@@ -369,7 +379,7 @@ public void createView(ConnectorSession session, ConnectorTableMetadata viewMeta
369379
encodeViewData(viewData));
370380
PrincipalPrivileges principalPrivileges = buildInitialPrivilegeSet(session.getUser());
371381

372-
Optional<Table> existing = metastore.getTable(metastoreContext, viewName.getSchemaName(), viewName.getTableName());
382+
Optional<Table> existing = getHiveTable(session, viewName);
373383
if (existing.isPresent()) {
374384
if (!replace || !isPrestoView(existing.get())) {
375385
throw new ViewAlreadyExistsException(viewName);
@@ -413,7 +423,7 @@ public Map<SchemaTableName, ConnectorViewDefinition> getViews(ConnectorSession s
413423
}
414424
MetastoreContext metastoreContext = getMetastoreContext(session);
415425
for (SchemaTableName schemaTableName : tableNames) {
416-
Optional<Table> table = metastore.getTable(metastoreContext, schemaTableName.getSchemaName(), schemaTableName.getTableName());
426+
Optional<Table> table = getHiveTable(session, schemaTableName);
417427
if (table.isPresent() && isPrestoView(table.get())) {
418428
verifyAndPopulateViews(table.get(), schemaTableName, decodeViewData(table.get().getViewOriginalText().get()), views);
419429
}
@@ -527,7 +537,7 @@ public void finishStatisticsCollection(ConnectorSession session, ConnectorTableH
527537
{
528538
IcebergTableHandle icebergTableHandle = (IcebergTableHandle) tableHandle;
529539
MetastoreContext metastoreContext = getMetastoreContext(session);
530-
Table table = metastore.getTable(metastoreContext, icebergTableHandle.getSchemaTableName().getSchemaName(), icebergTableHandle.getSchemaTableName().getTableName())
540+
Table table = getHiveTable(session, icebergTableHandle.getSchemaTableName())
531541
.orElseThrow(() -> new TableNotFoundException(icebergTableHandle.getSchemaTableName()));
532542

533543
List<Column> partitionColumns = table.getPartitionColumns();

0 commit comments

Comments
 (0)