Skip to content

Commit

Permalink
Merge branch 'master' into fk-bug1
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Nov 28, 2022
2 parents ffd143a + 73044b0 commit 16cdc55
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 102 deletions.
3 changes: 3 additions & 0 deletions executor/executor_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ func TestIssue22231(t *testing.T) {
tk.MustQuery("select cast('2020-05-28 23:59:59 00:00:00' as datetime)").Check(testkit.Rows("2020-05-28 23:59:59"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1292 Truncated incorrect datetime value: '2020-05-28 23:59:59 00:00:00'"))
tk.MustExec("drop table if exists t_issue_22231")

tk.MustQuery("SELECT CAST(\"1111111111-\" AS DATE);")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '1111111111-'"))
}

// TestIssue2612 is related with https://github.com/pingcap/tidb/issues/2612
Expand Down
2 changes: 0 additions & 2 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3762,8 +3762,6 @@ func TestExprPushdownBlacklist(t *testing.T) {
store := testkit.CreateMockStore(t)

tk := testkit.NewTestKit(t, store)
tk.MustQuery(`select * from mysql.expr_pushdown_blacklist`).Check(testkit.Rows(
"date_add tiflash DST(daylight saving time) does not take effect in TiFlash date_add"))

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
Expand Down
7 changes: 5 additions & 2 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,8 @@ func (e *Explain) RenderResult() error {
}
if pp.SCtx().GetSessionVars().CostModelVersion == modelVer2 {
// output cost formula and factor costs through warning under model ver2 and true_card_cost mode for cost calibration.
trace, _ := pp.getPlanCostVer2(property.RootTaskType, NewDefaultPlanCostOption())
cost, _ := pp.getPlanCostVer2(property.RootTaskType, NewDefaultPlanCostOption())
trace := cost.trace
pp.SCtx().GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("cost formula: %v", trace.formula))
data, err := json.Marshal(trace.factorCosts)
if err != nil {
Expand Down Expand Up @@ -942,7 +943,9 @@ func (e *Explain) getOperatorInfo(p Plan, id string) (string, string, string, st
if e.ctx != nil && e.ctx.GetSessionVars().CostModelVersion == modelVer2 {
costVer2, _ := pp.getPlanCostVer2(property.RootTaskType, NewDefaultPlanCostOption())
estCost = strconv.FormatFloat(costVer2.cost, 'f', 2, 64)
costFormula = costVer2.formula
if costVer2.trace != nil {
costFormula = costVer2.trace.formula
}
} else {
planCost, _ := getPlanCost(pp, property.RootTaskType, NewDefaultPlanCostOption())
estCost = strconv.FormatFloat(planCost, 'f', 2, 64)
Expand Down
189 changes: 95 additions & 94 deletions planner/core/plan_cost_ver2.go

Large diffs are not rendered by default.

42 changes: 40 additions & 2 deletions planner/core/plan_cost_ver2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
package core_test

import (
"context"
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"testing"

"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/planner"
"github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/planner/property"
"github.com/pingcap/tidb/sessiontxn"
"github.com/pingcap/tidb/testkit"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -144,8 +150,8 @@ func TestCostModelShowFormula(t *testing.T) {
actual = append(actual, []interface{}{row[0], row[3]}) // id,costFormula
}
require.Equal(t, actual, [][]interface{}{
{"TableReader_7", "((Selection_6) + (net(2*rowsize(16)*tidb_kv_net_factor(3.96))))/15"},
{"└─Selection_6", "(cpu(3*filters(1)*tikv_cpu_factor(49.9))) + (TableFullScan_5)"},
{"TableReader_7", "(((cpu(3*filters(1)*tikv_cpu_factor(49.9))) + (scan(3*logrowsize(32)*tikv_scan_factor(40.7)))) + (net(2*rowsize(16)*tidb_kv_net_factor(3.96))))/15.00"},
{"└─Selection_6", "(cpu(3*filters(1)*tikv_cpu_factor(49.9))) + (scan(3*logrowsize(32)*tikv_scan_factor(40.7)))"},
{" └─TableFullScan_5", "scan(3*logrowsize(32)*tikv_scan_factor(40.7))"},
})
}
Expand Down Expand Up @@ -242,3 +248,35 @@ func TestCostModelTraceVer2(t *testing.T) {
require.True(t, ok)
}
}

func BenchmarkGetPlanCost(b *testing.B) {
store := testkit.CreateMockStore(b)
tk := testkit.NewTestKit(b, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t (a int, b int);")

p := parser.New()
sql := "select sum(t1.b), t1.a from t t1, t t2 where t1.a>0 and t2.a>10 and t1.b=t2.b group by t1.a order by t1.a limit 5"
stmt, err := p.ParseOneStmt(sql, "", "")
if err != nil {
b.Fatal(err)
}
sctx := tk.Session()
sctx.GetSessionVars().CostModelVersion = 2
is := sessiontxn.GetTxnManager(sctx).GetTxnInfoSchema()
plan, _, err := planner.Optimize(context.TODO(), sctx, stmt, is)
if err != nil {
b.Fatal(err)
}
phyPlan := plan.(core.PhysicalPlan)
_, err = core.GetPlanCost(phyPlan, property.RootTaskType, core.NewDefaultPlanCostOption().WithCostFlag(core.CostFlagRecalculate))
if err != nil {
b.Fatal(err)
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = core.GetPlanCost(phyPlan, property.RootTaskType, core.NewDefaultPlanCostOption().WithCostFlag(core.CostFlagRecalculate))
}
}
2 changes: 0 additions & 2 deletions session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2267,8 +2267,6 @@ func doDMLWorks(s Session) {

writeNewCollationParameter(s, config.GetGlobalConfig().NewCollationsEnabledOnFirstBootstrap)

writeDefaultExprPushDownBlacklist(s)

writeStmtSummaryVars(s)

ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap)
Expand Down
6 changes: 6 additions & 0 deletions session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ func TestBootstrap(t *testing.T) {
se, err = CreateSession4Test(store)
require.NoError(t, err)
doDMLWorks(se)
r = mustExec(t, se, "select * from mysql.expr_pushdown_blacklist where name = 'date_add'")
req = r.NewChunk(nil)
err = r.Next(ctx, req)
require.NoError(t, err)
require.Equal(t, 0, req.NumRows())
se.Close()
}

func globalVarsCount() int64 {
Expand Down
4 changes: 4 additions & 0 deletions types/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"regexp"
"strconv"
Expand Down Expand Up @@ -1155,6 +1156,9 @@ func parseDatetime(sc *stmtctx.StatementContext, str string, fsp int, isFloat bo
hhmmss = true
}
if err != nil {
if err == io.EOF {
return ZeroDatetime, errors.Trace(ErrWrongValue.GenWithStackByArgs(DateTimeStr, str))
}
return ZeroDatetime, errors.Trace(err)
}

Expand Down

0 comments on commit 16cdc55

Please sign in to comment.