This is an automated email from the ASF dual-hosted git repository.
englefly 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 75854eeed05 [improvement](fe) Extend StringEmptyToLengthRule to handle
non-SlotReference string expressions (#62315)
75854eeed05 is described below
commit 75854eeed0553a73819479c1b11287237afa2c47
Author: minghong <[email protected]>
AuthorDate: Mon Apr 20 16:21:02 2026 +0800
[improvement](fe) Extend StringEmptyToLengthRule to handle
non-SlotReference string expressions (#62315)
…
### What problem does this PR solve?
Issue Number: close #xxx
Problem Summary: StringEmptyToLengthRule only rewrites `str_col = ''` to
`length(str_col) = 0` when the non-literal side is a direct
SlotReference. This means expressions like `element_at(struct_col, 'f3')
= ''` (which becomes `struct_element(struct_col, 'f3') = ''` after
analysis) are not rewritten, preventing the OFFSET-only column reading
optimization from applying to struct string fields.
### Release note
StringEmptyToLengthRule now rewrites any string-typed expression
compared against an empty string literal, not just direct column
references. This enables the OFFSET optimization for struct field access
patterns like `element_at(struct_col, 'f3') = ''`.
---
.../expression/rules/StringEmptyToLengthRule.java | 29 ++++++------
.../rules/StringEmptyToLengthRuleTest.java | 53 ++++++++++++++++++++++
.../string_length_column_pruning.groovy | 21 +++++++++
3 files changed, 89 insertions(+), 14 deletions(-)
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRule.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRule.java
index 85776dc4c15..5321eed9300 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRule.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRule.java
@@ -23,7 +23,6 @@ import
org.apache.doris.nereids.rules.expression.ExpressionRuleType;
import org.apache.doris.nereids.trees.expressions.EqualTo;
import org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Not;
-import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Length;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.literal.Literal;
@@ -41,13 +40,15 @@ import java.util.List;
* <li>{@code str_col = ''} → {@code length(str_col) = 0}</li>
* <li>{@code str_col <> ''} → {@code length(str_col) != 0}
* (represented as {@code NOT(length(str_col) = 0)})</li>
+ * <li>{@code element_at(struct_col, 'f3') = ''} → {@code
length(element_at(struct_col, 'f3')) = 0}</li>
* </ul>
*
* This is a semantics-preserving rewrite: for any non-NULL string, {@code s =
''} is equivalent
* to {@code length(s) = 0}; for NULL, both sides evaluate to NULL.
*
- * Only applies when the compared expression is a direct {@link SlotReference}
of string-like type,
- * so that the resulting {@code length(slot)} call can benefit from
OFFSET-only column reading.
+ * Applies when the compared expression is a non-literal expression of
string-like type (e.g. a
+ * {@code SlotReference}, a {@code StructElement} field access, etc.), so that
the resulting
+ * {@code length(expr)} call can benefit from OFFSET-only column reading.
*/
public class StringEmptyToLengthRule implements ExpressionPatternRuleFactory {
public static final StringEmptyToLengthRule INSTANCE = new
StringEmptyToLengthRule();
@@ -78,8 +79,8 @@ public class StringEmptyToLengthRule implements
ExpressionPatternRuleFactory {
}
/**
- * If {@code equalTo} compares a string-typed SlotReference against an
empty-string literal,
- * rewrites it to {@code length(slot) = 0}. Returns the original
expression unchanged otherwise.
+ * If {@code equalTo} compares a string-typed expression against an
empty-string literal,
+ * rewrites it to {@code length(expr) = 0}. Returns the original
expression unchanged otherwise.
*/
private static Expression rewriteEqualToEmpty(EqualTo equalTo) {
if (ConnectContext.get().getStatementContext().isDelete()) {
@@ -88,21 +89,21 @@ public class StringEmptyToLengthRule implements
ExpressionPatternRuleFactory {
Expression left = equalTo.left();
Expression right = equalTo.right();
- SlotReference slot = null;
- if (isStringSlot(left) && isEmptyStringLiteral(right)) {
- slot = (SlotReference) left;
- } else if (isStringSlot(right) && isEmptyStringLiteral(left)) {
- slot = (SlotReference) right;
+ Expression stringExpr = null;
+ if (isStringExpression(left) && isEmptyStringLiteral(right)) {
+ stringExpr = left;
+ } else if (isStringExpression(right) && isEmptyStringLiteral(left)) {
+ stringExpr = right;
}
- if (slot == null) {
+ if (stringExpr == null) {
return equalTo;
}
- return new EqualTo(new Length(slot), new IntegerLiteral(0));
+ return new EqualTo(new Length(stringExpr), new IntegerLiteral(0));
}
- private static boolean isStringSlot(Expression expr) {
- return expr instanceof SlotReference &&
expr.getDataType().isStringLikeType();
+ private static boolean isStringExpression(Expression expr) {
+ return !(expr instanceof Literal) &&
expr.getDataType().isStringLikeType();
}
private static boolean isEmptyStringLiteral(Expression expr) {
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRuleTest.java
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRuleTest.java
index c73cc5ae1b8..f9c2a1a2072 100644
---
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRuleTest.java
+++
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/rules/StringEmptyToLengthRuleTest.java
@@ -24,10 +24,13 @@ import
org.apache.doris.nereids.trees.expressions.Expression;
import org.apache.doris.nereids.trees.expressions.Not;
import org.apache.doris.nereids.trees.expressions.SlotReference;
import org.apache.doris.nereids.trees.expressions.functions.scalar.Length;
+import
org.apache.doris.nereids.trees.expressions.functions.scalar.StructElement;
import org.apache.doris.nereids.trees.expressions.literal.IntegerLiteral;
import org.apache.doris.nereids.trees.expressions.literal.VarcharLiteral;
import org.apache.doris.nereids.types.IntegerType;
import org.apache.doris.nereids.types.StringType;
+import org.apache.doris.nereids.types.StructField;
+import org.apache.doris.nereids.types.StructType;
import org.apache.doris.nereids.types.VarcharType;
import com.google.common.collect.ImmutableList;
@@ -138,4 +141,54 @@ public class StringEmptyToLengthRuleTest extends
ExpressionRewriteTestHelper {
SlotReference slot = new SlotReference("str_col", StringType.INSTANCE,
true);
assertRuleNoRewrite(new Not(new EqualTo(slot, new
VarcharLiteral("abc"))));
}
+
+ // ─── Struct element (non-SlotReference expression) cases
─────────────────────
+
+ @Test
+ public void testStructElementEqualEmptyRewrite() {
+ // struct_element(struct_col, 'f3') = '' →
length(struct_element(struct_col, 'f3')) = 0
+ SlotReference structSlot = new SlotReference("struct_col",
+ new StructType(ImmutableList.of(
+ new StructField("f1", IntegerType.INSTANCE, true, ""),
+ new StructField("f3", StringType.INSTANCE, true, "")
+ )), true);
+ StructElement structElement = new StructElement(structSlot, new
VarcharLiteral("f3"));
+ VarcharLiteral empty = new VarcharLiteral("");
+ assertRuleRewrite(
+ new EqualTo(structElement, empty),
+ new EqualTo(new Length(structElement), new IntegerLiteral(0))
+ );
+ }
+
+ @Test
+ public void testStructElementReversedOperandsRewrite() {
+ // '' = struct_element(struct_col, 'f3') →
length(struct_element(struct_col, 'f3')) = 0
+ SlotReference structSlot = new SlotReference("struct_col",
+ new StructType(ImmutableList.of(
+ new StructField("f1", IntegerType.INSTANCE, true, ""),
+ new StructField("f3", StringType.INSTANCE, true, "")
+ )), true);
+ StructElement structElement = new StructElement(structSlot, new
VarcharLiteral("f3"));
+ VarcharLiteral empty = new VarcharLiteral("");
+ assertRuleRewrite(
+ new EqualTo(empty, structElement),
+ new EqualTo(new Length(structElement), new IntegerLiteral(0))
+ );
+ }
+
+ @Test
+ public void testNotStructElementEqualEmptyRewrite() {
+ // NOT(struct_element(struct_col, 'f3') = '') →
NOT(length(struct_element(struct_col, 'f3')) = 0)
+ SlotReference structSlot = new SlotReference("struct_col",
+ new StructType(ImmutableList.of(
+ new StructField("f1", IntegerType.INSTANCE, true, ""),
+ new StructField("f3", StringType.INSTANCE, true, "")
+ )), true);
+ StructElement structElement = new StructElement(structSlot, new
VarcharLiteral("f3"));
+ VarcharLiteral empty = new VarcharLiteral("");
+ assertRuleRewrite(
+ new Not(new EqualTo(structElement, empty)),
+ new Not(new EqualTo(new Length(structElement), new
IntegerLiteral(0)))
+ );
+ }
}
diff --git
a/regression-test/suites/nereids_rules_p0/column_pruning/string_length_column_pruning.groovy
b/regression-test/suites/nereids_rules_p0/column_pruning/string_length_column_pruning.groovy
index 1159055c431..ab57bbc6962 100644
---
a/regression-test/suites/nereids_rules_p0/column_pruning/string_length_column_pruning.groovy
+++
b/regression-test/suites/nereids_rules_p0/column_pruning/string_length_column_pruning.groovy
@@ -299,6 +299,27 @@ suite("string_length_column_pruning") {
notContains "type=bigint"
}
+ // ─── Struct field empty-string rewrite cases
────────────────────────────────
+
+ // element_at(struct_col, 'f3') = '' rewrites to
length(element_at(struct_col, 'f3')) = 0
+ // → OFFSET optimization applies to the struct string field
+ explain {
+ sql "select 1 from slcp_str_tbl where element_at(struct_col, 'f3') =
''"
+ contains "length"
+ contains "nested columns"
+ contains "OFFSET"
+ notContains "type=bigint"
+ }
+
+ // element_at(struct_col, 'f3') <> '' rewrites to
length(element_at(struct_col, 'f3')) != 0
+ explain {
+ sql "select 1 from slcp_str_tbl where element_at(struct_col, 'f3') <>
''"
+ contains "length"
+ contains "nested columns"
+ contains "OFFSET"
+ notContains "type=bigint"
+ }
+
// Struct field also projected directly → field access is full, not
OFFSET-only
// The struct's nested-columns entry still appears (partial struct
pruning),
// but the pruned field type must remain text (not bigint).
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]