From 0eccf7957b4ae4c370d1e6cb3f9c14aa16c196a1 Mon Sep 17 00:00:00 2001 From: dash12653 <35824102+dash12653@users.noreply.github.com> Date: Thu, 10 Oct 2024 03:16:40 -0400 Subject: [PATCH] planner: merge FullSchema and FullNames of subplans when rewriting "in (subquery)" to inner join (#54334) close pingcap/tidb#53766 --- pkg/planner/core/expression_rewriter.go | 5 +++++ .../r/planner/core/issuetest/planner_issue.result | 5 +++++ .../t/planner/core/issuetest/planner_issue.test | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/pkg/planner/core/expression_rewriter.go b/pkg/planner/core/expression_rewriter.go index ec087316490be..8024d545cc614 100644 --- a/pkg/planner/core/expression_rewriter.go +++ b/pkg/planner/core/expression_rewriter.go @@ -1246,6 +1246,11 @@ func (er *expressionRewriter) handleInSubquery(ctx context.Context, planCtx *exp copy(join.OutputNames(), planCtx.plan.OutputNames()) copy(join.OutputNames()[planCtx.plan.Schema().Len():], agg.OutputNames()) join.AttachOnConds(expression.SplitCNFItems(checkCondition)) + // set FullSchema and FullNames for this join + if left, ok := planCtx.plan.(*logicalop.LogicalJoin); ok && left.FullSchema != nil { + join.FullSchema = left.FullSchema + join.FullNames = left.FullNames + } // Set join hint for this join. if planCtx.builder.TableHints() != nil { join.SetPreferredJoinTypeAndOrder(planCtx.builder.TableHints()) diff --git a/tests/integrationtest/r/planner/core/issuetest/planner_issue.result b/tests/integrationtest/r/planner/core/issuetest/planner_issue.result index a56fab9dd5d75..26f086d1d7e4b 100644 --- a/tests/integrationtest/r/planner/core/issuetest/planner_issue.result +++ b/tests/integrationtest/r/planner/core/issuetest/planner_issue.result @@ -718,3 +718,8 @@ Projection_11 6.00 root 1->Column#18 └─Projection_17(Probe) 10000.00 root cast(test.t61a85298.col_71, double BINARY)->Column#19 └─TableReader_19 10000.00 root data:TableFullScan_18 └─TableFullScan_18 10000.00 cop[tikv] table:t61a85298 keep order:false, stats:pseudo +drop table if exists t0, t1; +CREATE TABLE t0(c0 int); +CREATE TABLE t1(c0 int); +SELECT t0.c0, t1.c0 FROM t0 NATURAL JOIN t1 WHERE '1' AND (t0.c0 IN (SELECT c0 FROM t0)); +c0 c0 diff --git a/tests/integrationtest/t/planner/core/issuetest/planner_issue.test b/tests/integrationtest/t/planner/core/issuetest/planner_issue.test index cf0bffe8e90f5..00cfa31d3c392 100644 --- a/tests/integrationtest/t/planner/core/issuetest/planner_issue.test +++ b/tests/integrationtest/t/planner/core/issuetest/planner_issue.test @@ -492,3 +492,9 @@ FROM ( ) AS derived_table WHERE 16739493649928310215 MEMBER OF (derived_table.col_60767) OR NOT (JSON_CONTAINS(derived_table.col_60767, '6019730272580550835')); + +# TestIssue53766 +drop table if exists t0, t1; +CREATE TABLE t0(c0 int); +CREATE TABLE t1(c0 int); +SELECT t0.c0, t1.c0 FROM t0 NATURAL JOIN t1 WHERE '1' AND (t0.c0 IN (SELECT c0 FROM t0));