Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,7 @@ public Plan visitLogicalUnion(LogicalUnion union, Context context) {
} else {
pushDownExpr = constExpr;
}
for (int sp = entry.getKey().size() - 1; sp >= 0; sp--) {
VarcharLiteral path = new VarcharLiteral(entry.getKey().get(sp));
pushDownExpr = new ElementAt(pushDownExpr, path);
}
pushDownExpr = constructElementAt(pushDownExpr, entry.getKey());
constExprs.get(j).add(new Alias(pushDownExpr));

}
Expand Down Expand Up @@ -610,11 +607,7 @@ private List<NamedExpression> pushDownToProject(Context context, NamedExpression
Set<List<String>> subPaths = context.slotToSubPathsMap
.get((SlotReference) projection.toSlot());
for (List<String> subPath : subPaths) {
Expression pushDownExpr = child;
for (int i = subPath.size() - 1; i >= 0; i--) {
VarcharLiteral path = new VarcharLiteral(subPath.get(i));
pushDownExpr = new ElementAt(pushDownExpr, path);
}
Expression pushDownExpr = constructElementAt(child, subPath);
Alias alias = new Alias(pushDownExpr);
newProjections.add(alias);
subPathToSlot.put(subPath, (SlotReference) alias.toSlot());
Expand Down Expand Up @@ -781,6 +774,15 @@ public Void visit(Plan plan, Context context) {
}
}

/** Build nested ElementAt expressions from a canonical root-to-leaf sub-path. */
protected static Expression constructElementAt(Expression root, List<String> subPath) {
Expression result = root;
for (String path : subPath) {
result = new ElementAt(result, new VarcharLiteral(path));
}
return result;
}

protected static Pair<SlotReference, List<String>> extractSlotToSubPathPair(ElementAt elementAt) {
List<String> subPath = Lists.newArrayList();
while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,20 @@ public void testPushDownThroughUnion() {
);
}

@Test
public void testVariantSubPathConstructionOrder() {
SlotReference root = new SlotReference("v", VariantType.INSTANCE);
List<String> subPath = ImmutableList.of("a", "b", "c");

Expression expression = VariantSubPathPruning.constructElementAt(root, subPath);

Assertions.assertInstanceOf(ElementAt.class, expression);
Pair<SlotReference, List<String>> extracted = VariantSubPathPruning.extractSlotToSubPathPair(
(ElementAt) expression);
Assertions.assertEquals(root, extracted.first);
Assertions.assertEquals(subPath, extracted.second);
}

@Test
public void testDataTypeAccessTree() {
List<Pair<SlotReference, DataTypeAccessTree>> trees = getDataTypeAccessTrees(
Expand Down
11 changes: 11 additions & 0 deletions regression-test/data/variant_p0/test_variant_sub_path_order.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- This file is automatically generated. You should know what you did if you want to edit this
-- !union_constant_sub_path --
1 table 1
2 constant 1

-- !project_sub_path --
1 1

-- !union_constant_sub_path_with_nested_pruning --
1 table 1
2 constant 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// 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_sub_path_order", "p0") {
sql "DROP TABLE IF EXISTS variant_sub_path_order"
sql """
CREATE TABLE variant_sub_path_order (
id INT NOT NULL,
v VARIANT NULL
) ENGINE = OLAP
DUPLICATE KEY(id)
DISTRIBUTED BY HASH(id) BUCKETS 1
PROPERTIES ("replication_num" = "1")
"""
sql """
INSERT INTO variant_sub_path_order VALUES
(1, '{"a":{"b":1},"b":{"a":2}}')
"""

sql "SET experimental_enable_prune_nested_column = false"

order_qt_union_constant_sub_path """
WITH u AS (
SELECT id, 'table' AS branch_name, v AS c
FROM variant_sub_path_order
UNION ALL
SELECT 2 AS id, 'constant' AS branch_name,
CAST('{"a":{"b":1},"b":{"a":2}}' AS VARIANT) AS c
)
SELECT id, branch_name, CAST(c['a']['b'] AS INT) AS value
FROM u
ORDER BY id
"""

order_qt_project_sub_path """
SELECT id, CAST(c['a']['b'] AS INT) AS value
FROM (
SELECT id, IF(id > 0, v, CAST('{}' AS VARIANT)) AS c
FROM variant_sub_path_order
) projected
ORDER BY id
"""

sql "SET experimental_enable_prune_nested_column = true"

order_qt_union_constant_sub_path_with_nested_pruning """
WITH u AS (
SELECT id, 'table' AS branch_name, v AS c
FROM variant_sub_path_order
UNION ALL
SELECT 2 AS id, 'constant' AS branch_name,
CAST('{"a":{"b":1},"b":{"a":2}}' AS VARIANT) AS c
)
SELECT id, branch_name, CAST(c['a']['b'] AS INT) AS value
FROM u
ORDER BY id
"""
}
Loading