tokoko commented on code in PR #4023:
URL: https://github.com/apache/polaris/pull/4023#discussion_r3396897728


##########
polaris-core/src/main/java/org/apache/polaris/core/storage/StorageConfigOverrideResolver.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.core.storage;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.polaris.core.entity.PolarisEntity;
+import org.apache.polaris.core.entity.PolarisEntityConstants;
+import org.jspecify.annotations.NonNull;
+
+/**
+ * Resolves the effective {@link PolarisStorageConfigurationInfo} for an 
entity hierarchy by
+ * applying any nearest-ancestor {@code storage_name_override} on top of the 
catalog's base storage
+ * configuration.
+ *
+ * <p>The input list is in root-to-leaf order (the catalog is at index 0). The 
walk runs
+ * leaf-to-root and:
+ *
+ * <ol>
+ *   <li>Captures the first {@code storage_name_override} encountered (nearest 
wins).
+ *   <li>Returns the first {@code storage_configuration_info} encountered — 
the base config.
+ * </ol>
+ *
+ * <p>If a base config exists and an override was captured, the result is 
{@link
+ * PolarisStorageConfigurationInfo#withStorageName} applied to the base. If no 
base config exists,
+ * the result is empty regardless of any override.
+ */
+public final class StorageConfigOverrideResolver {
+
+  private StorageConfigOverrideResolver() {}
+
+  /**
+   * Walk {@code entityPath} leaf-to-root and return the effective storage 
configuration. The base
+   * config is sourced from the nearest entity that has {@code 
storage_configuration_info} in its
+   * internal properties; if any closer ancestor (including the leaf itself) 
carries {@code
+   * storage_name_override}, that name replaces the base config's {@code 
storageName}.
+   *
+   * @param entityPath entities in root-to-leaf order; the catalog is at index 0
+   * @return effective config, or empty if no entity in the chain has a base 
storage config
+   */
+  public static Optional<PolarisStorageConfigurationInfo> 
resolveEffectiveConfig(
+      @NonNull List<? extends PolarisEntity> entityPath) {
+    String override = null;
+    for (int i = entityPath.size() - 1; i >= 0; i--) {
+      Map<String, String> internalProps = 
entityPath.get(i).getInternalPropertiesAsMap();
+      if (override == null) {
+        String candidate =
+            
internalProps.get(PolarisEntityConstants.getStorageNameOverridePropertyName());
+        if (candidate != null && !candidate.isEmpty()) {
+          override = candidate;
+        }
+      }
+      String serializedBase =
+          
internalProps.get(PolarisEntityConstants.getStorageConfigInfoPropertyName());
+      if (serializedBase != null) {
+        PolarisStorageConfigurationInfo base =
+            PolarisStorageConfigurationInfo.deserialize(serializedBase);
+        if (override != null) {
+          return 
Optional.of(PolarisStorageConfigurationInfo.withStorageName(base, override));

Review Comment:
   I remember that introducing a separate crud resource for storages was 
rejected, but iiuc what you're talking about is allowing multiple 
`PolarisStorageConfigurationInfo` to be defined on catalog level and then 
tables/namespaces would be able to choose between them, is that right? I 
suppose that's a reasonably clean approach.
   
   One thing to note... `storagaName` field from #3409 is more of a pointer atm 
rather than a unique name designation for a specific 
`PolarisStorageConfigurationInfo`. I suppose it can be both, but it might be a 
little awkward: for example what if two catalogs define a storage config with 
same names, but want to resolve different credentials from the server config. I 
guess it shouldn't be a problem as long as it's well-documented.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to