diff --git a/pkg/planner/core/integration_test.go b/pkg/planner/core/integration_test.go index 453a9b4c0dabc..d3228d8060f08 100644 --- a/pkg/planner/core/integration_test.go +++ b/pkg/planner/core/integration_test.go @@ -1075,6 +1075,31 @@ func TestTiFlashFineGrainedShuffleWithMaxTiFlashThreads(t *testing.T) { require.Equal(t, uint64(16), streamCount[0]) } +func TestIssue37986(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + + tk.MustExec(`drop table if exists t3`) + tk.MustExec(`CREATE TABLE t3(c0 INT, primary key(c0))`) + tk.MustExec(`insert into t3 values(1), (2), (3), (4), (5), (6), (7), (8), (9), (10)`) + rs := tk.MustQuery(`SELECT v2.c0 FROM (select rand() as c0 from t3) v2 order by v2.c0 limit 10`).Rows() + lastVal := -1.0 + for _, r := range rs { + v := r[0].(string) + val, err := strconv.ParseFloat(v, 64) + require.NoError(t, err) + require.True(t, val >= lastVal) + lastVal = val + } + + tk.MustQuery(`explain format='brief' SELECT v2.c0 FROM (select rand() as c0 from t3) v2 order by v2.c0 limit 10`). + Check(testkit.Rows(`TopN 10.00 root Column#2, offset:0, count:10`, + `└─Projection 10000.00 root rand()->Column#2`, + ` └─TableReader 10000.00 root data:TableFullScan`, + ` └─TableFullScan 10000.00 cop[tikv] table:t3 keep order:false, stats:pseudo`)) +} + func TestIssue33175(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store) diff --git a/pkg/planner/core/rule_topn_push_down.go b/pkg/planner/core/rule_topn_push_down.go index 830995f55387d..5f4d73b0c3e8c 100644 --- a/pkg/planner/core/rule_topn_push_down.go +++ b/pkg/planner/core/rule_topn_push_down.go @@ -134,8 +134,17 @@ func (p *LogicalProjection) pushDownTopN(topN *LogicalTopN, opt *util.LogicalOpt } if topN != nil { exprCtx := p.SCtx().GetExprCtx() + substitutedExprs := make([]expression.Expression, 0, len(topN.ByItems)) for _, by := range topN.ByItems { - by.Expr = expression.FoldConstant(exprCtx, expression.ColumnSubstitute(exprCtx, by.Expr, p.schema, p.Exprs)) + substituted := expression.FoldConstant(exprCtx, expression.ColumnSubstitute(exprCtx, by.Expr, p.schema, p.Exprs)) + if !expression.IsImmutableFunc(substituted) { + // after substituting, if the order-by expression is un-deterministic like 'order by rand()', stop pushing down. + return p.baseLogicalPlan.pushDownTopN(topN, opt) + } + substitutedExprs = append(substitutedExprs, substituted) + } + for i, by := range topN.ByItems { + by.Expr = substitutedExprs[i] } // remove meaningless constant sort items.