TheNamesRai commented on code in PR #2307:
URL: https://github.com/apache/phoenix/pull/2307#discussion_r2570459503


##########
phoenix-core-server/src/main/java/org/apache/phoenix/util/ServerUtil.java:
##########
@@ -291,4 +291,33 @@ public static <T> Throwable 
getExceptionFromFailedFuture(Future<T> f) {
     }
     return t;
   }
+
+  /**
+   * Derives a safe row key for empty result sets based on scan or region 
boundaries. Used when
+   * constructing KeyValues for aggregate results or OFFSET responses when no 
actual data rows were
+   * scanned.
+   * @param scan   The scan being executed
+   * @param region The region being scanned
+   * @return A valid row key derived from scan or region boundaries
+   */
+  public static byte[] deriveRowKeyFromScanOrRegionBoundaries(Scan scan, 
Region region) {
+    byte[] startKey =
+      scan.getStartRow().length > 0 ? scan.getStartRow() : 
region.getRegionInfo().getStartKey();
+    byte[] endKey =
+      scan.getStopRow().length > 0 ? scan.getStopRow() : 
region.getRegionInfo().getEndKey();
+
+    byte[] rowKey = ByteUtil.getLargestPossibleRowKeyInRange(startKey, endKey);
+
+    if (rowKey == null) {
+      if (scan.includeStartRow()) {
+        rowKey = startKey;
+      } else if (scan.includeStopRow()) {
+        rowKey = endKey;

Review Comment:
   The existing code uses the same pattern as i used - 
[usecase1](https://github.com/apache/phoenix/blob/master/phoenix-core-server/src/main/java/org/apache/phoenix/iterate/NonAggregateRegionScannerFactory.java#L412-L425),
 
[usecase2](https://github.com/apache/phoenix/blob/master/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/IndexRepairRegionScanner.java#L504-L512),
 ..
   
   But actually you are right, we can use use the nextkey before jumping to the 
endkey here. Something like this should ? What do you think? 
   
   
   if (rowKey == null) {
       if (scan.includeStartRow()) {
           rowKey = startKey;
       } else {
           byte[] nextStartKey = ByteUtil.nextKey(startKey);
           if (nextStartKey != null) {
               rowKey = nextStartKey;
           } else if (scan.includeStopRow()) {
               rowKey = endKey;
           } else {
               rowKey = HConstants.EMPTY_END_ROW;
           }
      }
   }
   
   



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

Reply via email to