Skip to content

Commit 1cd1c1d

Browse files
committed
add map name to idx
1 parent b117ce6 commit 1cd1c1d

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

enginetest/queries/script_queries.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ var ScriptTests = []ScriptTest{
137137
{
138138
Query: `CREATE TABLE IF NOT EXISTS ost_user__cdata (
139139
PRIMARY KEY (user_id)
140-
) DEFAULT CHARSET=utf8 AS
141-
SELECT
142-
entry.object_id as user_id,
140+
) DEFAULT CHARSET=utf8 AS
141+
SELECT
142+
entry.object_id as user_id,
143143
MAX(IF(field.id='1',coalesce(ans.value_id, ans.value),NULL)) as email
144-
FROM ost_form_entry entry
145-
JOIN ost_form_entry_values ans
146-
ON ans.entry_id = entry.id
147-
JOIN ost_form_field field
148-
ON field.id=ans.field_id
144+
FROM ost_form_entry entry
145+
JOIN ost_form_entry_values ans
146+
ON ans.entry_id = entry.id
147+
JOIN ost_form_field field
148+
ON field.id=ans.field_id
149149
WHERE entry.object_type='U' GROUP BY entry.object_id`,
150150
},
151151
{

sql/analyzer/resolve_create_select.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,27 @@ func resolveCreateSelect(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.
3838
newSch[i] = &tempCol
3939
}
4040

41+
colNameToIdx := make(map[string]int, len(newSch))
42+
for i, col := range newSch {
43+
colNameToIdx[col.Name] = i
44+
}
45+
4146
// Apply primary key constraints from index definitions to the merged schema
4247
var nonPkIndexes sql.IndexDefs
48+
pkOrdinals := make([]int, 0)
4349
for _, idx := range ct.Indexes() {
4450
if idx.IsPrimary() {
4551
for _, idxCol := range idx.Columns {
46-
for i, schCol := range newSch {
47-
if schCol.Name == idxCol.Name {
48-
newSch[i].PrimaryKey = true
49-
break
50-
}
52+
if i, ok := colNameToIdx[idxCol.Name]; ok {
53+
newSch[i].PrimaryKey = true
54+
pkOrdinals = append(pkOrdinals, i)
5155
}
5256
}
5357
} else {
5458
nonPkIndexes = append(nonPkIndexes, idx)
5559
}
5660
}
5761

58-
pkOrdinals := make([]int, 0)
59-
for i, col := range newSch {
60-
if col.PrimaryKey {
61-
pkOrdinals = append(pkOrdinals, i)
62-
}
63-
}
64-
6562
newSpec := &plan.TableSpec{
6663
Schema: sql.NewPrimaryKeySchema(newSch, pkOrdinals...),
6764
IdxDefs: nonPkIndexes, // Only pass non-PK indexes since PK is in schema

0 commit comments

Comments
 (0)