Skip to content

Commit

Permalink
planner/core: keep sort operator when ordered by tablesample (#48315)
Browse files Browse the repository at this point in the history
close #48253
  • Loading branch information
tangenta authored Nov 8, 2023
1 parent 184aa7e commit 83e39bc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
25 changes: 15 additions & 10 deletions pkg/planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1084,12 +1084,7 @@ func (ds *DataSource) findBestTask(prop *property.PhysicalProperty, planCounter
}

ds.storeTask(prop, t)
if ds.SampleInfo != nil && !t.invalid() {
if _, ok := t.plan().(*PhysicalTableSample); !ok {
warning := expression.ErrInvalidTableSample.GenWithStackByArgs("plan not supported")
ds.SCtx().GetSessionVars().StmtCtx.AppendWarning(warning)
}
}
err = validateTableSamplePlan(ds, t, err)
}()

t, err = ds.tryToGetDualTask()
Expand Down Expand Up @@ -2243,10 +2238,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 @@ -2618,3 +2611,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
}
18 changes: 10 additions & 8 deletions tests/integrationtest/r/executor/sample.result
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,14 @@ a
2100
4500
select a from t use index (idx_0) tablesample regions() order by a;
a
1000
1001
2100
4500
show warnings;
Level Code Message
Warning 8128 Invalid TABLESAMPLE: plan not supported
Error 8128 (HY000): Invalid TABLESAMPLE: plan not supported
DROP TABLE IF EXISTS a;
CREATE TABLE a (pk bigint unsigned primary key clustered, v text);
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;
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;
SPLIT TABLE a BY (500);
SELECT * FROM a TABLESAMPLE REGIONS() ORDER BY pk;
pk v
500 a
9223372036854775809 b
set @@global.tidb_scatter_region=default;
10 changes: 9 additions & 1 deletion tests/integrationtest/t/executor/sample.test
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ split table t between (0) and (10000) regions 5;
insert into t values (1000, 1, '1'), (1001, 1, '1'), (2100, 2, '2'), (4500, 3, '3');
create index idx_0 on t (b);
select a from t tablesample regions() order by a;
-- error 8128
select a from t use index (idx_0) tablesample regions() order by a;
show warnings;

# TestTableSampleUnsignedIntHandle
DROP TABLE IF EXISTS a;
CREATE TABLE a (pk bigint unsigned primary key clustered, v text);
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;
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;
SPLIT TABLE a BY (500);
SELECT * FROM a TABLESAMPLE REGIONS() ORDER BY pk;

set @@global.tidb_scatter_region=default;

0 comments on commit 83e39bc

Please sign in to comment.