This is an automated email from the ASF dual-hosted git repository.

englefly pushed a commit to branch preserve-is-null
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 9bdf4d08d16d8e8719d053919b7d71fea5d387fc
Author: englefly <[email protected]>
AuthorDate: Wed Apr 15 14:47:48 2026 +0800

    [improvement](fe) Keep generated IS NOT NULL for variable-length types in 
EliminateNotNull
    
    ### What problem does this PR solve?
    
    Issue Number: close #xxx
    
    Problem Summary:
    The EliminateNotNull rule removes all generated IS NOT NULL predicates when 
they
    are implied by other predicates (e.g., `a > 'abc'` implies `a IS NOT NULL`).
    However, for variable-length types (VARCHAR, STRING, JSON, VARIANT, ARRAY, 
MAP,
    STRUCT), the IS NOT NULL check reads only the null bitmap which is very 
cheap and
    vectorizable, while the actual predicate (`a > 'abc'`) requires reading
    variable-length data which is expensive. Keeping the IS NOT NULL as a 
pre-filter
    can skip null rows before reading the variable-length data, improving scan 
performance.
    
    For fixed-length types (INT, BIGINT, DATE, etc.), the IS NOT NULL check 
offers no
    benefit because reading the column data is already cheap, so it is still 
removed.
    
    ### Release note
    
    Optimizer now preserves generated IS NOT NULL predicates for 
variable-length type
    columns (STRING, VARCHAR, JSON, VARIANT, complex types) to enable cheaper 
null
    bitmap pre-filtering at scan level.
    
    ### Check List (For Author)
    
    - Test: Unit Test (EliminateNotNullTest - 6 test cases)
    - Behavior changed: Yes - generated IS NOT NULL for variable-length types 
is no longer removed
    - Does this need documentation: No
    
    Co-authored-by: Copilot <[email protected]>
---
 .../nereids/rules/rewrite/EliminateNotNull.java    |  33 +++-
 .../rules/rewrite/EliminateNotNullTest.java        | 194 +++++++++++++++++++++
 2 files changed, 224 insertions(+), 3 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateNotNull.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateNotNull.java
index 251d7cd82b1..71f607aea4b 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateNotNull.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/EliminateNotNull.java
@@ -24,9 +24,11 @@ import org.apache.doris.nereids.trees.expressions.Expression;
 import org.apache.doris.nereids.trees.expressions.IsNull;
 import org.apache.doris.nereids.trees.expressions.Not;
 import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.SlotReference;
 import org.apache.doris.nereids.trees.plans.Plan;
 import org.apache.doris.nereids.trees.plans.logical.LogicalFilter;
 import org.apache.doris.nereids.trees.plans.logical.LogicalJoin;
+import org.apache.doris.nereids.types.DataType;
 import org.apache.doris.nereids.util.ExpressionUtils;
 import org.apache.doris.nereids.util.PlanUtils;
 import org.apache.doris.nereids.util.TypeUtils;
@@ -45,6 +47,11 @@ import java.util.Set;
  * Eliminate Predicate `is not null`, like
  * - redundant `is not null` predicate like `a > 0 and a is not null` -> `a > 
0`
  * - `is not null` predicate is generated by `InferFilterNotNull`
