gianm commented on code in PR #18342:
URL: https://github.com/apache/druid/pull/18342#discussion_r2244568673
##########
processing/src/main/java/org/apache/druid/segment/AggregateProjectionMetadata.java:
##########
@@ -396,6 +417,45 @@ public Projections.ProjectionMatch matches(
}
}
}
+
+ // if the projection has a filter, the query must contain this filter
match
+ if (filter != null) {
Review Comment:
IMO, put this logic and `rewriteFilter` in their own dedicated file. The
logic will only grow more complex over time.
##########
extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/exec/MSQInsertTest.java:
##########
@@ -553,6 +555,7 @@ public void
testInsertOnExternalDataSourceWithCatalogProjections(String contextN
new AggregateProjectionMetadata.Schema(
Review Comment:
add a builder to avoid the need for `null` filter everywhere?
##########
processing/src/main/java/org/apache/druid/segment/AggregateProjectionMetadata.java:
##########
@@ -554,9 +616,73 @@ public String toString()
", aggregators=" + Arrays.toString(aggregators) +
", ordering=" + ordering +
", timeColumnPosition=" + timeColumnPosition +
- ", granularity=" + granularity +
+ ", effectiveGranularity=" + effectiveGranularity +
", orderingWithTimeSubstitution=" + orderingWithTimeSubstitution +
'}';
}
}
+
+ /**
+ * Rewrites a query {@link Filter} if possible, removing the {@link Filter}
of a projection. To match a projection
+ * filter, the query filter must be equal to the projection filter, or must
contain the projection filter as the child
+ * of an AND filter. This method returns null
+ * indicating that a rewrite is impossible with the implication that the
query cannot use the projection because the
+ * projection doesn't contain all the rows the query would match if not
using the projection.
+ */
+ @Nullable
+ public static Filter rewriteFilter(@Nullable Filter projectionFilter,
@Nullable Filter queryFilter)
+ {
+ if (projectionFilter == null || queryFilter == null) {
Review Comment:
can `queryFilter` really be null? It seems like this method is only called
for nonnull query filters.
##########
processing/src/main/java/org/apache/druid/segment/AggregateProjectionMetadata.java:
##########
@@ -554,9 +616,73 @@ public String toString()
", aggregators=" + Arrays.toString(aggregators) +
", ordering=" + ordering +
", timeColumnPosition=" + timeColumnPosition +
- ", granularity=" + granularity +
+ ", effectiveGranularity=" + effectiveGranularity +
", orderingWithTimeSubstitution=" + orderingWithTimeSubstitution +
'}';
}
}
+
+ /**
+ * Rewrites a query {@link Filter} if possible, removing the {@link Filter}
of a projection. To match a projection
+ * filter, the query filter must be equal to the projection filter, or must
contain the projection filter as the child
+ * of an AND filter. This method returns null
+ * indicating that a rewrite is impossible with the implication that the
query cannot use the projection because the
+ * projection doesn't contain all the rows the query would match if not
using the projection.
+ */
+ @Nullable
+ public static Filter rewriteFilter(@Nullable Filter projectionFilter,
@Nullable Filter queryFilter)
+ {
+ if (projectionFilter == null || queryFilter == null) {
+ return queryFilter;
+ }
+ if (queryFilter.equals(projectionFilter)) {
+ return ProjectionFilterMatch.INSTANCE;
+ }
+ if (queryFilter instanceof IsBooleanFilter && ((IsBooleanFilter)
queryFilter).isTrue()) {
+ final IsBooleanFilter isTrueFilter = (IsBooleanFilter) queryFilter;
+ final Filter rewritten = rewriteFilter(projectionFilter,
isTrueFilter.getBaseFilter());
+ if (rewritten == null) {
+ return null;
+ }
+ //noinspection ObjectEquality
+ if (rewritten == ProjectionFilterMatch.INSTANCE) {
+ return ProjectionFilterMatch.INSTANCE;
+ }
+ return new IsBooleanFilter(rewritten, true);
+ }
+ if (queryFilter instanceof AndFilter) {
+ AndFilter andFilter = (AndFilter) queryFilter;
+ List<Filter> newChildren =
Lists.newArrayListWithExpectedSize(andFilter.getFilters().size());
+ boolean childRewritten = false;
+ for (Filter filter : andFilter.getFilters()) {
+ Filter rewritten = rewriteFilter(projectionFilter, filter);
+ //noinspection ObjectEquality
+ if (rewritten == ProjectionFilterMatch.INSTANCE) {
+ childRewritten = true;
+ } else {
+ if (rewritten != null) {
+ newChildren.add(rewritten);
+ childRewritten = true;
+ } else {
+ newChildren.add(filter);
+ }
+ }
+ }
+ // at least one child must have been rewritten to rewrite the AND
+ if (childRewritten) {
+ if (newChildren.size() > 1) {
Review Comment:
can `newChildren` be empty, if all filters under the `AND` of `queryFilter`
were part of `projectionFilter`?
--
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]