|
| 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 issuetest |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/pingcap/tidb/pkg/parser" |
| 22 | + "github.com/pingcap/tidb/pkg/planner" |
| 23 | + "github.com/pingcap/tidb/pkg/planner/core" |
| 24 | + "github.com/pingcap/tidb/pkg/planner/core/base" |
| 25 | + "github.com/pingcap/tidb/pkg/planner/core/resolve" |
| 26 | + "github.com/pingcap/tidb/pkg/testkit" |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | +) |
| 29 | + |
| 30 | +// It's a case for Columns in tableScan and indexScan with double reader |
| 31 | +func TestIssue43461(t *testing.T) { |
| 32 | + store, domain := testkit.CreateMockStoreAndDomain(t) |
| 33 | + tk := testkit.NewTestKit(t, store) |
| 34 | + tk.MustExec("use test") |
| 35 | + tk.MustExec("create table t(a int, b int, c int, index b(b), index b_c(b, c)) partition by hash(a) partitions 4;") |
| 36 | + tk.MustExec("analyze table t") |
| 37 | + |
| 38 | + stmt, err := parser.New().ParseOneStmt("select * from t use index(b) where b > 1 order by b limit 1", "", "") |
| 39 | + require.NoError(t, err) |
| 40 | + |
| 41 | + nodeW := resolve.NewNodeW(stmt) |
| 42 | + p, _, err := planner.Optimize(context.TODO(), tk.Session(), nodeW, domain.InfoSchema()) |
| 43 | + require.NoError(t, err) |
| 44 | + require.NotNil(t, p) |
| 45 | + |
| 46 | + var idxLookUpPlan *core.PhysicalIndexLookUpReader |
| 47 | + var ok bool |
| 48 | + |
| 49 | + for { |
| 50 | + idxLookUpPlan, ok = p.(*core.PhysicalIndexLookUpReader) |
| 51 | + if ok { |
| 52 | + break |
| 53 | + } |
| 54 | + p = p.(base.PhysicalPlan).Children()[0] |
| 55 | + } |
| 56 | + require.True(t, ok) |
| 57 | + |
| 58 | + is := idxLookUpPlan.IndexPlans[0].(*core.PhysicalIndexScan) |
| 59 | + ts := idxLookUpPlan.TablePlans[0].(*core.PhysicalTableScan) |
| 60 | + |
| 61 | + require.NotEqual(t, is.Columns, ts.Columns) |
| 62 | +} |
| 63 | + |
| 64 | +func Test53726(t *testing.T) { |
| 65 | + // test for RemoveUnnecessaryFirstRow |
| 66 | + store := testkit.CreateMockStore(t) |
| 67 | + tk := testkit.NewTestKit(t, store) |
| 68 | + tk.MustExec("use test") |
| 69 | + tk.MustExec("create table t7(c int); ") |
| 70 | + tk.MustExec("insert into t7 values (575932053), (-258025139);") |
| 71 | + tk.MustQuery("select distinct cast(c as decimal), cast(c as signed) from t7"). |
| 72 | + Sort().Check(testkit.Rows("-258025139 -258025139", "575932053 575932053")) |
| 73 | + tk.MustQuery("explain select distinct cast(c as decimal), cast(c as signed) from t7"). |
| 74 | + Check(testkit.Rows( |
| 75 | + "HashAgg_8 8000.00 root group by:Column#7, Column#8, funcs:firstrow(Column#7)->Column#3, funcs:firstrow(Column#8)->Column#4", |
| 76 | + "└─TableReader_9 8000.00 root data:HashAgg_4", |
| 77 | + " └─HashAgg_4 8000.00 cop[tikv] group by:cast(test.t7.c, bigint(22) BINARY), cast(test.t7.c, decimal(10,0) BINARY), ", |
| 78 | + " └─TableFullScan_7 10000.00 cop[tikv] table:t7 keep order:false, stats:pseudo")) |
| 79 | + |
| 80 | + tk.MustExec("analyze table t7 all columns") |
| 81 | + tk.MustQuery("select distinct cast(c as decimal), cast(c as signed) from t7"). |
| 82 | + Sort(). |
| 83 | + Check(testkit.Rows("-258025139 -258025139", "575932053 575932053")) |
| 84 | + tk.MustQuery("explain select distinct cast(c as decimal), cast(c as signed) from t7"). |
| 85 | + Check(testkit.Rows( |
| 86 | + "HashAgg_6 2.00 root group by:Column#11, Column#12, funcs:firstrow(Column#11)->Column#3, funcs:firstrow(Column#12)->Column#4", |
| 87 | + "└─Projection_12 2.00 root cast(test.t7.c, decimal(10,0) BINARY)->Column#11, cast(test.t7.c, bigint(22) BINARY)->Column#12", |
| 88 | + " └─TableReader_11 2.00 root data:TableFullScan_10", |
| 89 | + " └─TableFullScan_10 2.00 cop[tikv] table:t7 keep order:false")) |
| 90 | +} |
| 91 | + |
| 92 | +func TestIssue54535(t *testing.T) { |
| 93 | + // test for tidb_enable_inl_join_inner_multi_pattern system variable |
| 94 | + store := testkit.CreateMockStore(t) |
| 95 | + tk := testkit.NewTestKit(t, store) |
| 96 | + tk.MustExec("use test") |
| 97 | + tk.MustExec("set session tidb_enable_inl_join_inner_multi_pattern='ON'") |
| 98 | + tk.MustExec("create table ta(a1 int, a2 int, a3 int, index idx_a(a1))") |
| 99 | + tk.MustExec("create table tb(b1 int, b2 int, b3 int, index idx_b(b1))") |
| 100 | + tk.MustExec("analyze table ta") |
| 101 | + tk.MustExec("analyze table tb") |
| 102 | + |
| 103 | + tk.MustQuery("explain SELECT /*+ inl_join(tmp) */ * FROM ta, (SELECT b1, COUNT(b3) AS cnt FROM tb GROUP BY b1, b2) as tmp where ta.a1 = tmp.b1"). |
| 104 | + Check(testkit.Rows( |
| 105 | + "Projection_9 9990.00 root test.ta.a1, test.ta.a2, test.ta.a3, test.tb.b1, Column#9", |
| 106 | + "└─IndexJoin_16 9990.00 root inner join, inner:HashAgg_14, outer key:test.ta.a1, inner key:test.tb.b1, equal cond:eq(test.ta.a1, test.tb.b1)", |
| 107 | + " ├─TableReader_43(Build) 9990.00 root data:Selection_42", |
| 108 | + " │ └─Selection_42 9990.00 cop[tikv] not(isnull(test.ta.a1))", |
| 109 | + " │ └─TableFullScan_41 10000.00 cop[tikv] table:ta keep order:false, stats:pseudo", |
| 110 | + " └─HashAgg_14(Probe) 79840080.00 root group by:test.tb.b1, test.tb.b2, funcs:count(Column#11)->Column#9, funcs:firstrow(test.tb.b1)->test.tb.b1", |
| 111 | + " └─IndexLookUp_15 79840080.00 root ", |
| 112 | + " ├─Selection_12(Build) 9990.00 cop[tikv] not(isnull(test.tb.b1))", |
| 113 | + " │ └─IndexRangeScan_10 10000.00 cop[tikv] table:tb, index:idx_b(b1) range: decided by [eq(test.tb.b1, test.ta.a1)], keep order:false, stats:pseudo", |
| 114 | + " └─HashAgg_13(Probe) 79840080.00 cop[tikv] group by:test.tb.b1, test.tb.b2, funcs:count(test.tb.b3)->Column#11", |
| 115 | + " └─TableRowIDScan_11 9990.00 cop[tikv] table:tb keep order:false, stats:pseudo")) |
| 116 | + // test for issues/55169 |
| 117 | + tk.MustExec("create table t1(col_1 int, index idx_1(col_1));") |
| 118 | + tk.MustExec("create table t2(col_1 int, col_2 int, index idx_2(col_1));") |
| 119 | + tk.MustQuery("select /*+ inl_join(tmp) */ * from t1 inner join (select col_1, group_concat(col_2) from t2 group by col_1) tmp on t1.col_1 = tmp.col_1;").Check(testkit.Rows()) |
| 120 | + tk.MustQuery("select /*+ inl_join(tmp) */ * from t1 inner join (select col_1, group_concat(distinct col_2 order by col_2) from t2 group by col_1) tmp on t1.col_1 = tmp.col_1;").Check(testkit.Rows()) |
| 121 | +} |
| 122 | + |
| 123 | +func TestIssue53175(t *testing.T) { |
| 124 | + store := testkit.CreateMockStore(t) |
| 125 | + tk := testkit.NewTestKit(t, store) |
| 126 | + tk.MustExec("use test") |
| 127 | + tk.MustExec(`create table t(a int)`) |
| 128 | + tk.MustExec(`set @@sql_mode = default`) |
| 129 | + tk.MustQuery(`select @@sql_mode REGEXP 'ONLY_FULL_GROUP_BY'`).Check(testkit.Rows("1")) |
| 130 | + tk.MustContainErrMsg(`select * from t group by null`, "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by") |
| 131 | + tk.MustExec(`create view v as select * from t group by null`) |
| 132 | + tk.MustContainErrMsg(`select * from v`, "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by") |
| 133 | + tk.MustExec(`set @@sql_mode = ''`) |
| 134 | + tk.MustQuery(`select * from t group by null`) |
| 135 | + tk.MustQuery(`select * from v`) |
| 136 | +} |
| 137 | + |
| 138 | +func TestIssues57583(t *testing.T) { |
| 139 | + store := testkit.CreateMockStore(t) |
| 140 | + tk := testkit.NewTestKit(t, store) |
| 141 | + tk.MustExec("use test;") |
| 142 | + tk.MustExec("create table t1(id int, v1 int, v2 int, v3 int);") |
| 143 | + tk.MustExec(" create table t2(id int, v1 int, v2 int, v3 int);") |
| 144 | + tk.MustQuery("explain select t1.id from t1 join t2 on t1.v1 = t2.v2 intersect select t1.id from t1 join t2 on t1.v1 = t2.v2;").Check(testkit.Rows( |
| 145 | + "HashJoin_15 6393.60 root semi join, left side:HashAgg_16, equal:[nulleq(test.t1.id, test.t1.id)]", |
| 146 | + "├─HashJoin_26(Build) 12487.50 root inner join, equal:[eq(test.t1.v1, test.t2.v2)]", |
| 147 | + "│ ├─TableReader_33(Build) 9990.00 root data:Selection_32", |
| 148 | + "│ │ └─Selection_32 9990.00 cop[tikv] not(isnull(test.t2.v2))", |
| 149 | + "│ │ └─TableFullScan_31 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo", |
| 150 | + "│ └─TableReader_30(Probe) 9990.00 root data:Selection_29", |
| 151 | + "│ └─Selection_29 9990.00 cop[tikv] not(isnull(test.t1.v1))", |
| 152 | + "│ └─TableFullScan_28 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo", |
| 153 | + "└─HashAgg_16(Probe) 7992.00 root group by:test.t1.id, funcs:firstrow(test.t1.id)->test.t1.id", |
| 154 | + " └─HashJoin_17 12487.50 root inner join, equal:[eq(test.t1.v1, test.t2.v2)]", |
| 155 | + " ├─TableReader_24(Build) 9990.00 root data:Selection_23", |
| 156 | + " │ └─Selection_23 9990.00 cop[tikv] not(isnull(test.t2.v2))", |
| 157 | + " │ └─TableFullScan_22 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo", |
| 158 | + " └─TableReader_21(Probe) 9990.00 root data:Selection_20", |
| 159 | + " └─Selection_20 9990.00 cop[tikv] not(isnull(test.t1.v1))", |
| 160 | + " └─TableFullScan_19 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo")) |
| 161 | +} |
| 162 | + |
| 163 | +func TestIssue58476(t *testing.T) { |
| 164 | + store := testkit.CreateMockStore(t) |
| 165 | + tk := testkit.NewTestKit(t, store) |
| 166 | + tk.MustExec("use test;") |
| 167 | + tk.MustExec("CREATE TABLE t3 (id int PRIMARY KEY,c1 varchar(256),c2 varchar(256) GENERATED ALWAYS AS (concat(c1, c1)) VIRTUAL,KEY (id));") |
| 168 | + tk.MustExec("insert into t3(id, c1) values (50, 'c');") |
| 169 | + tk.MustQuery("SELECT /*+ USE_INDEX_MERGE(`t3`)*/ id FROM `t3` WHERE c2 BETWEEN 'a' AND 'b' GROUP BY id HAVING id < 100 or id > 0;").Check(testkit.Rows()) |
| 170 | + tk.MustQuery("explain format='brief' SELECT /*+ USE_INDEX_MERGE(`t3`)*/ id FROM `t3` WHERE c2 BETWEEN 'a' AND 'b' GROUP BY id HAVING id < 100 or id > 0;"). |
| 171 | + Check(testkit.Rows( |
| 172 | + `Projection 249.75 root test.t3.id`, |
| 173 | + `└─Selection 249.75 root ge(test.t3.c2, "a"), le(test.t3.c2, "b")`, |
| 174 | + ` └─Projection 9990.00 root test.t3.id, test.t3.c2`, |
| 175 | + ` └─IndexMerge 9990.00 root type: union`, |
| 176 | + ` ├─IndexRangeScan(Build) 3323.33 cop[tikv] table:t3, index:id(id) range:[-inf,100), keep order:false, stats:pseudo`, |
| 177 | + ` ├─TableRangeScan(Build) 3333.33 cop[tikv] table:t3 range:(0,+inf], keep order:false, stats:pseudo`, |
| 178 | + ` └─TableRowIDScan(Probe) 9990.00 cop[tikv] table:t3 keep order:false, stats:pseudo`)) |
| 179 | +} |
0 commit comments