Skip to content

Commit 473f703

Browse files
committed
[fix](variant) fix array subscript on pruned variant subpath
1 parent 6e27f11 commit 473f703

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/VariantSubPathPruning.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,8 @@ public Plan visitLogicalProject(LogicalProject<? extends Plan> project, Context
386386
.withChildren(context.elementAtToSlotMap.get(child));
387387
} else {
388388
addOthers = false;
389-
newProjection = projection;
389+
newProjection = (NamedExpression) projection
390+
.withChildren(ExpressionUtils.replace(child, context.elementAtToSlotMap));
390391

391392
// try push element_at on this slot
392393
if (extractSlotToSubPathPair((ElementAt) child) == null) {

fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/VariantPruningLogicTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ public void testVariantNumericIndexSubPath() throws Exception {
7272
);
7373
}
7474

75+
@Test
76+
public void testVariantArraySubscriptUsesPrunedSubPath() throws Exception {
77+
String sql = "select cast(v['items']['type'] as array<string>)[1] from variant_tbl";
78+
String explain = getSQLPlanOrErrorMsg(sql, true);
79+
Assertions.assertTrue(explain.contains("final projections: element_at(CAST(v AS array<text>), 1)"),
80+
explain);
81+
Assertions.assertTrue(explain.contains("subColPath=[items, type]"),
82+
explain);
83+
Assertions.assertFalse(explain.contains("element_at(CAST(element_at(element_at("),
84+
explain);
85+
assertVariantSubColumnSlots(
86+
sql,
87+
ImmutableList.of(
88+
ImmutableList.of("items", "type")
89+
)
90+
);
91+
}
92+
7593
@Test
7694
public void testVariantOrPredicatePaths() throws Exception {
7795
assertPredicateAccessPathsEqual(
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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_array_subscript_cir_20316", "p0") {
19+
sql "set default_variant_enable_nested_group = false"
20+
sql "set default_variant_max_subcolumns_count = 100"
21+
22+
sql "DROP TABLE IF EXISTS test_variant_array_subscript_cir_20316"
23+
sql """
24+
CREATE TABLE test_variant_array_subscript_cir_20316 (
25+
id BIGINT,
26+
v VARIANT
27+
) ENGINE=OLAP
28+
DUPLICATE KEY(id)
29+
DISTRIBUTED BY HASH(id) BUCKETS 1
30+
PROPERTIES (
31+
"replication_allocation" = "tag.location.default: 1",
32+
"disable_auto_compaction" = "true"
33+
)
34+
"""
35+
36+
sql """
37+
INSERT INTO test_variant_array_subscript_cir_20316 VALUES
38+
(1, '{"items":{"type":["e2e_QC","platform_QC"]}}')
39+
"""
40+
sql "sync"
41+
42+
test {
43+
sql """
44+
SELECT id,
45+
CAST(v['items']['type'] AS ARRAY<STRING>)[0],
46+
CAST(v['items']['type'] AS ARRAY<STRING>)[1],
47+
CAST(v['items']['type'] AS ARRAY<STRING>)[2],
48+
element_at(CAST(v['items']['type'] AS ARRAY<STRING>), 1),
49+
element_at(CAST(v['items']['type'] AS ARRAY<STRING>), 2)
50+
FROM test_variant_array_subscript_cir_20316
51+
ORDER BY id
52+
"""
53+
result([[1L, null, "e2e_QC", "platform_QC", "e2e_QC", "platform_QC"]])
54+
}
55+
}

0 commit comments

Comments
 (0)