|
| 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 core |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/pingcap/tidb/pkg/expression" |
| 19 | + "github.com/pingcap/tidb/pkg/planner/core/base" |
| 20 | + "github.com/pingcap/tidb/pkg/planner/core/operator/logicalop" |
| 21 | + "github.com/pingcap/tidb/pkg/planner/property" |
| 22 | + "github.com/pingcap/tidb/pkg/planner/util" |
| 23 | + "github.com/pingcap/tidb/pkg/planner/util/optimizetrace" |
| 24 | + "github.com/pingcap/tidb/pkg/planner/util/optimizetrace/logicaltrace" |
| 25 | + "github.com/pingcap/tidb/pkg/planner/util/utilfuncp" |
| 26 | + "github.com/pingcap/tidb/pkg/util/plancodec" |
| 27 | +) |
| 28 | + |
| 29 | +// LogicalUnionAll represents LogicalUnionAll plan. |
| 30 | +type LogicalUnionAll struct { |
| 31 | + logicalop.LogicalSchemaProducer |
| 32 | +} |
| 33 | + |
| 34 | +// Init initializes LogicalUnionAll. |
| 35 | +func (p LogicalUnionAll) Init(ctx base.PlanContext, offset int) *LogicalUnionAll { |
| 36 | + p.BaseLogicalPlan = logicalop.NewBaseLogicalPlan(ctx, plancodec.TypeUnion, &p, offset) |
| 37 | + return &p |
| 38 | +} |
| 39 | + |
| 40 | +// *************************** start implementation of logicalPlan interface *************************** |
| 41 | + |
| 42 | +// HashCode inherits BaseLogicalPlan.LogicalPlan.<0th> implementation. |
| 43 | + |
| 44 | +// PredicatePushDown implements base.LogicalPlan.<1st> interface. |
| 45 | +func (p *LogicalUnionAll) PredicatePushDown(predicates []expression.Expression, opt *optimizetrace.LogicalOptimizeOp) (ret []expression.Expression, retPlan base.LogicalPlan) { |
| 46 | + for i, proj := range p.Children() { |
| 47 | + newExprs := make([]expression.Expression, 0, len(predicates)) |
| 48 | + newExprs = append(newExprs, predicates...) |
| 49 | + retCond, newChild := proj.PredicatePushDown(newExprs, opt) |
| 50 | + utilfuncp.AddSelection(p, newChild, retCond, i, opt) |
| 51 | + } |
| 52 | + return nil, p |
| 53 | +} |
| 54 | + |
| 55 | +// PruneColumns implements base.LogicalPlan.<2nd> interface. |
| 56 | +func (p *LogicalUnionAll) PruneColumns(parentUsedCols []*expression.Column, opt *optimizetrace.LogicalOptimizeOp) (base.LogicalPlan, error) { |
| 57 | + eCtx := p.SCtx().GetExprCtx().GetEvalCtx() |
| 58 | + used := expression.GetUsedList(eCtx, parentUsedCols, p.Schema()) |
| 59 | + hasBeenUsed := false |
| 60 | + for i := range used { |
| 61 | + hasBeenUsed = hasBeenUsed || used[i] |
| 62 | + if hasBeenUsed { |
| 63 | + break |
| 64 | + } |
| 65 | + } |
| 66 | + if !hasBeenUsed { |
| 67 | + parentUsedCols = make([]*expression.Column, len(p.Schema().Columns)) |
| 68 | + copy(parentUsedCols, p.Schema().Columns) |
| 69 | + for i := range used { |
| 70 | + used[i] = true |
| 71 | + } |
| 72 | + } |
| 73 | + var err error |
| 74 | + for i, child := range p.Children() { |
| 75 | + p.Children()[i], err = child.PruneColumns(parentUsedCols, opt) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + prunedColumns := make([]*expression.Column, 0) |
| 82 | + for i := len(used) - 1; i >= 0; i-- { |
| 83 | + if !used[i] { |
| 84 | + prunedColumns = append(prunedColumns, p.Schema().Columns[i]) |
| 85 | + p.Schema().Columns = append(p.Schema().Columns[:i], p.Schema().Columns[i+1:]...) |
| 86 | + } |
| 87 | + } |
| 88 | + logicaltrace.AppendColumnPruneTraceStep(p, prunedColumns, opt) |
| 89 | + if hasBeenUsed { |
| 90 | + // It's possible that the child operator adds extra columns to the schema. |
| 91 | + // Currently, (*LogicalAggregation).PruneColumns() might do this. |
| 92 | + // But we don't need such columns, so we add an extra Projection to prune this column when this happened. |
| 93 | + for i, child := range p.Children() { |
| 94 | + if p.Schema().Len() < child.Schema().Len() { |
| 95 | + schema := p.Schema().Clone() |
| 96 | + exprs := make([]expression.Expression, len(p.Schema().Columns)) |
| 97 | + for j, col := range schema.Columns { |
| 98 | + exprs[j] = col |
| 99 | + } |
| 100 | + proj := LogicalProjection{Exprs: exprs, AvoidColumnEvaluator: true}.Init(p.SCtx(), p.QueryBlockOffset()) |
| 101 | + proj.SetSchema(schema) |
| 102 | + |
| 103 | + proj.SetChildren(child) |
| 104 | + p.Children()[i] = proj |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return p, nil |
| 109 | +} |
| 110 | + |
| 111 | +// FindBestTask inherits BaseLogicalPlan.LogicalPlan.<3rd> implementation. |
| 112 | + |
| 113 | +// BuildKeyInfo inherits BaseLogicalPlan.LogicalPlan.<4th> implementation. |
| 114 | + |
| 115 | +// PushDownTopN implements the base.LogicalPlan.<5th> interface. |
| 116 | +func (p *LogicalUnionAll) PushDownTopN(topNLogicalPlan base.LogicalPlan, opt *optimizetrace.LogicalOptimizeOp) base.LogicalPlan { |
| 117 | + var topN *LogicalTopN |
| 118 | + if topNLogicalPlan != nil { |
| 119 | + topN = topNLogicalPlan.(*LogicalTopN) |
| 120 | + } |
| 121 | + for i, child := range p.Children() { |
| 122 | + var newTopN *LogicalTopN |
| 123 | + if topN != nil { |
| 124 | + newTopN = LogicalTopN{Count: topN.Count + topN.Offset, PreferLimitToCop: topN.PreferLimitToCop}.Init(p.SCtx(), topN.QueryBlockOffset()) |
| 125 | + for _, by := range topN.ByItems { |
| 126 | + newTopN.ByItems = append(newTopN.ByItems, &util.ByItems{Expr: by.Expr, Desc: by.Desc}) |
| 127 | + } |
| 128 | + // newTopN to push down Union's child |
| 129 | + appendNewTopNTraceStep(topN, p, opt) |
| 130 | + } |
| 131 | + p.Children()[i] = child.PushDownTopN(newTopN, opt) |
| 132 | + } |
| 133 | + if topN != nil { |
| 134 | + return topN.AttachChild(p, opt) |
| 135 | + } |
| 136 | + return p |
| 137 | +} |
| 138 | + |
| 139 | +// DeriveTopN inherits BaseLogicalPlan.LogicalPlan.<6th> implementation. |
| 140 | + |
| 141 | +// PredicateSimplification inherits BaseLogicalPlan.LogicalPlan.<7th> implementation. |
| 142 | + |
| 143 | +// ConstantPropagation inherits BaseLogicalPlan.LogicalPlan.<8th> implementation. |
| 144 | + |
| 145 | +// PullUpConstantPredicates inherits BaseLogicalPlan.LogicalPlan.<9th> implementation. |
| 146 | + |
| 147 | +// RecursiveDeriveStats inherits BaseLogicalPlan.LogicalPlan.<10th> implementation. |
| 148 | + |
| 149 | +// DeriveStats implement base.LogicalPlan.<11th> interface. |
| 150 | +func (p *LogicalUnionAll) DeriveStats(childStats []*property.StatsInfo, selfSchema *expression.Schema, _ []*expression.Schema, _ [][]*expression.Column) (*property.StatsInfo, error) { |
| 151 | + if p.StatsInfo() != nil { |
| 152 | + return p.StatsInfo(), nil |
| 153 | + } |
| 154 | + p.SetStats(&property.StatsInfo{ |
| 155 | + ColNDVs: make(map[int64]float64, selfSchema.Len()), |
| 156 | + }) |
| 157 | + for _, childProfile := range childStats { |
| 158 | + p.StatsInfo().RowCount += childProfile.RowCount |
| 159 | + for _, col := range selfSchema.Columns { |
| 160 | + p.StatsInfo().ColNDVs[col.UniqueID] += childProfile.ColNDVs[col.UniqueID] |
| 161 | + } |
| 162 | + } |
| 163 | + return p.StatsInfo(), nil |
| 164 | +} |
| 165 | + |
| 166 | +// ExtractColGroups inherits BaseLogicalPlan.LogicalPlan.<12th> implementation. |
| 167 | + |
| 168 | +// PreparePossibleProperties implements base.LogicalPlan.<13th> interface. |
| 169 | + |
| 170 | +// ExhaustPhysicalPlans implements base.LogicalPlan.<14th> interface. |
| 171 | +func (p *LogicalUnionAll) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]base.PhysicalPlan, bool, error) { |
| 172 | + return exhaustUnionAllPhysicalPlans(p, prop) |
| 173 | +} |
| 174 | + |
| 175 | +// ExtractCorrelatedCols inherits BaseLogicalPlan.LogicalPlan.<15th> implementation. |
| 176 | + |
| 177 | +// MaxOneRow inherits BaseLogicalPlan.LogicalPlan.<16th> implementation. |
| 178 | + |
| 179 | +// Children inherits BaseLogicalPlan.LogicalPlan.<17th> implementation. |
| 180 | + |
| 181 | +// SetChildren inherits BaseLogicalPlan.LogicalPlan.<18th> implementation. |
| 182 | + |
| 183 | +// SetChild inherits BaseLogicalPlan.LogicalPlan.<19th> implementation. |
| 184 | + |
| 185 | +// RollBackTaskMap inherits BaseLogicalPlan.LogicalPlan.<20th> implementation. |
| 186 | + |
| 187 | +// CanPushToCop inherits BaseLogicalPlan.LogicalPlan.<21st> implementation. |
| 188 | + |
| 189 | +// ExtractFD inherits BaseLogicalPlan.LogicalPlan.<22nd> implementation. |
| 190 | + |
| 191 | +// GetBaseLogicalPlan inherits BaseLogicalPlan.LogicalPlan.<23rd> implementation. |
| 192 | + |
| 193 | +// ConvertOuterToInnerJoin inherits BaseLogicalPlan.LogicalPlan.<24th> implementation. |
| 194 | + |
| 195 | +// *************************** end implementation of logicalPlan interface *************************** |
0 commit comments