gfphoenix78 commented on code in PR #1241: URL: https://github.com/apache/cloudberry/pull/1241#discussion_r2253221002
########## 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: The value of InvalidFileSegNumber should not be assumed -1. -- 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: commits-unsubscr...@cloudberry.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cloudberry.apache.org For additional commands, e-mail: commits-h...@cloudberry.apache.org