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

eldenmoon pushed a commit to branch branch-cir-20316-variant-array-subscript
in repository https://gitbox.apache.org/repos/asf/doris.git

commit 223b55355d38c33175dc27e31e45a30f3e85da38
Author: lihangyu <[email protected]>
AuthorDate: Fri May 29 13:15:05 2026 +0800

    [fix](variant) fix array subscript on pruned variant subpath
---
 .../rules/rewrite/VariantSubPathPruning.java       |  3 +-
 .../rules/rewrite/VariantPruningLogicTest.java     | 18 +++++++
 ...variant_nested_array_subscript_cir_20316.groovy | 55 ++++++++++++++++++++++
 .../test_variant_array_subscript_cir_20316.groovy  | 55 ++++++++++++++++++++++
 4 files changed, 130 insertions(+), 1 deletion(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/VariantSubPathPruning.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/VariantSubPathPruning.java
index df0eebc8d38..5b081ce2c24 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/VariantSubPathPruning.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/VariantSubPathPruning.java
@@ -386,7 +386,8 @@ public class VariantSubPathPruning implements 
CustomRewriter {
                                     
.withChildren(context.elementAtToSlotMap.get(child));
                         } else {
                             addOthers = false;
-                            newProjection = projection;
+                            newProjection = (NamedExpression) projection
+                                    
.withChildren(ExpressionUtils.replace(child, context.elementAtToSlotMap));
 
                             // try push element_at on this slot
                             if (extractSlotToSubPathPair((ElementAt) child) == 
null) {
diff --git 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/VariantPruningLogicTest.java
 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/VariantPruningLogicTest.java
index 480c98b76e2..5268569e0b8 100644
--- 
a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/VariantPruningLogicTest.java
+++ 
b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/VariantPruningLogicTest.java
@@ -72,6 +72,24 @@ public class VariantPruningLogicTest extends 
TestWithFeService {
         );
     }
 
+    @Test
+    public void testVariantArraySubscriptUsesPrunedSubPath() throws Exception {
+        String sql = "select cast(v['items']['type'] as array<string>)[1] from 
variant_tbl";
+        String explain = getSQLPlanOrErrorMsg(sql, true);
+        Assertions.assertTrue(explain.contains("final projections: 
element_at(CAST(v AS array<text>), 1)"),
+                explain);
+        Assertions.assertTrue(explain.contains("subColPath=[items, type]"),
+                explain);
+        
Assertions.assertFalse(explain.contains("element_at(CAST(element_at(element_at("),
+                explain);
+        assertVariantSubColumnSlots(
+                sql,
+                ImmutableList.of(
+                        ImmutableList.of("items", "type")
+                )
+        );
+    }
+
     @Test
     public void testVariantOrPredicatePaths() throws Exception {
         assertPredicateAccessPathsEqual(
diff --git 
a/regression-test/suites/variant_p0/nested_group/test_variant_nested_array_subscript_cir_20316.groovy
 
b/regression-test/suites/variant_p0/nested_group/test_variant_nested_array_subscript_cir_20316.groovy
new file mode 100644
index 00000000000..8df0bd9eaa8
--- /dev/null
+++ 
b/regression-test/suites/variant_p0/nested_group/test_variant_nested_array_subscript_cir_20316.groovy
@@ -0,0 +1,55 @@
+// 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_variant_nested_array_subscript_cir_20316", "p0") {
+    sql "set default_variant_enable_nested_group = true"
+    sql "set default_variant_max_subcolumns_count = 0"
+
+    sql "DROP TABLE IF EXISTS test_variant_nested_array_subscript_cir_20316"
+    sql """
+        CREATE TABLE test_variant_nested_array_subscript_cir_20316 (
+            id BIGINT,
+            v VARIANT<PROPERTIES("variant_enable_nested_group" = "true")>
+        ) ENGINE=OLAP
+        DUPLICATE KEY(id)
+        DISTRIBUTED BY HASH(id) BUCKETS 1
+        PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1",
+            "disable_auto_compaction" = "true"
+        )
+    """
+
+    sql """
+        INSERT INTO test_variant_nested_array_subscript_cir_20316 VALUES
+        (1, '{"items":[{"type":"e2e_QC"},{"type":"platform_QC"}]}')
+    """
+    sql "sync"
+
+    test {
+        sql """
+            SELECT id,
+                   CAST(v['items']['type'] AS ARRAY<STRING>)[0],
+                   CAST(v['items']['type'] AS ARRAY<STRING>)[1],
+                   CAST(v['items']['type'] AS ARRAY<STRING>)[2],
+                   element_at(CAST(v['items']['type'] AS ARRAY<STRING>), 1),
+                   element_at(CAST(v['items']['type'] AS ARRAY<STRING>), 2)
+            FROM test_variant_nested_array_subscript_cir_20316
+            ORDER BY id
+        """
+        result([[1L, null, "e2e_QC", "platform_QC", "e2e_QC", "platform_QC"]])
+    }
+}
diff --git 
a/regression-test/suites/variant_p0/test_variant_array_subscript_cir_20316.groovy
 
b/regression-test/suites/variant_p0/test_variant_array_subscript_cir_20316.groovy
new file mode 100644
index 00000000000..58b5f90697b
--- /dev/null
+++ 
b/regression-test/suites/variant_p0/test_variant_array_subscript_cir_20316.groovy
@@ -0,0 +1,55 @@
+// 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_variant_array_subscript_cir_20316", "p0") {
+    sql "set default_variant_enable_nested_group = false"
+    sql "set default_variant_max_subcolumns_count = 100"
+
+    sql "DROP TABLE IF EXISTS test_variant_array_subscript_cir_20316"
+    sql """
+        CREATE TABLE test_variant_array_subscript_cir_20316 (
+            id BIGINT,
+            v VARIANT
+        ) ENGINE=OLAP
+        DUPLICATE KEY(id)
+        DISTRIBUTED BY HASH(id) BUCKETS 1
+        PROPERTIES (
+            "replication_allocation" = "tag.location.default: 1",
+            "disable_auto_compaction" = "true"
+        )
+    """
+
+    sql """
+        INSERT INTO test_variant_array_subscript_cir_20316 VALUES
+        (1, '{"items":{"type":["e2e_QC","platform_QC"]}}')
+    """
+    sql "sync"
+
+    test {
+        sql """
+            SELECT id,
+                   CAST(v['items']['type'] AS ARRAY<STRING>)[0],
+                   CAST(v['items']['type'] AS ARRAY<STRING>)[1],
+                   CAST(v['items']['type'] AS ARRAY<STRING>)[2],
+                   element_at(CAST(v['items']['type'] AS ARRAY<STRING>), 1),
+                   element_at(CAST(v['items']['type'] AS ARRAY<STRING>), 2)
+            FROM test_variant_array_subscript_cir_20316
+            ORDER BY id
+        """
+        result([[1L, null, "e2e_QC", "platform_QC", "e2e_QC", "platform_QC"]])
+    }
+}


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

Reply via email to