yjhjstz commented on code in PR #1241:
URL: https://github.com/apache/cloudberry/pull/1241#discussion_r2254647732


##########
src/backend/access/appendonly/appendonlyam.c:
##########
@@ -1118,6 +1118,311 @@ getNextBlock(AppendOnlyScanDesc scan)
        return true;
 }
 
+static int
+appendonly_locate_target_segment(AppendOnlyScanDesc scan, int64 targrow)
+{
+       int64 rowcount;
+
+       for (int i = scan->aos_segfiles_processed - 1; i < 
scan->aos_total_segfiles; i++)
+       {
+               if (i < 0)
+                       continue;
+
+               rowcount = scan->aos_segfile_arr[i]->total_tupcount;
+               if (rowcount <= 0)
+                       continue;
+
+               if (scan->aos_segfile_arr[i]->state == 
AOSEG_STATE_AWAITING_DROP)
+               {
+                       /* skip this segment, it is awaiting drop */
+                       continue;
+               }
+
+               if (scan->segfirstrow + rowcount - 1 >= targrow)
+               {
+                       /* found the target segment */
+                       return i;
+               }
+
+               /* continue next segment */
+               scan->segfirstrow += rowcount;
+               scan->segrowsprocessed = 0;
+       }
+
+       /* row is beyond the total number of rows in the relation */
+       return -1;
+}
+
+/*
+ * returns the segfile number in which `targrow` locates  
+ */
+static int
+appendonly_getsegment(AppendOnlyScanDesc scan, int64 targrow)
+{
+       int segidx, segno;
+
+       /* locate the target segment */
+       segidx = appendonly_locate_target_segment(scan, targrow);
+       if (segidx < 0)
+       {
+               CloseScannedFileSeg(scan);
+
+               /* done reading all segfiles */
+               Assert(scan->aos_done_all_segfiles);
+
+               return -1;
+       }
+
+       if (segidx + 1 > scan->aos_segfiles_processed)
+       {
+               /* done current segfile */
+               CloseScannedFileSeg(scan);
+               /*
+                * Adjust aos_segfiles_processed to guide
+                * SetNextFileSegForRead() opening next
+                * right segfile.
+                */
+               scan->aos_segfiles_processed = segidx;
+       }
+
+       segno = scan->aos_segfile_arr[segidx]->segno;
+       Assert(segno > InvalidFileSegNumber && segno <= 
AOTupleId_MaxSegmentFileNum);

Review Comment:
   #define InvalidFileSegNumber                 -1 
   import by `Initial Greenplum code dump` [6b0e52b]



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