Skip to content

Commit

Permalink
Merge branch 'master' into temp_table_scan_reader
Browse files Browse the repository at this point in the history
  • Loading branch information
lcwangchao authored Jul 20, 2021
2 parents 733aa2f + 0c7f9ac commit e569e42
Show file tree
Hide file tree
Showing 29 changed files with 214 additions and 177 deletions.
6 changes: 3 additions & 3 deletions cmd/explaintest/r/explain_generate_column_substitute.result
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ a b c e
desc select * from t order by a+1;
id estRows task access object operator info
Projection_5 10000.00 root test.t.a, test.t.b, test.t.c, test.t.e
└─Projection_13 10000.00 root test.t.a, test.t.b, test.t.c, test.t.e, EMPTY_NAME
└─Projection_13 10000.00 root test.t.a, test.t.b, test.t.c, test.t.e, plus(test.t.a, 1)
└─IndexLookUp_12 10000.00 root
├─IndexFullScan_10(Build) 10000.00 cop[tikv] table:t, index:expr_idx_c(`a` + 1) keep order:true, stats:pseudo
└─TableRowIDScan_11(Probe) 10000.00 cop[tikv] table:t keep order:false, stats:pseudo
Expand Down Expand Up @@ -432,12 +432,12 @@ Projection 0.00 root test.t.a, test.t.b
└─TableRowIDScan(Probe) 0.00 cop[tikv] table:t keep order:false
desc format = 'brief' select count(upper(b)) from t group by upper(b);
id estRows task access object operator info
StreamAgg 4.80 root group by:EMPTY_NAME, funcs:count(EMPTY_NAME)->Column#7
StreamAgg 4.80 root group by:upper(test.t.b), funcs:count(upper(test.t.b))->Column#7
└─IndexReader 6.00 root index:IndexFullScan
└─IndexFullScan 6.00 cop[tikv] table:t, index:expression_index_2(upper(`b`)) keep order:true
desc format = 'brief' select max(upper(b)) from t group by upper(b);
id estRows task access object operator info
StreamAgg 4.80 root group by:EMPTY_NAME, funcs:max(EMPTY_NAME)->Column#7
StreamAgg 4.80 root group by:upper(test.t.b), funcs:max(upper(test.t.b))->Column#7
└─IndexReader 6.00 root index:IndexFullScan
└─IndexFullScan 6.00 cop[tikv] table:t, index:expression_index_2(upper(`b`)) keep order:true
desc format = 'brief' select count(upper(b)) from t use index() group by upper(b);
Expand Down
5 changes: 5 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2349,6 +2349,11 @@ func (s *testSerialDBSuite1) TestCreateExpressionIndexError(c *C) {

tk.MustGetErrCode("create table t1 (col1 int, index ((concat(''))));", errno.ErrWrongKeyColumnFunctionalIndex)
tk.MustGetErrCode("CREATE TABLE t1 (col1 INT, PRIMARY KEY ((ABS(col1))) NONCLUSTERED);", errno.ErrFunctionalIndexPrimaryKey)

// For issue 26349
tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(id char(10) primary key, short_name char(10), name char(10), key n((upper(`name`))));")
tk.MustExec("update t t1 set t1.short_name='a' where t1.id='1';")
}

func (s *testSerialDBSuite1) TestAddExpressionIndexOnPartition(c *C) {
Expand Down
2 changes: 1 addition & 1 deletion ddl/placement/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"fmt"
"strings"

"github.com/go-yaml/yaml"
"gopkg.in/yaml.v2"
)

// PeerRoleType is the expected peer type of the placement rule.
Expand Down
6 changes: 3 additions & 3 deletions executor/infoschema_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,13 @@ func (s *testInfoschemaTableSuite) TestUserPrivilegesTable(c *C) {
tk.MustQuery(`SELECT * FROM information_schema.user_privileges WHERE grantee="'usageuser'@'%'"`).Check(testkit.Rows("'usageuser'@'%' def USAGE NO"))
// the usage row disappears when there is a non-dynamic privilege added
tk1.MustExec("GRANT SELECT ON *.* to usageuser")
tk.MustQuery(`SELECT * FROM information_schema.user_privileges WHERE grantee="'usageuser'@'%'"`).Check(testkit.Rows("'usageuser'@'%' def Select NO"))
tk.MustQuery(`SELECT * FROM information_schema.user_privileges WHERE grantee="'usageuser'@'%'"`).Check(testkit.Rows("'usageuser'@'%' def SELECT NO"))
// test grant privilege
tk1.MustExec("GRANT SELECT ON *.* to usageuser WITH GRANT OPTION")
tk.MustQuery(`SELECT * FROM information_schema.user_privileges WHERE grantee="'usageuser'@'%'"`).Check(testkit.Rows("'usageuser'@'%' def Select YES"))
tk.MustQuery(`SELECT * FROM information_schema.user_privileges WHERE grantee="'usageuser'@'%'"`).Check(testkit.Rows("'usageuser'@'%' def SELECT YES"))
// test DYNAMIC privs
tk1.MustExec("GRANT BACKUP_ADMIN ON *.* to usageuser")
tk.MustQuery(`SELECT * FROM information_schema.user_privileges WHERE grantee="'usageuser'@'%'" ORDER BY privilege_type`).Check(testkit.Rows("'usageuser'@'%' def BACKUP_ADMIN NO", "'usageuser'@'%' def Select YES"))
tk.MustQuery(`SELECT * FROM information_schema.user_privileges WHERE grantee="'usageuser'@'%'" ORDER BY privilege_type`).Check(testkit.Rows("'usageuser'@'%' def BACKUP_ADMIN NO", "'usageuser'@'%' def SELECT YES"))
}

