hello-stephen commented on code in PR #70388:
URL: https://github.com/apache/doris/pull/70388#discussion_r4740237814
##########
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:
[P2] Preserve mandatory consumers outside the index filter
The global exemption also removes a slot used by another operator below the
outer TopN. For example, on a supported base-index table with the default
session settings:
```sql
select 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 order by body limit 2
) b order by k limit 1;
```
Its reduced input plan (distribution/sort phases omitted) is:
```text
TopN(k)
Project(y, k)
TopN(body)
Project(body, body AS y, k)
TopN(k)
Filter(body MATCH_ANY 'error')
Scan(k, body)
```
`collectRequiredAliasSources` skips `body` because the filter added it to
this global set, even though the middle TopN needs it. Probing `y` passes that
TopN, resolves `y -> body` below it, and `visitPhysicalFilter` now returns a
lazy source. Since bare `body` is absent from the outer output,
`collectRequiredOutputSlots` does not rescue it. `LazySlotPruning` then inserts
`Project(k,rowid)` above the filter; the inner TopN stops producing `body`,
while the retained project above it still references `body`. That TopN prevents
the later project merge from restoring the slot, so the final Validator rejects
the query with the default `feDebug=false`.
Please exclude index-only inputs at their own filter, while retaining any
mandatory use by another operator. Add this combined nested-TopN/MATCH case to
the alias regression coverage.
##########
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:
[P3] Wait only for the schema-change job after light ADD INDEX
In the default shared-nothing path, this fresh table's inverted-index ADD
goes through `modifyTableLightSchemaChange`, which records a finished
COLUMN/schema-change job but creates no BUILD INDEX job for ADD.
`wait_for_last_schema_change_finish` also calls
`wait_for_last_build_index_finish`; with no matching job, that helper performs
all 60 one-second sleeps before returning. This adds an unnecessary minute to
the new `nonConcurrent` suite. Use `wait_for_last_col_change_finish` here so
the intended old unindexed segments are preserved without polling for a
nonexistent build job.
--
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]