github-actions[bot] commented on code in PR #62453:
URL: https://github.com/apache/doris/pull/62453#discussion_r3079942140


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertIntoTableCommand.java:
##########
@@ -285,6 +294,50 @@ public AbstractInsertExecutor initPlan(ConnectContext ctx, 
StmtExecutor stmtExec
                 return insertExecutor;
             }
 
+            List<ScanNode> tableStreamScanNodes =
+                    buildResult.planner.getScanNodes().stream()
+                            .filter(s -> s.getTableIf() instanceof 
OlapTableStreamWrapper).collect(Collectors.toList());
+
+            if (!tableStreamScanNodes.isEmpty()) {
+                // stream id -> <partition id, offset>
+                Map<Pair<Long, Long>, Map<Long, Long>> 
distinctPartitionOffsets =
+                        new HashMap<>(tableStreamScanNodes.size());
+                Map<Pair<Long, Long>, Map<Long, Long>> 
distinctPartitionNextOffsets =
+                        new HashMap<>(tableStreamScanNodes.size());
+                for (ScanNode scanNode : tableStreamScanNodes) {
+                    // only support OlapScanNode currently
+                    Preconditions.checkArgument(scanNode instanceof 
OlapScanNode);
+                    OlapScanNode olapScanNode = (OlapScanNode) scanNode;
+                    OlapTableStreamWrapper wrapper = (OlapTableStreamWrapper) 
scanNode.getTableIf();
+                    if (!distinctPartitionOffsets.containsKey(
+                            Pair.of(wrapper.getStreamDbId(), 
wrapper.getStreamId()))) {
+                        
distinctPartitionOffsets.put(Pair.of(wrapper.getStreamDbId(), 
wrapper.getStreamId()),
+                                new HashMap<>());
+                        
distinctPartitionNextOffsets.put(Pair.of(wrapper.getStreamDbId(), 
wrapper.getStreamId()),
+                                new HashMap<>());

Review Comment:
   `distinctPartitionNextOffsets` is always left empty, but 
`DatabaseTransactionMgr.checkStreamOffset()` now requires `nextOffset` for 
every partition once the stream leaves the historical path. That means a 
`show_initial_rows=true` stream can be consumed once, but the next `INSERT INTO 
... SELECT ... FROM stream` after new data arrives will fail with `nextOffset 
missing ...`. The code needs to compute and persist the post-consume offset 
here; otherwise the stream cannot progress beyond its first successful consume.



##########
fe/fe-core/src/main/java/org/apache/doris/catalog/stream/OlapTableStream.java:
##########
@@ -154,4 +164,69 @@ void fillTableStreamConsumptionInfo(List<TRow> dataBatch) {
             }
         }
     }
+
+    public boolean hasData(Partition partition) {
+        // if all available visible data has been consumed, return false
+        // todo(TsukiokaKogane): change offset from partition version to 
commit tso
+        return !partitionOffset.containsKey(partition.getId())
+                || 
!partitionOffset.get(partition.getId()).equals(partition.getVisibleVersion())
+                || historicalPartitionOffset.containsKey(partition.getId());
+    }
+
+    public Map<Long, Long> getPartitionOffset(List<Long> selectedPartitionIds) 
{
+        if (selectedPartitionIds.isEmpty()) {
+            return null;
+        }
+        Map<Long, Long> result = new HashMap<>(selectedPartitionIds.size());
+        for (Long partitionId : selectedPartitionIds) {
+            if (historicalPartitionOffset.containsKey(partitionId)) {
+                result.put(partitionId, 
historicalPartitionOffset.get(partitionId));
+            } else {
+                result.put(partitionId, 
partitionOffset.getOrDefault(partitionId, Partition.PARTITION_INIT_VERSION));
+            }
+        }
+        return result;
+    }
+
+    @Override
+    public void unprotectedCheckStreamOffset(Map<Long, Long> offset, Map<Long, 
Long> nextOffset)
+            throws UserException {
+        // check valid
+        for (Map.Entry<Long, Long> entry : offset.entrySet()) {
+            if (historicalPartitionOffset.containsKey(entry.getKey())) {
+                if 
(!historicalPartitionOffset.get(entry.getKey()).equals(entry.getValue())) {
+                    throw new TransactionCommitFailedException("history offset 
already consumed: "
+                            + getDatabase().getId() + '-' + getId() + '-' + 
entry.getKey() + '-' + entry.getValue()
+                            + " vs "  + 
historicalPartitionOffset.get(entry.getKey()));
+                }
+            } else if (partitionOffset.containsKey(entry.getKey())) {
+                if 
(!partitionOffset.get(entry.getKey()).equals(entry.getValue())) {
+                    throw new TransactionCommitFailedException("target offset 
already consumed: "
+                            + getDatabase().getId() + '-' + getId() + '-' + 
entry.getKey() + '-' + entry.getValue()
+                            + " vs "  + partitionOffset.get(entry.getKey()));
+                }
+                if (nextOffset == null || 
!nextOffset.containsKey(entry.getKey())) {
+                    throw new TransactionCommitFailedException(
+                            "nextOffset missing for partition=" + 
entry.getKey() + ", db=" + getDatabase().getId()
+                            + ", stream=" + getId());
+                }
+            } else {
+                // the new partition id may not be updated in time, so key in 
table stream not exist is also valid
+            }
+        }
+    }
+
+    @Override
+    public void unprotectedUpdateStreamOffset(Map<Long, Long> offset, 
Map<Long, Long> nextOffset, Long ts) {

Review Comment:
   This transition is not compatible with how the scan path uses 
`partitionOffset`. `OlapScanNode.addScanRangeLocations()` treats 
`partitionOffset` as the partition visible-version upper bound, so after 
consuming historical rows at version `V` this stores `V` back into the normal 
offset state. When the base table later advances to `V+1`, `hasData()` becomes 
true, but `getPartitionOffset()` still returns `V`, so the next stream read 
scans the old snapshot again and replays historical rows instead of the new 
append. The offset state here needs to represent the *next readable position*, 
not the already-consumed snapshot version.



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