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: integrate hashEqual interface into LogicalPlan and expression.Expression. #55652

Merged
merged 20 commits into from
Aug 27, 2024
Prev Previous commit
Next Next commit
.
Signed-off-by: arenatlx <314806019@qq.com>
  • Loading branch information
AilinKid committed Aug 26, 2024
commit 6de1387e71fc08f8ea937eb4f1dfe2d7b87ac773
1 change: 0 additions & 1 deletion pkg/expression/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ go_library(
"//pkg/parser/opcode",
"//pkg/parser/terror",
"//pkg/parser/types",
"//pkg/planner/cascades/memo",
"//pkg/sessionctx/stmtctx",
"//pkg/sessionctx/variable",
"//pkg/types",
Expand Down
10 changes: 4 additions & 6 deletions pkg/expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package expression

import (
"fmt"
"github.com/pingcap/tidb/pkg/planner/cascades/memo"
"strings"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -168,12 +167,11 @@ const (
)

// Expression represents all scalar expression in SQL.
type Expression[T any] interface {
type Expression interface {
VecExpr
CollationInfo
memo.HashEquals[T]

Traverse(TraverseAction) Expression[any]
Traverse(TraverseAction) Expression

// Eval evaluates an expression through a row.
Eval(ctx EvalContext, row chunk.Row) (types.Datum, error)
Expand Down Expand Up @@ -206,10 +204,10 @@ type Expression[T any] interface {
GetType(ctx EvalContext) *types.FieldType

// Clone copies an expression totally.
Clone() Expression[any]
Clone() Expression

// Equal checks whether two expressions are equal.
Equal(ctx EvalContext, e Expression[any]) bool
Equal(ctx EvalContext, e Expression) bool

// IsCorrelated checks if this expression has correlated key.
IsCorrelated() bool
Expand Down
1 change: 0 additions & 1 deletion pkg/planner/core/base/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ go_library(
deps = [
"//pkg/expression",
"//pkg/kv",
"//pkg/planner/cascades/memo",
"//pkg/planner/context",
"//pkg/planner/funcdep",
"//pkg/planner/property",
Expand Down
28 changes: 13 additions & 15 deletions pkg/planner/core/base/plan_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package base

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

"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/kv"
Expand Down Expand Up @@ -191,9 +190,8 @@ func (c *PlanCounterTp) IsForce() bool {

// LogicalPlan is a tree of logical operators.
// We can do a lot of logical optimizations to it, like predicate push-down and column pruning.
type LogicalPlan[T any] interface {
type LogicalPlan interface {
Plan
memo.HashEquals[T]

// HashCode encodes a LogicalPlan to fast compare whether a LogicalPlan equals to another.
// We use a strict encode method here which ensures there is no conflict.
Expand All @@ -202,10 +200,10 @@ type LogicalPlan[T any] interface {
// PredicatePushDown pushes down the predicates in the where/on/having clauses as deeply as possible.
// It will accept a predicate that is an expression slice, and return the expressions that can't be pushed.
// Because it might change the root if the having clause exists, we need to return a plan that represents a new root.
PredicatePushDown([]expression.Expression[any], *optimizetrace.LogicalOptimizeOp) ([]expression.Expression[any], LogicalPlan[any])
PredicatePushDown([]expression.Expression, *optimizetrace.LogicalOptimizeOp) ([]expression.Expression, LogicalPlan)

// PruneColumns prunes the unused columns, and return the new logical plan if changed, otherwise it's same.
PruneColumns([]*expression.Column, *optimizetrace.LogicalOptimizeOp) (LogicalPlan[any], error)
PruneColumns([]*expression.Column, *optimizetrace.LogicalOptimizeOp) (LogicalPlan, error)

// FindBestTask converts the logical plan to the physical plan. It's a new interface.
// It is called recursively from the parent to the children to create the result physical plan.
Expand All @@ -225,19 +223,19 @@ type LogicalPlan[T any] interface {

// PushDownTopN will push down the topN or limit operator during logical optimization.
// interface definition should depend on concrete implementation type.
PushDownTopN(topN LogicalPlan[any], opt *optimizetrace.LogicalOptimizeOp) LogicalPlan[any]
PushDownTopN(topN LogicalPlan, opt *optimizetrace.LogicalOptimizeOp) LogicalPlan

// DeriveTopN derives an implicit TopN from a filter on row_number window function...
DeriveTopN(opt *optimizetrace.LogicalOptimizeOp) LogicalPlan[any]
DeriveTopN(opt *optimizetrace.LogicalOptimizeOp) LogicalPlan

// PredicateSimplification consolidates different predcicates on a column and its equivalence classes.
PredicateSimplification(opt *optimizetrace.LogicalOptimizeOp) LogicalPlan[any]
PredicateSimplification(opt *optimizetrace.LogicalOptimizeOp) LogicalPlan

// ConstantPropagation generate new constant predicate according to column equivalence relation
ConstantPropagation(parentPlan LogicalPlan[any], currentChildIdx int, opt *optimizetrace.LogicalOptimizeOp) (newRoot LogicalPlan[any])
ConstantPropagation(parentPlan LogicalPlan, currentChildIdx int, opt *optimizetrace.LogicalOptimizeOp) (newRoot LogicalPlan)

// PullUpConstantPredicates recursive find constant predicate, used for the constant propagation rule
PullUpConstantPredicates() []expression.Expression[any]
PullUpConstantPredicates() []expression.Expression

// RecursiveDeriveStats derives statistic info between plans.
RecursiveDeriveStats(colGroups [][]*expression.Column) (*property.StatsInfo, error)
Expand Down Expand Up @@ -271,13 +269,13 @@ type LogicalPlan[T any] interface {
MaxOneRow() bool

// Children Get all the children.
Children() []LogicalPlan[any]
Children() []LogicalPlan

// SetChildren sets the children for the plan.
SetChildren(...LogicalPlan[any])
SetChildren(...LogicalPlan)

// SetChild sets the ith child for the plan.
SetChild(i int, child LogicalPlan[any])
SetChild(i int, child LogicalPlan)

// RollBackTaskMap roll back all taskMap's logs after TimeStamp TS.
RollBackTaskMap(TS uint64)
Expand All @@ -289,8 +287,8 @@ type LogicalPlan[T any] interface {
ExtractFD() *fd.FDSet

// GetBaseLogicalPlan return the baseLogicalPlan inside each logical plan.
GetBaseLogicalPlan() LogicalPlan[any]
GetBaseLogicalPlan() LogicalPlan

// ConvertOuterToInnerJoin converts outer joins if the matching rows are filtered.
ConvertOuterToInnerJoin(predicates []expression.Expression[any]) LogicalPlan[any]
ConvertOuterToInnerJoin(predicates []expression.Expression) LogicalPlan
}