|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package memo |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/pingcap/tidb/pkg/planner/core/base" |
| 19 | +) |
| 20 | + |
| 21 | +// MemoExpression is a unified representation of logical and group expression. |
| 22 | +// 1: when stepping into memo optimization, we may convert a logical plan tree into a memoExpr, |
| 23 | +// |
| 24 | +// so each node of them is simply an LP. |
| 25 | +// |
| 26 | +// 2: when XForm is called from a specific rule, substitution happened, the output may be a mixture |
| 27 | +// |
| 28 | +// of logical plan and group expression. |
| 29 | +// |
| 30 | +// # Thus, memoExpr is responsible for representing both of them, leveraging the unified portal of |
| 31 | +// |
| 32 | +// MemoExpression managing memo group generation and hashing functionality. |
| 33 | +// |
| 34 | +//revive:disable:exported |
| 35 | +type MemoExpression struct { |
| 36 | + LP base.LogicalPlan |
| 37 | + |
| 38 | + GE *GroupExpression |
| 39 | + |
| 40 | + Inputs []*MemoExpression |
| 41 | +} |
| 42 | + |
| 43 | +// IsLogicalPlan checks whether this me is a logical plan. |
| 44 | +func (me *MemoExpression) IsLogicalPlan() bool { |
| 45 | + return me.LP != nil && me.GE == nil |
| 46 | +} |
| 47 | + |
| 48 | +// IsGroupExpression checks whether this me is a group expression. |
| 49 | +func (me *MemoExpression) IsGroupExpression() bool { |
| 50 | + return me.LP == nil && me.GE != nil |
| 51 | +} |
| 52 | + |
| 53 | +// NewMemoExpressionFromPlan init a memeExpression with a logical plan. |
| 54 | +func NewMemoExpressionFromPlan(plan base.LogicalPlan) *MemoExpression { |
| 55 | + return &MemoExpression{ |
| 56 | + LP: plan, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// NewMemoExpressionFromGroupExpression inits a memoExpression with an existed group expression. |
| 61 | +func NewMemoExpressionFromGroupExpression(ge *GroupExpression) *MemoExpression { |
| 62 | + return &MemoExpression{ |
| 63 | + GE: ge, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// NewMemoExpressionFromPlanAndInputs inits a memoExpression with mixed logical plan as current node, |
| 68 | +// and certain memoExpressions as its input. |
| 69 | +func NewMemoExpressionFromPlanAndInputs(plan base.LogicalPlan, inputs []*MemoExpression) *MemoExpression { |
| 70 | + return &MemoExpression{ |
| 71 | + LP: plan, |
| 72 | + Inputs: inputs, |
| 73 | + } |
| 74 | +} |
0 commit comments