924060929 commented on code in PR #70388:
URL: https://github.com/apache/doris/pull/70388#discussion_r4778773745
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/materialize/MaterializeProbeVisitor.java:
##########
@@ -80,37 +85,69 @@ public ProbeContext(SlotReference slot, Set<Slot>
requiredMaterializedSlots) {
}
/**
- * Whether {@code filter} keeps its predicate slots lazy.
+ * The predicate slots of {@code filter} that stay lazy above the TopN.
*
- * <p>With {@code topn_lazy_materialization_using_index} the predicate of
a filter directly above an OLAP
- * scan is evaluated while scanning, so its slots only have to be
re-materialized above the TopN instead
- * of being carried through it. This is the single definition of that
shape:
- * {@link MaterializeProbeVisitor#visitPhysicalFilter} reports those slots
as lazy sources,
- * {@link LazySlotPruning#visitPhysicalFilter} keeps them out of the
scan's lazy slots (the scan still
- * produces them for the predicate) and {@link LazyMaterializeTopN} must
not require them materialized.
+ * <p>The scan evaluates the predicate of a filter directly above it, so a
predicate slot only has to be
+ * produced by the scan for the predicate and can be fetched again above
the TopN instead of being
+ * carried through it. By default that is done for the slots read only by
conjuncts made of search() and
+ * MATCH: the inverted index answers those, so where the segments have the
index the scan does not read the
+ * column, and only the rows that survive the TopN fetch it. With
+ * {@code topn_lazy_materialization_using_index} it is done for every
predicate slot. For
+ *
+ * <pre>
+ * filter(search('body:error') AND ts > '2026-01-01') -> scan(ts, body)
+ * </pre>
+ *
+ * body stays lazy by default, ts only with the session variable.
+ *
+ * <p>This is the single definition of these slots: {@link
MaterializeProbeVisitor#visitPhysicalFilter}
+ * reports them as lazy sources, {@link
LazySlotPruning#visitPhysicalFilter} keeps them out of the scan's
+ * lazy slots (the scan still produces them for the predicate) and {@link
LazyMaterializeTopN} does not count
+ * the filter's read of them when it decides which slots stay materialized.
*/
- static boolean isIndexLazyFilter(PhysicalFilter<? extends Plan> filter) {
+ static Set<Slot> indexLazySlots(PhysicalFilter<? extends Plan> filter) {
if (!(filter.child() instanceof PhysicalOlapScan)) {
- return false;
+ return ImmutableSet.of();
}
- if (!SessionVariable.getTopNLazyMaterializationUsingIndex()) {
- return false;
+ PhysicalOlapScan scan = (PhysicalOlapScan) filter.child();
+ // As in visitPhysicalOlapScan: the row-id locates a row of the base
index, and the table's storage
+ // semantics must let one row-id reconstruct the row.
+ if (scan.getSelectedIndexId() != scan.getTable().getBaseIndexId()
+ || !supportOlapTopnLazyMaterialize(scan.getTable())) {
+ return ImmutableSet.of();
}
- // Reject OLAP tables whose storage semantics cannot be reconstructed
from one row-id.
- return supportOlapTopnLazyMaterialize(((PhysicalOlapScan)
filter.child()).getTable());
+ if (SessionVariable.getTopNLazyMaterializationUsingIndex()) {
+ return filter.getInputSlots();
+ }
+ Set<Slot> indexOnlySlots = new HashSet<>();
+ Set<Slot> otherSlots = new HashSet<>();
+ for (Expression conjunct : filter.getConjuncts()) {
+ if (isIndexPredicate(conjunct)) {
+ indexOnlySlots.addAll(conjunct.getInputSlots());
+ } else {
+ otherSlots.addAll(conjunct.getInputSlots());
+ }
+ }
+ indexOnlySlots.removeAll(otherSlots);
+ return indexOnlySlots;
+ }
+
+ // search() and MATCH, alone or combined by AND, OR and NOT: (body
MATCH_ALL 'a b' OR body MATCH_ANY 'c').
+ private static boolean isIndexPredicate(Expression expression) {
+ if (expression instanceof SearchExpression || expression instanceof
Match) {
+ return true;
+ }
+ return (expression instanceof CompoundPredicate || expression
instanceof Not)
+ &&
expression.children().stream().allMatch(MaterializeProbeVisitor::isIndexPredicate);
}
@Override
public Optional<MaterializeSource> visitPhysicalFilter(PhysicalFilter<?
extends Plan> filter,
ProbeContext
context) {
- if (isIndexLazyFilter(filter)) {
- if (filter.getInputSlots().contains(context.slot)) {
- Relation relation = (Relation) filter.child();
- return Optional.of(new MaterializeSource(
- relation, findRelationOutputSlot(relation,
context.slot).orElse(context.slot)));
- } else {
- return filter.child().accept(this, context);
- }
+ if (indexLazySlots(filter).contains(context.slot)) {
+ Relation relation = (Relation) filter.child();
+ return Optional.of(new MaterializeSource(
+ relation, findRelationOutputSlot(relation,
context.slot).orElse(context.slot)));
}
return this.visit(filter, context);
Review Comment:
[P3] Two minor improvements here:
1. **Avoid recomputing `indexLazySlots(filter)` for non-predicate columns**:
`LazyMaterializeTopN` probes every TopN output slot through
`visitPhysicalFilter`. Checking
`!filter.getInputSlots().contains(context.slot)` first avoids walking
`filter.getConjuncts()` and allocating two `HashSet`s for every unrelated
column on wide tables.
2. **Delegate to `filter.child().accept(this, context)`** (pre-existing
pattern): when `indexLazySlots(filter).contains(context.slot)` is true,
`filter.child()` is already guaranteed to be an eligible base-index
`PhysicalOlapScan`. Delegating reuses `visitPhysicalOlapScan` (including the
`context.requiredMaterializedSlots` check, currently only caught downstream by
`collectRequiredOutputSlots`) instead of duplicating `MaterializeSource`
construction.
```java
if (!filter.getInputSlots().contains(context.slot)
|| indexLazySlots(filter).contains(context.slot)) {
return filter.child().accept(this, context);
}
return Optional.empty();
```
--
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]