924060929 commented on code in PR #70884:
URL: https://github.com/apache/doris/pull/70884#discussion_r4676813214


##########
fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonSchemaPin.java:
##########
@@ -245,6 +250,197 @@ static long fallbackSchemaId(Map<String, String> options, 
String path, LongSuppl
         return pinned == null ? defaultId.getAsLong() : Long.parseLong(pinned);
     }
 
+    static Optional<ConnectorSqlCacheVersion> resolveSqlCacheVersion(
+            Table table, String branchName, Map<String, String> scanOptions) {
+        if (!PaimonScanParams.hasPinnedVersion(scanOptions)) {
+            return Optional.empty();
+        }
+        if (hasQueryAuth(table)) {
+            // Dynamic server-side row/column authorization runs in 
scan.plan(); do not serve cached results.
+            return Optional.empty();
+        }
+        if (!coordinates(scanOptions).isEmpty()) {
+            validate(table, scanOptions);
+        }
+        Optional<ResolvedVersion> resolved = resolveTableVersion(table, 
scanOptions, "");
+        if (!resolved.isPresent()) {
+            return Optional.empty();
+        }
+        String branch = branchName == null ? "main" : branchName;
+        String payload = "branch=" + branch
+                + "|options=" + 
PaimonScanParams.canonicalCacheOptions(scanOptions)
+                + "|table=" + resolved.get().token;
+        return Optional.of(new ConnectorSqlCacheVersion(digest(payload), 
resolved.get().updateTimeMillis));
+    }
+
+    private static boolean hasQueryAuth(Table table) {
+        if (table instanceof FallbackReadFileStoreTable) {
+            FallbackReadFileStoreTable pair = (FallbackReadFileStoreTable) 
table;
+            return hasQueryAuth(pair.wrapped()) || hasQueryAuth(pair.other());
+        }
+        if (table instanceof DelegatedFileStoreTable) {
+            return hasQueryAuth(((DelegatedFileStoreTable) table).wrapped());
+        }
+        return table instanceof FileStoreTable && ((FileStoreTable) 
table).coreOptions().queryAuthEnabled();
+    }
+
+    private static Optional<ResolvedVersion> resolveTableVersion(
+            Table table, Map<String, String> scanOptions, String path) {
+        if (table instanceof FallbackReadFileStoreTable) {
+            FallbackReadFileStoreTable pair = (FallbackReadFileStoreTable) 
table;
+            Optional<ResolvedVersion> main = 
resolveTableVersion(pair.wrapped(), scanOptions, path);
+            if (!main.isPresent()) {
+                return Optional.empty();
+            }
+            Optional<ResolvedVersion> fallback = resolveFallbackBranchVersion(
+                    pair.other(), scanOptions, path + "fallback.", 
main.get().mainSnapshotMillis);
+            if (!fallback.isPresent()) {
+                return Optional.empty();
+            }
+            return Optional.of(new ResolvedVersion(
+                    "fallback(" + main.get().token + "," + 
fallback.get().token + ")",
+                    Math.max(main.get().updateTimeMillis, 
fallback.get().updateTimeMillis),
+                    main.get().mainSnapshotMillis));
+        }
+        if (table instanceof DelegatedFileStoreTable) {
+            return resolveTableVersion(((DelegatedFileStoreTable) 
table).wrapped(), scanOptions, path);
+        }
+        if (!(table instanceof DataTable)) {
+            return Optional.empty();
+        }
+        return resolveDataTableVersion((DataTable) table, scanOptions);
+    }
+
+    private static Optional<ResolvedVersion> resolveDataTableVersion(
+            DataTable data, Map<String, String> scanOptions) {
+        OptionalLong boundSchema = PaimonScanParams.boundSchemaId(scanOptions);
+        String tagName = scanOptions.get(CoreOptions.SCAN_TAG_NAME.key());
+        if (tagName != null) {
+            Optional<Tag> tagOpt = data.tagManager().get(tagName);
+            if (!tagOpt.isPresent()) {
+                return Optional.empty();
+            }
+            Tag tag = tagOpt.get();
+            long schemaId = boundSchema.orElse(tag.schemaId());
+            Optional<TableSchema> schema = readSchema(data.schemaManager(), 
schemaId);
+            if (!schema.isPresent()) {
+                return Optional.empty();
+            }
+            String token = data.location() + "|schema:" + schemaId + ":" + 
schemaDigest(schema.get())
+                    + "|tag:" + tagName + ":" + tag.id() + ":" + 
digest(tag.toJson());
+            return Optional.of(new ResolvedVersion(token, tag.timeMillis(), 
OptionalLong.of(tag.timeMillis())));
+        }
+        if (PaimonScanParams.isPinnedEmptyScan(scanOptions)) {
+            Optional<TableSchema> schema = boundSchema.isPresent()
+                    ? readSchema(data.schemaManager(), boundSchema.getAsLong())
+                    : data.schemaManager().latest();
+            if (!schema.isPresent()) {
+                return Optional.empty();
+            }
+            String token = data.location() + "|schema:" + schema.get().id() + 
":"
+                    + schemaDigest(schema.get()) + "|empty";
+            return Optional.of(new ResolvedVersion(token, 0L, 
OptionalLong.empty()));
+        }
+        String snapshotIdValue = 
scanOptions.get(CoreOptions.SCAN_SNAPSHOT_ID.key());
+        if (snapshotIdValue == null) {
+            return Optional.empty();
+        }
+        long snapshotId = Long.parseLong(snapshotIdValue);
+        if (!data.snapshotManager().snapshotExists(snapshotId)) {
+            return Optional.empty();
+        }
+        Snapshot snap = snapshot(data, snapshotId);
+        long schemaId = boundSchema.orElse(snap.schemaId());
+        Optional<TableSchema> schema = readSchema(data.schemaManager(), 
schemaId);
+        if (!schema.isPresent()) {
+            return Optional.empty();
+        }
+        String token = data.location() + "|schema:" + schemaId + ":" + 
schemaDigest(schema.get())
+                + "|snap:" + snapshotId + ":" + digest(snap.toJson());
+        return Optional.of(new ResolvedVersion(token, snap.timeMillis(), 
OptionalLong.of(snap.timeMillis())));
+    }
+
+    private static Optional<ResolvedVersion> resolveFallbackBranchVersion(
+            FileStoreTable other, Map<String, String> scanOptions, String 
fallbackPath,
+            OptionalLong mainSnapshotMillis) {
+        if (other instanceof FallbackReadFileStoreTable) {
+            // Chain tables nest a ChainGroupReadTable (snapshotBranch, 
deltaBranch) as pair.other();
+            // mirror capture() by walking both branches so delta-branch 
commits invalidate SQL cache.
+            FallbackReadFileStoreTable nested = (FallbackReadFileStoreTable) 
other;
+            Optional<ResolvedVersion> first = resolveFallbackBranchVersion(
+                    nested.wrapped(), scanOptions, fallbackPath, 
mainSnapshotMillis);
+            if (!first.isPresent()) {
+                return Optional.empty();
+            }
+            Optional<ResolvedVersion> second = resolveFallbackBranchVersion(
+                    nested.other(), scanOptions, fallbackPath + "fallback.", 
first.get().mainSnapshotMillis);
+            if (!second.isPresent()) {
+                return Optional.empty();
+            }
+            return Optional.of(new ResolvedVersion(
+                    "fallback(" + first.get().token + "," + second.get().token 
+ ")",
+                    Math.max(first.get().updateTimeMillis, 
second.get().updateTimeMillis),
+                    first.get().mainSnapshotMillis));
+        }
+        if (other instanceof DelegatedFileStoreTable) {
+            return resolveFallbackBranchVersion(
+                    ((DelegatedFileStoreTable) other).wrapped(), scanOptions, 
fallbackPath, mainSnapshotMillis);
+        }
+        String pinnedFallbackSchema = scanOptions.get(PREFIX + fallbackPath + 
"schema-id");
+        Optional<TableSchema> schema = pinnedFallbackSchema != null
+                ? readSchema(other.schemaManager(), 
Long.parseLong(pinnedFallbackSchema))
+                : other.schemaManager().latest();
+        if (!schema.isPresent()) {
+            return Optional.empty();
+        }
+        SnapshotManager manager = other.snapshotManager();
+        String pinnedFallbackSnapshot = fallbackSnapshotId(scanOptions, 
fallbackPath);
+        Long selectedSnapshotId;
+        if (pinnedFallbackSnapshot != null) {
+            selectedSnapshotId = Long.parseLong(pinnedFallbackSnapshot);
+        } else if (Boolean.parseBoolean(scanOptions.get(PREFIX + fallbackPath 
+ "snapshot-absent"))) {
+            selectedSnapshotId = null;
+        } else if (mainSnapshotMillis.isPresent()) {
+            Snapshot eligible = 
manager.earlierOrEqualTimeMills(mainSnapshotMillis.getAsLong());

Review Comment:
   Fixed in 0dec3d6a1b9: `PaimonScanParams.hasPinnedVersion` now declines 
mutable name-only `scan.tag-name` pins for SQL cache / partition-view 
versioning (resolving both fallback-branch tag divergence and mutable tag 
replacement races). `resolveFallbackBranchVersion` also resolves the fallback 
branch's effective historical `snap.schemaId()` when no explicit `schema-pin` 
coordinate is present.



##########
fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonSchemaPin.java:
##########
@@ -245,6 +250,197 @@ static long fallbackSchemaId(Map<String, String> options, 
String path, LongSuppl
         return pinned == null ? defaultId.getAsLong() : Long.parseLong(pinned);
     }
 
+    static Optional<ConnectorSqlCacheVersion> resolveSqlCacheVersion(
+            Table table, String branchName, Map<String, String> scanOptions) {
+        if (!PaimonScanParams.hasPinnedVersion(scanOptions)) {
+            return Optional.empty();
+        }
+        if (hasQueryAuth(table)) {

Review Comment:
   Fixed in 0dec3d6a1b9: replaced the raw cached-table `hasQueryAuth(table)` 
check with per-branch `isQueryAuthEnabled(branch, effectiveSchema, 
scanOptions)` evaluated against each physical branch's effective bound/selected 
`TableSchema` (plus any runtime/catalog/query overlay), and added tests in 
`PaimonNativePartitionAndCacheVersionTest` covering stale cached schema-0 
handles across both main and fallback-branch schema-only ALTERs.



##########
fe/fe-connector/fe-connector-paimon/src/main/java/org/apache/doris/connector/paimon/PaimonSchemaPin.java:
##########
@@ -245,6 +250,197 @@ static long fallbackSchemaId(Map<String, String> options, 
String path, LongSuppl
         return pinned == null ? defaultId.getAsLong() : Long.parseLong(pinned);
     }
 
+    static Optional<ConnectorSqlCacheVersion> resolveSqlCacheVersion(
+            Table table, String branchName, Map<String, String> scanOptions) {
+        if (!PaimonScanParams.hasPinnedVersion(scanOptions)) {
+            return Optional.empty();
+        }
+        if (hasQueryAuth(table)) {
+            // Dynamic server-side row/column authorization runs in 
scan.plan(); do not serve cached results.
+            return Optional.empty();
+        }
+        if (!coordinates(scanOptions).isEmpty()) {
+            validate(table, scanOptions);
+        }
+        Optional<ResolvedVersion> resolved = resolveTableVersion(table, 
scanOptions, "");
+        if (!resolved.isPresent()) {
+            return Optional.empty();
+        }
+        String branch = branchName == null ? "main" : branchName;
+        String payload = "branch=" + branch
+                + "|options=" + 
PaimonScanParams.canonicalCacheOptions(scanOptions)
+                + "|table=" + resolved.get().token;
+        return Optional.of(new ConnectorSqlCacheVersion(digest(payload), 
resolved.get().updateTimeMillis));
+    }
+
+    private static boolean hasQueryAuth(Table table) {
+        if (table instanceof FallbackReadFileStoreTable) {
+            FallbackReadFileStoreTable pair = (FallbackReadFileStoreTable) 
table;
+            return hasQueryAuth(pair.wrapped()) || hasQueryAuth(pair.other());
+        }
+        if (table instanceof DelegatedFileStoreTable) {
+            return hasQueryAuth(((DelegatedFileStoreTable) table).wrapped());
+        }
+        return table instanceof FileStoreTable && ((FileStoreTable) 
table).coreOptions().queryAuthEnabled();
+    }
+
+    private static Optional<ResolvedVersion> resolveTableVersion(
+            Table table, Map<String, String> scanOptions, String path) {
+        if (table instanceof FallbackReadFileStoreTable) {
+            FallbackReadFileStoreTable pair = (FallbackReadFileStoreTable) 
table;
+            Optional<ResolvedVersion> main = 
resolveTableVersion(pair.wrapped(), scanOptions, path);
+            if (!main.isPresent()) {
+                return Optional.empty();
+            }
+            Optional<ResolvedVersion> fallback = resolveFallbackBranchVersion(
+                    pair.other(), scanOptions, path + "fallback.", 
main.get().mainSnapshotMillis);
+            if (!fallback.isPresent()) {
+                return Optional.empty();
+            }
+            return Optional.of(new ResolvedVersion(
+                    "fallback(" + main.get().token + "," + 
fallback.get().token + ")",
+                    Math.max(main.get().updateTimeMillis, 
fallback.get().updateTimeMillis),
+                    main.get().mainSnapshotMillis));
+        }
+        if (table instanceof DelegatedFileStoreTable) {
+            return resolveTableVersion(((DelegatedFileStoreTable) 
table).wrapped(), scanOptions, path);
+        }
+        if (!(table instanceof DataTable)) {
+            return Optional.empty();
+        }
+        return resolveDataTableVersion((DataTable) table, scanOptions);
+    }
+
+    private static Optional<ResolvedVersion> resolveDataTableVersion(
+            DataTable data, Map<String, String> scanOptions) {
+        OptionalLong boundSchema = PaimonScanParams.boundSchemaId(scanOptions);
+        String tagName = scanOptions.get(CoreOptions.SCAN_TAG_NAME.key());
+        if (tagName != null) {
+            Optional<Tag> tagOpt = data.tagManager().get(tagName);

Review Comment:
   Fixed in 0dec3d6a1b9: declined SQL cache / partition-view versioning for 
mutable name-only `scan.tag-name` pins (both direct tag time travel and 
`@options` `scan.tag-name`) while preserving tag-aware `listPartitions`, and 
updated `PaimonNativePartitionAndCacheVersionTest` accordingly.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to