This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new 43b89881648 [fix](be) Distinguish NULL literals from zero in condition
cache digests (#71143)
43b89881648 is described below
commit 43b8988164815eb3b9137884534c8047fab03bf8
Author: HappenLee <[email protected]>
AuthorDate: Wed Dec 2 15:19:51 2026 +0800
[fix](be) Distinguish NULL literals from zero in condition cache digests
(#71143)
### Background
The condition cache remembers which ranges of a segment had no matching
rows. A later scan with the same predicate digest can skip those ranges.
### What problem does this PR solve?
Issue Number: None
Related PR: None
PR Type: Bugfix
Problem Summary: A typed NULL and INT 0 both contributed four zero bytes
to the predicate digest. A query comparing a column with a CASE
expression that returned NULL could therefore warm the cache with an
empty result. A subsequent query comparing the same column with a CASE
expression returning 0 could skip matching rows.
Compute the digest of a top-level NULL literal from an empty payload,
mixing its primitive type into the incoming seed. The type distinguishes
it from an empty string and other typed NULLs. Each scalar literal still
takes one hash, and non-NULL values use the existing column hash.
### Testing
- BE unit tests exercise NULL vs zero, a nullable non-NULL zero, typed
NULLs, an empty string, and the position of NULL among sibling
expressions. The added test fails on the original implementation. All
seven tests in VLiteralDigestTest pass with the fix, including the
existing cache bypass for unsupported columns.
- The SQL regression warms the condition cache with the NULL predicate,
then checks the zero predicate with the condition cache on and off. Both
returned IDs 3 and 7 on a local ASAN BE. Repeating the zero predicate
returned 3 and 7 again and the BE recorded a condition cache hit.
- A local check using the bundled XXH3 compared the old and new inputs
across 65,536 seeds. The old inputs for INT NULL and zero had equal
digests for all 65,536. The new inputs had equal digests for none.
### Release note
Fix missing rows when an earlier query populates the condition cache
with a predicate containing a NULL literal and a later predicate uses
INT 0.
### Check List (For Author)
- Test: BE unit test and SQL regression.
- Behavior changed: Yes. A warm cache now returns the same matching rows
as an uncached scan for this case.
- Does this need documentation: No
- Pre-submission status: Ready
### AI Disclosure
- Tool: OpenAI Codex
- Generated: Implementation, tests, and PR description.
- Human review: The contributor discussed the root cause and the use of
the primitive type in the digest. Code review requested.
---
be/src/exprs/vliteral.cpp | 6 +++
be/test/exprs/vliteral_digest_test.cpp | 33 ++++++++++++++
.../test_condition_cache_null_literal.out | 11 +++++
.../test_condition_cache_null_literal.groovy | 52 ++++++++++++++++++++++
4 files changed, 102 insertions(+)
diff --git a/be/src/exprs/vliteral.cpp b/be/src/exprs/vliteral.cpp
index ee8f7e7641e..473c449db32 100644
--- a/be/src/exprs/vliteral.cpp
+++ b/be/src/exprs/vliteral.cpp
@@ -35,6 +35,7 @@
#include "core/field.h"
#include "core/types.h"
#include "exprs/aggregate/aggregate_function.h"
+#include "util/hash_util.hpp"
namespace doris {
@@ -128,6 +129,11 @@ uint64_t VLiteral::get_digest(uint64_t seed) const {
if (!supports_literal_digest(*_column_ptr)) {
return 0;
}
+ if (_column_ptr->is_null_at(0)) {
+ // Column hashing encodes NULL as four zero bytes, just like INT 0.
Hash an
+ // empty payload with a typed seed so an empty string stays distinct
as well.
+ return HashUtil::xxHash64WithSeed("", 0, seed ^
_data_type->get_primitive_type());
+ }
_column_ptr->update_xxHash_with_value(0, 1, seed, nullptr);
return seed;
}
diff --git a/be/test/exprs/vliteral_digest_test.cpp
b/be/test/exprs/vliteral_digest_test.cpp
index 59d2f9c8074..f365255eab4 100644
--- a/be/test/exprs/vliteral_digest_test.cpp
+++ b/be/test/exprs/vliteral_digest_test.cpp
@@ -35,6 +35,39 @@
namespace doris {
+TEST(VLiteralDigestTest, TypedNullDoesNotAliasScalarValues) {
+ auto int_type = std::make_shared<DataTypeInt32>();
+ auto string_type = std::make_shared<DataTypeString>();
+ auto int_null = VLiteral::create_shared(make_nullable(int_type), Field());
+ auto string_null = VLiteral::create_shared(make_nullable(string_type),
Field());
+ auto zero = VLiteral::create_shared(int_type,
Field::create_field<TYPE_INT>(0));
+ auto nullable_zero =
+ VLiteral::create_shared(make_nullable(int_type),
Field::create_field<TYPE_INT>(0));
+ auto empty = VLiteral::create_shared(string_type,
Field::create_field<TYPE_STRING>(String("")));
+
+ for (uint64_t seed : {0ULL, 1ULL, 42ULL, 0x123456789abcdef0ULL}) {
+ SCOPED_TRACE(seed);
+ auto digest = int_null->get_digest(seed);
+ EXPECT_NE(digest, 0);
+ EXPECT_NE(digest, zero->get_digest(seed));
+ EXPECT_EQ(zero->get_digest(seed), nullable_zero->get_digest(seed));
+ EXPECT_NE(digest, string_null->get_digest(seed));
+ EXPECT_NE(string_null->get_digest(seed), empty->get_digest(seed));
+ }
+
+ auto type_id = VLiteral::create_shared(int_type,
Field::create_field<TYPE_INT>(TYPE_INT));
+ // Moving NULL past the values 0 and its primitive type ID must change the
parent digest.
+ auto first = MockFnCall::create("array");
+ first->add_child(int_null);
+ first->add_child(zero);
+ first->add_child(type_id);
+ auto last = MockFnCall::create("array");
+ last->add_child(zero);
+ last->add_child(type_id);
+ last->add_child(int_null);
+ EXPECT_NE(first->get_digest(42), last->get_digest(42));
+}
+
TEST(VLiteralDigestTest, ComplexNullDisablesParentDigest) {
for (auto type : {TYPE_BITMAP, TYPE_HLL, TYPE_QUANTILE_STATE,
TYPE_VARBINARY}) {
SCOPED_TRACE(type_to_string(type));
diff --git
a/regression-test/data/query_p0/condition_cache/test_condition_cache_null_literal.out
b/regression-test/data/query_p0/condition_cache/test_condition_cache_null_literal.out
new file mode 100644
index 00000000000..386775462b9
--- /dev/null
+++
b/regression-test/data/query_p0/condition_cache/test_condition_cache_null_literal.out
@@ -0,0 +1,11 @@
+-- This file is automatically generated. You should know what you did if you
want to edit this
+-- !warm --
+
+-- !cached --
+3
+7
+
+-- !uncached --
+3
+7
+
diff --git
a/regression-test/suites/query_p0/condition_cache/test_condition_cache_null_literal.groovy
b/regression-test/suites/query_p0/condition_cache/test_condition_cache_null_literal.groovy
new file mode 100644
index 00000000000..3c270947802
--- /dev/null
+++
b/regression-test/suites/query_p0/condition_cache/test_condition_cache_null_literal.groovy
@@ -0,0 +1,52 @@
+// 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_condition_cache_null_literal") {
+ sql "set enable_sql_cache = false"
+ sql "set enable_condition_cache = true"
+ sql "drop table if exists condition_cache_null_literal"
+ sql """
+ create table condition_cache_null_literal (id int not null, n int,
flag boolean)
+ unique key(id) distributed by hash(id) buckets 1
+ properties ("replication_num" = "1")
+ """
+ sql """
+ insert into condition_cache_null_literal values
+ (1, null, true), (2, -1, false), (3, 0, null), (4, 1, true), (5, 2,
false),
+ (6, null, null), (7, 0, true), (8, 3, false), (9, -1, null)
+ """
+
+ // Warm the segment cache with a predicate that matches no rows.
+ order_qt_warm """
+ select id from condition_cache_null_literal where n = case
+ when flag then cast(null as int)
+ when not flag then if(flag, cast(9 as int), cast(null as int))
+ else coalesce(case when flag is not null then cast(9 as int) end,
cast(null as int))
+ end
+ """
+
+ def matchZero = """
+ select id from condition_cache_null_literal where n <=> case
+ when flag then cast(0 as int)
+ when not flag then if(flag, cast(9 as int), cast(0 as int))
+ else coalesce(case when flag is not null then cast(9 as int) end,
cast(0 as int))
+ end
+ """
+ order_qt_cached matchZero
+ sql "set enable_condition_cache = false"
+ order_qt_uncached matchZero
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]