Skip to content

Commit

Permalink
planner: move logical expand into logicalop pkg. (#55428)
Browse files Browse the repository at this point in the history
ref #51664, ref #52714
  • Loading branch information
AilinKid authored Aug 19, 2024
1 parent 9e9e9c2 commit 061d865
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 24 deletions.
1 change: 0 additions & 1 deletion pkg/planner/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ go_library(
"initialize.go",
"logical_cte.go",
"logical_datasource.go",
"logical_expand.go",
"logical_index_scan.go",
"logical_initialize.go",
"logical_plan_builder.go",
Expand Down
1 change: 1 addition & 0 deletions pkg/planner/core/core_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func init() {
utilfuncp.ExhaustPhysicalPlans4LogicalApply = exhaustPhysicalPlans4LogicalApply
utilfuncp.ExhaustPhysicalPlans4LogicalLimit = exhaustPhysicalPlans4LogicalLimit
utilfuncp.ExhaustPhysicalPlans4LogicalWindow = exhaustPhysicalPlans4LogicalWindow
utilfuncp.ExhaustPhysicalPlans4LogicalExpand = exhaustPhysicalPlans4LogicalExpand
utilfuncp.ExhaustPhysicalPlans4LogicalUnionAll = exhaustPhysicalPlans4LogicalUnionAll
utilfuncp.ExhaustPhysicalPlans4LogicalSequence = exhaustPhysicalPlans4LogicalSequence
utilfuncp.ExhaustPhysicalPlans4LogicalSelection = exhaustPhysicalPlans4LogicalSelection
Expand Down
5 changes: 3 additions & 2 deletions pkg/planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,8 @@ func exhaustPhysicalPlans4LogicalJoin(lp base.LogicalPlan, prop *property.Physic
return joins, true, nil
}

func exhaustPhysicalPlans4LogicalExpand(p *LogicalExpand, prop *property.PhysicalProperty) ([]base.PhysicalPlan, bool, error) {
func exhaustPhysicalPlans4LogicalExpand(lp base.LogicalPlan, prop *property.PhysicalProperty) ([]base.PhysicalPlan, bool, error) {
p := lp.(*logicalop.LogicalExpand)
// under the mpp task type, if the sort item is not empty, refuse it, cause expanded data doesn't support any sort items.
if !prop.IsSortItemEmpty() {
// false, meaning we can add a sort enforcer.
Expand Down Expand Up @@ -2461,7 +2462,7 @@ func canPushToCopImpl(lp base.LogicalPlan, storeTp kv.StoreType, considerDual bo
return false
}
ret = ret && canPushToCopImpl(&c.BaseLogicalPlan, storeTp, considerDual)
case *LogicalExpand:
case *logicalop.LogicalExpand:
// Expand itself only contains simple col ref and literal projection. (always ok, check its child)
if storeTp != kv.TiFlash {
return false
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ type exprRewriterPlanCtx struct {
// of the "INSERT" statement.
insertPlan *Insert

rollExpand *LogicalExpand
rollExpand *logicalop.LogicalExpand
}

type expressionRewriter struct {
Expand Down Expand Up @@ -2342,7 +2342,7 @@ func (er *expressionRewriter) funcCallToExpressionWithPlanCtx(planCtx *exprRewri
return
}
// resolve grouping args in group by items or not.
resolvedCols, err := planCtx.rollExpand.resolveGroupingFuncArgsInGroupBy(args)
resolvedCols, err := planCtx.rollExpand.ResolveGroupingFuncArgsInGroupBy(args)
if err != nil {
er.err = err
er.ctxStackAppend(nil, types.EmptyName)
Expand Down
14 changes: 7 additions & 7 deletions pkg/planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (b *PlanBuilder) buildExpand(p base.LogicalPlan, gbyItems []expression.Expr
// for grouping set {}, project it as: [null, null, null, d, gid]
expandSchema := proj.Schema().Clone()
expression.AdjustNullabilityFromGroupingSets(rollupGroupingSets, expandSchema)
expand := LogicalExpand{
expand := logicalop.LogicalExpand{
RollupGroupingSets: rollupGroupingSets,
DistinctGroupByCol: distinctGbyCols,
DistinctGbyColNames: distinctGbyColNames,
Expand Down Expand Up @@ -261,8 +261,8 @@ func (b *PlanBuilder) buildAggregation(ctx context.Context, p base.LogicalPlan,
if b.buildingCTE {
b.outerCTEs[len(b.outerCTEs)-1].containAggOrWindow = true
}
var rollupExpand *LogicalExpand
if expand, ok := p.(*LogicalExpand); ok {
var rollupExpand *logicalop.LogicalExpand
if expand, ok := p.(*logicalop.LogicalExpand); ok {
rollupExpand = expand
}

Expand Down Expand Up @@ -1210,7 +1210,7 @@ func findColFromNaturalUsingJoin(p base.LogicalPlan, col *expression.Column) (na
}

type resolveGroupingTraverseAction struct {
CurrentBlockExpand *LogicalExpand
CurrentBlockExpand *logicalop.LogicalExpand
}

func (r resolveGroupingTraverseAction) Transform(expr expression.Expression) (res expression.Expression) {
Expand All @@ -1219,18 +1219,18 @@ func (r resolveGroupingTraverseAction) Transform(expr expression.Expression) (re
// when meeting a column, judge whether it's a relate grouping set col.
// eg: select a, b from t group by a, c with rollup, here a is, while b is not.
// in underlying Expand schema (a,b,c,a',c'), a select list should be resolved to a'.
res, _ = r.CurrentBlockExpand.trySubstituteExprWithGroupingSetCol(x)
res, _ = r.CurrentBlockExpand.TrySubstituteExprWithGroupingSetCol(x)
case *expression.CorrelatedColumn:
// select 1 in (select t2.a from t group by t2.a, b with rollup) from t2;
// in this case: group by item has correlated column t2.a, and it's select list contains t2.a as well.
res, _ = r.CurrentBlockExpand.trySubstituteExprWithGroupingSetCol(x)
res, _ = r.CurrentBlockExpand.TrySubstituteExprWithGroupingSetCol(x)
case *expression.Constant:
// constant just keep it real: select 1 from t group by a, b with rollup.
res = x
case *expression.ScalarFunction:
// scalar function just try to resolve itself first, then if not changed, trying resolve its children.
var substituted bool
res, substituted = r.CurrentBlockExpand.trySubstituteExprWithGroupingSetCol(x)
res, substituted = r.CurrentBlockExpand.TrySubstituteExprWithGroupingSetCol(x)
if !substituted {
// if not changed, try to resolve it children.
// select a+1, grouping(b) from t group by a+1 (projected as c), b with rollup: in this case, a+1 is resolved as c as a whole.
Expand Down
2 changes: 1 addition & 1 deletion pkg/planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (
_ base.LogicalPlan = &logicalop.LogicalLock{}
_ base.LogicalPlan = &logicalop.LogicalLimit{}
_ base.LogicalPlan = &logicalop.LogicalWindow{}
_ base.LogicalPlan = &LogicalExpand{}
_ base.LogicalPlan = &logicalop.LogicalExpand{}
_ base.LogicalPlan = &logicalop.LogicalUnionScan{}
_ base.LogicalPlan = &logicalop.LogicalMemTable{}
_ base.LogicalPlan = &logicalop.LogicalShow{}
Expand Down
1 change: 1 addition & 0 deletions pkg/planner/core/operator/logicalop/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"logical_aggregation.go",
"logical_apply.go",
"logical_cte_table.go",
"logical_expand.go",
"logical_join.go",
"logical_limit.go",
"logical_lock.go",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package core
package logicalop

import (
"bytes"
"fmt"

"github.com/pingcap/tidb/pkg/expression"
"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/planner/core/operator/logicalop"
fd "github.com/pingcap/tidb/pkg/planner/funcdep"
"github.com/pingcap/tidb/pkg/planner/property"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace/logicaltrace"
"github.com/pingcap/tidb/pkg/planner/util/utilfuncp"
"github.com/pingcap/tidb/pkg/types"
"github.com/pingcap/tidb/pkg/util/dbterror/plannererrors"
"github.com/pingcap/tidb/pkg/util/plancodec"
Expand All @@ -33,7 +33,7 @@ import (

// LogicalExpand represents a logical Expand OP serves for data replication requirement.
type LogicalExpand struct {
logicalop.LogicalSchemaProducer
LogicalSchemaProducer

// distinct group by columns. (maybe projected below if it's a non-col)
DistinctGroupByCol []*expression.Column
Expand Down Expand Up @@ -66,7 +66,7 @@ type LogicalExpand struct {

// Init initializes LogicalProjection.
func (p LogicalExpand) Init(ctx base.PlanContext, offset int) *LogicalExpand {
p.BaseLogicalPlan = logicalop.NewBaseLogicalPlan(ctx, plancodec.TypeExpand, &p, offset)
p.BaseLogicalPlan = NewBaseLogicalPlan(ctx, plancodec.TypeExpand, &p, offset)
return &p
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func (p *LogicalExpand) PruneColumns(parentUsedCols []*expression.Column, opt *o

// ExhaustPhysicalPlans implements base.LogicalPlan.<14th> interface.
func (p *LogicalExpand) ExhaustPhysicalPlans(prop *property.PhysicalProperty) ([]base.PhysicalPlan, bool, error) {
return exhaustPhysicalPlans4LogicalExpand(p, prop)
return utilfuncp.ExhaustPhysicalPlans4LogicalExpand(p, prop)
}

// ExtractCorrelatedCols implements base.LogicalPlan.<15th> interface.
Expand Down Expand Up @@ -294,7 +294,8 @@ func (p *LogicalExpand) GenerateGroupingMarks(sourceCols []*expression.Column) [
return resSliceMap
}

func (p *LogicalExpand) trySubstituteExprWithGroupingSetCol(expr expression.Expression) (expression.Expression, bool) {
// TrySubstituteExprWithGroupingSetCol is used to substitute the original gby expression with new gby col.
func (p *LogicalExpand) TrySubstituteExprWithGroupingSetCol(expr expression.Expression) (expression.Expression, bool) {
// since all the original group items has been projected even single col,
// let's check the origin gby expression here, and map it to new gby col.
for i, oneExpr := range p.DistinctGbyExprs {
Expand All @@ -307,8 +308,8 @@ func (p *LogicalExpand) trySubstituteExprWithGroupingSetCol(expr expression.Expr
return expr, false
}

// CheckGroupingFuncArgsInGroupBy checks whether grouping function args is in grouping items.
func (p *LogicalExpand) resolveGroupingFuncArgsInGroupBy(groupingFuncArgs []expression.Expression) ([]*expression.Column, error) {
// ResolveGroupingFuncArgsInGroupBy checks whether grouping function args is in grouping items.
func (p *LogicalExpand) ResolveGroupingFuncArgsInGroupBy(groupingFuncArgs []expression.Expression) ([]*expression.Column, error) {
// build GBYColMap
distinctGBYColMap := make(map[int64]struct{}, len(p.DistinctGroupByCol))
for _, oneDistinctGBYCol := range p.DistinctGroupByCol {
Expand Down
4 changes: 2 additions & 2 deletions pkg/planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ type PlanBuilder struct {
outerNames [][]*types.FieldName
outerCTEs []*cteInfo
// outerBlockExpand register current Expand OP for rollup syntax in every select query block.
outerBlockExpand []*LogicalExpand
currentBlockExpand *LogicalExpand
outerBlockExpand []*logicalop.LogicalExpand
currentBlockExpand *logicalop.LogicalExpand
// colMapper stores the column that must be pre-resolved.
colMapper map[*ast.ColumnNameExpr]int
// visitInfo is used for privilege check.
Expand Down
3 changes: 2 additions & 1 deletion pkg/planner/core/rule_resolve_grouping_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"

"github.com/pingcap/tidb/pkg/planner/core/base"
"github.com/pingcap/tidb/pkg/planner/core/operator/logicalop"
"github.com/pingcap/tidb/pkg/planner/util/optimizetrace"
)

Expand Down Expand Up @@ -97,7 +98,7 @@ func genExpand(p base.LogicalPlan, opt *optimizetrace.LogicalOptimizeOp) (base.L
}
p.Children()[i] = np
}
if expand, ok := p.(*LogicalExpand); ok {
if expand, ok := p.(*logicalop.LogicalExpand); ok {
expand.GenLevelProjections()
}
return p, nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/planner/util/utilfuncp/func_pointer_misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ var ExhaustPhysicalPlans4LogicalPartitionUnionAll func(lp base.LogicalPlan, prop
var ExhaustPhysicalPlans4LogicalUnionAll func(lp base.LogicalPlan, prop *property.PhysicalProperty) (
[]base.PhysicalPlan, bool, error)

// ExhaustPhysicalPlans4LogicalExpand will be called by LogicalExpand in logicalOp pkg.
var ExhaustPhysicalPlans4LogicalExpand func(lp base.LogicalPlan, prop *property.PhysicalProperty) (
[]base.PhysicalPlan, bool, error)

// *************************************** physical op related *******************************************

// GetEstimatedProbeCntFromProbeParents will be called by BasePhysicalPlan in physicalOp pkg.
Expand Down

0 comments on commit 061d865

Please sign in to comment.