private boolean resolveEntitiesIfNeeded()

in polaris-core/src/main/java/org/apache/polaris/core/persistence/transactional/PolarisEntityResolver.java [240:301]


  private boolean resolveEntitiesIfNeeded(
      @Nonnull PolarisCallContext callCtx,
      @Nonnull TransactionalPersistence ms,
      @Nullable List<PolarisEntityCore> catalogPath,
      @Nullable PolarisEntityCore resolvedEntity,
      @Nullable List<PolarisEntityCore> otherTopLevelEntities) {

    // determine the number of entities to resolved
    int resolveCount =
        ((catalogPath != null) ? catalogPath.size() : 0)
            + ((resolvedEntity != null) ? 1 : 0)
            + ((otherTopLevelEntities != null) ? otherTopLevelEntities.size() : 0);

    // nothing to do if 0
    if (resolveCount == 0) {
      return true;
    }

    // construct full list of entities to resolve
    final List<PolarisEntityCore> toResolve = new ArrayList<>(resolveCount);

    // first add the other top-level catalog entities, then the catalog path, then the entity
    if (otherTopLevelEntities != null) {
      toResolve.addAll(otherTopLevelEntities);
    }
    if (catalogPath != null) {
      toResolve.addAll(catalogPath);
    }
    if (resolvedEntity != null) {
      toResolve.add(resolvedEntity);
    }

    // now build a list of entity active keys
    List<PolarisEntitiesActiveKey> entityActiveKeys =
        toResolve.stream()
            .map(
                entityCore ->
                    new PolarisEntitiesActiveKey(
                        entityCore.getCatalogId(),
                        entityCore.getParentId(),
                        entityCore.getTypeCode(),
                        entityCore.getName()))
            .collect(Collectors.toList());

    // now lookup all these entities by name
    Iterator<EntityNameLookupRecord> activeRecordIt =
        ms.lookupEntityActiveBatchInCurrentTxn(callCtx, entityActiveKeys).iterator();

    // now validate if there was a change and if yes, re-resolve again
    for (PolarisEntityCore resolveEntity : toResolve) {
      // get associate active record
      EntityNameLookupRecord activeEntityRecord = activeRecordIt.next();

      // if this entity has been dropped (null) or replaced (<> ids), then fail validation
      if (activeEntityRecord == null || activeEntityRecord.getId() != resolveEntity.getId()) {
        return false;
      }
    }

    // all good, everything was resolved successfully
    return true;
  }