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: add cascade basic interface definition and basic task type. #57981

Merged
merged 7 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions pkg/planner/cascades/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ go_library(
srcs = [
"base.go",
"hash_equaler.go",
"task_scheduler_base.go",
"task_stack_base.go",
],
importpath = "github.com/pingcap/tidb/pkg/planner/cascades/base",
visibility = ["//visibility:public"],
deps = ["//pkg/planner/cascades/util"],
)

go_test(
Expand Down
12 changes: 12 additions & 0 deletions pkg/planner/cascades/base/cascadesctx/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "cascadesctx",
srcs = ["cascades_ctx.go"],
importpath = "github.com/pingcap/tidb/pkg/planner/cascades/base/cascadesctx",
visibility = ["//visibility:public"],
deps = [
"//pkg/planner/cascades/base",
"//pkg/planner/cascades/memo",
],
)
32 changes: 32 additions & 0 deletions pkg/planner/cascades/base/cascadesctx/cascades_ctx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 cascadesctx

import (
"github.com/pingcap/tidb/pkg/planner/cascades/base"
"github.com/pingcap/tidb/pkg/planner/cascades/memo"
)

// CascadesContext define the cascades context as interface, since it will be defined
// in cascades pkg, which ref task pkg with no doubt.
// while in the task pkg, the concrete task need receive cascades context as its
// constructing args, which will lead an import cycle.
// so that's why we separate it out of base pkg.
type CascadesContext interface {
Destroy()
GetScheduler() base.Scheduler
PushTask(task base.Task)
GetMemo() *memo.Memo
}
25 changes: 25 additions & 0 deletions pkg/planner/cascades/base/task_scheduler_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 base

// Scheduler is a scheduling interface defined for serializing(single thread)/concurrent(multi thread) running.
type Scheduler interface {
// ExecuteTasks start the internal scheduling.
ExecuteTasks() error
// Destroy release the internal resource if any.
Destroy()
// PushTask is outside portal for inserting a new task in. task running can also trigger another successive task.
PushTask(task Task)
}
36 changes: 36 additions & 0 deletions pkg/planner/cascades/base/task_stack_base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 base

import (
"github.com/pingcap/tidb/pkg/planner/cascades/util"
)

// Stack is abstract definition of task container.(TaskStack is a kind of array stack implementation of it)
type Stack interface {
Push(one Task)
Pop() Task
Empty() bool
Destroy()
}

// Task is an interface defined for all type of optimizing work: exploring, implementing,
// deriving-stats, join-reordering and so on.
type Task interface {
// Execute task self executing logic
Execute() error
// Desc task self description string.
Desc(w util.StrBufferWriter)
}
1 change: 1 addition & 0 deletions pkg/planner/cascades/memo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ go_library(
"//pkg/planner/core/base",
"//pkg/planner/property",
"//pkg/util/intest",
"@com_github_bits_and_blooms_bitset//:bitset",
"@com_github_pingcap_failpoint//:failpoint",
],
)
Expand Down
32 changes: 32 additions & 0 deletions pkg/planner/cascades/memo/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,35 @@ func NewGroup(prop *property.LogicalProperty) *Group {
}
return g
}

// GetLogicalProperty return this group's logical property.
func (g *Group) GetLogicalProperty() *property.LogicalProperty {
return g.logicalProp
}

// SetLogicalProperty set this group's logical property.
func (g *Group) SetLogicalProperty(prop *property.LogicalProperty) {
g.logicalProp = prop
}

// IsExplored returns whether this group is explored.
func (g *Group) IsExplored() bool {
return g.explored
}

// SetExplored set the group as tagged as explored.
func (g *Group) SetExplored() {
g.explored = true
}

// ForEachGE traverse the inside group expression with f call on them each.
func (g *Group) ForEachGE(f func(ge *GroupExpression) bool) {
var next bool
for elem := g.logicalExpressions.Front(); elem != nil; elem = elem.Next() {
expr := elem.Value.(*GroupExpression)
next = f(expr)
if !next {
break
}
}
}
32 changes: 32 additions & 0 deletions pkg/planner/cascades/memo/group_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package memo

import (
"github.com/bits-and-blooms/bitset"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/expression"
base2 "github.com/pingcap/tidb/pkg/planner/cascades/base"
Expand Down Expand Up @@ -42,6 +43,15 @@ type GroupExpression struct {

// hash64 is the unique fingerprint of the GroupExpression.
hash64 uint64

// mask indicate what rules have been applied in this group expression.
mask *bitset.BitSet

// abandoned is used in a case, when this gE has been encapsulated (say) 3 tasks
// and pushed into the task, this 3 task are all referring to this same gE, one
// of them has been substituted halfway, the successive task waiting on the task
// should feel this gE is out of date, and this task is abandoned.
abandoned bool
}

// GetGroup returns the Group that this GroupExpression belongs to.
Expand Down Expand Up @@ -117,6 +127,8 @@ func NewGroupExpression(lp base.LogicalPlan, inputs []*Group) *GroupExpression {
Inputs: inputs,
LogicalPlan: lp,
hash64: 0,
// todo: add rule set length
mask: bitset.New(1),
}
}

Expand All @@ -126,6 +138,26 @@ func (e *GroupExpression) Init(h base2.Hasher) {
e.hash64 = h.Sum64()
}

// IsExplored return whether this gE has explored rule i.
func (e *GroupExpression) IsExplored(i uint) bool {
return e.mask.Test(i)
}

// SetExplored set this gE as explored in rule i.
func (e *GroupExpression) SetExplored(i uint) {
e.mask.Set(i)
}

// IsAbandoned returns whether this gE is abandoned.
func (e *GroupExpression) IsAbandoned() bool {
return e.abandoned
}

// SetAbandoned set this gE as abandoned.
func (e *GroupExpression) SetAbandoned() {
e.abandoned = true
}

// DeriveLogicalProp derive the new group's logical property from a specific GE.
// DeriveLogicalProp is not called with recursive, because we only examine and
// init new group from bottom-up, so we can sure that this new group's children
Expand Down
17 changes: 16 additions & 1 deletion pkg/planner/cascades/task/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "task",
srcs = [
"base.go",
"task.go",
"task_apply_rule.go",
"task_opt_group.go",
"task_opt_group_expression.go",
"task_scheduler.go",
],
importpath = "github.com/pingcap/tidb/pkg/planner/cascades/task",
visibility = ["//visibility:public"],
deps = [
"//pkg/planner/cascades/base",
"//pkg/planner/cascades/base/cascadesctx",
"//pkg/planner/cascades/memo",
"//pkg/planner/cascades/rule",
"//pkg/planner/cascades/util",
],
)

go_test(
Expand All @@ -20,5 +31,9 @@ go_test(
embed = [":task"],
flaky = True,
shard_count = 3,
deps = ["@com_github_stretchr_testify//require"],
deps = [
"//pkg/planner/cascades/base",
"//pkg/planner/cascades/util",
"@com_github_stretchr_testify//require",
],
)
30 changes: 30 additions & 0 deletions pkg/planner/cascades/task/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 task

import (
"github.com/pingcap/tidb/pkg/planner/cascades/base"
"github.com/pingcap/tidb/pkg/planner/cascades/base/cascadesctx"
)

// BaseTask is base task wrapper structure for encapsulating basic things.
type BaseTask struct {
ctx cascadesctx.CascadesContext
}

// Push pushes a new task into inside stack.
func (b *BaseTask) Push(t base.Task) {
b.ctx.PushTask(t)
}
Loading