diff --git a/core/trino-main/src/main/java/io/trino/sql/planner/PlanOptimizers.java b/core/trino-main/src/main/java/io/trino/sql/planner/PlanOptimizers.java index ea99b2a14b60..7b029fde0848 100644 --- a/core/trino-main/src/main/java/io/trino/sql/planner/PlanOptimizers.java +++ b/core/trino-main/src/main/java/io/trino/sql/planner/PlanOptimizers.java @@ -897,7 +897,6 @@ public PlanOptimizers( // pushdown into the connectors. Invoke PredicatePushdown and PushPredicateIntoTableScan after this // to leverage predicate pushdown on projected columns and to pushdown dynamic filters. builder.add(new StatsRecordingPlanOptimizer(optimizerStats, new PredicatePushDown(plannerContext, typeAnalyzer, true, true))); - builder.add(new RemoveUnsupportedDynamicFilters(plannerContext)); // Remove unsupported dynamic filters introduced by PredicatePushdown builder.add(new IterativeOptimizer( plannerContext, ruleStats, @@ -908,6 +907,9 @@ public PlanOptimizers( .add(new PushPredicateIntoTableScan(plannerContext, typeAnalyzer, false)) .add(new RemoveRedundantPredicateAboveTableScan(plannerContext, typeAnalyzer)) .build())); + // Remove unsupported dynamic filters introduced by PredicatePushdown. Also, cleanup dynamic filters removed by + // PushPredicateIntoTableScan and RemoveRedundantPredicateAboveTableScan due to those rules replacing table scans with empty ValuesNode + builder.add(new RemoveUnsupportedDynamicFilters(plannerContext)); builder.add(inlineProjections); builder.add(new UnaliasSymbolReferences(metadata)); // Run unalias after merging projections to simplify projections more efficiently builder.add(columnPruningOptimizer); diff --git a/testing/trino-faulttolerant-tests/src/test/java/io/trino/faulttolerant/TestDistributedFaultTolerantEngineOnlyQueries.java b/testing/trino-faulttolerant-tests/src/test/java/io/trino/faulttolerant/TestDistributedFaultTolerantEngineOnlyQueries.java index 40151ba1bd02..f970e177a4d7 100644 --- a/testing/trino-faulttolerant-tests/src/test/java/io/trino/faulttolerant/TestDistributedFaultTolerantEngineOnlyQueries.java +++ b/testing/trino-faulttolerant-tests/src/test/java/io/trino/faulttolerant/TestDistributedFaultTolerantEngineOnlyQueries.java @@ -26,6 +26,7 @@ import org.testng.annotations.Test; import static io.airlift.testing.Closeables.closeAllSuppress; +import static io.trino.testing.TestingNames.randomNameSuffix; public class TestDistributedFaultTolerantEngineOnlyQueries extends AbstractDistributedEngineOnlyQueries @@ -73,4 +74,27 @@ public void testSelectiveLimit() { // FTE mode does not terminate query when limit is reached } + + @Test + public void testIssue18383() + { + String tableName = "test_issue_18383_" + randomNameSuffix(); + assertUpdate("CREATE TABLE " + tableName + " (id VARCHAR)"); + + assertQueryReturnsEmptyResult( + """ + WITH + t1 AS ( + SELECT NULL AS address_id FROM %s i1 + INNER JOIN %s i2 ON i1.id = i2.id), + t2 AS ( + SELECT id AS address_id FROM %s + UNION + SELECT * FROM t1) + SELECT * FROM t2 + INNER JOIN %s i ON i.id = t2.address_id + """.formatted(tableName, tableName, tableName, tableName)); + + assertUpdate("DROP TABLE " + tableName); + } }