github-actions[bot] commented on code in PR #65821:
URL: https://github.com/apache/doris/pull/65821#discussion_r3793404514


##########
fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/PushDownScoreTopNIntoOlapScan.java:
##########
@@ -239,6 +243,19 @@ private Plan pushDown(
         return topN.withChildren(newProject);
     }
 
+    private boolean shouldDisableSearchTopN(Set<Expression> conjuncts, 
Expression extractedScorePredicate) {
+        List<Expression> nonScoreConjuncts = conjuncts.stream()
+                .filter(conjunct -> extractedScorePredicate == null || 
!conjunct.equals(extractedScorePredicate))
+                .collect(ImmutableList.toImmutableList());
+
+        boolean hasSearchPredicate = nonScoreConjuncts.stream()
+                .anyMatch(conjunct -> !conjunct.collect(e -> e instanceof 
SearchExpression).isEmpty());
+        if (!hasSearchPredicate) {
+            return false;
+        }
+        return nonScoreConjuncts.size() > 1 || !(nonScoreConjuncts.get(0) 
instanceof SearchExpression);

Review Comment:
   [P1] Account for storage visibility before keeping SEARCH Top-K
   
   A lone SEARCH is not sufficient to make early Top-K safe. For example:
   
   ```text
   TopN(score DESC, LIMIT 1)
     Project(id, score() AS score)
       Filter(search('title:apple'))
         Scan(unique-key MOW table)
   ```
   
   This branch keeps `score_sort_limit = 1`. In BE, 
`SegmentIterator::_lazy_init()` evaluates SEARCH in 
`_get_row_ranges_by_column_conditions()` before subtracting 
`_opts.delete_bitmap`. If a deleted old version has the highest score and a 
live row is second, SEARCH returns only the deleted row; the later bitmap 
subtraction removes it, and the upper TopN cannot recover the live runner-up. 
SEARCH is supported on MOW tables, so this is reachable even with no extra SQL 
conjunct. Please either disable the early limit whenever post-SEARCH 
visibility/range filters may exist, or apply those masks before SEARCH selects 
Top-K, and add a deleted-highest-score regression.



##########
regression-test/suites/inverted_index_p0/test_search_score_topn_predicates.groovy:
##########
@@ -0,0 +1,191 @@
+// 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.
+
+suite("test_search_score_topn_predicates", "p0") {
+    sql "DROP TABLE IF EXISTS test_search_score_topn_predicates"
+
+    sql """
+        CREATE TABLE test_search_score_topn_predicates (
+            id INT,
+            status VARCHAR(20),
+            plain_status VARCHAR(20),
+            category VARCHAR(20),
+            title TEXT,
+            body TEXT,
+            INDEX idx_status (status) USING INVERTED,
+            INDEX idx_category (category) USING INVERTED,
+            INDEX idx_title (title) USING INVERTED PROPERTIES("parser" = 
"english", "support_phrase" = "true"),
+            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 test_search_score_topn_predicates VALUES
+            (1, 'drop', 'drop', 'plain', 'apple apple apple apple apple apple 
apple apple apple apple apple apple', 'alpha'),
+            (2, 'keep', 'keep', 'plain', 'apple', 'alpha'),
+            (3, 'keep', 'keep', 'plain', 'apple apple apple apple apple', 
'alpha'),
+            (4, 'keep', 'keep', 'plain', 'apple apple apple', 'beta beta 
beta'),
+            (5, 'drop', 'drop', 'plain', 'banana', 'beta beta beta beta beta 
beta beta beta beta beta beta beta'),
+            (6, 'keep', 'keep', 'plain', 'pear', 'beta beta beta beta beta'),
+            (7, 'keep', 'keep', 'special', 'cherry cherry cherry cherry', 
'gamma'),
+            (8, 'drop', 'drop', 'special', 'cherry cherry cherry cherry cherry 
cherry cherry cherry cherry cherry', 'gamma')
+    """
+
+    sql "sync"
+    sql "set enable_nereids_planner = true"
+    sql "set enable_fallback_to_original_planner = false"
+    sql "set enable_segment_limit_pushdown = true"
+    sql "set enable_inverted_index_query_cache = false"
+
+    qt_single_search """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple')
+            ORDER BY s DESC
+            LIMIT 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_equal_predicate """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND status = 'keep'
+            ORDER BY s DESC
+            LIMIT 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_plain_equal_predicate """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND plain_status = 'keep'
+            ORDER BY s DESC
+            LIMIT 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_equal_limit_two """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND status = 'keep'
+            ORDER BY s DESC
+            LIMIT 2
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_equal_offset """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND status = 'keep'
+            ORDER BY s DESC
+            LIMIT 1 OFFSET 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_match_predicate """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND status MATCH 'keep'
+            ORDER BY s DESC
+            LIMIT 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_range_predicate """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND id > 1
+            ORDER BY s DESC
+            LIMIT 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_multiple_search_predicates """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND search('body:beta')
+            ORDER BY s DESC
+            LIMIT 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_score_range_only """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND score() > 0
+            ORDER BY s DESC
+            LIMIT 2
+        ) t
+        ORDER BY id
+    """
+
+    qt_search_with_score_range_and_other_predicate """
+        SELECT id FROM (
+            SELECT id, score() AS s
+            FROM test_search_score_topn_predicates
+            WHERE search('title:apple') AND score() > 0 AND status = 'keep'
+            ORDER BY s DESC
+            LIMIT 1
+        ) t
+        ORDER BY id
+    """
+
+    qt_nested_search_with_other_predicate """

Review Comment:
   [P2] Make the nested cases depend on recursive SEARCH detection
   
   Neither named case fails with a shallow top-level-only classifier. In the OR 
case, `category MATCH 'special'` independently restores id 7 after SEARCH Top-1 
keeps id 8; in the NOT case, the separate top-level `search('body:beta')` 
already causes the limit to be disabled. If the selective recursive classifier 
remains after the correctness fix, please make the alternative OR branch 
nonmatching and add a negated-only witness (or a direct FE rule test) so losing 
`.collect(SearchExpression)` changes the expected result.



##########
regression-test/suites/inverted_index_p0/test_search_score_topn_predicates.groovy:
##########
@@ -0,0 +1,191 @@
+// 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.
+
+suite("test_search_score_topn_predicates", "p0") {
+    sql "DROP TABLE IF EXISTS test_search_score_topn_predicates"
+
+    sql """
+        CREATE TABLE test_search_score_topn_predicates (
+            id INT,
+            status VARCHAR(20),
+            plain_status VARCHAR(20),
+            category VARCHAR(20),
+            title TEXT,
+            body TEXT,
+            INDEX idx_status (status) USING INVERTED,
+            INDEX idx_category (category) USING INVERTED,
+            INDEX idx_title (title) USING INVERTED PROPERTIES("parser" = 
"english", "support_phrase" = "true"),
+            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 test_search_score_topn_predicates VALUES
+            (1, 'drop', 'drop', 'plain', 'apple apple apple apple apple apple 
apple apple apple apple apple apple', 'alpha'),
+            (2, 'keep', 'keep', 'plain', 'apple', 'alpha'),
+            (3, 'keep', 'keep', 'plain', 'apple apple apple apple apple', 
'alpha'),
+            (4, 'keep', 'keep', 'plain', 'apple apple apple', 'beta beta 
beta'),
+            (5, 'drop', 'drop', 'plain', 'banana', 'beta beta beta beta beta 
beta beta beta beta beta beta beta'),
+            (6, 'keep', 'keep', 'plain', 'pear', 'beta beta beta beta beta'),
+            (7, 'keep', 'keep', 'special', 'cherry cherry cherry cherry', 
'gamma'),
+            (8, 'drop', 'drop', 'special', 'cherry cherry cherry cherry cherry 
cherry cherry cherry cherry cherry', 'gamma')
+    """
+
+    sql "sync"
+    sql "set enable_nereids_planner = true"
+    sql "set enable_fallback_to_original_planner = false"
+    sql "set enable_segment_limit_pushdown = true"
+    sql "set enable_inverted_index_query_cache = false"
+
+    qt_single_search """

Review Comment:
   [P2] Cover the positive branch if it remains
   
   These result-only queries cannot distinguish `SCORE SORT LIMIT: 1` from the 
disable sentinel `0`, because the upper TopN produces the same rows either way; 
the complete changed suite passes if the helper always returns zero. After 
addressing the visibility issue above, if any selective positive-limit branch 
remains, please add deterministic EXPLAIN assertions for that demonstrably safe 
case and for an extra-predicate zero case. If the fix removes positive SEARCH 
Top-K entirely, this assertion is unnecessary.



-- 
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