func (s *testInfoschemaTableSerialSuite) TestDataForTableStatsField(c *C) {
Expand Down
4 changes: 4 additions & 0 deletions expression/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,10 @@ const columnPrefix = "Column#"

// String implements Stringer interface.
func (col *Column) String() string {
if col.IsHidden {
// A hidden column must be a virtual generated column, we should output its expression.
return col.VirtualExpr.String()
}
if col.OrigName != "" {
return col.OrigName
}
Expand Down
5 changes: 4 additions & 1 deletion expression/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,10 @@ func scalarExprSupportedByFlash(function *ScalarFunction) bool {
}
case ast.Cast:
switch function.Function.PbCode() {
case tipb.ScalarFuncSig_CastIntAsInt, tipb.ScalarFuncSig_CastIntAsReal, tipb.ScalarFuncSig_CastIntAsDecimal, tipb.ScalarFuncSig_CastIntAsString, tipb.ScalarFuncSig_CastIntAsTime,
case tipb.ScalarFuncSig_CastIntAsTime:
// ban the function of casting year type as time type pushing down to tiflash because of https://github.com/pingcap/tidb/issues/26215
return function.GetArgs()[0].GetType().Tp != mysql.TypeYear
case tipb.ScalarFuncSig_CastIntAsInt, tipb.ScalarFuncSig_CastIntAsReal, tipb.ScalarFuncSig_CastIntAsDecimal, tipb.ScalarFuncSig_CastIntAsString,
tipb.ScalarFuncSig_CastRealAsInt, tipb.ScalarFuncSig_CastRealAsReal, tipb.ScalarFuncSig_CastRealAsDecimal, tipb.ScalarFuncSig_CastRealAsString, tipb.ScalarFuncSig_CastRealAsTime,
tipb.ScalarFuncSig_CastStringAsInt, tipb.ScalarFuncSig_CastStringAsReal, tipb.ScalarFuncSig_CastStringAsDecimal, tipb.ScalarFuncSig_CastStringAsString, tipb.ScalarFuncSig_CastStringAsTime,
tipb.ScalarFuncSig_CastDecimalAsInt /*, tipb.ScalarFuncSig_CastDecimalAsReal*/, tipb.ScalarFuncSig_CastDecimalAsDecimal, tipb.ScalarFuncSig_CastDecimalAsString, tipb.ScalarFuncSig_CastDecimalAsTime,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/fatih/color v1.10.0 // indirect
github.com/fsouza/fake-gcs-server v1.17.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/go-yaml/yaml v2.1.0+incompatible
github.com/gogo/protobuf v1.3.2
github.com/golang/protobuf v1.3.4
github.com/golang/snappy v0.0.2-0.20190904063534-ff6b7dc882cf
Expand Down Expand Up @@ -78,6 +77,7 @@ require (
golang.org/x/tools v0.1.4
google.golang.org/grpc v1.27.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0
modernc.org/mathutil v1.2.2 // indirect
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,6 @@ github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/goccy/go-graphviz v0.0.5/go.mod h1:wXVsXxmyMQU6TN3zGRttjNn3h+iCAS7xQFC6TlNvLhk=
github.com/gogo/protobuf v0.0.0-20171007142547-342cbe0a0415/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v0.0.0-20180717141946-636bf0302bc9/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
Expand Down
6 changes: 3 additions & 3 deletions planner/core/exhaust_physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ func (p *LogicalJoin) constructInnerTableScanTask(
// TableScan as inner child of IndexJoin can return at most 1 tuple for each outer row.
RowCount: math.Min(1.0, countAfterAccess),
StatsVersion: ds.stats.StatsVersion,
// Cardinality would not be used in cost computation of IndexJoin, set leave it as default nil.
// NDV would not be used in cost computation of IndexJoin, set leave it as default nil.
}
rowSize := ds.TblColHists.GetTableAvgRowSize(p.ctx, ds.TblCols, ts.StoreType, true)
sessVars := ds.ctx.GetSessionVars()
Expand Down Expand Up @@ -1432,7 +1432,7 @@ func (ijHelper *indexJoinBuildHelper) updateBestChoice(ranges []*ranger.Range, p
}
var innerNDV float64
if stats := ijHelper.innerPlan.statsInfo(); stats != nil && stats.StatsVersion != statistics.PseudoVersion {
innerNDV = getCardinality(path.IdxCols[:usedColsLen], ijHelper.innerPlan.Schema(), stats)
innerNDV = getColsNDV(path.IdxCols[:usedColsLen], ijHelper.innerPlan.Schema(), stats)
}
// We choose the index by the NDV of the used columns, the larger the better.
// If NDVs are same, we choose index which uses more columns.
Expand Down Expand Up @@ -2141,7 +2141,7 @@ func (la *LogicalApply) exhaustPhysicalPlans(prop *property.PhysicalProperty) ([
}
cacheHitRatio := 0.0
if la.stats.RowCount != 0 {
ndv := getCardinality(columns, la.schema, la.stats)
ndv := getColsNDV(columns, la.schema, la.stats)
// for example, if there are 100 rows and the number of distinct values of these correlated columns
// are 70, then we can assume 30 rows can hit the cache so the cache hit ratio is 1 - (70/100) = 0.3
cacheHitRatio = 1 - (ndv / la.stats.RowCount)
Expand Down
10 changes: 10 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3970,3 +3970,13 @@ func (s *testIntegrationSerialSuite) TestSelectIgnoreTemporaryTableInView(c *C)
tk.MustQuery("select * from v5").Check(testkit.Rows("1 2", "3 4"))

}

func (s *testIntegrationSerialSuite) TestIssue26250(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table tp (id int primary key) partition by range (id) (partition p0 values less than (100));")
tk.MustExec("create table tn (id int primary key);")
tk.MustExec("insert into tp values(1),(2);")
tk.MustExec("insert into tn values(1),(2);")
tk.MustQuery("select * from tp,tn where tp.id=tn.id and tn.id=1 for update;").Check(testkit.Rows("1 1"))
}
7 changes: 3 additions & 4 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3773,7 +3773,7 @@ func (b *PlanBuilder) tryBuildCTE(ctx context.Context, tn *ast.TableName, asName
}

cte.recursiveRef = true
p := LogicalCTETable{name: cte.def.Name.String(), idForStorage: cte.storageID, seedPlan: cte.seedLP}.Init(b.ctx, b.getSelectOffset())
p := LogicalCTETable{name: cte.def.Name.String(), idForStorage: cte.storageID, seedStat: cte.seedStat}.Init(b.ctx, b.getSelectOffset())
p.SetSchema(getResultCTESchema(cte.seedLP.Schema(), b.ctx.GetSessionVars()))
p.SetOutputNames(cte.seedLP.OutputNames())
return p, nil
Expand Down Expand Up @@ -3801,7 +3801,7 @@ func (b *PlanBuilder) tryBuildCTE(ctx context.Context, tn *ast.TableName, asName
lp := LogicalCTE{cteAsName: tn.Name, cte: &CTEClass{IsDistinct: cte.isDistinct, seedPartLogicalPlan: cte.seedLP,
recursivePartLogicalPlan: cte.recurLP, IDForStorage: cte.storageID,
optFlag: cte.optFlag, HasLimit: hasLimit, LimitBeg: limitBeg,
LimitEnd: limitEnd}}.Init(b.ctx, b.getSelectOffset())
LimitEnd: limitEnd}, seedStat: cte.seedStat}.Init(b.ctx, b.getSelectOffset())
lp.SetSchema(getResultCTESchema(cte.seedLP.Schema(), b.ctx.GetSessionVars()))
p = lp
p.SetOutputNames(cte.seedLP.OutputNames())
Expand Down Expand Up @@ -4025,7 +4025,6 @@ func (b *PlanBuilder) buildDataSource(ctx context.Context, tn *ast.TableName, as
ColName: col.Name,
OrigTblName: tableInfo.Name,
OrigColName: col.Name,
Hidden: col.Hidden,
// For update statement and delete statement, internal version should see the special middle state column, while user doesn't.
NotExplicitUsable: col.State != model.StatePublic,
})
Expand Down Expand Up @@ -6228,7 +6227,7 @@ func (b *PlanBuilder) buildWith(ctx context.Context, w *ast.WithClause) error {
nameMap[cte.Name.L] = struct{}{}
}
for _, cte := range w.CTEs {
b.outerCTEs = append(b.outerCTEs, &cteInfo{def: cte, nonRecursive: !w.IsRecursive, isBuilding: true, storageID: b.allocIDForCTEStorage})
b.outerCTEs = append(b.outerCTEs, &cteInfo{def: cte, nonRecursive: !w.IsRecursive, isBuilding: true, storageID: b.allocIDForCTEStorage, seedStat: &property.StatsInfo{}})
b.allocIDForCTEStorage++
saveFlag := b.optFlag
// Init the flag to flagPrunColumns, otherwise it's missing.
Expand Down
3 changes: 2 additions & 1 deletion planner/core/logical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,13 +1216,14 @@ type LogicalCTE struct {

cte *CTEClass
cteAsName model.CIStr
seedStat *property.StatsInfo
}

// LogicalCTETable is for CTE table
type LogicalCTETable struct {
logicalSchemaProducer

seedPlan LogicalPlan
seedStat *property.StatsInfo
name string
idForStorage int
}
Expand Down
4 changes: 2 additions & 2 deletions planner/core/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func optimizeByShuffle4Window(pp *PhysicalWindow, ctx sessionctx.Context) *Physi
for _, item := range pp.PartitionBy {
partitionBy = append(partitionBy, item.Col)
}
NDV := int(getCardinality(partitionBy, dataSource.Schema(), dataSource.statsInfo()))
NDV := int(getColsNDV(partitionBy, dataSource.Schema(), dataSource.statsInfo()))
if NDV <= 1 {
return nil
}
Expand Down Expand Up @@ -167,7 +167,7 @@ func optimizeByShuffle4StreamAgg(pp *PhysicalStreamAgg, ctx sessionctx.Context)
partitionBy = append(partitionBy, col)
}
}
NDV := int(getCardinality(partitionBy, dataSource.Schema(), dataSource.statsInfo()))
NDV := int(getColsNDV(partitionBy, dataSource.Schema(), dataSource.statsInfo()))
if NDV <= 1 {
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ type cteInfo struct {
enterSubquery bool
recursiveRef bool
limitLP LogicalPlan
// seedStat is shared between logicalCTE and logicalCTETable.
seedStat *property.StatsInfo
}

// PlanBuilder builds Plan from an ast.Node.
Expand Down Expand Up @@ -1234,6 +1236,10 @@ func (b *PlanBuilder) buildSelectLock(src LogicalPlan, lock *ast.SelectLockInfo)
func addExtraPIDColumnToDataSource(p LogicalPlan, info *extraPIDInfo) error {
switch raw := p.(type) {
case *DataSource:
// Fix issue 26250, do not add extra pid column to normal table.
if raw.tableInfo.GetPartitionInfo() == nil {
return nil
}
raw.addExtraPIDColumn(info)
return nil
default:
Expand Down
1 change: 0 additions & 1 deletion planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ func (s *partitionProcessor) reconstructTableColNames(ds *DataSource) ([]*types.
ColName: colInfo.Name,
OrigTblName: ds.tableInfo.Name,
OrigColName: colInfo.Name,
Hidden: colInfo.Hidden,
})
continue
}
Expand Down
Loading

0 comments on commit e569e42

Please sign in to comment.