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


##########
src/backend/access/aocs/aocsam.c:
##########
@@ -592,23 +638,37 @@ aocs_beginscan_internal(Relation relation,
 
        scan->columnScanInfo.ds = NULL;
 
+       if ((flags & SO_TYPE_ANALYZE) != 0)
+       {
+               scan->segfirstrow = 0;
+               scan->targrow = 0;
+       }
+
        GetAppendOnlyEntryAttributes(RelationGetRelid(relation),
                                                                 NULL,
                                                                 NULL,
                                                                 
&scan->checksum,
                                                                 NULL);
 
        GetAppendOnlyEntryAuxOids(relation,
-                                                         NULL, NULL, NULL,
+                                                         NULL, &blkdirrelid, 
NULL,
                                                          &visimaprelid, 
&visimapidxid);
 
        if (scan->total_seg != 0)
+       {
                AppendOnlyVisimap_Init(&scan->visibilityMap,
                                                           visimaprelid,
                                                           visimapidxid,
                                                           AccessShareLock,
                                                           
appendOnlyMetaDataSnapshot);
 
+               if ((flags & SO_TYPE_ANALYZE) != 0)

Review Comment:
   ```
   if ((flags & SO_TYPE_ANALYZE)  &&  OidIsValid(blkdirrelid)) {
     aocs_blkdirscan_init(scan);
   }
   ```
   



##########
src/backend/access/aocs/aocsam.c:
##########
@@ -467,6 +472,46 @@ close_cur_scan_seg(AOCSScanDesc scan)
                AppendOnlyBlockDirectory_End_forInsert(scan->blockDirectory);
 }
 
+static void
+aocs_blkdirscan_init(AOCSScanDesc scan)
+{
+       if (scan->aocsfetch == NULL)
+       {
+               int natts = RelationGetNumberOfAttributes(scan->rs_base.rs_rd);
+               scan->proj = palloc(natts * sizeof(*scan->proj));
+               MemSet(scan->proj, true, natts * sizeof(*scan->proj));
+
+               scan->aocsfetch = aocs_fetch_init(scan->rs_base.rs_rd,
+                                                                               
  scan->rs_base.rs_snapshot,
+                                                                               
  scan->appendOnlyMetaDataSnapshot,
+                                                                               
  scan->proj);
+       }
+
+       scan->blkdirscan = palloc0(sizeof(AOBlkDirScanData));
+       AOBlkDirScan_Init(scan->blkdirscan, &scan->aocsfetch->blockDirectory);
+}
+
+static void
+aocs_blkdirscan_finish(AOCSScanDesc scan)
+{
+       AOBlkDirScan_Finish(scan->blkdirscan);
+       pfree(scan->blkdirscan);

Review Comment:
   can move L498-L499 into `AOBlkDirScan_Finish`?



##########
src/backend/access/aocs/aocsam.c:
##########
@@ -467,6 +472,46 @@ close_cur_scan_seg(AOCSScanDesc scan)
                AppendOnlyBlockDirectory_End_forInsert(scan->blockDirectory);
 }
 
+static void
+aocs_blkdirscan_init(AOCSScanDesc scan)
+{
+       if (scan->aocsfetch == NULL)
+       {
+               int natts = RelationGetNumberOfAttributes(scan->rs_base.rs_rd);
+               scan->proj = palloc(natts * sizeof(*scan->proj));

Review Comment:
   ```
   scan->proj = palloc(natts * sizeof(bool));
   ```
   in L490, used `sizeof({typedef struct name}`, used the same style here.



##########
src/backend/access/aocs/aocsam.c:
##########
@@ -675,6 +738,402 @@ aocs_endscan(AOCSScanDesc scan)
        pfree(scan);
 }
 
+static int
+aocs_locate_target_segment(AOCSScanDesc scan, int64 targrow)
+{
+       int64 rowcount;
+
+       for (int i = scan->cur_seg; i < scan->total_seg; i++)
+       {
+               if (i < 0)
+                       continue;
+
+               rowcount = scan->seginfo[i]->total_tupcount;
+               if (rowcount <= 0)
+                       continue;
+
+               if (scan->seginfo[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;
+}
+
+/*
+ * block directory based get_target_tuple()
+ */
+static bool
+aocs_blkdirscan_get_target_tuple(AOCSScanDesc scan, int64 targrow, 
TupleTableSlot *slot)
+{
+       int segno, segidx;
+       int64 rownum = -1;
+       int64 rowsprocessed;
+       AOTupleId aotid;
+       int ncols = scan->columnScanInfo.relationTupleDesc->natts;
+       AppendOnlyBlockDirectory *blkdir = &scan->aocsfetch->blockDirectory;
+
+       Assert(scan->blkdirscan != NULL);
+
+       /* locate the target segment */
+       segidx = aocs_locate_target_segment(scan, targrow);
+       if (segidx < 0)
+               return false;
+
+       /* next starting position in locating segfile */
+       scan->cur_seg = segidx;
+
+       segno = scan->seginfo[segidx]->segno;
+       Assert(segno > InvalidFileSegNumber && segno <= 
AOTupleId_MaxSegmentFileNum);
+
+       /*
+        * Note: It is safe to assume that the scan's segfile array and the
+        * blockdir's segfile array are identical. Otherwise, we should stop
+        * processing and throw an exception to make the error visible.
+        */
+       if (blkdir->segmentFileInfo[segidx]->segno != segno)
+       {
+               ereport(ERROR,
+                               (errcode(ERRCODE_INTERNAL_ERROR),
+                                errmsg("segfile array contents in both scan 
descriptor "
+                                               "and block directory are not 
identical on "
+                                               "append-optimized relation 
'%s'",
+                                               
RelationGetRelationName(blkdir->aoRel))));
+       }
+
+       /*
+        * Unlike ao_row, we set currentSegmentFileNum for ao_column here
+        * just for passing the assertion in extract_minipage() called by
+        * AOBlkDirScan_GetRowNum().
+        * Since we don't invoke AppendOnlyBlockDirectory_GetCachedEntry()
+        * for ao_column, it shoule be restored back to the original value
+        * for AppendOnlyBlockDirectory_GetEntry() working properly.
+        */
+       int currentSegmentFileNum = blkdir->currentSegmentFileNum;
+       blkdir->currentSegmentFileNum = blkdir->segmentFileInfo[segidx]->segno;
+
+       /* locate the target row by seqscan block directory */
+       for (int col = 0; col < ncols; col++)
+       {
+               /*
+                * "segfirstrow" should be always pointing to the first row of
+                * a new segfile, only locate_target_segment could update
+                * its value.
+                * 
+                * "segrowsprocessed" is used for tracking the position of
+                * processed rows in the current segfile.
+                */
+               rowsprocessed = scan->segfirstrow + scan->segrowsprocessed;
+
+               if ((scan->rs_base.rs_rd)->rd_att->attrs[col].attisdropped)
+                       continue;
+
+               rownum = AOBlkDirScan_GetRowNum(scan->blkdirscan,

Review Comment:
   will rownum eq 0 ?



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

Reply via email to