maytasm commented on code in PR #19718:
URL: https://github.com/apache/druid/pull/19718#discussion_r3633711449
##########
sql/src/main/java/org/apache/druid/sql/calcite/schema/SystemSchema.java:
##########
@@ -517,6 +536,118 @@ private Iterator<AvailableSegmentMetadata>
getAuthorizedAvailableSegments(
return authorizedSegments.iterator();
}
+ /**
+ * Best-effort extraction of an exact-match {@code datasource} constraint
(column
+ * {@link #DATASOURCE_COLUMN}) from the pushed-down filters, so
sys.segments can restrict its scan
+ * to the matching datasources rather than materializing every segment in
the cluster. Handles
+ * {@code datasource = 'x'}, {@code datasource IN (...)} (normalized by
Calcite to SEARCH),
+ * OR-of-equalities, and conjunctions - including a whole {@code WHERE}
passed as a single
+ * {@code AND(...)} RexCall (as Calcite's filter-scan rule may do), where
any non-datasource
+ * conjunct (e.g. {@code is_active = 1}) is simply ignored. Returns {@code
null} when no usable
+ * datasource predicate is present, in which case the previous full-scan
behavior is retained.
+ * Because the filters are not removed from the planner's filter list,
Calcite still applies them
+ * and correctness holds even if this extraction is conservative or
over-broad.
+ */
+ @Nullable
+ static Set<String> getDataSourceFilter(List<RexNode> filters)
Review Comment:
nit: there's a similar function in
SystemServerPropertiesTable#extractColumnEqualityFilters. Do you think this can
be combine / reuse / refactor somehow? (Also, I think the server filtering
would also benefit from handling IN, etc from this function)
##########
sql/src/main/java/org/apache/druid/sql/calcite/schema/MetadataSegmentView.java:
##########
@@ -185,12 +189,28 @@ private void poll()
*/
Iterator<SegmentStatusInCluster> getSegments()
{
+ return getSegments(null);
+ }
+
+ /**
+ * Returns published (and, with centralized schema, realtime) segment
metadata, optionally
+ * restricted to {@code dataSources} - a {@code datasource} predicate pushed
down from sys.segments.
+ * The restriction is a cheap membership test applied before the caller's
authorization and row
+ * construction, so segments of non-matching datasources skip that per-row
work.
+ */
+ Iterator<SegmentStatusInCluster> getSegments(@Nullable Set<String>
dataSources)
+ {
+ final Iterator<SegmentStatusInCluster> base;
if (isCacheEnabled) {
Uninterruptibles.awaitUninterruptibly(cachePopulated);
- return publishedSegments.iterator();
+ base = publishedSegments.iterator();
} else {
- return fetchSegmentMetadataFromCoordinator();
+ // Cache disabled: the Coordinator returns all used segments; filter
client-side to preserve semantics.
+ base = fetchSegmentMetadataFromCoordinator();
}
+ return dataSources == null
+ ? base
+ : Iterators.filter(base, s ->
dataSources.contains(s.getDataSegment().getDataSource()));
Review Comment:
is this expensive? Should we maintain a datasource to segments Map here
similar to SegmentMetadataCache?
--
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]