Skip to content

Commit

Permalink
Add log lines to help debug NPE in resolveLocationForPath (#585)
Browse files Browse the repository at this point in the history
* Add log line to debug npe in resolveLocationForPath

* Fix per Yufei's comments

* use checkNotNull
  • Loading branch information
d87zhang authored Jan 6, 2025
1 parent 4747bbe commit ebfc008
Showing 1 changed file with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -351,7 +352,7 @@ protected String defaultWarehouseLocation(TableIdentifier tableIdentifier) {
"Namespace does not exist: %s", tableIdentifier.namespace());
}
List<PolarisEntity> namespacePath = resolvedNamespace.getRawFullPath();
String namespaceLocation = resolveLocationForPath(namespacePath);
String namespaceLocation = resolveLocationForPath(callContext, namespacePath);
return SLASH.join(namespaceLocation, tableIdentifier.name());
}
}
Expand Down Expand Up @@ -532,13 +533,14 @@ private String resolveNamespaceLocation(Namespace namespace, Map<String, String>
? getResolvedParentNamespace(namespace).getRawFullPath()
: List.of(resolvedEntityView.getResolvedReferenceCatalogEntity().getRawLeafEntity());

String parentLocation = resolveLocationForPath(parentPath);
String parentLocation = resolveLocationForPath(callContext, parentPath);

return parentLocation + "/" + namespace.level(namespace.length() - 1);
}
}

private static @Nonnull String resolveLocationForPath(List<PolarisEntity> parentPath) {
private static @Nonnull String resolveLocationForPath(
@Nonnull CallContext callContext, List<PolarisEntity> parentPath) {
// always take the first object. If it has the base-location, stop there
AtomicBoolean foundBaseLocation = new AtomicBoolean(false);
return parentPath.reversed().stream()
Expand All @@ -551,24 +553,45 @@ private String resolveNamespaceLocation(Namespace namespace, Map<String, String>
.toList()
.reversed()
.stream()
.map(
entity -> {
if (entity.getType().equals(PolarisEntityType.CATALOG)) {
return CatalogEntity.of(entity).getDefaultBaseLocation();
} else {
String baseLocation =
entity.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
if (baseLocation != null) {
return baseLocation;
} else {
return entity.getName();
}
}
})
.map(entity -> baseLocation(callContext, entity))
.map(BasePolarisCatalog::stripLeadingTrailingSlash)
.collect(Collectors.joining("/"));
}

private static @Nullable String baseLocation(
@Nonnull CallContext callContext, PolarisEntity entity) {
if (entity.getType().equals(PolarisEntityType.CATALOG)) {
CatalogEntity catEntity = CatalogEntity.of(entity);
String catalogDefaultBaseLocation = catEntity.getDefaultBaseLocation();
callContext
.getPolarisCallContext()
.getDiagServices()
.checkNotNull(
catalogDefaultBaseLocation,
"Tried to resolve location with catalog with null default base location",
"catalog = {}",
catEntity);
return catalogDefaultBaseLocation;
} else {
String baseLocation =
entity.getPropertiesAsMap().get(PolarisEntityConstants.ENTITY_BASE_LOCATION);
if (baseLocation != null) {
return baseLocation;
} else {
String entityName = entity.getName();
callContext
.getPolarisCallContext()
.getDiagServices()
.checkNotNull(
entityName,
"Tried to resolve location with entity without base location or name",
"entity = {}",
entity);
return entityName;
}
}
}

private static String stripLeadingTrailingSlash(String location) {
if (location.startsWith("/")) {
return stripLeadingTrailingSlash(location.substring(1));
Expand Down

0 comments on commit ebfc008

Please sign in to comment.