+ *
+ * For generated IS NOT NULL on variable-length types (VARCHAR, STRING, JSON, 
etc.),
+ * the predicate is kept even when implied by other predicates, because the 
null bitmap
+ * check is much cheaper than reading variable-length data and serves as an 
efficient
+ * pre-filter at the scan level.
  */
 public class EliminateNotNull implements RewriteRuleFactory {
     @Override
@@ -83,12 +90,26 @@ public class EliminateNotNull implements RewriteRuleFactory 
{
         // predicatesNotContainIsNotNull infer nonNullable slots: `id`
         // slotsFromIsNotNull: `id`, `name`
         // remove `name` (it's generated), remove `id` (because `id > 0` 
already contains it)
+        //
+        // However, for variable-length types (STRING, VARCHAR, JSON, etc.), 
generated IS NOT NULL
+        // is kept even when implied, because null bitmap check is much 
cheaper than reading
+        // variable-length data, serving as an efficient pre-filter.
         Set<Expression> predicatesNotContainIsNotNull = 
Sets.newLinkedHashSet();
         List<Slot> slotsFromIsNotNull = Lists.newArrayList();
+        Set<Slot> generatedVariableLengthSlots = Sets.newLinkedHashSet();
 
         for (Expression expr : exprs) {
-            // remove generated `is not null`
-            if (!(expr instanceof Not) || !((Not) 
expr).isGeneratedIsNotNull()) {
+            if (expr instanceof Not && ((Not) expr).isGeneratedIsNotNull()
+                    && ((Not) expr).child() instanceof IsNull
+                    && ((IsNull) ((Not) expr).child()).child() instanceof 
SlotReference) {
+                // generated IS NOT NULL
+                Slot slot = (SlotReference) ((IsNull) ((Not) 
expr).child()).child();
+                if (isVariableLengthType(slot.getDataType())) {
+                    slotsFromIsNotNull.add(slot);
+                    generatedVariableLengthSlots.add(slot);
+                }
+                // fixed-length type: skip (remove)
+            } else {
                 Optional<Slot> notNullSlot = TypeUtils.isNotNull(expr);
                 if (notNullSlot.isPresent()) {
                     slotsFromIsNotNull.add(notNullSlot.get());
@@ -104,7 +125,8 @@ public class EliminateNotNull implements RewriteRuleFactory 
{
         ImmutableSet.Builder<Expression> keepIsNotNull
                 = 
ImmutableSet.builderWithExpectedSize(slotsFromIsNotNull.size());
         for (Slot slot : slotsFromIsNotNull) {
-            if (slot.nullable() && !inferNonNotSlots.contains(slot)) {
+            if (slot.nullable()
+                    && (!inferNonNotSlots.contains(slot) || 
generatedVariableLengthSlots.contains(slot))) {
                 keepIsNotNull.add(new Not(new IsNull(slot)));
             }
         }
@@ -116,6 +138,11 @@ public class EliminateNotNull implements 
RewriteRuleFactory {
                 .build();
     }
 
+    private static boolean isVariableLengthType(DataType type) {
+        return type.isStringLikeType() || type.isJsonType()
+                || type.isVariantType() || type.isComplexType();
+    }
+
     private static boolean containsNot(Plan plan) {
         for (Expression expression : plan.getExpressions()) {
             if (expression.containsType(Not.class)) {
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateNotNullTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateNotNullTest.java
new file mode 100644
index 00000000000..2efff28119e
--- /dev/null
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/EliminateNotNullTest.java
@@ -0,0 +1,194 @@
+// 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.
+
+package org.apache.doris.nereids.rules.rewrite;
+
+import org.apache.doris.catalog.AggregateType;
+import org.apache.doris.catalog.Column;
+import org.apache.doris.catalog.HashDistributionInfo;
+import org.apache.doris.catalog.KeysType;
+import org.apache.doris.catalog.OlapTable;
+import org.apache.doris.catalog.PartitionInfo;
+import org.apache.doris.catalog.Type;
+import org.apache.doris.nereids.CascadesContext;
+import org.apache.doris.nereids.trees.expressions.Expression;
+import org.apache.doris.nereids.trees.expressions.GreaterThan;
+import org.apache.doris.nereids.trees.expressions.IsNull;
+import org.apache.doris.nereids.trees.expressions.Not;
+import org.apache.doris.nereids.trees.expressions.Slot;
+import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
+import org.apache.doris.nereids.trees.expressions.literal.StringLiteral;
+import org.apache.doris.nereids.trees.plans.RelationId;
+import org.apache.doris.nereids.trees.plans.logical.LogicalOlapScan;
+import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
+import org.apache.doris.nereids.util.LogicalPlanBuilder;
+import org.apache.doris.nereids.util.MemoPatternMatchSupported;
+import org.apache.doris.nereids.util.MemoTestUtils;
+import org.apache.doris.nereids.util.PlanChecker;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+/**
+ * Tests for EliminateNotNull rule.
+ * Verifies that generated IS NOT NULL predicates are:
+ * - Removed for fixed-length types (INT, BIGINT, etc.) when implied by other 
predicates
+ * - Kept for variable-length types (STRING, VARCHAR, JSON, etc.) even when 
implied,
+ *   because null bitmap check is cheaper than reading variable-length data
+ */
+class EliminateNotNullTest implements MemoPatternMatchSupported {
+    // Table with nullable columns: id(INT, nullable), name(STRING, nullable)
+    private static final LogicalOlapScan scan1 = newNullableScan(0, "t1");
+
+    @BeforeAll
+    static void makeSureSlotIdStable() {
+        scan1.getOutput();
+    }
+
+    private static LogicalOlapScan newNullableScan(long tableId, String 
tableName) {
+        List<Column> columns = ImmutableList.of(
+                new Column("id", Type.INT, false, AggregateType.NONE, true, 
"0", ""),
+                new Column("name", Type.STRING, false, AggregateType.NONE, 
true, "", ""));
+        HashDistributionInfo hashDistributionInfo = new HashDistributionInfo(3,
+                ImmutableList.of(columns.get(0)));
+        OlapTable table = new OlapTable(tableId, tableName, columns,
+                KeysType.DUP_KEYS, new PartitionInfo(), hashDistributionInfo);
+        table.setIndexMeta(-1, tableName, table.getFullSchema(), 0, 0, (short) 
0,
+                org.apache.doris.thrift.TStorageType.COLUMN, 
KeysType.DUP_KEYS);
+        return new LogicalOlapScan(RelationId.createGenerator().getNextId(), 
table,
+                ImmutableList.of("db"));
+    }
+
+    @Test
+    void testRemoveGeneratedIsNotNullForFixedLengthType() {
+        // id IS NOT NULL(generated) AND id > 1 → remove IS NOT NULL (INT is 
fixed-length)
+        Slot idSlot = scan1.getOutput().get(0);
+        Expression generatedIsNotNull = new Not(new IsNull(idSlot), true);
+        Expression greaterThan = new GreaterThan(idSlot, new 
IntegerLiteral(1));
+
+        LogicalPlan plan = new LogicalPlanBuilder(scan1)
+                .filter(ImmutableSet.of(greaterThan, generatedIsNotNull))
+                .build();
+
+        PlanChecker checker = 
PlanChecker.from(MemoTestUtils.createConnectContext(), plan);
+        checker.getCascadesContext().bottomUpRewrite(new EliminateNotNull());
+        checker.matches(
+                logicalFilter(logicalOlapScan())
+                        .when(f -> f.getConjuncts().size() == 1)
+                        .when(f -> f.getConjuncts().stream()
+                                .allMatch(e -> e instanceof GreaterThan))
+        );
+    }
+
+    @Test
+    void testKeepGeneratedIsNotNullForVariableLengthType() {
+        // name IS NOT NULL(generated) AND name > 'abc' → keep IS NOT NULL 
(STRING is variable-length)
+        Slot nameSlot = scan1.getOutput().get(1);
+        Expression generatedIsNotNull = new Not(new IsNull(nameSlot), true);
+        Expression greaterThan = new GreaterThan(nameSlot, new 
StringLiteral("abc"));
+
+        LogicalPlan plan = new LogicalPlanBuilder(scan1)
+                .filter(ImmutableSet.of(greaterThan, generatedIsNotNull))
+                .build();
+
+        PlanChecker checker = 
PlanChecker.from(MemoTestUtils.createConnectContext(), plan);
+        checker.getCascadesContext().bottomUpRewrite(new EliminateNotNull());
+        checker.matches(
+                logicalFilter(logicalOlapScan())
+                        .when(f -> f.getConjuncts().size() == 2)
+        );
+    }
+
+    @Test
+    void testRemoveStandaloneGeneratedIsNotNullForFixedLengthType() {
+        // id IS NOT NULL(generated) alone → removed (INT, no pre-filter value)
+        Slot idSlot = scan1.getOutput().get(0);
+        Expression generatedIsNotNull = new Not(new IsNull(idSlot), true);
+
+        LogicalPlan plan = new LogicalPlanBuilder(scan1)
+                .filter(ImmutableSet.of(generatedIsNotNull))
+                .build();
+
+        PlanChecker checker = 
PlanChecker.from(MemoTestUtils.createConnectContext(), plan);
+        checker.getCascadesContext().bottomUpRewrite(new EliminateNotNull());
+        checker.matches(logicalOlapScan());
+    }
+
+    @Test
+    void testKeepStandaloneGeneratedIsNotNullForVariableLengthType() {
+        // name IS NOT NULL(generated) alone → kept (STRING, cheap null bitmap 
pre-filter)
+        Slot nameSlot = scan1.getOutput().get(1);
+        Expression generatedIsNotNull = new Not(new IsNull(nameSlot), true);
+
+        LogicalPlan plan = new LogicalPlanBuilder(scan1)
+                .filter(ImmutableSet.of(generatedIsNotNull))
+                .build();
+
+        PlanChecker checker = 
PlanChecker.from(MemoTestUtils.createConnectContext(), plan);
+        checker.getCascadesContext().bottomUpRewrite(new EliminateNotNull());
+        checker.matches(
+                logicalFilter(logicalOlapScan())
+                        .when(f -> f.getConjuncts().size() == 1)
+        );
+    }
+
+    @Test
+    void testEndToEndInferAndEliminateForVariableLengthType() {
+        // Full pipeline: name > 'abc' → InferFilterNotNull adds IS NOT NULL → 
EliminateNotNull keeps it
+        Slot nameSlot = scan1.getOutput().get(1);
+        Expression greaterThan = new GreaterThan(nameSlot, new 
StringLiteral("abc"));
+
+        LogicalPlan plan = new LogicalPlanBuilder(scan1)
+                .filter(ImmutableSet.of(greaterThan))
+                .build();
+
+        PlanChecker checker = 
PlanChecker.from(MemoTestUtils.createConnectContext(), plan);
+        CascadesContext ctx = checker.getCascadesContext();
+        ctx.topDownRewrite(new InferFilterNotNull());
+        ctx.bottomUpRewrite(new EliminateNotNull());
+        checker.matches(
+                logicalFilter(logicalOlapScan())
+                        .when(f -> f.getConjuncts().size() == 2)
+        );
+    }
+
+    @Test
+    void testEndToEndInferAndEliminateForFixedLengthType() {
+        // Full pipeline: id > 1 → InferFilterNotNull adds IS NOT NULL → 
EliminateNotNull removes it
+        Slot idSlot = scan1.getOutput().get(0);
+        Expression greaterThan = new GreaterThan(idSlot, new 
IntegerLiteral(1));
+
+        LogicalPlan plan = new LogicalPlanBuilder(scan1)
+                .filter(ImmutableSet.of(greaterThan))
+                .build();
+
+        PlanChecker checker = 
PlanChecker.from(MemoTestUtils.createConnectContext(), plan);
+        CascadesContext ctx = checker.getCascadesContext();
+        ctx.topDownRewrite(new InferFilterNotNull());
+        ctx.bottomUpRewrite(new EliminateNotNull());
+        checker.matches(
+                logicalFilter(logicalOlapScan())
+                        .when(f -> f.getConjuncts().size() == 1)
+                        .when(f -> f.getConjuncts().stream()
+                                .allMatch(e -> e instanceof GreaterThan))
+        );
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to