Skip to content

Commit ce1ffe3

Browse files
committed
[fix](be) Restrict the Variant null-test guard to Variant casts
Make the scan normalization guard check the cast source type explicitly (Variant) instead of skipping every cast, and add the `variant_p0/test_variant_cast_isnull_pushdown` regression: `CAST(v AS INT) IS NULL`, `IS NOT NULL` and `<=> NULL` over a `VARIANT NOT NULL` column. Without the guard the `IS NULL` / `<=> NULL` filters return no rows, because the scan rewrites the null test on the cast into `v IS NULL` and drops the non-NULL rows whose cast result is NULL.
1 parent 43ccf7d commit ce1ffe3

3 files changed

Lines changed: 65 additions & 3 deletions

File tree

‎be/src/exec/operator/scan_operator.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,12 @@ Status ScanLocalStateBase::_normalize_is_null_predicate(
943943
return Status::OK();
944944
}
945945

946-
// A Variant cast can introduce NULLs for non-NULL stored values. Keep its null test as an
947-
// expression instead of replacing it with a NullPredicate on the source column.
948-
if (root->children()[0]->node_type() == TExprNodeType::CAST_EXPR) {
946+
// A Variant cast can introduce NULLs for non-NULL stored values, so `CAST(v AS T) IS NULL`
947+
// is not equivalent to `v IS NULL`. Keep the null test in the expression tree instead of
948+
// pushing a NullPredicate down on the source Variant column.
949+
if (root->children()[0]->node_type() == TExprNodeType::CAST_EXPR &&
950+
VExpr::expr_without_cast(root->children()[0])->data_type()->get_primitive_type() ==
951+
PrimitiveType::TYPE_VARIANT) {
949952
return Status::OK();
950953
}
951954

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !cast_int --
3+
1 1
4+
2 \N
5+
3 \N
6+
7+
-- !is_null --
8+
2
9+
3
10+
11+
-- !is_not_null --
12+
1
13+
14+
-- !null_safe --
15+
2
16+
3
17+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("test_variant_cast_isnull_pushdown", "p0") {
19+
// A Variant cast can turn a non-NULL Variant value into NULL, so `CAST(v AS INT) IS NULL`
20+
// is not equivalent to `v IS NULL`. The scan node must keep this null test as an expression
21+
// instead of pushing a null predicate down on the source Variant column.
22+
sql "DROP TABLE IF EXISTS variant_cast_isnull_pushdown"
23+
sql """
24+
CREATE TABLE variant_cast_isnull_pushdown (
25+
id INT NOT NULL,
26+
v VARIANT NOT NULL
27+
) DUPLICATE KEY(id)
28+
DISTRIBUTED BY HASH(id) BUCKETS 1
29+
PROPERTIES("replication_num" = "1")
30+
"""
31+
sql """
32+
INSERT INTO variant_cast_isnull_pushdown VALUES
33+
(1, '1'),
34+
(2, '"x"'),
35+
(3, '[1,2]')
36+
"""
37+
38+
order_qt_cast_int "SELECT id, CAST(v AS INT) FROM variant_cast_isnull_pushdown ORDER BY id"
39+
order_qt_is_null "SELECT id FROM variant_cast_isnull_pushdown WHERE CAST(v AS INT) IS NULL ORDER BY id"
40+
order_qt_is_not_null "SELECT id FROM variant_cast_isnull_pushdown WHERE CAST(v AS INT) IS NOT NULL ORDER BY id"
41+
order_qt_null_safe "SELECT id FROM variant_cast_isnull_pushdown WHERE CAST(v AS INT) <=> NULL ORDER BY id"
42+
}

0 commit comments

Comments
 (0)