1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/hanchuanchuan-goInception

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Клонировать/Скачать
optimizer.go 4.4 КБ
Копировать Редактировать Исходные данные Просмотреть построчно История
hanchuanchuan Отправлено 6 лет назад 27f3c5a
// Copyright 2015 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package core
import (
"math"
"github.com/hanchuanchuan/goInception/ast"
"github.com/hanchuanchuan/goInception/expression"
"github.com/hanchuanchuan/goInception/infoschema"
"github.com/hanchuanchuan/goInception/planner/property"
"github.com/hanchuanchuan/goInception/privilege"
"github.com/hanchuanchuan/goInception/sessionctx"
"github.com/pingcap/errors"
)
// OptimizeAstNode optimizes the query to a physical plan directly.
var OptimizeAstNode func(ctx sessionctx.Context, node ast.Node, is infoschema.InfoSchema) (Plan, error)
// AllowCartesianProduct means whether tidb allows cartesian join without equal conditions.
var AllowCartesianProduct = true
const (
flagPrunColumns uint64 = 1 << iota
flagBuildKeyInfo
flagDecorrelate
flagEliminateAgg
flagEliminateProjection
flagMaxMinEliminate
flagPredicatePushDown
flagPartitionProcessor
flagPushDownAgg
flagPushDownTopN
)
var optRuleList = []logicalOptRule{
&columnPruner{},
&buildKeySolver{},
&decorrelateSolver{},
&aggregationEliminator{},
&projectionEliminater{},
&maxMinEliminator{},
&ppdSolver{},
&partitionProcessor{},
&aggregationPushDownSolver{},
&pushDownTopNOptimizer{},
}
// logicalOptRule means a logical optimizing rule, which contains decorrelate, ppd, column pruning, etc.
type logicalOptRule interface {
optimize(LogicalPlan) (LogicalPlan, error)
}
// BuildLogicalPlan used to build logical plan from ast.Node.
func BuildLogicalPlan(ctx sessionctx.Context, node ast.Node, is infoschema.InfoSchema) (Plan, error) {
ctx.GetSessionVars().PlanID = 0
ctx.GetSessionVars().PlanColumnID = 0
builder := &PlanBuilder{
ctx: ctx,
is: is,
colMapper: make(map[*ast.ColumnNameExpr]int),
}
p, err := builder.Build(node)
if err != nil {
return nil, errors.Trace(err)
}
return p, nil
}
// CheckPrivilege checks the privilege for a user.
func CheckPrivilege(pm privilege.Manager, vs []visitInfo) bool {
for _, v := range vs {
if !pm.RequestVerification(v.db, v.table, v.column, v.privilege) {
return false
}
}
return true
}
// DoOptimize optimizes a logical plan to a physical plan.
func DoOptimize(flag uint64, logic LogicalPlan) (PhysicalPlan, error) {
logic, err := logicalOptimize(flag, logic)
if err != nil {
return nil, errors.Trace(err)
}
if !AllowCartesianProduct && existsCartesianProduct(logic) {
return nil, errors.Trace(ErrCartesianProductUnsupported)
}
physical, err := physicalOptimize(logic)
if err != nil {
return nil, errors.Trace(err)
}
finalPlan := eliminatePhysicalProjection(physical)
return finalPlan, nil
}
func logicalOptimize(flag uint64, logic LogicalPlan) (LogicalPlan, error) {
var err error
for i, rule := range optRuleList {
// The order of flags is same as the order of optRule in the list.
// We use a bitmask to record which opt rules should be used. If the i-th bit is 1, it means we should
// apply i-th optimizing rule.
if flag&(1<<uint(i)) == 0 {
continue
}
logic, err = rule.optimize(logic)
if err != nil {
return nil, errors.Trace(err)
}
}
return logic, errors.Trace(err)
}
func physicalOptimize(logic LogicalPlan) (PhysicalPlan, error) {
if _, err := logic.deriveStats(); err != nil {
return nil, errors.Trace(err)
}
logic.preparePossibleProperties()
prop := &property.PhysicalProperty{
TaskTp: property.RootTaskType,
ExpectedCnt: math.MaxFloat64,
}
t, err := logic.findBestTask(prop)
if err != nil {
return nil, errors.Trace(err)
}
if t.invalid() {
return nil, ErrInternal.GenWithStackByArgs("Can't find a proper physical plan for this query")
}
t.plan().ResolveIndices()
return t.plan(), nil
}
func existsCartesianProduct(p LogicalPlan) bool {
if join, ok := p.(*LogicalJoin); ok && len(join.EqualConditions) == 0 {
return join.JoinType == InnerJoin || join.JoinType == LeftOuterJoin || join.JoinType == RightOuterJoin
}
for _, child := range p.Children() {
if existsCartesianProduct(child) {
return true
}
}
return false
}
func init() {
expression.EvalAstExpr = evalAstExpr
}

Опубликовать ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/hanchuanchuan-goInception.git
git@gitlife.ru:oschina-mirror/hanchuanchuan-goInception.git
oschina-mirror
hanchuanchuan-goInception
hanchuanchuan-goInception
v1.3.0