airborne12 commented on code in PR #70388:
URL: https://github.com/apache/doris/pull/70388#discussion_r4750714257


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/processor/post/materialize/LazyMaterializeTopN.java:
##########
@@ -367,9 +367,8 @@ private void collectConsumedSlots(Plan plan, Set<Slot> 
consumedSlots, Set<Slot>
             // transparent Alias(Slot) lazy because that alias output may 
still be fetched later.
             consumedSlots.addAll(plan.getInputSlots());
         }
-        if (plan instanceof PhysicalFilter
-                && 
MaterializeProbeVisitor.isIndexLazyFilter((PhysicalFilter<?>) plan)) {
-            indexLazySlots.addAll(plan.getInputSlots());
+        if (plan instanceof PhysicalFilter) {
+            
indexLazySlots.addAll(MaterializeProbeVisitor.indexLazySlots((PhysicalFilter<?>)
 plan));

Review Comment:
   Confirmed and fixed in b6eadf16b20.
   
   The SQL in this comment does not reach that shape on the current planner. It 
sorts the middle TopN by `y` and drops the bare `body` (`TopN(orderKeys=[y#3])` 
over `Project(body#1 AS y#3, k#0)`), so the probe stops at that TopN and 
nothing is fetched lazily. The defect is real when another operator reads the 
bare column, for example a join:
   
   ```sql
   select b.y from (select body, body as y, k from (
     select k, body from t where body match_any 'error' order by k limit 10
   ) a) b join d on b.body = d.s order by b.k limit 1;
   ```
   
   On 8be030223cf this fails in the final Validator with `An expression 
references an unavailable slot, Slot: body#1`, by the mechanism described here: 
the join's read of `body` was skipped, `LazySlotPruning` dropped `body` above 
the filter, and the inner TopN kept the project merge from restoring it.
   
   `collectConsumedSlots` now leaves out only a filter's read of its own 
index-lazy slots, so any other operator's read keeps the column materialized. 
`TopnLazyMaterializeTest#testIndexPredicateSlotReadByAnotherOperatorStaysMaterialized`
 covers the join (fails before the fix, passes after), a variant where `other` 
is still fetched lazily, and a nested-TopN case where only MATCH reads `body`, 
so its alias is still fetched lazily. I did not add a regression case, because 
the suite was not run here and its `.out` has to come from a run.
   



##########
regression-test/suites/fault_injection_p0/test_topn_lazy_index_predicate_slots.groovy:
##########
@@ -0,0 +1,170 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// A TopN query fetches a column read only by search() or MATCH above the 
TopN, for the rows that survive it:
+// the scan answers the predicate from the inverted index and does not read 
the column.
+suite("test_topn_lazy_index_predicate_slots", "nonConcurrent") {
+    sql "DROP TABLE IF EXISTS topn_lazy_index_predicate_slots"
+    sql """
+        CREATE TABLE topn_lazy_index_predicate_slots (
+            `id` INT NOT NULL,
+            `body` STRING NULL,
+            `level` VARCHAR(16) NULL,
+            INDEX idx_body (`body`) USING INVERTED PROPERTIES("parser" = 
"english", "support_phrase" = "true")
+        ) ENGINE=OLAP
+        DUPLICATE KEY(`id`)
+        DISTRIBUTED BY HASH(`id`) BUCKETS 1
+        PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1",
+            "disable_auto_compaction" = "true"
+        )
+    """
+    sql """
+        INSERT INTO topn_lazy_index_predicate_slots VALUES
+            (1, 'disk error on node a', 'ERROR'),
+            (2, 'request served', 'INFO'),
+            (3, 'error while reading block', 'ERROR'),
+            (4, 'cache miss', 'WARN'),
+            (5, 'timeout error from upstream', 'ERROR'),
+            (6, NULL, 'INFO'),
+            (7, 'error error error', 'ERROR')
+    """
+    sql "sync"
+
+    // Only search() or MATCH reads body, so body is fetched lazily with level.
+    qt_search_shape """
+        explain shape plan
+        select id, body, level from topn_lazy_index_predicate_slots
+        where search('body:error') order by id desc limit 2
+    """
+    qt_search """
+        select id, body, level from topn_lazy_index_predicate_slots
+        where search('body:error') order by id desc limit 2
+    """
+    qt_match_shape """
+        explain shape plan
+        select id, body, level from topn_lazy_index_predicate_slots
+        where body match_any 'error' order by id limit 2
+    """
+    qt_match """
+        select id, body, level from topn_lazy_index_predicate_slots
+        where body match_any 'error' order by id limit 2
+    """
+    qt_or_shape """
+        explain shape plan
+        select id, body, level from topn_lazy_index_predicate_slots
+        where (body match_all 'disk node' or body match_any 'timeout') order 
by id limit 2
+    """
+    qt_or """
+        select id, body, level from topn_lazy_index_predicate_slots
+        where (body match_all 'disk node' or body match_any 'timeout') order 
by id limit 2
+    """
+    qt_alias """
+        select id, body as b from topn_lazy_index_predicate_slots
+        where search('body:error') and level = 'ERROR' order by id limit 3
+    """
+    // LIKE reads body row by row, so the scan reads body and keeps it.
+    qt_like_shape """
+        explain shape plan
+        select id, body, level from topn_lazy_index_predicate_slots
+        where search('body:error') and body like '%block%' order by id limit 2
+    """
+    qt_like """
+        select id, body, level from topn_lazy_index_predicate_slots
+        where search('body:error') and body like '%block%' order by id limit 2
+    """
+
+    try {
+        // Any read of body during the scan fails the query.
+        
GetDebugPoint().enableDebugPointForAllBEs("segment_iterator._read_columns_by_index",
 [column_name: "body"])
+        qt_search_without_reading_body """
+            select id, body, level from topn_lazy_index_predicate_slots
+            where search('body:error') order by id desc limit 2
+        """
+        qt_match_without_reading_body """
+            select id, body, level from topn_lazy_index_predicate_slots
+            where body match_any 'error' order by id limit 2
+        """
+        // Without lazy materialization the scan reads body for the output.
+        test {
+            sql """
+                select /*+ SET_VAR(topn_lazy_materialization_threshold=0) */ 
id, body, level
+                from topn_lazy_index_predicate_slots where 
search('body:error') order by id desc limit 2
+            """
+            exception "does not need to read data"
+        }
+    } finally {
+        
GetDebugPoint().disableDebugPointForAllBEs("segment_iterator._read_columns_by_index")
+    }
+
+    // Where a segment has no index, MATCH falls back to row-by-row evaluation 
and the scan reads body for it. Here the
+    // first segment is written before the index is added for new data only, 
the second one after.
+    sql "DROP TABLE IF EXISTS topn_lazy_index_predicate_slots_fallback"
+    sql """
+        CREATE TABLE topn_lazy_index_predicate_slots_fallback (
+            `id` INT NOT NULL,
+            `body` STRING NULL,
+            `level` VARCHAR(16) NULL
+        ) ENGINE=OLAP
+        DUPLICATE KEY(`id`)
+        DISTRIBUTED BY HASH(`id`) BUCKETS 1
+        PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1",
+            "disable_auto_compaction" = "true"
+        )
+    """
+    sql """
+        INSERT INTO topn_lazy_index_predicate_slots_fallback VALUES
+            (1, 'disk error on node a', 'ERROR'),
+            (2, 'request served', 'INFO'),
+            (3, 'error while reading block', 'ERROR')
+    """
+    sql "SET enable_add_index_for_new_data = true"
+    sql """
+        ALTER TABLE topn_lazy_index_predicate_slots_fallback
+        ADD INDEX idx_body (`body`) USING INVERTED PROPERTIES("parser" = 
"english")
+    """
+    
wait_for_last_schema_change_finish("topn_lazy_index_predicate_slots_fallback", 
60000)

Review Comment:
   Confirmed and fixed in 11578eabfb3.
   
   `wait_for_last_schema_change_finish` 
(`regression-test/plugins/plugin_index_change.groovy`) also runs 
`wait_for_last_build_index_finish`, which polls `SHOW BUILD INDEX` for all 60 
rounds when no job exists. For ADD INDEX, `modifyTableLightSchemaChange` 
records only a finished schema change job. The suite now calls 
`wait_for_last_col_change_finish`, which also covers cloud mode, where this ADD 
INDEX runs as a heavy schema change job. The suite was not run after 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: [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]

Reply via email to