Skip to content

Dynamic filtering causes query planning failure when using min/max/count functions in a scalar subquery using Iceberg connector #28364

Description

@acarpente-denodo

Queries utilizing subqueries with aggregations (e.g., min() / max() / count()) against Iceberg tables fail during logical plan optimization.

The issue appears to be a regression introduced by the changes in #27085, since reverting those changes makes the problem disappear.

Disabling dynamic filtering provides a workaround, but this may have performance implications for workloads that rely on dynamic filtering.

Your Environment

Steps to Reproduce

  1. Enable dynamic filtering in etc/config.properties:
enable-dynamic-filtering=true

Alternatively, start the server and enable it for the session:

SET SESSION enable_dynamic_filtering = true;
  1. Start the Presto server.
  2. Create an Iceberg table:
CREATE TABLE iceberg.iceberg.my_table (id int);
  1. Insert two rows:
INSERT INTO iceberg.iceberg.my_table VALUES(1), (2);
  1. Execute the following query:
SELECT id
FROM iceberg.iceberg.my_table
WHERE id <= (SELECT count(*) FROM iceberg.iceberg.my_table);
  1. The query fails during query planning with:
java.lang.IllegalArgumentException: Right join input doesn't contain symbol for dynamic filter: count, rightVariables: [], dynamicFilters.values(): [count]
  1. Disable dynamic filtering:
SET SESSION enable_dynamic_filtering = false;
  1. Execute the same query again. The query succeeds.
  2. As an additional confirmation, reverting the commit containing the changes from feat(plugin-iceberg): Push down min/max/count based on file stats #27085 makes the issue disappear.

Expected Behavior

The query should execute successfully and return the expected result.

Current Behavior

When dynamic filtering is enabled, the query fails during logical plan optimization with the following exception:

java.lang.IllegalArgumentException: Right join input doesn't contain symbol for dynamic filter: count, rightVariables: [], dynamicFilters.values(): [count]
	at com.facebook.presto.common.Utils.checkArgument(Utils.java:60)
	at com.facebook.presto.spi.plan.JoinNode.<init>(JoinNode.java:149)
	at com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs$Rewriter.visitJoin(PruneUnreferencedOutputs.java:264)
	at com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs$Rewriter.visitJoin(PruneUnreferencedOutputs.java:129)
	at com.facebook.presto.spi.plan.JoinNode.accept(JoinNode.java:322)
	at com.facebook.presto.sql.planner.plan.SimplePlanRewriter$RewriteContext.rewrite(SimplePlanRewriter.java:97)
	at com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs$Rewriter.visitProject(PruneUnreferencedOutputs.java:737)
	at com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs$Rewriter.visitProject(PruneUnreferencedOutputs.java:129)
	at com.facebook.presto.spi.plan.ProjectNode.accept(ProjectNode.java:107)
	at com.facebook.presto.sql.planner.plan.SimplePlanRewriter$RewriteContext.rewrite(SimplePlanRewriter.java:97)
	at com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs$Rewriter.visitOutput(PruneUnreferencedOutputs.java:746)
	at com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs$Rewriter.visitOutput(PruneUnreferencedOutputs.java:129)
	at com.facebook.presto.spi.plan.OutputNode.accept(OutputNode.java:97)
	at com.facebook.presto.sql.planner.plan.SimplePlanRewriter.rewriteWith(SimplePlanRewriter.java:35)
	at com.facebook.presto.sql.planner.optimizations.PruneUnreferencedOutputs.optimize(PruneUnreferencedOutputs.java:125)
	at com.facebook.presto.sql.Optimizer.validateAndOptimizePlan(Optimizer.java:114)
	at com.facebook.presto.execution.SqlQueryExecution.lambda$doCreateLogicalPlanAndOptimize$1(SqlQueryExecution.java:603)
	at com.facebook.presto.common.RuntimeStats.recordWallAndCpuTime(RuntimeStats.java:158)
	at com.facebook.presto.execution.SqlQueryExecution.doCreateLogicalPlanAndOptimize(SqlQueryExecution.java:601)
	at com.facebook.presto.common.RuntimeStats.recordWallAndCpuTime(RuntimeStats.java:158)
	at com.facebook.presto.execution.SqlQueryExecution.createLogicalPlanAndOptimize(SqlQueryExecution.java:572)
	at com.facebook.presto.execution.SqlQueryExecution.start(SqlQueryExecution.java:500)
	at com.facebook.presto.$gen.Presto_null__testversion____20260817_162121_1.run(Unknown Source)
	at com.facebook.presto.execution.SqlQueryManager.createQuery(SqlQueryManager.java:326)
	at com.facebook.presto.dispatcher.LocalDispatchQuery.lambda$startExecution$0(LocalDispatchQuery.java:217)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:840)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status
    🆕 Unprioritized

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions