Skip to content

Commit

Permalink
Merge branch 'master' into bug-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zz-jason authored Oct 16, 2018
2 parents 3aedd6d + 1bb15f1 commit e9dbe95
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 21 deletions.
6 changes: 3 additions & 3 deletions cmd/explaintest/r/explain_easy.result
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ create table t(a int primary key, b int, c int, index idx(b));
explain select t.c in (select count(*) from t s ignore index(idx), t t1 where s.a = t.a and s.a = t1.a) from t;
id count task operator info
Projection_11 10000.00 root 9_aux_0
└─Apply_13 10000.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, count(*))]
└─Apply_13 10000.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, 7_col_0)]
├─TableReader_15 10000.00 root data:TableScan_14
│ └─TableScan_14 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
└─StreamAgg_20 1.00 root funcs:count(1)
Expand All @@ -281,7 +281,7 @@ Projection_11 10000.00 root 9_aux_0
explain select t.c in (select count(*) from t s use index(idx), t t1 where s.b = t.a and s.a = t1.a) from t;
id count task operator info
Projection_11 10000.00 root 9_aux_0
└─Apply_13 10000.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, count(*))]
└─Apply_13 10000.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, 7_col_0)]
├─TableReader_15 10000.00 root data:TableScan_14
│ └─TableScan_14 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
└─StreamAgg_20 1.00 root funcs:count(1)
Expand All @@ -293,7 +293,7 @@ Projection_11 10000.00 root 9_aux_0
explain select t.c in (select count(*) from t s use index(idx), t t1 where s.b = t.a and s.c = t1.a) from t;
id count task operator info
Projection_11 10000.00 root 9_aux_0
└─Apply_13 10000.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, count(*))]
└─Apply_13 10000.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, 7_col_0)]
├─TableReader_15 10000.00 root data:TableScan_14
│ └─TableScan_14 10000.00 cop table:t, range:[-inf,+inf], keep order:false, stats:pseudo
└─StreamAgg_20 1.00 root funcs:count(1)
Expand Down
5 changes: 5 additions & 0 deletions executor/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,11 @@ func (s *testSuite) TestAggPrune(c *C) {
tk.MustExec("create table t(id int primary key, b float, c float, d float)")
tk.MustExec("insert into t values(1, 1, 3, NULL), (2, 1, NULL, 6), (3, NULL, 1, 2), (4, NULL, NULL, 1), (5, NULL, 2, NULL), (6, 3, NULL, NULL), (7, NULL, NULL, NULL), (8, 1, 2 ,3)")
tk.MustQuery("select count(distinct b, c, d) from t group by id").Check(testkit.Rows("0", "0", "0", "0", "0", "0", "0", "1"))

tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int primary key, b varchar(10))")
tk.MustExec("insert into t value(1, 11),(3, NULL)")
tk.MustQuery("SELECT a, MIN(b), MAX(b) FROM t GROUP BY a").Check(testkit.Rows("1 11 11", "3 <nil> <nil>"))
}

