Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 7e11c50

Browse files
committed
Fixes for column duplicates and existence
Signed-off-by: Daylon Wilkins <[email protected]>
1 parent b554a79 commit 7e11c50

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

engine_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,11 +1908,21 @@ func TestInsertIntoErrors(t *testing.T) {
19081908
"too many values no columns specified",
19091909
"INSERT INTO mytable VALUES (999, 'x', 'y');",
19101910
},
1911+
{
1912+
"non-existent column",
1913+
"INSERT INTO mytable (i, s, z) VALUES (999, 'x', 999);",
1914+
},
1915+
{
1916+
"duplicate column",
1917+
"INSERT INTO mytable (i, s, s) VALUES (999, 'x', 'x');",
1918+
},
19111919
}
19121920

19131921
for _, expectedFailure := range expectedFailures {
1914-
_, _, err := newEngine(t).Query(newCtx(), expectedFailure.query)
1915-
require.Error(t, err)
1922+
t.Run(expectedFailure.name, func(t *testing.T) {
1923+
_, _, err := newEngine(t).Query(newCtx(), expectedFailure.query)
1924+
require.Error(t, err)
1925+
})
19161926
}
19171927
}
19181928

sql/plan/insert.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ var ErrInsertIntoNotSupported = errors.NewKind("table doesn't support INSERT INT
1414
var ErrInsertIntoMismatchValueCount =
1515
errors.NewKind("number of values does not match number of columns provided")
1616
var ErrInsertIntoUnsupportedValues = errors.NewKind("%T is unsupported for inserts")
17+
var ErrInsertIntoDuplicateColumn = errors.NewKind("duplicate column name %v")
18+
var ErrInsertIntoNonexistentColumn = errors.NewKind("invalid column name %v")
1719

1820
// InsertInto is a node describing the insertion into some table.
1921
type InsertInto struct {
@@ -77,6 +79,11 @@ func (p *InsertInto) Execute(ctx *sql.Context) (int, error) {
7779
for i, f := range dstSchema {
7880
p.Columns[i] = f.Name
7981
}
82+
} else {
83+
err = p.validateColumns(ctx, dstSchema)
84+
if err != nil {
85+
return 0, err
86+
}
8087
}
8188

8289
err = p.validateValueCount(ctx)
@@ -168,3 +175,22 @@ func (p *InsertInto) validateValueCount(ctx *sql.Context) error {
168175
}
169176
return nil
170177
}
178+
179+
func (p *InsertInto) validateColumns(ctx *sql.Context, dstSchema sql.Schema) error {
180+
dstColNames := make(map[string]struct{})
181+
for _, dstCol := range dstSchema {
182+
dstColNames[dstCol.Name] = struct{}{}
183+
}
184+
columnNames := make(map[string]struct{})
185+
for _, columnName := range p.Columns {
186+
if _, exists := dstColNames[columnName]; !exists {
187+
return ErrInsertIntoNonexistentColumn.New(columnName)
188+
}
189+
if _, exists := columnNames[columnName]; !exists {
190+
columnNames[columnName] = struct{}{}
191+
} else {
192+
return ErrInsertIntoDuplicateColumn.New(columnName)
193+
}
194+
}
195+
return nil
196+
}

0 commit comments

Comments
 (0)