Skip to content
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
21 changes: 15 additions & 6 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4211,12 +4211,17 @@ func fullRangePartition(idxArr []int) bool {
return len(idxArr) == 1 && idxArr[0] == plannercore.FullRange
}

func (b *executorBuilder) buildTableSample(v *plannercore.PhysicalTableSample) *TableSampleExecutor {
if v.TableInfo.Meta().TempTableType != model.TempTableNone {
b.err = errors.New("TABLESAMPLE clause can not be applied to temporary tables")
return nil
}
type emptySampler struct{}

func (s *emptySampler) writeChunk(_ *chunk.Chunk) error {
return nil
}

func (s *emptySampler) finished() bool {
return true
}

func (b *executorBuilder) buildTableSample(v *plannercore.PhysicalTableSample) *TableSampleExecutor {
startTS, err := b.getSnapshotTS()
if err != nil {
b.err = err
Expand All @@ -4227,11 +4232,15 @@ func (b *executorBuilder) buildTableSample(v *plannercore.PhysicalTableSample) *
table: v.TableInfo,
startTS: startTS,
}
if v.TableSampleInfo.AstNode.SampleMethod == ast.SampleMethodTypeTiDBRegion {

if v.TableInfo.Meta().TempTableType != model.TempTableNone {
e.sampler = &emptySampler{}
} else if v.TableSampleInfo.AstNode.SampleMethod == ast.SampleMethodTypeTiDBRegion {
e.sampler = newTableRegionSampler(
b.ctx, v.TableInfo, startTS, v.TableSampleInfo.Partitions, v.Schema(),
v.TableSampleInfo.FullSchema, e.retFieldTypes, v.Desc)
}

return e
}

Expand Down
44 changes: 42 additions & 2 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8583,8 +8583,6 @@ func (s *testStaleTxnSuite) TestInvalidReadTemporaryTable(c *C) {
// sleep 1us to make test stale
time.Sleep(time.Microsecond)

tk.MustGetErrMsg("select * from tmp1 tablesample regions()", "TABLESAMPLE clause can not be applied to temporary tables")

queries := []struct {
sql string
}{
Expand Down Expand Up @@ -8658,6 +8656,48 @@ func (s *testStaleTxnSuite) TestInvalidReadTemporaryTable(c *C) {
}
}

func (s *testSuite) TestEmptyTableSampleTemporaryTable(c *C) {
tk := testkit.NewTestKit(c, s.store)
// For mocktikv, safe point is not initialized, we manually insert it for snapshot to use.
safePointName := "tikv_gc_safe_point"
safePointValue := "20160102-15:04:05 -0700"
safePointComment := "All versions after safe point can be accessed. (DO NOT EDIT)"
updateSafePoint := fmt.Sprintf(`INSERT INTO mysql.tidb VALUES ('%[1]s', '%[2]s', '%[3]s')
ON DUPLICATE KEY
UPDATE variable_value = '%[2]s', comment = '%[3]s'`, safePointName, safePointValue, safePointComment)
tk.MustExec(updateSafePoint)

tk.MustExec("set @@tidb_enable_global_temporary_table=1")
tk.MustExec("use test")
tk.MustExec("drop table if exists tmp1")
tk.MustExec("create global temporary table tmp1 " +
"(id int not null primary key, code int not null, value int default null, unique key code(code))" +
"on commit delete rows")

// sleep 1us to make test stale
time.Sleep(time.Microsecond)

// test tablesample return empty
rs := tk.MustQuery("select * from tmp1 tablesample regions()")
rs.Check(testkit.Rows())

tk.MustExec("begin")
tk.MustExec("insert into tmp1 values (1, 1, 1)")
rs = tk.MustQuery("select * from tmp1 tablesample regions()")
rs.Check(testkit.Rows())
tk.MustExec("commit")

// tablesample should not return error for compatibility of tools like dumpling
tk.MustExec("set @@tidb_snapshot=NOW(6)")
rs = tk.MustQuery("select * from tmp1 tablesample regions()")
rs.Check(testkit.Rows())

tk.MustExec("begin")
rs = tk.MustQuery("select * from tmp1 tablesample regions()")
rs.Check(testkit.Rows())
tk.MustExec("commit")
}

func (s *testSuite) TestIssue25506(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down