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

WIP #1

Open
wants to merge 1 commit into
base: v407
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package core

import (
"context"
"github.com/pingcap/tidb/planner/util"
"math"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -136,15 +137,31 @@ func DoOptimize(ctx context.Context, sctx sessionctx.Context, flag uint64, logic
if err != nil {
return nil, 0, err
}
finalPlan := postOptimize(sctx, physical)
return finalPlan, cost, nil
finalPlan, err := postOptimize(sctx, physical)
return finalPlan, cost, err
}

func postOptimize(sctx sessionctx.Context, plan PhysicalPlan) PhysicalPlan {
func postOptimize(sctx sessionctx.Context, plan PhysicalPlan) (PhysicalPlan, error) {
plan = eliminatePhysicalProjection(plan)
plan = injectExtraProjection(plan)
plan = eliminateUnionScanAndLock(sctx, plan)
return plan
return stabilizeResults(sctx, plan)
}

func stabilizeResults(sctx sessionctx.Context, plan PhysicalPlan) (PhysicalPlan, error) {
if !sctx.GetSessionVars().EnableStableResults {
return plan, nil
}
cols := plan.Schema().Columns
items := property.ItemsFromCols(cols, false)
byItems := make([]*util.ByItems, 0, len(cols))
for _, col := range cols {
byItems = append(byItems, &util.ByItems{Expr: col})
}
sortReqProp := &property.PhysicalProperty{TaskTp: property.RootTaskType, Items: items, ExpectedCnt: math.MaxFloat64}
sort := PhysicalSort{ByItems: byItems}.Init(sctx, plan.Stats(), 0, sortReqProp)
sort.SetChildren(plan)
return sort, sort.ResolveIndices()
}

func logicalOptimize(ctx context.Context, flag uint64, logic LogicalPlan) (LogicalPlan, error) {
Expand Down
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,7 @@ var builtinGlobalVariable = []string{
variable.TiDBRedactLog,
variable.TiDBEnableTelemetry,
variable.TiDBEnableAmendPessimisticTxn,
variable.TiDBEnableStableResults,
}

var (
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,9 @@ type SessionVars struct {

// EnableAmendPessimisticTxn indicates if schema change amend is enabled for pessimistic transactions.
EnableAmendPessimisticTxn bool

// EnableStableResults if stabilize query results.
EnableStableResults bool
}

// PreparedParams contains the parameters of the current prepared statement when executing it.
Expand Down Expand Up @@ -1348,6 +1351,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
config.SetRedactLog(TiDBOptOn(val))
case TiDBEnableAmendPessimisticTxn:
s.EnableAmendPessimisticTxn = TiDBOptOn(val)
case TiDBEnableStableResults:
s.EnableStableResults = TiDBOptOn(val)
}
s.systems[name] = val
return nil
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ var defaultSysVars = []*SysVar{
{ScopeGlobal, TiDBRedactLog, strconv.Itoa(config.DefTiDBRedactLog)},
{ScopeGlobal, TiDBEnableTelemetry, BoolToIntStr(DefTiDBEnableTelemetry)},
{ScopeGlobal | ScopeSession, TiDBEnableAmendPessimisticTxn, boolToOnOff(DefTiDBEnableAmendPessimisticTxn)},
{ScopeGlobal | ScopeSession, TiDBEnableStableResults, BoolToIntStr(DefTiDBEnableStableResults)},
}

// SynonymsSysVariables is synonyms of system variables.
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,9 @@ const (

// TiDBEnableAmendPessimisticTxn indicates if amend pessimistic transactions is enabled.
TiDBEnableAmendPessimisticTxn = "tidb_enable_amend_pessimistic_txn"

// TiDBEnableStableResults indicates if stabilize query results.
TiDBEnableStableResults = "tidb_enable_stable_results"
)

// Default TiDB system variable values.
Expand Down Expand Up @@ -516,6 +519,7 @@ const (
DefTiDBAllowAutoRandExplicitInsert = false
DefTiDBEnableTelemetry = true
DefTiDBEnableAmendPessimisticTxn = false
DefTiDBEnableStableResults = false
)

// Process global variables.
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/varsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ func ValidateSetSystemVar(vars *SessionVars, name string, value string, scope Sc
TiDBCheckMb4ValueInUTF8, TiDBEnableSlowLog, TiDBRecordPlanInSlowLog,
TiDBScatterRegion, TiDBGeneralLog, TiDBConstraintCheckInPlace,
TiDBEnableVectorizedExpression, TiDBFoundInPlanCache, TiDBEnableCollectExecutionInfo,
TiDBAllowAutoRandExplicitInsert, TiDBEnableTelemetry, TiDBEnableAmendPessimisticTxn:
TiDBAllowAutoRandExplicitInsert, TiDBEnableTelemetry, TiDBEnableAmendPessimisticTxn, TiDBEnableStableResults:
fallthrough
case GeneralLog, AvoidTemporalUpgrade, BigTables, CheckProxyUsers, LogBin,
CoreFile, EndMakersInJSON, SQLLogBin, OfflineMode, PseudoSlaveMode, LowPriorityUpdates,
Expand Down