hello-stephen commented on code in PR #71246:
URL: https://github.com/apache/doris/pull/71246#discussion_r4808112456


##########
gensrc/thrift/PlanNodes.thrift:
##########
@@ -87,7 +87,8 @@ enum TChangelogScanType {
   APPEND_ONLY = 1,
   MIN_DELTA = 2,
   DETAIL = 3,
-  UNKNOWN = 4
+  SNAPSHOT_BEFORE = 4,

Review Comment:
   [P2] Allocate a fresh value for SNAPSHOT_BEFORE
   
   The pinned master base already assigns `TChangelogScanType` value `4` to 
`UNKNOWN`. Reusing `4` for `SNAPSHOT_BEFORE` changes the meaning of an existing 
enum ID, even though this enum is absent from the four pinned release branches. 
Keep `UNKNOWN = 4` and use a fresh value for the new mode, as required by 
`gensrc/AGENTS.md`.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/NormalizeOlapTableStreamScan.java:
##########
@@ -329,41 +326,44 @@ private Plan makeSnapshotScan(LogicalOlapTableStreamScan 
scan, CascadesContext c
             normalPlan = makeOlapScanOnBaseTable(scan, cascadesContext, 
baseTable, normalPartitionIds);
         }
         if (!rebuildPartitionIds.isEmpty()) {
+            if (!baseTable.getChangelogConfig().getNeedHistoricalValue()) {
+                throw new AnalysisException("Snapshot reconstruction requires 
changelog.need_historical_value=true"
+                        + " on table " + baseTable.getQualifiedName());
+            }
             // base table scan part
             // build base table offset
             // for row commit tso <= consumption tso we scan from base table
             Map<Long, Pair<Long, Long>> partitionOffsetMap =
                     
streamWrapper.getHistoryPartitionOffsets(rebuildPartitionIds);
             OlapTableWrapper table = new OlapTableWrapper(baseTable, 
makePartitionScanStates(
-                    streamWrapper, rebuildPartitionIds, partitionOffsetMap, 
Collections.emptyMap()));
+                    scan, rebuildPartitionIds, partitionOffsetMap, 
Collections.emptyMap()));
             Plan basePartPlan = makeOlapScanOnBaseTable(scan, cascadesContext, 
table, rebuildPartitionIds);
             // we rebuild by add back updated & deleted rows from changelog
             Plan changelogPartPlan = 
makeIncrementalScanFromChangelog(cascadesContext, scan, rebuildPartitionIds,
                     baseTable, 
streamWrapper.getPartitionOffsets(rebuildPartitionIds),
-                    BaseTableStream.StreamScanType.MIN_DELTA, originSlots, 
originSlots, false);
+                    BaseTableStream.StreamScanType.SNAPSHOT_BEFORE, 
originSlots, originSlots);
             rebuildPlan = combineTwoPlan(cascadesContext, basePartPlan, 
changelogPartPlan, originSlots);
         }
         return projectToOriginSlots(combineTwoPlan(cascadesContext, 
normalPlan, rebuildPlan, originSlots), originSlots);
     }
 
