Skip to content

Commit

Permalink
Fix incorrect pushdown involving aggreagations and unnest
Browse files Browse the repository at this point in the history
Complex conjuncts that might fail were being reordered before simpler conjuncts.
  • Loading branch information
martint committed Jul 19, 2024
1 parent 2a06c88 commit d7b0077
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1492,8 +1492,15 @@ public PlanNode visitAggregation(AggregationNode node, RewriteContext<Expression
.forEach(postAggregationConjuncts::add);
inheritedPredicate = filterDeterministicConjuncts(inheritedPredicate);

// Sort non-equality predicates by those that can be pushed down and those that cannot
Set<Symbol> groupingKeys = ImmutableSet.copyOf(node.getGroupingKeys());

// Add the equality predicates back in
EqualityInference.EqualityPartition equalityPartition = equalityInference.generateEqualitiesPartitionedBy(groupingKeys);
pushdownConjuncts.addAll(equalityPartition.getScopeEqualities());
postAggregationConjuncts.addAll(equalityPartition.getScopeComplementEqualities());
postAggregationConjuncts.addAll(equalityPartition.getScopeStraddlingEqualities());

// Sort non-equality predicates by those that can be pushed down and those that cannot
EqualityInference.nonInferrableConjuncts(inheritedPredicate).forEach(conjunct -> {
if (node.getGroupIdSymbol().isPresent() && extractUnique(conjunct).contains(node.getGroupIdSymbol().get())) {
// aggregation operator synthesizes outputs for group ids corresponding to the global grouping set (i.e., ()), so we
Expand All @@ -1513,12 +1520,6 @@ public PlanNode visitAggregation(AggregationNode node, RewriteContext<Expression
}
});

// Add the equality predicates back in
EqualityInference.EqualityPartition equalityPartition = equalityInference.generateEqualitiesPartitionedBy(groupingKeys);
pushdownConjuncts.addAll(equalityPartition.getScopeEqualities());
postAggregationConjuncts.addAll(equalityPartition.getScopeComplementEqualities());
postAggregationConjuncts.addAll(equalityPartition.getScopeStraddlingEqualities());

PlanNode rewrittenSource = context.rewrite(node.getSource(), combineConjuncts(pushdownConjuncts));

PlanNode output = node;
Expand Down Expand Up @@ -1554,8 +1555,15 @@ public PlanNode visitUnnest(UnnestNode node, RewriteContext<Expression> context)
.forEach(postUnnestConjuncts::add);
inheritedPredicate = filterDeterministicConjuncts(inheritedPredicate);

// Sort non-equality predicates by those that can be pushed down and those that cannot
Set<Symbol> replicatedSymbols = ImmutableSet.copyOf(node.getReplicateSymbols());

// Add the equality predicates back in
EqualityInference.EqualityPartition equalityPartition = equalityInference.generateEqualitiesPartitionedBy(replicatedSymbols);
pushdownConjuncts.addAll(equalityPartition.getScopeEqualities());
postUnnestConjuncts.addAll(equalityPartition.getScopeComplementEqualities());
postUnnestConjuncts.addAll(equalityPartition.getScopeStraddlingEqualities());

// Sort non-equality predicates by those that can be pushed down and those that cannot
EqualityInference.nonInferrableConjuncts(inheritedPredicate).forEach(conjunct -> {
Expression rewrittenConjunct = equalityInference.rewrite(conjunct, replicatedSymbols);
if (rewrittenConjunct != null) {
Expand All @@ -1566,12 +1574,6 @@ public PlanNode visitUnnest(UnnestNode node, RewriteContext<Expression> context)
}
});

// Add the equality predicates back in
EqualityInference.EqualityPartition equalityPartition = equalityInference.generateEqualitiesPartitionedBy(replicatedSymbols);
pushdownConjuncts.addAll(equalityPartition.getScopeEqualities());
postUnnestConjuncts.addAll(equalityPartition.getScopeComplementEqualities());
postUnnestConjuncts.addAll(equalityPartition.getScopeStraddlingEqualities());

PlanNode rewrittenSource = context.rewrite(node.getSource(), combineConjuncts(pushdownConjuncts));

PlanNode output = node;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed 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 io.trino.sql.query;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class TestIssue22731
{
@Test
public void test()
{
try (QueryAssertions assertions = new QueryAssertions()) {
assertThat(assertions.query(
"""
WITH t(a) as (
VALUES ARRAY[ARRAY['a']]
),
u as (
SELECT
e[cardinality(e)] AS v1,
cardinality(e) AS v2
FROM t CROSS JOIN UNNEST(t.a) AS z(e)
GROUP BY e
)
SELECT *
FROM u
WHERE v2 = 2 AND v1 = ''
"""))
.returnsEmptyResult();
}
}
}

0 comments on commit d7b0077

Please sign in to comment.