Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner/core: keep sort operator when ordered by tablesample (#48315) #48449

Merged
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
14 changes: 11 additions & 3 deletions executor/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,17 @@ func TestTableSampleNotSupportedPlanWarning(t *testing.T) {
tk.MustExec("create index idx_0 on t (b);")
tk.MustQuery("select a from t tablesample regions() order by a;").Check(
testkit.Rows("1000", "2100", "4500"))
tk.MustQuery("select a from t use index (idx_0) tablesample regions() order by a;").Check(
testkit.Rows("1000", "1001", "2100", "4500"))
tk.MustQuery("show warnings;").Check(testkit.Rows("Warning 8128 Invalid TABLESAMPLE: plan not supported"))
tk.MustGetErrCode("select a from t use index (idx_0) tablesample regions() order by a;", errno.ErrInvalidTableSample)
}

func TestTableSampleUnsignedIntHandle(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := createSampleTestkit(t, store)
tk.MustExec("CREATE TABLE a (pk bigint unsigned primary key clustered, v text);")
tk.MustExec("INSERT INTO a WITH RECURSIVE b(pk) AS (SELECT 1 UNION ALL SELECT pk+1 FROM b WHERE pk < 1000) SELECT pk, 'a' FROM b;")
tk.MustExec("INSERT INTO a WITH RECURSIVE b(pk) AS (SELECT 1 UNION ALL SELECT pk+1 FROM b WHERE pk < 1000) SELECT pk + (1<<63), 'b' FROM b;")
tk.MustQuery("SPLIT TABLE a BY (500);").Check(testkit.Rows("1 1"))
tk.MustQuery("SELECT * FROM a TABLESAMPLE REGIONS() ORDER BY pk;").Check(testkit.Rows("500 a", "9223372036854775809 b"))
}

func TestMaxChunkSize(t *testing.T) {
Expand Down
25 changes: 15 additions & 10 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,12 +913,7 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter
prop.CanAddEnforcer = true
}
ds.storeTask(prop, t)
if ds.SampleInfo != nil && !t.invalid() {
if _, ok := t.plan().(*PhysicalTableSample); !ok {
warning := expression.ErrInvalidTableSample.GenWithStackByArgs("plan not supported")
ds.ctx.GetSessionVars().StmtCtx.AppendWarning(warning)
}
}
err = validateTableSamplePlan(ds, t, err)
}()

t, err = ds.tryToGetDualTask()
Expand Down Expand Up @@ -2122,10 +2117,8 @@ func (ds *DataSource) convertToSampleTable(prop *property.PhysicalProperty,
return invalidTask, nil
}
if candidate.isMatchProp {
// TableSample on partition table can't keep order.
if ds.tableInfo.GetPartitionInfo() != nil {
return invalidTask, nil
}
// Disable keep order property for sample table path.
return invalidTask, nil
}
p := PhysicalTableSample{
TableSampleInfo: ds.SampleInfo,
Expand Down Expand Up @@ -2478,3 +2471,15 @@ func pushDownNot(ctx sessionctx.Context, conds []expression.Expression) []expres
}
return conds
}

func validateTableSamplePlan(ds *DataSource, t task, err error) error {
if err != nil {
return err
}
if ds.SampleInfo != nil && !t.invalid() {
if _, ok := t.plan().(*PhysicalTableSample); !ok {
return expression.ErrInvalidTableSample.GenWithStackByArgs("plan not supported")
}
}
return nil
}