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

Open
wants to merge 3 commits into
base: master
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
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 @@ -247,3 +247,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)`)
}
37 changes: 24 additions & 13 deletions pkg/planner/core/physical_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,16 +1059,28 @@ 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
// All columns with negative cid should be placed at last.
if cid < 0 {
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we set break to continue? Maybe issue #56013 could be resolved at the same time.

}
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 @@ -1083,10 +1095,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