Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 18 additions & 28 deletions pkg/planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,13 +728,8 @@ func (er *expressionRewriter) handleCompareSubquery(ctx context.Context, planCtx
er.err = err
return v, true
}

noDecorrelate := hintFlags&hint.HintFlagNoDecorrelate > 0
if noDecorrelate && len(coreusage.ExtractCorColumnsBySchema4LogicalPlan(np, planCtx.plan.Schema())) == 0 {
b.ctx.GetSessionVars().StmtCtx.SetHintWarning(
"NO_DECORRELATE() is inapplicable because there are no correlated columns.")
noDecorrelate = false
}
corCols := coreusage.ExtractCorColumnsBySchema4LogicalPlan(np, planCtx.plan.Schema())
noDecorrelate := isNoDecorrelate(planCtx, corCols, hintFlags)

// Only (a,b,c) = any (...) and (a,b,c) != all (...) can use row expression.
canMultiCol := (!v.All && v.Op == opcode.EQ) || (v.All && v.Op == opcode.NE)
Expand Down Expand Up @@ -1039,13 +1034,8 @@ func (er *expressionRewriter) handleExistSubquery(ctx context.Context, planCtx *
return v, true
}
np = er.popExistsSubPlan(planCtx, np)

noDecorrelate := hintFlags&hint.HintFlagNoDecorrelate > 0
if noDecorrelate && len(coreusage.ExtractCorColumnsBySchema4LogicalPlan(np, planCtx.plan.Schema())) == 0 {
b.ctx.GetSessionVars().StmtCtx.SetHintWarning(
"NO_DECORRELATE() is inapplicable because there are no correlated columns.")
noDecorrelate = false
}
corCols := coreusage.ExtractCorColumnsBySchema4LogicalPlan(np, planCtx.plan.Schema())
noDecorrelate := isNoDecorrelate(planCtx, corCols, hintFlags)
semiJoinRewrite := hintFlags&hint.HintFlagSemiJoinRewrite > 0
if semiJoinRewrite && noDecorrelate {
b.ctx.GetSessionVars().StmtCtx.SetHintWarning(
Expand Down Expand Up @@ -1212,16 +1202,11 @@ func (er *expressionRewriter) handleInSubquery(ctx context.Context, planCtx *exp
// If the leftKey and the rightKey have different collations, don't convert the sub-query to an inner-join
// since when converting we will add a distinct-agg upon the right child and this distinct-agg doesn't have the right collation.
// To keep it simple, we forbid this converting if they have different collations.
// tested by TestCollateSubQuery.
lt, rt := lexpr.GetType(er.sctx.GetEvalCtx()), rexpr.GetType(er.sctx.GetEvalCtx())
collFlag := collate.CompatibleCollate(lt.GetCollate(), rt.GetCollate())

noDecorrelate := hintFlags&hint.HintFlagNoDecorrelate > 0
corCols := coreusage.ExtractCorColumnsBySchema4LogicalPlan(np, planCtx.plan.Schema())
if len(corCols) == 0 && noDecorrelate {
planCtx.builder.ctx.GetSessionVars().StmtCtx.SetHintWarning(
"NO_DECORRELATE() is inapplicable because there are no correlated columns.")
noDecorrelate = false
}
noDecorrelate := isNoDecorrelate(planCtx, corCols, hintFlags)

// If it's not the form of `not in (SUBQUERY)`,
// and has no correlated column from the current level plan(if the correlated column is from upper level,
Expand Down Expand Up @@ -1272,6 +1257,16 @@ func (er *expressionRewriter) handleInSubquery(ctx context.Context, planCtx *exp
return v, true
}

func isNoDecorrelate(planCtx *exprRewriterPlanCtx, corCols []*expression.CorrelatedColumn, hintFlags uint64) bool {
noDecorrelate := hintFlags&hint.HintFlagNoDecorrelate > 0
if noDecorrelate && len(corCols) == 0 {
planCtx.builder.ctx.GetSessionVars().StmtCtx.SetHintWarning(
"NO_DECORRELATE() is inapplicable because there are no correlated columns.")
noDecorrelate = false
}
return noDecorrelate
}

func (er *expressionRewriter) handleScalarSubquery(ctx context.Context, planCtx *exprRewriterPlanCtx, v *ast.SubqueryExpr) (ast.Node, bool) {
intest.AssertNotNil(planCtx)
ci := planCtx.builder.prepareCTECheckForSubQuery()
Expand All @@ -1282,13 +1277,8 @@ func (er *expressionRewriter) handleScalarSubquery(ctx context.Context, planCtx
return v, true
}
np = planCtx.builder.buildMaxOneRow(np)

noDecorrelate := hintFlags&hint.HintFlagNoDecorrelate > 0
if noDecorrelate && len(coreusage.ExtractCorColumnsBySchema4LogicalPlan(np, planCtx.plan.Schema())) == 0 {
planCtx.builder.ctx.GetSessionVars().StmtCtx.SetHintWarning(
"NO_DECORRELATE() is inapplicable because there are no correlated columns.")
noDecorrelate = false
}
correlatedColumn := coreusage.ExtractCorColumnsBySchema4LogicalPlan(np, planCtx.plan.Schema())
noDecorrelate := isNoDecorrelate(planCtx, correlatedColumn, hintFlags)

if planCtx.builder.disableSubQueryPreprocessing || len(coreusage.ExtractCorrelatedCols4LogicalPlan(np)) > 0 || hasCTEConsumerInSubPlan(np) {
planCtx.plan = planCtx.builder.buildApplyWithJoinType(planCtx.plan, np, logicalop.LeftOuterJoin, noDecorrelate)
Expand Down
16 changes: 16 additions & 0 deletions pkg/planner/core/tests/subquery/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "subquery_test",
timeout = "short",
srcs = [
"main_test.go",
"subquery_test.go",
],
flaky = True,
deps = [
"//pkg/testkit",
"//pkg/testkit/testsetup",
"@org_uber_go_goleak//:goleak",
],
)
34 changes: 34 additions & 0 deletions pkg/planner/core/tests/subquery/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2025 PingCAP, Inc.
//
// 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 subquery

import (
"flag"
"testing"

"github.com/pingcap/tidb/pkg/testkit/testsetup"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testsetup.SetupForCommonTest()
flag.Parse()
opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"),
goleak.IgnoreTopFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"),
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
}
goleak.VerifyTestMain(m, opts...)
}
48 changes: 48 additions & 0 deletions pkg/planner/core/tests/subquery/subquery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2025 PingCAP, Inc.
//
// 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 subquery

import (
"testing"

"github.com/pingcap/tidb/pkg/testkit"
)

func TestCollateSubQuery(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(id int, col varchar(100), key ix(col)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
tk.MustExec("create table t1(id varchar(100)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
samePlan := testkit.Rows(
"IndexHashJoin 8000.00 root inner join, inner:IndexLookUp, outer key:Column#6, inner key:test.t.col, equal cond:eq(Column#6, test.t.col)",
"├─HashAgg(Build) 6400.00 root group by:Column#12, funcs:firstrow(Column#12)->Column#6",
"│ └─TableReader 6400.00 root data:HashAgg",
"│ └─HashAgg 6400.00 cop[tikv] group by:cast(test.t1.id, var_string(5)), ",
"│ └─Selection 8000.00 cop[tikv] not(isnull(cast(test.t1.id, var_string(5))))",
"│ └─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo",
"└─IndexLookUp(Probe) 8000.00 root ",
" ├─Selection(Build) 8000.00 cop[tikv] not(isnull(test.t.col))",
" │ └─IndexRangeScan 8008.01 cop[tikv] table:t, index:ix(col) range: decided by [eq(test.t.col, Column#6)], keep order:false, stats:pseudo",
" └─TableRowIDScan(Probe) 8000.00 cop[tikv] table:t keep order:false, stats:pseudo")
tk.MustQuery(`explain format="brief" select * from t use index(ix) where col in (select cast(id as char) from t1);`).
Check(samePlan)
tk.MustExec(`set collation_connection='utf8_bin';`)
tk.MustQuery(`explain format="brief" select * from t use index(ix) where col in (select cast(id as char) from t1);`).
Check(samePlan)
tk.MustExec(`set collation_connection='latin1_bin';`)
tk.MustQuery(`explain format="brief" select * from t use index(ix) where col in (select cast(id as char) from t1);`).
Check(samePlan)
}
2 changes: 1 addition & 1 deletion pkg/util/collate/collate.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func NewCollationEnabled() bool {
func CompatibleCollate(collate1, collate2 string) bool {
if (collate1 == "utf8mb4_general_ci" || collate1 == "utf8_general_ci") && (collate2 == "utf8mb4_general_ci" || collate2 == "utf8_general_ci") {
return true
} else if (collate1 == "utf8mb4_bin" || collate1 == "utf8_bin" || collate1 == "latin1_bin") && (collate2 == "utf8mb4_bin" || collate2 == "utf8_bin") {
} else if (collate1 == "utf8mb4_bin" || collate1 == "utf8_bin" || collate1 == "latin1_bin") && (collate2 == "utf8mb4_bin" || collate2 == "utf8_bin" || collate2 == "latin1_bin") {
return true
} else if (collate1 == "utf8mb4_unicode_ci" || collate1 == "utf8_unicode_ci") && (collate2 == "utf8mb4_unicode_ci" || collate2 == "utf8_unicode_ci") {
return true
Expand Down