-    private Map<Long, PartitionScanState> 
makePartitionScanStates(OlapTableStreamWrapper wrapper,
+    private Map<Long, PartitionScanState> 
makePartitionScanStates(LogicalOlapTableStreamScan scan,
             List<Long> partitionIds, Map<Long, Pair<Long, Long>> offsets, 
Map<Long, Long> rowCountEstimates) {
+        OlapTableStreamWrapper wrapper = scan.getTable();
+        boolean pinVersion = scan.isSnapshot() || wrapper.hasCloudReadStates();
         Map<Long, PartitionScanState> states = 
Maps.newHashMapWithExpectedSize(partitionIds.size());
         for (Long id : partitionIds) {
-            Long visibleVersion = wrapper.hasCloudReadStates()
-                    ? Preconditions.checkNotNull(wrapper.getCloudReadState(id),
-                            "Cloud Table Stream read state is missing for 
partition %s", id).getVisibleVersion()
-                    : null;
-            states.put(id, new PartitionScanState(offsets.get(id), 
visibleVersion, rowCountEstimates.get(id)));
+            states.put(id, new PartitionScanState(
+                    offsets.get(id), pinVersion ? 
wrapper.getReadVisibleVersion(id) : null, rowCountEstimates.get(id)));
         }
         return states;
     }
 
     private Plan makeOlapScanOnBaseTable(LogicalOlapTableStreamScan scan, 
CascadesContext cascadesContext,
                                          OlapTable baseTable, List<Long> 
partitionIds) {
-        if (scan.getTable().hasCloudReadStates() && !(baseTable instanceof 
OlapTableWrapper)) {
+        if ((scan.isSnapshot() || scan.getTable().hasCloudReadStates()) && 
!(baseTable instanceof OlapTableWrapper)) {

Review Comment:
   [P2] Preserve secondary indexes on the pinned base scan
   
   For a caught-up shared-nothing UNIQUE stream whose base table has an 
inverted index on `payload`, `SELECT k FROM s@snapshot() WHERE 
search('payload:error')` now lowers to a filter on an `OlapTableWrapper`. The 
wrapper copies the table properties but leaves `OlapTable.indexes` unset. After 
filter pushdown, `RewriteSearchToSlots` checks that wrapper and rejects the 
query as having no inverted index. The same rewrite previously reached the 
original indexed table. Preserve base index metadata on the pinned base view 
without exposing it on changelog views.



##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/NormalizeOlapTableStreamScan.java:
##########
@@ -329,41 +326,44 @@ private Plan makeSnapshotScan(LogicalOlapTableStreamScan 
scan, CascadesContext c
             normalPlan = makeOlapScanOnBaseTable(scan, cascadesContext, 
baseTable, normalPartitionIds);
         }
         if (!rebuildPartitionIds.isEmpty()) {
+            if (!baseTable.getChangelogConfig().getNeedHistoricalValue()) {
+                throw new AnalysisException("Snapshot reconstruction requires 
changelog.need_historical_value=true"
+                        + " on table " + baseTable.getQualifiedName());
+            }
             // base table scan part
             // build base table offset
             // for row commit tso <= consumption tso we scan from base table
             Map<Long, Pair<Long, Long>> partitionOffsetMap =
                     
streamWrapper.getHistoryPartitionOffsets(rebuildPartitionIds);
             OlapTableWrapper table = new OlapTableWrapper(baseTable, 
makePartitionScanStates(
-                    streamWrapper, rebuildPartitionIds, partitionOffsetMap, 
Collections.emptyMap()));
+                    scan, rebuildPartitionIds, partitionOffsetMap, 
Collections.emptyMap()));
             Plan basePartPlan = makeOlapScanOnBaseTable(scan, cascadesContext, 
table, rebuildPartitionIds);
             // we rebuild by add back updated & deleted rows from changelog
             Plan changelogPartPlan = 
makeIncrementalScanFromChangelog(cascadesContext, scan, rebuildPartitionIds,
                     baseTable, 
streamWrapper.getPartitionOffsets(rebuildPartitionIds),
-                    BaseTableStream.StreamScanType.MIN_DELTA, originSlots, 
originSlots, false);
+                    BaseTableStream.StreamScanType.SNAPSHOT_BEFORE, 
originSlots, originSlots);

Review Comment:
   [P1] Prevent duplicate snapshot rows after base compaction
   
   If an upsert of `k=30` flushes while `k=10` is visible at the stream offset, 
it records that row as its predecessor. If another transaction deletes `k` and 
base FULL compaction prunes the tombstone before the upsert publishes, publish 
only probes the replacement rowset. The `KEY_NOT_FOUND` path in 
`BaseTablet::calc_segment_delete_bitmap` leaves the stale predecessor intact. 
Both the delete and the upsert now pass the `SNAPSHOT_BEFORE` TSO predicate, 
and the `UNION ALL` returns `k=10` twice. The old `MIN_DELTA` path emitted only 
the first before image for that key. Invalidate the stale predecessor on 
publish or keep a per-key safeguard, and cover the delayed upsert, delete, and 
full-compaction sequence.



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