Skip to content

Commit 2d20078

Browse files
author
James Cor
committed
feedback
1 parent a98a0e9 commit 2d20078

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

sql/analyzer/inserts.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
8181
}
8282

8383
// The schema of the destination node and the underlying table differ subtly in terms of defaults
84-
var missingValFlags []bool
85-
project, firstGeneratedAutoIncRowIdx, missingValFlags, err := wrapRowSource(
84+
var deferredDefaults sql.FastIntSet
85+
project, firstGeneratedAutoIncRowIdx, deferredDefaults, err := wrapRowSource(
8686
ctx,
8787
source,
8888
insertable,
@@ -95,7 +95,7 @@ func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
9595

9696
return insert.WithSource(project).
9797
WithAutoIncrementIdx(firstGeneratedAutoIncRowIdx).
98-
WithMissingValFlags(missingValFlags),
98+
WithDeferredDefaults(deferredDefaults),
9999
transform.NewTree,
100100
nil
101101
})
@@ -128,9 +128,9 @@ func findColIdx(colName string, colNames []string) int {
128128
// wrapRowSource returns a projection that wraps the original row source so that its schema matches the full schema of
129129
// the underlying table in the same order. Also, returns an integer value that indicates when this row source will
130130
// result in an automatically generated value for an auto_increment column.
131-
func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, schema sql.Schema, columnNames []string) (sql.Node, int, []bool, error) {
131+
func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, schema sql.Schema, columnNames []string) (sql.Node, int, sql.FastIntSet, error) {
132132
projExprs := make([]sql.Expression, len(schema))
133-
missingVals := make([]bool, len(schema))
133+
deferredDefaults := sql.NewFastIntSet()
134134
firstGeneratedAutoIncRowIdx := -1
135135

136136
for i, col := range schema {
@@ -142,7 +142,7 @@ func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, s
142142
defaultExpr = col.Generated
143143
}
144144
if !col.Nullable && defaultExpr == nil && !col.AutoIncrement {
145-
missingVals[i] = true
145+
deferredDefaults.Add(i)
146146
}
147147

148148
var err error
@@ -163,7 +163,7 @@ func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, s
163163
}
164164
})
165165
if err != nil {
166-
return nil, -1, nil, err
166+
return nil, -1, sql.FastIntSet{}, err
167167
}
168168
projExprs[i] = def
169169
} else {
@@ -175,7 +175,7 @@ func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, s
175175
// wrap it in an AutoIncrement expression.
176176
ai, err := expression.NewAutoIncrement(ctx, destTbl, projExprs[i])
177177
if err != nil {
178-
return nil, -1, nil, err
178+
return nil, -1, sql.FastIntSet{}, err
179179
}
180180
projExprs[i] = ai
181181

@@ -218,7 +218,7 @@ func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, s
218218
// ColumnDefaultValue to create the UUID), then update the project to include the AutoUuid expression.
219219
newExpr, identity, err := insertAutoUuidExpression(ctx, columnDefaultValue, autoUuidCol)
220220
if err != nil {
221-
return nil, -1, nil, err
221+
return nil, -1, sql.FastIntSet{}, err
222222
}
223223
if identity == transform.NewTree {
224224
projExprs[autoUuidColIdx] = newExpr
@@ -229,12 +229,12 @@ func wrapRowSource(ctx *sql.Context, insertSource sql.Node, destTbl sql.Table, s
229229
// the AutoUuid expression to it.
230230
err := wrapAutoUuidInValuesTuples(ctx, autoUuidCol, insertSource, columnNames)
231231
if err != nil {
232-
return nil, -1, nil, err
232+
return nil, -1, sql.FastIntSet{}, err
233233
}
234234
}
235235
}
236236

237-
return plan.NewProject(projExprs, insertSource), firstGeneratedAutoIncRowIdx, missingVals, nil
237+
return plan.NewProject(projExprs, insertSource), firstGeneratedAutoIncRowIdx, deferredDefaults, nil
238238
}
239239

240240
// isZero returns true if the specified literal value |lit| has a value equal to 0.

sql/plan/insert.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ type InsertInto struct {
7373
// FirstGenerateAutoIncRowIdx is the index of the first row inserted that increments last_insert_id.
7474
FirstGeneratedAutoIncRowIdx int
7575

76-
// MissingValFlags marks which columns in the destination schema are expected to have default values.
77-
MissingValFlags []bool
76+
// DeferredDefaults marks which columns in the destination schema are expected to have default values.
77+
DeferredDefaults sql.FastIntSet
7878
}
7979

8080
var _ sql.Databaser = (*InsertInto)(nil)
@@ -204,11 +204,11 @@ func (ii *InsertInto) WithAutoIncrementIdx(firstGeneratedAutoIncRowIdx int) *Ins
204204
return &np
205205
}
206206

207-
// WithMissingValFlags sets the flags for the insert destination columns, which mark which of the columns are expected
207+
// WithDeferredDefaults sets the flags for the insert destination columns, which mark which of the columns are expected
208208
// to be filled with the DEFAULT or GENERATED value.
209-
func (ii *InsertInto) WithMissingValFlags(missingValFlags []bool) *InsertInto {
209+
func (ii *InsertInto) WithDeferredDefaults(deferredDefaults sql.FastIntSet) *InsertInto {
210210
np := *ii
211-
np.MissingValFlags = missingValFlags
211+
np.DeferredDefaults = deferredDefaults
212212
return &np
213213
}
214214

sql/rowexec/dml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (b *BaseBuilder) buildInsertInto(ctx *sql.Context, ii *plan.InsertInto, row
9090
ctx: ctx,
9191
ignore: ii.Ignore,
9292
firstGeneratedAutoIncRowIdx: ii.FirstGeneratedAutoIncRowIdx,
93-
missingValFlags: ii.MissingValFlags,
93+
deferredDefaults: ii.DeferredDefaults,
9494
}
9595

9696
var ed sql.EditOpenerCloser

sql/rowexec/insert.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type insertIter struct {
4646

4747
firstGeneratedAutoIncRowIdx int
4848

49-
missingValFlags []bool
49+
deferredDefaults sql.FastIntSet
5050
}
5151

5252
func getInsertExpressions(values sql.Node) []sql.Expression {
@@ -398,7 +398,7 @@ func (i *insertIter) validateNullability(ctx *sql.Context, dstSchema sql.Schema,
398398
if !col.Nullable && row[count] == nil {
399399
// In the case of an IGNORE we set the nil value to a default and add a warning
400400
if !i.ignore {
401-
if i.missingValFlags[count] {
401+
if i.deferredDefaults.Contains(count) {
402402
return sql.ErrInsertIntoNonNullableDefaultNullColumn.New(col.Name)
403403
}
404404
return sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)

0 commit comments

Comments
 (0)