func (s *testSuite) TestGroupConcatAggr(c *C) {
Expand Down
9 changes: 9 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ func (s *testSuite) TestAdmin(c *C) {
tk.MustExec("alter table t1 add index idx_i(i);")
tk.MustExec("alter table t1 add index idx_m(a,c,d,e,f,g,h,i,j);")
tk.MustExec("admin check table t1;")

tk.MustExec("drop table if exists t1;")
tk.MustExec("CREATE TABLE t1 (c1 int);")
tk.MustExec("INSERT INTO t1 SET c1 = 1;")
tk.MustExec("ALTER TABLE t1 ADD COLUMN cc1 CHAR(36) NULL DEFAULT '';")
tk.MustExec("ALTER TABLE t1 ADD COLUMN cc2 VARCHAR(36) NULL DEFAULT ''")
tk.MustExec("ALTER TABLE t1 ADD INDEX idx1 (cc1);")
tk.MustExec("ALTER TABLE t1 ADD INDEX idx2 (cc2);")
tk.MustExec("admin check table t1;")
}

func (s *testSuite) fillData(tk *testkit.TestKit, table string) {
Expand Down
2 changes: 1 addition & 1 deletion planner/core/cbo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ func (s *testAnalyzeSuite) TestCorrelatedEstimation(c *C) {
tk.MustQuery("explain select t.c in (select count(*) from t s , t t1 where s.a = t.a and s.a = t1.a) from t;").
Check(testkit.Rows(
"Projection_11 10.00 root 9_aux_0",
"└─Apply_13 10.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, count(*))]",
"└─Apply_13 10.00 root left outer semi join, inner:StreamAgg_20, equal:[eq(test.t.c, 7_col_0)]",
" ├─TableReader_15 10.00 root data:TableScan_14",
" │ └─TableScan_14 10.00 cop table:t, range:[-inf,+inf], keep order:false",
" └─StreamAgg_20 1.00 root funcs:count(1)",
Expand Down
6 changes: 3 additions & 3 deletions planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1536,11 +1536,11 @@ func (s *testPlanSuite) TestAggPrune(c *C) {
},
{
sql: "select tt.a, sum(tt.b) from (select a, b from t) tt group by tt.a",
best: "DataScan(t)->Projection->Projection->Projection",
best: "DataScan(t)->Projection->Projection",
},
{
sql: "select count(1) from (select count(1), a as b from t group by a) tt group by b",
best: "DataScan(t)->Projection->Projection->Projection->Projection",
best: "DataScan(t)->Projection->Projection",
},
}
for _, tt := range tests {
Expand All @@ -1551,7 +1551,7 @@ func (s *testPlanSuite) TestAggPrune(c *C) {
p, err := BuildLogicalPlan(s.ctx, stmt, s.is)
c.Assert(err, IsNil)

p, err = logicalOptimize(flagPredicatePushDown|flagPrunColumns|flagBuildKeyInfo|flagEliminateAgg, p.(LogicalPlan))
p, err = logicalOptimize(flagPredicatePushDown|flagPrunColumns|flagBuildKeyInfo|flagEliminateAgg|flagEliminateProjection, p.(LogicalPlan))
c.Assert(err, IsNil)
c.Assert(ToString(p), Equals, tt.best, comment)
}
Expand Down
4 changes: 2 additions & 2 deletions planner/core/optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ var AllowCartesianProduct = true

const (
flagPrunColumns uint64 = 1 << iota
flagEliminateProjection
flagBuildKeyInfo
flagDecorrelate
flagEliminateAgg
flagEliminateProjection
flagMaxMinEliminate
flagPredicatePushDown
flagPartitionProcessor
Expand All @@ -46,10 +46,10 @@ const (

var optRuleList = []logicalOptRule{
&columnPruner{},
&projectionEliminater{},
&buildKeySolver{},
&decorrelateSolver{},
&aggregationEliminator{},
&projectionEliminater{},
&maxMinEliminator{},
&ppdSolver{},
&partitionProcessor{},
Expand Down
38 changes: 38 additions & 0 deletions types/datum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package types

import (
"reflect"
"testing"
"time"

. "github.com/pingcap/check"
Expand Down Expand Up @@ -362,3 +364,39 @@ func (ts *testDatumSuite) TestCopyDatum(c *C) {
}
}
}

func prepareCompareDatums() ([]Datum, []Datum) {
vals := make([]Datum, 0, 5)
vals = append(vals, NewIntDatum(1))
vals = append(vals, NewFloat64Datum(1.23))
vals = append(vals, NewStringDatum("abcde"))
vals = append(vals, NewDecimalDatum(NewDecFromStringForTest("1.2345")))
vals = append(vals, NewTimeDatum(Time{Time: FromGoTime(time.Date(2018, 3, 8, 16, 1, 0, 315313000, time.UTC)), Fsp: 6, Type: mysql.TypeTimestamp}))

vals1 := make([]Datum, 0, 5)
vals1 = append(vals1, NewIntDatum(1))
vals1 = append(vals1, NewFloat64Datum(1.23))
vals1 = append(vals1, NewStringDatum("abcde"))
vals1 = append(vals1, NewDecimalDatum(NewDecFromStringForTest("1.2345")))
vals1 = append(vals1, NewTimeDatum(Time{Time: FromGoTime(time.Date(2018, 3, 8, 16, 1, 0, 315313000, time.UTC)), Fsp: 6, Type: mysql.TypeTimestamp}))
return vals, vals1
}

func BenchmarkCompareDatum(b *testing.B) {
vals, vals1 := prepareCompareDatums()
sc := new(stmtctx.StatementContext)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j, v := range vals {
v.CompareDatum(sc, &vals1[j])
}
}
}

func BenchmarkCompareDatumByReflect(b *testing.B) {
vals, vals1 := prepareCompareDatums()
b.ResetTimer()
for i := 0; i < b.N; i++ {
reflect.DeepEqual(vals, vals1)
}
}
18 changes: 6 additions & 12 deletions util/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package admin
import (
"fmt"
"io"
"reflect"
"sort"

"github.com/pingcap/tidb/expression"
Expand Down Expand Up @@ -353,7 +352,7 @@ func checkIndexAndRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table
if err != nil {
return errors.Trace(err)
}
sc := new(stmtctx.StatementContext)
sc := sessCtx.GetSessionVars().StmtCtx
for {
vals1, h, err := it.Next()
if terror.ErrorEqual(err, io.EOF) {
Expand Down Expand Up @@ -391,15 +390,9 @@ func compareDatumSlice(sc *stmtctx.StatementContext, val1s, val2s []types.Datum)
return false
}
for i, v := range val1s {
if v.Kind() == types.KindMysqlDecimal {
res, err := v.CompareDatum(sc, &val2s[i])
if err != nil || res != 0 {
return false
}
} else {
if !reflect.DeepEqual(v, val2s[i]) {
return false
}
res, err := v.CompareDatum(sc, &val2s[i])
if err != nil || res != 0 {
return false
}
}
return true
Expand Down Expand Up @@ -523,6 +516,7 @@ func CompareTableRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table.
}

startKey := t.RecordKey(0)
sc := sessCtx.GetSessionVars().StmtCtx
filterFunc := func(h int64, vals []types.Datum, cols []*table.Column) (bool, error) {
vals2, ok := m[h]
if !ok {
Expand All @@ -534,7 +528,7 @@ func CompareTableRecord(sessCtx sessionctx.Context, txn kv.Transaction, t table.
return true, nil
}

if !reflect.DeepEqual(vals, vals2) {
if !compareDatumSlice(sc, vals, vals2) {
record1 := &RecordData{Handle: h, Values: vals2}
record2 := &RecordData{Handle: h, Values: vals}
return false, ErrDataInConsistent.GenWithStack("data:%#v != record:%#v", record1, record2)
Expand Down

0 comments on commit e9dbe95

Please sign in to comment.