Weiwei Su created FLINK-41624:
---------------------------------

             Summary: CannotPlanException in batch mode when LEFT JOIN ON 
contains OR with EXISTS subquery over 3-way join and LIMIT
                 Key: FLINK-41624
                 URL: https://issues.apache.org/jira/browse/FLINK-41624
             Project: Flink
          Issue Type: Bug
          Components: Table SQL / Planner
    Affects Versions: 2.3.0, 1.19.1
         Environment: Apache Flink 2.3.0 (official Docker image 
flink:2.3.0-scala_2.12-java17), SQL Gateway REST API, 
execution.runtime-mode=batch. Also observed on Flink 1.19.1.
            Reporter: Weiwei Su


h3. Summary
In batch execution mode, planning a {{LEFT JOIN}} whose {{ON}} condition 
combines an {{EXISTS}} subquery (containing a 3-way join and {{LIMIT}}) with an 
{{OR}} predicate fails with {{RelOptPlanner$CannotPlanException}} (reporting 
infinite cost, and throwing {{CyclicMetadataException}} while dumping the plan 
state in {{RelSubset$CheapestPlanReplacer.visit}}).

Replacing the 3-way join inside {{EXISTS}} with a 1-way or 2-way join, or 
changing {{LEFT JOIN}} to an inner {{JOIN}}, plans cleanly without error.

h3. Steps to Reproduce
Run the following self-contained SQL in Flink SQL Client or SQL Gateway (batch 
mode, built-in {{datagen}} connector, no external catalog required):

{code:sql}
SET 'execution.runtime-mode' = 'batch';

CREATE TEMPORARY TABLE t (
  id BIGINT,
  v INT,
  s STRING
) WITH (
  'connector' = 'datagen',
  'number-of-rows' = '10'
);

EXPLAIN PLAN FOR
SELECT t1.id
FROM t t1
LEFT JOIN t t2
  ON EXISTS (
    SELECT t3.id
    FROM t t3
    JOIN t t4 ON t3.id < t4.id
    JOIN t t5 ON t3.id < t5.id
    LIMIT 1
  )
  OR t1.id > t1.v;
{code}

h3. Actual Behavior
Planning fails with {{TableException}} / {{CannotPlanException}}:

{noformat}
Caused by: org.apache.flink.table.api.TableException: Cannot generate a valid 
execution plan for the given query:
Caused by: org.apache.calcite.plan.RelOptPlanner$CannotPlanException: There are 
not enough rules to produce a node with desired properties: 
convention=BATCH_PHYSICAL, FlinkRelDistributionTraitDef=any, sort=[]. All the 
inputs have relevant nodes, however the cost is still infinite.
...
Error when dumping plan state:
org.apache.calcite.rel.metadata.CyclicMetadataException
  at 
org.apache.calcite.plan.volcano.RelSubset$CheapestPlanReplacer.visit(RelSubset.java:718)
  at 
org.apache.calcite.plan.volcano.RelSubset.buildCheapestPlan(RelSubset.java:391)
{noformat}

h3. Expected Behavior
The batch planner should produce a valid physical execution plan for the query, 
consistent with the equivalent 2-way join and inner {{JOIN}} variants.




--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to