You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by "cecemei (via GitHub)" <gi...@apache.org> on 2026/01/08 04:36:28 UTC

[PR] Add numRows to DataSegment (druid)

cecemei opened a new pull request, #18892:
URL: https://github.com/apache/druid/pull/18892

   Add numRows to DataSegment
   
   ##### Key changed/added classes in this PR
    * `DataSegment`
   
   <hr>
   
   <!-- Check the items by putting "x" in the brackets for the done things. Not all of these items apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items from the checklist below are strictly necessary, but it would be very helpful if you at least self-review the PR. -->
   
   This PR has:
   
   - [ ] been self-reviewed.
      - [ ] using the [concurrency checklist](https://github.com/apache/druid/blob/master/dev/code-review/concurrency.md) (Remove this item if the PR doesn't have any relation to concurrency.)
   - [ ] added documentation for new or modified features or behaviors.
   - [ ] a release note entry in the PR description.
   - [ ] added Javadocs for most classes and all non-trivial methods. Linked related entities via Javadoc links.
   - [ ] added or updated version, license, or notice information in [licenses.yaml](https://github.com/apache/druid/blob/master/dev/license.md)
   - [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for [code coverage](https://github.com/apache/druid/blob/master/dev/code-review/code-coverage.md) is met.
   - [ ] added integration tests.
   - [ ] been tested in a test Druid cluster.


-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2670876105


##########
sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemSchema.java:
##########
@@ -332,22 +332,29 @@
             final AvailableSegmentMetadata availableSegmentMetadata =
                 availableMetadataCache.getAvailableSegmentMetadata(segment.getDataSource(), segment.getId());
             segmentsAlreadySeen.add(segment.getId());
-            long numReplicas = 0L, numRows = 0L, isRealtime, isAvailable = 0L;
 
+            long numReplicas = 0L, isAvailable = 0L;
             if (availableSegmentMetadata != null) {
               numReplicas = availableSegmentMetadata.getNumReplicas();
               isAvailable = availableSegmentMetadata.getNumReplicas() > 0 ? IS_AVAILABLE_TRUE : IS_ACTIVE_FALSE;
-              numRows = availableSegmentMetadata.getNumRows();
             }
 
-            // If druid.centralizedDatasourceSchema.enabled is set on the Coordinator, SegmentMetadataCache on the
-            // broker might have outdated or no information regarding numRows and rowSignature for a segment.
-            // In that case, we should use {@code numRows} from the segment polled from the coordinator.
-            if (null != val.getNumRows()) {
+            final long numRows;
+            if (segment.getNumRows() != null) {
+              // the recent version of DataSegment stores numRows
+              numRows = segment.getNumRows().longValue();
+            } else if (val.getNumRows() != null) {
+              // If druid.centralizedDatasourceSchema.enabled is set on the Coordinator, SegmentMetadataCache on the
+              // broker might have outdated or no information regarding numRows and rowSignature for a segment.
+              // In that case, we should use {@code numRows} from the segment polled from the coordinator.
               numRows = val.getNumRows();
+            } else if (availableSegmentMetadata != null) {
+              numRows = availableSegmentMetadata.getNumRows();

Review Comment:
   ## Deprecated method or constructor invocation
   
   Invoking [AvailableSegmentMetadata.getNumRows](1) should be avoided because it has been deprecated.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/10657)



##########
sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemSchema.java:
##########
@@ -401,7 +408,7 @@
                 segment.getVersion(),
                 (long) segment.getShardSpec().getPartitionNum(),
                 val.getNumReplicas(),
-                val.getNumRows(),
+                segment.getNumRows() != null ? segment.getNumRows() : val.getNumRows(),

Review Comment:
   ## Deprecated method or constructor invocation
   
   Invoking [AvailableSegmentMetadata.getNumRows](1) should be avoided because it has been deprecated.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/10658)



##########
server/src/main/java/org/apache/druid/server/http/MetadataResource.java:
##########
@@ -227,15 +227,19 @@
           Integer replicationFactor = isOvershadowed ? (Integer) 0
                                                      : coordinator.getReplicationFactor(segment.getId());
 
-          Long numRows = null;
-          if (coordinatorSegmentMetadataCache != null) {
+          final Long numRows;
+          if (segment.getNumRows() != null) {
+            // the recent version of DataSegment stores numRows
+            numRows = segment.getNumRows().longValue();
+          } else if (coordinatorSegmentMetadataCache != null) {
+            // if centralized schema is enabled, SchemaPayloadPlus stores numRows
             AvailableSegmentMetadata availableSegmentMetadata = coordinatorSegmentMetadataCache.getAvailableSegmentMetadata(
                 segment.getDataSource(),
                 segment.getId()
             );
-            if (null != availableSegmentMetadata) {
-              numRows = availableSegmentMetadata.getNumRows();
-            }
+            numRows = availableSegmentMetadata == null ? null : availableSegmentMetadata.getNumRows();

Review Comment:
   ## Deprecated method or constructor invocation
   
   Invoking [AvailableSegmentMetadata.getNumRows](1) should be avoided because it has been deprecated.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/10656)



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "cecemei (via GitHub)" <gi...@apache.org>.
cecemei commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2684549573


##########
processing/src/main/java/org/apache/druid/segment/SegmentReference.java:
##########
@@ -49,23 +51,51 @@ public static SegmentReference missing(SegmentDescriptor segmentDescriptor)
     return new SegmentReference(segmentDescriptor, Optional.empty(), null);
   }
 
+  @Nullable
+  private final DataSegment dataSegment;
   private final SegmentDescriptor segmentDescriptor;
   private final Optional<Segment> segmentReference;
   @Nullable
   private final Closeable cleanupHold;
   private final AtomicBoolean closed = new AtomicBoolean(false);
 
+  public SegmentReference(
+      DataSegmentAndDescriptor segment,
+      Optional<Segment> segmentReference,
+      @Nullable Closeable cleanupHold
+  )
+  {
+    this(segment.getDataSegment(), segment.getDescriptor(), segmentReference, cleanupHold);
+  }
+
   public SegmentReference(
       SegmentDescriptor segmentDescriptor,
       Optional<Segment> segmentReference,
       @Nullable Closeable cleanupHold
   )
   {
+    this(null, segmentDescriptor, segmentReference, cleanupHold);
+  }
+
+  private SegmentReference(
+      @Nullable DataSegment segment,

Review Comment:
   yea we can probably do this a better way, reverted this change



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "clintropolis (via GitHub)" <gi...@apache.org>.
clintropolis commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2684533007


##########
server/src/main/java/org/apache/druid/segment/realtime/appenderator/BatchAppenderator.java:
##########
@@ -835,6 +836,14 @@ private DataSegmentWithMetadata mergeAndPush(
         metrics.setMergeTime(mergeTimeMillis);
 
         log.debug("Segment[%s] built in %,dms.", identifier, mergeTimeMillis);
+        QueryableIndex index = indexIO.loadIndex(mergedFile);
+        closer.register(index);

Review Comment:
   can this just use `sink.getNumRows()` or is that not cool for some reason?



##########
processing/src/main/java/org/apache/druid/query/metadata/SegmentMetadataQueryRunnerFactory.java:
##########
@@ -67,6 +68,59 @@ public SegmentMetadataQueryRunnerFactory(
     this.queryWatcher = queryWatcher;
   }
 
+  public QueryRunner<SegmentAnalysis> createRunner(SegmentReference segmentRef)
+  {
+    if (segmentRef.getDataSegment() == null) {
+      throw DruidException.defensive("Missing Segment[%s]", segmentRef.getSegmentDescriptor());
+    } else if (segmentRef.getSegmentReference().isEmpty()) {
+      throw DruidException.defensive("Missing Segment[%s]", segmentRef.getDataSegment().getId());
+    }
+
+    Segment segment = segmentRef.getSegmentReference().get();
+    if (!Objects.equals(segmentRef.getDataSegment().getId(), segment.getId())) {
+      throw DruidException.defensive(
+          "SegmentId mismatch in DataSegment[%s] and Segment[%s]",
+          segmentRef.getDataSegment().getId(),
+          segment.getId()
+      );
+    }
+
+    return new QueryRunner<>()
+    {
+      @Override
+      public Sequence<SegmentAnalysis> run(QueryPlus<SegmentAnalysis> inQ, ResponseContext responseContext)
+      {
+        SegmentMetadataQuery updatedQuery = ((SegmentMetadataQuery) inQ.getQuery()).withFinalizedAnalysisTypes(toolChest.getConfig());
+        final SegmentAnalyzer analyzer = new SegmentAnalyzer(updatedQuery.getAnalysisTypes());
+
+        Integer numRows = segmentRef.getDataSegment().getTotalRows();
+        if (numRows == null) {
+          numRows = analyzer.numRows(segment);
+        }
+        long totalSize = analyzer.analyzingSize() ? segmentRef.getDataSegment().getSize() : 0L;

Review Comment:
   this is is a breaking change to size analysis. While the current size analysis is arguably nonsense, I don't think we want to do an incompatible change like this, see discussion in https://github.com/apache/druid/issues/7124, and so this change should be reverted.
   
   I'm also not terribly sure the total size isn't particularly useful, I think the size analysis we want to replace this with would be a breakdown of column parts since that is actionable (e.g. can decide to not have indexes or whatever else). In other words, I don't think `DataSegment` is particularly useful to use here since we already have the `Segment`, so it doesn't save us anything, and costs us dragging around the overhead of an extra memory reference linking back to the `DataSegment` for anything a query engine processes



##########
processing/src/main/java/org/apache/druid/segment/SegmentReference.java:
##########
@@ -49,23 +51,51 @@ public static SegmentReference missing(SegmentDescriptor segmentDescriptor)
     return new SegmentReference(segmentDescriptor, Optional.empty(), null);
   }
 
+  @Nullable
+  private final DataSegment dataSegment;
   private final SegmentDescriptor segmentDescriptor;
   private final Optional<Segment> segmentReference;
   @Nullable
   private final Closeable cleanupHold;
   private final AtomicBoolean closed = new AtomicBoolean(false);
 
+  public SegmentReference(
+      DataSegmentAndDescriptor segment,
+      Optional<Segment> segmentReference,
+      @Nullable Closeable cleanupHold
+  )
+  {
+    this(segment.getDataSegment(), segment.getDescriptor(), segmentReference, cleanupHold);
+  }
+
   public SegmentReference(
       SegmentDescriptor segmentDescriptor,
       Optional<Segment> segmentReference,
       @Nullable Closeable cleanupHold
   )
   {
+    this(null, segmentDescriptor, segmentReference, cleanupHold);
+  }
+
+  private SegmentReference(
+      @Nullable DataSegment segment,

Review Comment:
   this change feels controversial to me, I don't love the idea of dragging around a reference to `DataSegment` everywhere we are using segments, what is this for. just for the segment metadata query changes?



##########
server/src/main/java/org/apache/druid/segment/realtime/appenderator/StreamAppenderator.java:
##########
@@ -970,6 +971,14 @@ private DataSegmentWithMetadata mergeAndPush(
         metrics.setMergeTime(mergeTimeMillis);
 
         log.debug("Segment[%s] built in %,dms.", identifier, mergeTimeMillis);
+        QueryableIndex index = indexIO.loadIndex(mergedFile);
+        closer.register(index);

Review Comment:
   same comment about `sink.getNumRows()`



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "clintropolis (via GitHub)" <gi...@apache.org>.
clintropolis commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2684544218


##########
server/src/main/java/org/apache/druid/segment/realtime/appenderator/BatchAppenderator.java:
##########
@@ -835,6 +836,14 @@ private DataSegmentWithMetadata mergeAndPush(
         metrics.setMergeTime(mergeTimeMillis);
 
         log.debug("Segment[%s] built in %,dms.", identifier, mergeTimeMillis);
+        QueryableIndex index = indexIO.loadIndex(mergedFile);
+        closer.register(index);

Review Comment:
   i guess we would need to change the signatures of merge/persist methods to return some `MergeResult` or something that has both the file and extra stuff like row count, i don't think we need to worry about that in this PR, though maybe something to consider in a follow-up to save us from having to load the segment



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "github-advanced-security[bot] (via GitHub)" <gi...@apache.org>.
github-advanced-security[bot] commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2678140290


##########
sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemSchema.java:
##########
@@ -401,7 +408,7 @@
                 segment.getVersion(),
                 (long) segment.getShardSpec().getPartitionNum(),
                 val.getNumReplicas(),
-                val.getNumRows(),
+                segment.getTotalRows() != null ? segment.getTotalRows() : val.getNumRows(),

Review Comment:
   ## Deprecated method or constructor invocation
   
   Invoking [AvailableSegmentMetadata.getNumRows](1) should be avoided because it has been deprecated.
   
   [Show more details](https://github.com/apache/druid/security/code-scanning/10661)



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add totalRows to DataSegment (druid)

Posted by "cecemei (via GitHub)" <gi...@apache.org>.
cecemei merged PR #18892:
URL: https://github.com/apache/druid/pull/18892


-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "clintropolis (via GitHub)" <gi...@apache.org>.
clintropolis commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2684537806


##########
server/src/main/java/org/apache/druid/segment/realtime/appenderator/BatchAppenderator.java:
##########
@@ -835,6 +836,14 @@ private DataSegmentWithMetadata mergeAndPush(
         metrics.setMergeTime(mergeTimeMillis);
 
         log.debug("Segment[%s] built in %,dms.", identifier, mergeTimeMillis);
+        QueryableIndex index = indexIO.loadIndex(mergedFile);
+        closer.register(index);

Review Comment:
   oh i guess that is before merging, and would be wrong in the case of rollup, still wonder if there is some way to get it without mapping new segment since the merger knows it when writing, will look at some stuff



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "cecemei (via GitHub)" <gi...@apache.org>.
cecemei commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2684552635


##########
processing/src/main/java/org/apache/druid/query/metadata/SegmentMetadataQueryRunnerFactory.java:
##########
@@ -67,6 +68,59 @@ public SegmentMetadataQueryRunnerFactory(
     this.queryWatcher = queryWatcher;
   }
 
+  public QueryRunner<SegmentAnalysis> createRunner(SegmentReference segmentRef)
+  {
+    if (segmentRef.getDataSegment() == null) {
+      throw DruidException.defensive("Missing Segment[%s]", segmentRef.getSegmentDescriptor());
+    } else if (segmentRef.getSegmentReference().isEmpty()) {
+      throw DruidException.defensive("Missing Segment[%s]", segmentRef.getDataSegment().getId());
+    }
+
+    Segment segment = segmentRef.getSegmentReference().get();
+    if (!Objects.equals(segmentRef.getDataSegment().getId(), segment.getId())) {
+      throw DruidException.defensive(
+          "SegmentId mismatch in DataSegment[%s] and Segment[%s]",
+          segmentRef.getDataSegment().getId(),
+          segment.getId()
+      );
+    }
+
+    return new QueryRunner<>()
+    {
+      @Override
+      public Sequence<SegmentAnalysis> run(QueryPlus<SegmentAnalysis> inQ, ResponseContext responseContext)
+      {
+        SegmentMetadataQuery updatedQuery = ((SegmentMetadataQuery) inQ.getQuery()).withFinalizedAnalysisTypes(toolChest.getConfig());
+        final SegmentAnalyzer analyzer = new SegmentAnalyzer(updatedQuery.getAnalysisTypes());
+
+        Integer numRows = segmentRef.getDataSegment().getTotalRows();
+        if (numRows == null) {
+          numRows = analyzer.numRows(segment);
+        }
+        long totalSize = analyzer.analyzingSize() ? segmentRef.getDataSegment().getSize() : 0L;

Review Comment:
   makes sense! it'd probably be more useful when we add all the columns size breakdown stuff in the analysis and deprecate the SIZE analysis type, reverted this change.



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org


Re: [PR] Add numRows to DataSegment (druid)

Posted by "clintropolis (via GitHub)" <gi...@apache.org>.
clintropolis commented on code in PR #18892:
URL: https://github.com/apache/druid/pull/18892#discussion_r2691623847


##########
server/src/main/java/org/apache/druid/segment/realtime/appenderator/BatchAppenderator.java:
##########
@@ -835,6 +836,14 @@ private DataSegmentWithMetadata mergeAndPush(
         metrics.setMergeTime(mergeTimeMillis);
 
         log.debug("Segment[%s] built in %,dms.", identifier, mergeTimeMillis);
+        QueryableIndex index = indexIO.loadIndex(mergedFile);
+        closer.register(index);
+        mergedSegment =
+            sink.getSegment()
+                .toBuilder()
+                .dimensions(IndexMerger.getMergedDimensionsFromQueryableIndexes(indexes, schema.getDimensionsSpec()))

Review Comment:
   Oh, i think this could just use `index.getAvailableDimensions` since we are loading the merged segment now



-- 
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-unsubscribe@druid.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org