@@ -2529,6 +2529,53 @@ func TestIssue46298(t *testing.T) {
2529
2529
tk .MustQuery ("select *, first_value(v) over (partition by p order by o range between 3.1 preceding and 2.9 following) as a from test.first_range;" )
2530
2530
}
2531
2531
2532
+ func TestIssue46177 (t * testing.T ) {
2533
+ store := testkit .CreateMockStore (t )
2534
+ tk := testkit .NewTestKit (t , store )
2535
+ tk .MustExec (`use test` )
2536
+ tk .MustExec (` CREATE TABLE sbtest (
2537
+ id int(10) unsigned NOT NULL AUTO_INCREMENT,
2538
+ k int(10) unsigned NOT NULL DEFAULT '0',
2539
+ c char(120) NOT NULL DEFAULT '',
2540
+ pad char(60) NOT NULL DEFAULT '',
2541
+ PRIMARY KEY (id) /*T![clustered_index] CLUSTERED */,
2542
+ KEY k (k)
2543
+ )` )
2544
+
2545
+ // cannot choose the best plan with RangeScan.
2546
+ tk .MustExec (`set @@tidb_opt_fix_control = '46177:off'` )
2547
+ tk .MustQuery (`explain format='brief' select row_number() over(order by a.k) from (select * from sbtest where id<10) a` ).Check (testkit .Rows (
2548
+ `Projection 10.00 root Column#6->Column#7` ,
2549
+ `└─Window 10.00 root row_number()->Column#6 over(order by test.sbtest.k rows between current row and current row)` ,
2550
+ ` └─IndexReader 10.00 root index:Selection` ,
2551
+ ` └─Selection 10.00 cop[tikv] lt(test.sbtest.id, 10)` ,
2552
+ ` └─IndexFullScan 10000.00 cop[tikv] table:sbtest, index:k(k) keep order:true, stats:pseudo` ))
2553
+
2554
+ tk .MustExec (`set @@tidb_opt_fix_control = '46177:on'` )
2555
+ tk .MustQuery (`explain format='brief' select row_number() over(order by a.k) from (select * from sbtest where id<10) a` ).Check (testkit .Rows (
2556
+ `Projection 10.00 root Column#6->Column#7` ,
2557
+ `└─Window 10.00 root row_number()->Column#6 over(order by test.sbtest.k rows between current row and current row)` ,
2558
+ ` └─Sort 10.00 root test.sbtest.k` ,
2559
+ ` └─TableReader 10.00 root data:TableRangeScan` ,
2560
+ ` └─TableRangeScan 10.00 cop[tikv] table:sbtest range:[0,10), keep order:false, stats:pseudo` ))
2561
+
2562
+ // cannot choose the range scan plan.
2563
+ tk .MustExec (`set @@tidb_opt_fix_control = '46177:off'` )
2564
+ tk .MustQuery (`explain format='brief' select /*+ stream_agg() */ count(1) from sbtest where id<1 group by k` ).Check (testkit .Rows (
2565
+ `StreamAgg 1.00 root group by:test.sbtest.k, funcs:count(Column#6)->Column#5` ,
2566
+ `└─IndexReader 1.00 root index:StreamAgg` ,
2567
+ ` └─StreamAgg 1.00 cop[tikv] group by:test.sbtest.k, funcs:count(1)->Column#6` ,
2568
+ ` └─Selection 1.00 cop[tikv] lt(test.sbtest.id, 1)` ,
2569
+ ` └─IndexFullScan 10000.00 cop[tikv] table:sbtest, index:k(k) keep order:true, stats:pseudo` ))
2570
+
2571
+ tk .MustExec (`set @@tidb_opt_fix_control = '46177:on'` )
2572
+ tk .MustQuery (`explain format='brief' select /*+ stream_agg() */ count(1) from sbtest where id<1 group by k` ).Check (testkit .Rows (
2573
+ `StreamAgg 1.00 root group by:test.sbtest.k, funcs:count(1)->Column#5` ,
2574
+ `└─Sort 1.00 root test.sbtest.k` ,
2575
+ ` └─TableReader 1.00 root data:TableRangeScan` ,
2576
+ ` └─TableRangeScan 1.00 cop[tikv] table:sbtest range:[0,1), keep order:false, stats:pseudo` ))
2577
+ }
2578
+
2532
2579
// https://github.com/pingcap/tidb/issues/41458
2533
2580
func TestIssue41458 (t * testing.T ) {
2534
2581
store := testkit .CreateMockStore (t )
0 commit comments