Skip to content

Commit

Permalink
planner: move table sample logic to planner util (#53100)
Browse files Browse the repository at this point in the history
ref #51664, ref #52714
  • Loading branch information
AilinKid authored May 8, 2024
1 parent 50b8dfa commit a5c4031
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 39 deletions.
1 change: 1 addition & 0 deletions pkg/planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ go_library(
"//pkg/planner/util/debugtrace",
"//pkg/planner/util/fixcontrol",
"//pkg/planner/util/optimizetrace",
"//pkg/planner/util/tablesampler",
"//pkg/privilege",
"//pkg/sessionctx",
"//pkg/sessionctx/stmtctx",
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/pingcap/tidb/pkg/planner/util/coreusage"
"github.com/pingcap/tidb/pkg/planner/util/debugtrace"
"github.com/pingcap/tidb/pkg/planner/util/fixcontrol"
"github.com/pingcap/tidb/pkg/planner/util/tablesampler"
"github.com/pingcap/tidb/pkg/privilege"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/sessionctx/variable"
Expand Down Expand Up @@ -5018,7 +5019,7 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as
ds.SetSchema(schema)
ds.names = names
ds.setPreferredStoreType(b.TableHints())
ds.SampleInfo = NewTableSampleInfo(tn.TableSample, schema, b.partitionedTable)
ds.SampleInfo = tablesampler.NewTableSampleInfo(tn.TableSample, schema, b.partitionedTable)
b.isSampling = ds.SampleInfo != nil

for i, colExpr := range ds.Schema().Columns {
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/pkg/planner/util"
"github.com/pingcap/tidb/pkg/planner/util/coreusage"
"github.com/pingcap/tidb/pkg/planner/util/debugtrace"
"github.com/pingcap/tidb/pkg/planner/util/tablesampler"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/statistics"
"github.com/pingcap/tidb/pkg/table"
Expand Down Expand Up @@ -1470,7 +1471,7 @@ type DataSource struct {
preferStoreType int
// preferPartitions store the map, the key represents store type, the value represents the partition name list.
preferPartitions map[int][]model.CIStr
SampleInfo *TableSampleInfo
SampleInfo *tablesampler.TableSampleInfo
is infoschema.InfoSchema
// isForUpdateRead should be true in either of the following situations
// 1. use `inside insert`, `update`, `delete` or `select for update` statement
Expand Down
40 changes: 3 additions & 37 deletions pkg/planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/pingcap/tidb/pkg/planner/util"
"github.com/pingcap/tidb/pkg/planner/util/coreusage"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
"github.com/pingcap/tidb/pkg/planner/util/tablesampler"
"github.com/pingcap/tidb/pkg/sessionctx"
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
"github.com/pingcap/tidb/pkg/statistics"
Expand Down Expand Up @@ -898,7 +899,7 @@ type PhysicalTableScan struct {

PlanPartInfo PhysPlanPartInfo

SampleInfo *TableSampleInfo
SampleInfo *tablesampler.TableSampleInfo

// required by cost model
// tblCols and tblColHists contains all columns before pruning, which are used to calculate row-size
Expand Down Expand Up @@ -2525,47 +2526,12 @@ func SafeClone(v base.PhysicalPlan) (_ base.PhysicalPlan, err error) {
// It returns the sample rows to its parent operand.
type PhysicalTableSample struct {
physicalSchemaProducer
TableSampleInfo *TableSampleInfo
TableSampleInfo *tablesampler.TableSampleInfo
TableInfo table.Table
PhysicalTableID int64
Desc bool
}

// TableSampleInfo contains the information for PhysicalTableSample.
type TableSampleInfo struct {
AstNode *ast.TableSample
FullSchema *expression.Schema
Partitions []table.PartitionedTable
}

// MemoryUsage return the memory usage of TableSampleInfo
func (t *TableSampleInfo) MemoryUsage() (sum int64) {
if t == nil {
return
}

sum = size.SizeOfPointer*2 + size.SizeOfSlice + int64(cap(t.Partitions))*size.SizeOfInterface
if t.AstNode != nil {
sum += int64(unsafe.Sizeof(ast.TableSample{}))
}
if t.FullSchema != nil {
sum += t.FullSchema.MemoryUsage()
}
return
}

// NewTableSampleInfo creates a new TableSampleInfo.
func NewTableSampleInfo(node *ast.TableSample, fullSchema *expression.Schema, pt []table.PartitionedTable) *TableSampleInfo {
if node == nil {
return nil
}
return &TableSampleInfo{
AstNode: node,
FullSchema: fullSchema.Clone(),
Partitions: pt,
}
}

// PhysicalCTE is for CTE.
type PhysicalCTE struct {
physicalSchemaProducer
Expand Down
14 changes: 14 additions & 0 deletions pkg/planner/util/tablesampler/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "tablesampler",
srcs = ["sample.go"],
importpath = "github.com/pingcap/tidb/pkg/planner/util/tablesampler",
visibility = ["//visibility:public"],
deps = [
"//pkg/expression",
"//pkg/parser/ast",
"//pkg/table",
"//pkg/util/size",
],
)
60 changes: 60 additions & 0 deletions pkg/planner/util/tablesampler/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tablesampler

import (
"unsafe"

"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/table"
"github.com/pingcap/tidb/pkg/util/size"
)

// TableSampleInfo contains the information for PhysicalTableSample.
type TableSampleInfo struct {
AstNode *ast.TableSample
FullSchema *expression.Schema
Partitions []table.PartitionedTable
}

// MemoryUsage return the memory usage of TableSampleInfo
func (t *TableSampleInfo) MemoryUsage() (sum int64) {
if t == nil {
return
}

sum = size.SizeOfPointer*2 + size.SizeOfSlice + int64(cap(t.Partitions))*size.SizeOfInterface
if t.AstNode != nil {
sum += int64(unsafe.Sizeof(ast.TableSample{}))
}
if t.FullSchema != nil {
sum += t.FullSchema.MemoryUsage()
}
return
}

// NewTableSampleInfo creates a new TableSampleInfo.
func NewTableSampleInfo(node *ast.TableSample, fullSchema *expression.Schema,
pt []table.PartitionedTable) *TableSampleInfo {
if node == nil {
return nil
}
return &TableSampleInfo{
AstNode: node,
FullSchema: fullSchema.Clone(),
Partitions: pt,
}
}

0 comments on commit a5c4031

Please sign in to comment.