This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.2
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.2 by this push:
new 44c84625f79 branch-4.2: [fix](scan) Read a column that a cast-only
conjunct uses when the index answers its other filters #70849 (#71385)
44c84625f79 is described below
commit 44c84625f7903f9df80f95bee66551d943286124
Author: guptashivam04 <[email protected]>
AuthorDate: Sat Dec 5 19:20:25 2026 +0530
branch-4.2: [fix](scan) Read a column that a cast-only conjunct uses when
the index answers its other filters #70849 (#71385)
### What problem does this PR solve?
Issue Number: None
Related PR: #70849
PR Type: Bug fix
Problem Summary:
Backport of #70849 to branch-4.2. It is the same change with one
adaptation: on branch-4.2 a slot's `column_id()` is its position in the
read schema, so the new code maps it with `_schema->column_id()`, as the
rest of `_calculate_common_expr_index_exec_status` does. Master no
longer needs that mapping. A plain cherry-pick builds, but records the
cast on the wrong column, and `n IN ('0', '1') AND CAST(n AS BOOLEAN)`
still returned no rows.
```sql
SELECT count(*) FROM t WHERE s MATCH 'true' AND CAST(s AS BOOLEAN)
```
returned 0 rows with the inverted index on, and the matching rows with
it off. A conjunct that is only a cast of a column was not counted among
the filters on that column, so when the index answered the other filters
and the column was not selected, the column was not read and the cast
ran on empty values. The same happens with SEARCH, MATCH_ANY and IN.
Such a conjunct now counts as one the index does not answer. See #70849.
### Release note
Fix a filter that only casts a column, such as `CAST(s AS BOOLEAN)`,
returning no rows when the inverted index answers another filter on the
same column.
### Check List (For Author)
- Test
- [x] Regression test: `inverted_index_p0/test_index_cast_root_conjunct`
fails on branch-4.2 without this change and passes with it, including
the IN case above.
- [x] `inverted_index_p0` and `search` (296 suites) on a local
branch-4.2 cluster: no suite fails only with this change (the ones that
fail, fail the same way without it, from local setup and leftovers of
earlier runs).
- Behavior changed: Yes. Such queries now return the matching rows.
- Does this need documentation: No
Co-authored-by: Shivam Gupta <[email protected]>
---
be/src/storage/segment/segment_iterator.cpp | 11 +
.../test_index_cast_root_conjunct.out | 244 +++++++++++++++++++++
.../test_index_cast_root_conjunct.groovy | 72 ++++++
3 files changed, 327 insertions(+)
diff --git a/be/src/storage/segment/segment_iterator.cpp
b/be/src/storage/segment/segment_iterator.cpp
index 6b15eb67c83..86020d0b763 100644
--- a/be/src/storage/segment/segment_iterator.cpp
+++ b/be/src/storage/segment/segment_iterator.cpp
@@ -3803,6 +3803,17 @@ void
SegmentIterator::_calculate_common_expr_index_exec_status() {
}
_common_expr_to_slotref_map[root_expr_ctx.get()] =
std::unordered_map<ColumnId, VExpr*>();
+ // A conjunct that is a column, or a cast of one, reads the column
itself and no index
+ // answers it, so the column keeps its data even when the index
answers every other
+ // conjunct on it. The walk below records slots by the parent they sit
under, which a
+ // root has none of.
+ if (const auto root_column = VExpr::expr_without_cast(root_expr);
+ root_column->is_slot_ref()) {
+ const auto* slot_ref = assert_cast<const
VSlotRef*>(root_column.get());
+
_common_expr_index_exec_status[_schema->column_id(slot_ref->column_id())]
+ [root_expr.get()] = false;
+ }
+
std::stack<VExprSPtr> stack;
stack.emplace(root_expr);
diff --git
a/regression-test/data/inverted_index_p0/test_index_cast_root_conjunct.out
b/regression-test/data/inverted_index_p0/test_index_cast_root_conjunct.out
new file mode 100644
index 00000000000..3a96fe1ef6f
--- /dev/null
+++ b/regression-test/data/inverted_index_p0/test_index_cast_root_conjunct.out
@@ -0,0 +1,244 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !match_index_on_count --
+129 19284
+
+-- !match_index_on_rows --
+0
+10
+12
+14
+16
+2
+4
+8
+
+-- !match_index_on_selected --
+0 true 0
+10 true 0
+12 true 2
+14 true 4
+16 true 1
+2 true 2
+4 true 4
+8 true 3
+
+-- !match_index_off_count --
+129 19284
+
+-- !match_index_off_rows --
+0
+10
+12
+14
+16
+2
+4
+8
+
+-- !match_index_off_selected --
+0 true 0
+10 true 0
+12 true 2
+14 true 4
+16 true 1
+2 true 2
+4 true 4
+8 true 3
+
+-- !match_any_index_on_count --
+129 19284
+
+-- !match_any_index_on_rows --
+0
+10
+12
+14
+16
+2
+4
+8
+
+-- !match_any_index_on_selected --
+0 true 0
+10 true 0
+12 true 2
+14 true 4
+16 true 1
+2 true 2
+4 true 4
+8 true 3
+
+-- !match_any_index_off_count --
+129 19284
+
+-- !match_any_index_off_rows --
+0
+10
+12
+14
+16
+2
+4
+8
+
+-- !match_any_index_off_selected --
+0 true 0
+10 true 0
+12 true 2
+14 true 4
+16 true 1
+2 true 2
+4 true 4
+8 true 3
+
+-- !match_not_index_on_count --
+129 19287
+
+-- !match_not_index_on_rows --
+1
+11
+15
+17
+3
+5
+7
+9
+
+-- !match_not_index_on_selected --
+1 false 1
+11 false 1
+15 false 0
+17 false 2
+3 false 3
+5 false 0
+7 false 2
+9 false 4
+
+-- !match_not_index_off_count --
+129 19287
+
+-- !match_not_index_off_rows --
+1
+11
+15
+17
+3
+5
+7
+9
+
+-- !match_not_index_off_selected --
+1 false 1
+11 false 1
+15 false 0
+17 false 2
+3 false 3
+5 false 0
+7 false 2
+9 false 4
+
+-- !in_list_index_on_count --
+60 8910
+
+-- !in_list_index_on_rows --
+1
+11
+16
+21
+26
+31
+36
+6
+
+-- !in_list_index_on_selected --
+1 false 1
+11 false 1
+16 true 1
+21 false 1
+26 true 1
+31 false 1
+36 true 1
+6 \N 1
+
+-- !in_list_index_off_count --
+60 8910
+
+-- !in_list_index_off_rows --
+1
+11
+16
+21
+26
+31
+36
+6
+
+-- !in_list_index_off_selected --
+1 false 1
+11 false 1
+16 true 1
+21 false 1
+26 true 1
+31 false 1
+36 true 1
+6 \N 1
+
+-- !in_list_int_index_on_count --
+60 8910
+
+-- !in_list_int_index_on_rows --
+1
+11
+16
+21
+26
+31
+36
+6
+
+-- !in_list_int_index_on_selected --
+1 false 1
+11 false 1
+16 true 1
+21 false 1
+26 true 1
+31 false 1
+36 true 1
+6 \N 1
+
+-- !in_list_int_index_off_count --
+60 8910
+
+-- !in_list_int_index_off_rows --
+1
+11
+16
+21
+26
+31
+36
+6
+
+-- !in_list_int_index_off_selected --
+1 false 1
+11 false 1
+16 true 1
+21 false 1
+26 true 1
+31 false 1
+36 true 1
+6 \N 1
+
+-- !search_count --
+129 19284
+
+-- !search_rows --
+0
+10
+12
+14
+16
+2
+4
+8
+
diff --git
a/regression-test/suites/inverted_index_p0/test_index_cast_root_conjunct.groovy
b/regression-test/suites/inverted_index_p0/test_index_cast_root_conjunct.groovy
new file mode 100644
index 00000000000..4404745688a
--- /dev/null
+++
b/regression-test/suites/inverted_index_p0/test_index_cast_root_conjunct.groovy
@@ -0,0 +1,72 @@
+// 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 conjunct that is only a cast of a column, such as CAST(s AS BOOLEAN),
reads the column
+// itself. When the index answers every other filter on that column and the
column is not
+// selected, the column must still be read for that conjunct. The same queries
run with the
+// index on and off, so both must match the same expected rows.
+suite("test_index_cast_root_conjunct") {
+ def table = "test_index_cast_root_conjunct"
+ sql "DROP TABLE IF EXISTS ${table}"
+ sql """
+ CREATE TABLE ${table} (
+ id INT,
+ s VARCHAR(16),
+ n VARCHAR(8),
+ INDEX idx_s (s) USING INVERTED PROPERTIES("parser" = "english"),
+ INDEX idx_n (n) USING INVERTED
+ ) ENGINE=OLAP
+ DUPLICATE KEY(id)
+ DISTRIBUTED BY HASH(id) BUCKETS 1
+ PROPERTIES ("replication_allocation" = "tag.location.default: 1")
+ """
+ sql """
+ INSERT INTO ${table}
+ SELECT number,
+ if(number % 7 = 6, NULL, if(number % 2 = 0, 'true', 'false')),
+ cast(number % 5 AS STRING)
+ FROM numbers("number" = "300")
+ """
+ sql "sync"
+ sql "set enable_common_expr_pushdown = true"
+ sql "set enable_condition_cache = false"
+ sql "set enable_inverted_index_query_cache = false"
+
+ def filters = [
+ match : "s MATCH 'true' AND CAST(s AS BOOLEAN)",
+ match_any : "s MATCH_ANY 'true false' AND CAST(s AS BOOLEAN)",
+ match_not : "s MATCH_ANY 'true false' AND NOT CAST(s AS BOOLEAN)",
+ in_list : "n IN ('0', '1') AND CAST(n AS BOOLEAN)",
+ in_list_int : "n IN ('0', '1') AND CAST(n AS INT) = 1",
+ ]
+ filters.each { tag, filter ->
+ [index_on: "true", index_off: "false"].each { mode, index ->
+ sql "set enable_inverted_index_query = ${index}"
+ "order_qt_${tag}_${mode}_count"("SELECT count(id), sum(id) FROM
${table} WHERE ${filter}")
+ "order_qt_${tag}_${mode}_rows"("SELECT id FROM ${table} WHERE
${filter} ORDER BY id LIMIT 8")
+ // Selecting the column reads it anyway.
+ "order_qt_${tag}_${mode}_selected"("SELECT id, s, n FROM ${table}
WHERE ${filter} ORDER BY id LIMIT 8")
+ }
+ }
+
+ // SEARCH needs the index, so it has no index-off run; it must match the
MATCH rows above.
+ sql "set enable_inverted_index_query = true"
+ order_qt_search_count "SELECT count(id), sum(id) FROM ${table} WHERE
search('s:true') AND CAST(s AS BOOLEAN)"
+ order_qt_search_rows "SELECT id FROM ${table} WHERE search('s:true') AND
CAST(s AS BOOLEAN) ORDER BY id LIMIT 8"
+
+ sql "DROP TABLE IF EXISTS ${table}"
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]