Skip to content

Commit f9d7c68

Browse files
authored
expression: add cast for eq expr when doing constant propagation (#63327)
close #57247
1 parent 013cacf commit f9d7c68

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

pkg/expression/constant_propagation.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,18 @@ func (s *propConstSolver) pickNewEQConds(visited []bool) (retMapper map[int]*Con
425425
return nil
426426
}
427427
if updated {
428-
retMapper[s.getColID(col)] = con
428+
colType := col.GetType(s.ctx.GetEvalCtx())
429+
conType := con.GetType(s.ctx.GetEvalCtx())
430+
castedCon := con
431+
if !colType.Equal(conType) {
432+
oriWarningCnt := s.ctx.GetEvalCtx().WarningCount()
433+
newExpr := BuildCastFunction(s.ctx, con, col.GetType(s.ctx.GetEvalCtx()))
434+
s.ctx.GetEvalCtx().TruncateWarnings(oriWarningCnt)
435+
if newCon, ok := newExpr.(*Constant); ok {
436+
castedCon = newCon
437+
}
438+
}
439+
retMapper[s.getColID(col)] = castedCon
429440
}
430441
}
431442
return
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_test")
2+
3+
go_test(
4+
name = "constantpropagation_test",
5+
timeout = "short",
6+
srcs = [
7+
"constant_propagation_test.go",
8+
"main_test.go",
9+
],
10+
flaky = True,
11+
deps = [
12+
"//pkg/config",
13+
"//pkg/testkit",
14+
"//pkg/testkit/testmain",
15+
"//pkg/testkit/testsetup",
16+
"//pkg/util/timeutil",
17+
"@com_github_tikv_client_go_v2//tikv",
18+
"@org_uber_go_goleak//:goleak",
19+
],
20+
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2022 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package constantpropagation
16+
17+
import (
18+
"testing"
19+
20+
"github.com/pingcap/tidb/pkg/testkit"
21+
)
22+
23+
func TestConstantPropagationMissingCastExpr(t *testing.T) {
24+
store := testkit.CreateMockStore(t)
25+
tk := testkit.NewTestKit(t, store)
26+
tk.MustExec("use test")
27+
tk.MustExec("create table tl50cb7440 (" +
28+
" col_43 decimal(30,30) not null," +
29+
" primary key (col_43) /*t![clustered_index] clustered */," +
30+
" unique key idx_12 (col_43)," +
31+
" key idx_13 (col_43)," +
32+
" unique key idx_14 (col_43)" +
33+
") engine=innodb default charset=utf8 collate=utf8_bin;")
34+
tk.MustExec("insert into tl50cb7440 values(0.000000000000000000000000000000),(0.400000000000000000000000000000);")
35+
tk.MustQuery("with cte_8911 (col_47665) as" +
36+
" (select mid(tl50cb7440.col_43, 6, 9) as r0" +
37+
" from tl50cb7440" +
38+
" where tl50cb7440.col_43 in (0, 0) and tl50cb7440.col_43 in (0))" +
39+
" (select 1" +
40+
" from cte_8911 where cte_8911.col_47665!='');").Check(testkit.Rows("1"))
41+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2023 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package constantpropagation
16+
17+
import (
18+
"testing"
19+
20+
"github.com/pingcap/tidb/pkg/config"
21+
"github.com/pingcap/tidb/pkg/testkit/testmain"
22+
"github.com/pingcap/tidb/pkg/testkit/testsetup"
23+
"github.com/pingcap/tidb/pkg/util/timeutil"
24+
"github.com/tikv/client-go/v2/tikv"
25+
"go.uber.org/goleak"
26+
)
27+
28+
func TestMain(m *testing.M) {
29+
testsetup.SetupForCommonTest()
30+
testmain.ShortCircuitForBench(m)
31+
32+
config.UpdateGlobal(func(conf *config.Config) {
33+
conf.TiKVClient.AsyncCommit.SafeWindow = 0
34+
conf.TiKVClient.AsyncCommit.AllowedClockDrift = 0
35+
conf.Experimental.AllowsExpressionIndex = true
36+
})
37+
tikv.EnableFailpoints()
38+
39+
// Some test depends on the values of timeutil.SystemLocation()
40+
// If we don't SetSystemTZ() here, the value would change unpredictable.
41+
// Affected by the order whether a testsuite runs before or after integration test.
42+
// Note, SetSystemTZ() is a sync.Once operation.
43+
timeutil.SetSystemTZ("system")
44+
45+
opts := []goleak.Option{
46+
goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"),
47+
goleak.IgnoreTopFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"),
48+
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
49+
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
50+
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
51+
goleak.IgnoreTopFunction("github.com/pingcap/tidb/pkg/ttl/ttlworker.(*ttlScanWorker).loop"),
52+
goleak.IgnoreTopFunction("github.com/pingcap/tidb/pkg/ttl/client.(*mockClient).WatchCommand.func1"),
53+
goleak.IgnoreTopFunction("github.com/pingcap/tidb/pkg/ttl/ttlworker.(*JobManager).jobLoop"),
54+
}
55+
56+
goleak.VerifyTestMain(m, opts...)
57+
}

0 commit comments

Comments
 (0)