Skip to content

Commit

Permalink
planner: fix cte-schema-clone will clone the old hashcode of its colu…
Browse files Browse the repository at this point in the history
…mn if any (#35415) (#35495)

close #35404
  • Loading branch information
chrysan authored Jun 19, 2022
1 parent 1a89dec commit 10f167d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cmd/explaintest/r/explain_cte.result
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,22 @@ CTE_1 8000.00 root Non-Recursive CTE
CTE_0 10000.00 root Non-Recursive CTE
└─TableReader(Seed Part) 10000.00 root data:TableFullScan
└─TableFullScan 10000.00 cop[tikv] table:t1 keep order:false, stats:pseudo
drop table if exists tbl;
create table tbl (id int);
explain with t1 as (select id from tbl), t2 as (select a.id from t1 a join t1 b on a.id = b.id) select * from t2 where id in (select id from t2);
id estRows task access object operator info
HashJoin_33 8000.00 root inner join, equal:[eq(test.tbl.id, test.tbl.id)]
├─HashAgg_37(Build) 5120.00 root group by:test.tbl.id, funcs:firstrow(test.tbl.id)->test.tbl.id
│ └─Selection_38 8000.00 root not(isnull(test.tbl.id))
│ └─CTEFullScan_39 10000.00 root CTE:t2 data:CTE_1
└─Selection_35(Probe) 8000.00 root not(isnull(test.tbl.id))
└─CTEFullScan_36 10000.00 root CTE:t2 data:CTE_1
CTE_1 10000.00 root Non-Recursive CTE
└─HashJoin_25(Seed Part) 10000.00 root inner join, equal:[eq(test.tbl.id, test.tbl.id)]
├─Selection_29(Build) 8000.00 root not(isnull(test.tbl.id))
│ └─CTEFullScan_30 10000.00 root CTE:b data:CTE_0
└─Selection_27(Probe) 8000.00 root not(isnull(test.tbl.id))
└─CTEFullScan_28 10000.00 root CTE:a data:CTE_0
CTE_0 10000.00 root Non-Recursive CTE
└─TableReader_22(Seed Part) 10000.00 root data:TableFullScan_21
└─TableFullScan_21 10000.00 cop[tikv] table:tbl keep order:false, stats:pseudo
5 changes: 5 additions & 0 deletions cmd/explaintest/t/explain_cte.test
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,8 @@ desc format='brief' with all_data as
select v1.tps v1_tps,v2.tps v2_tps
from version1 v1, version2 v2
where v1.bench_type =v2.bench_type;

# issue 35404
drop table if exists tbl;
create table tbl (id int);
explain with t1 as (select id from tbl), t2 as (select a.id from t1 a join t1 b on a.id = b.id) select * from t2 where id in (select id from t2);
5 changes: 5 additions & 0 deletions expression/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,11 @@ func (col *Column) HashCode(_ *stmtctx.StatementContext) []byte {
return col.hashcode
}

// CleanHashCode will clean the hashcode you may be cached before. It's used especially in schema-cloned & reallocated-uniqueID's cases.
func (col *Column) CleanHashCode() {
col.hashcode = make([]byte, 0, 9)
}

// ResolveIndices implements Expression interface.
func (col *Column) ResolveIndices(schema *Schema) (Expression, error) {
newCol := col.Clone()
Expand Down
2 changes: 2 additions & 0 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6889,6 +6889,8 @@ func getResultCTESchema(seedSchema *expression.Schema, svar *variable.SessionVar
col.RetType = col.RetType.Clone()
col.UniqueID = svar.AllocPlanColumnID()
col.RetType.DelFlag(mysql.NotNullFlag)
// Since you have reallocated unique id here, the old-cloned-cached hash code is not valid anymore.
col.CleanHashCode()
}
return res
}

0 comments on commit 10f167d

Please sign in to comment.