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

planner/core: move all extra columns to the end in ExpandVirtualColumn (#58499) #58583

Open
wants to merge 1 commit into
base: release-7.5
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
4 changes: 4 additions & 0 deletions pkg/planner/core/casetest/partition/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ go_test(
],
data = glob(["testdata/**"]),
flaky = True,
<<<<<<< HEAD
shard_count = 6,
=======
shard_count = 10,
>>>>>>> b11cce72848 (planner/core: move all extra columns to the end in ExpandVirtualColumn (#58499))
deps = [
"//pkg/config",
"//pkg/planner/core/internal",
Expand Down
20 changes: 20 additions & 0 deletions pkg/planner/core/casetest/partition/integration_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,23 @@ func TestBatchPointGetPartitionForAccessObject(t *testing.T) {
tk.MustQuery(tt).Check(testkit.Rows(output[i].Plan...))
}
}

// Issue 58475
func TestGeneratedColumnWithPartition(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tk.MustExec(`
CREATE TABLE tp (
id int,
c1 int,
c2 int GENERATED ALWAYS AS (c1) VIRTUAL,
KEY idx (id)
) PARTITION BY RANGE (id)
(PARTITION p0 VALUES LESS THAN (0),
PARTITION p1 VALUES LESS THAN (10000))
`)
tk.MustExec(`INSERT INTO tp (id, c1) VALUES (0, 1)`)
tk.MustExec(`select /*+ FORCE_INDEX(tp, idx) */id from tp where c2 = 2 group by id having id in (0)`)
}
39 changes: 26 additions & 13 deletions pkg/planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,16 +975,30 @@ func (ts *PhysicalTableScan) ResolveCorrelatedColumns() ([]*ranger.Range, error)
// ExpandVirtualColumn expands the virtual column's dependent columns to ts's schema and column.
func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,
colsInfo []*model.ColumnInfo) []*model.ColumnInfo {
copyColumn := make([]*model.ColumnInfo, len(columns))
copy(copyColumn, columns)
var extraColumn *expression.Column
var extraColumnModel *model.ColumnInfo
if schema.Columns[len(schema.Columns)-1].ID == model.ExtraHandleID {
extraColumn = schema.Columns[len(schema.Columns)-1]
extraColumnModel = copyColumn[len(copyColumn)-1]
schema.Columns = schema.Columns[:len(schema.Columns)-1]
copyColumn = copyColumn[:len(copyColumn)-1]
copyColumn := make([]*model.ColumnInfo, 0, len(columns))
copyColumn = append(copyColumn, columns...)

oldNumColumns := len(schema.Columns)
numExtraColumns := 0
for i := oldNumColumns - 1; i >= 0; i-- {
cid := schema.Columns[i].ID
// Move extra columns to the end.
// ExtraRowChecksumID is ignored here since it's treated as an ordinary column.
// https://github.com/pingcap/tidb/blob/3c407312a986327bc4876920e70fdd6841b8365f/pkg/util/rowcodec/decoder.go#L206-L222
if cid != model.ExtraHandleID && cid != model.ExtraPhysTblID {
break
}
numExtraColumns++
}

extraColumns := make([]*expression.Column, numExtraColumns)
copy(extraColumns, schema.Columns[oldNumColumns-numExtraColumns:])
schema.Columns = schema.Columns[:oldNumColumns-numExtraColumns]

extraColumnModels := make([]*model.ColumnInfo, numExtraColumns)
copy(extraColumnModels, copyColumn[len(copyColumn)-numExtraColumns:])
copyColumn = copyColumn[:len(copyColumn)-numExtraColumns]

schemaColumns := schema.Columns
for _, col := range schemaColumns {
if col.VirtualExpr == nil {
Expand All @@ -999,10 +1013,9 @@ func ExpandVirtualColumn(columns []*model.ColumnInfo, schema *expression.Schema,
}
}
}
if extraColumn != nil {
schema.Columns = append(schema.Columns, extraColumn)
copyColumn = append(copyColumn, extraColumnModel) // nozero
}

schema.Columns = append(schema.Columns, extraColumns...)
copyColumn = append(copyColumn, extraColumnModels...)
return copyColumn
}

Expand Down