Skip to content

Commit 18a580a

Browse files
author
James Cor
committed
fix
1 parent ceaa26f commit 18a580a

File tree

5 files changed

+30
-87
lines changed

5 files changed

+30
-87
lines changed

sql/analyzer/inserts.go

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -90,65 +90,6 @@ func resolveInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Sc
9090
})
9191
}
9292

93-
func validateInsertRows(ctx *sql.Context, a *Analyzer, n sql.Node, scope *plan.Scope, sel RuleSelector, qFlags *sql.QueryFlags) (sql.Node, transform.TreeIdentity, error) {
94-
if _, ok := n.(*plan.TriggerExecutor); ok {
95-
return n, transform.SameTree, nil
96-
} else if _, ok := n.(*plan.CreateProcedure); ok {
97-
return n, transform.SameTree, nil
98-
}
99-
// We capture all INSERTs along the tree, such as those inside of block statements.
100-
var err error
101-
transform.Inspect(n, func(n sql.Node) bool {
102-
insert, ok := n.(*plan.InsertInto)
103-
if !ok {
104-
return true
105-
}
106-
107-
var insertable sql.InsertableTable
108-
table := getResolvedTable(insert.Destination)
109-
insertable, err = plan.GetInsertable(table)
110-
if err != nil {
111-
return false
112-
}
113-
114-
source := insert.Source
115-
dstSchema := insertable.Schema()
116-
117-
// normalize the column name
118-
columnNames := make([]string, len(insert.ColumnNames))
119-
for i, name := range insert.ColumnNames {
120-
columnNames[i] = strings.ToLower(name)
121-
}
122-
123-
// If no columns are given and value tuples are not all empty, use the full schema
124-
if len(columnNames) == 0 && existsNonZeroValueCount(source) {
125-
columnNames = make([]string, len(dstSchema))
126-
for i, f := range dstSchema {
127-
columnNames[i] = f.Name
128-
}
129-
}
130-
131-
for _, col := range dstSchema {
132-
colIdx := findColIdx(col.Name, columnNames)
133-
if colIdx != -1 {
134-
continue
135-
}
136-
137-
if _, isTrigExec := source.(*plan.TriggerExecutor); !isTrigExec && !col.AutoIncrement && !col.Nullable && col.Default == nil && col.Generated == nil {
138-
err = sql.ErrInsertIntoNonNullableDefaultNullColumn.New(col.Name)
139-
return false
140-
}
141-
}
142-
143-
return true
144-
})
145-
146-
if err != nil {
147-
return nil, transform.SameTree, err
148-
}
149-
return n, transform.SameTree, nil
150-
}
151-
15293
// Ensures that the number of elements in each Value tuple is empty
15394
func existsNonZeroValueCount(values sql.Node) bool {
15495
switch node := values.(type) {

sql/analyzer/rule_ids.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ const (
6161
applyHashInId // applyHashIn
6262
resolveInsertRowsId // resolveInsertRows
6363
applyTriggersId // applyTriggers
64-
validateInsertRowsId // validateInsertRows
6564
applyProceduresId // applyProcedures
6665
assignRoutinesId // assignRoutines
6766
modifyUpdateExprsForJoinId // modifyUpdateExprsForJoin

sql/analyzer/ruleid_string.go

Lines changed: 20 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sql/analyzer/rules.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func init() {
2020
// resolveInsertRows inserts a projection wrapping values that cannot be seen by fixup
2121
{resolveInsertRowsId, resolveInsertRows},
2222
{applyTriggersId, applyTriggers},
23-
{validateInsertRowsId, validateInsertRows},
2423
{applyProceduresId, applyProcedures},
2524
{inlineSubqueryAliasRefsId, inlineSubqueryAliasRefs},
2625
{cacheSubqueryAliasesInJoinsId, cacheSubqueryAliasesInJoins},

sql/rowexec/insert.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,19 @@ func (i *insertIter) evaluateChecks(ctx *sql.Context, row sql.Row) error {
393393

394394
func (i *insertIter) validateNullability(ctx *sql.Context, dstSchema sql.Schema, row sql.Row) error {
395395
for count, col := range dstSchema {
396-
if !col.Nullable && row[count] == nil {
396+
if row[count] != nil {
397+
continue
398+
}
399+
if !col.Nullable {
397400
// In the case of an IGNORE we set the nil value to a default and add a warning
398-
if i.ignore {
399-
row[count] = col.Type.Zero()
400-
_ = warnOnIgnorableError(ctx, row, sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)) // will always return nil
401-
} else {
401+
if !i.ignore {
402+
if col.Default != nil || col.Generated != nil {
403+
return sql.ErrInsertIntoNonNullableDefaultNullColumn.New(col.Name)
404+
}
402405
return sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)
403406
}
407+
row[count] = col.Type.Zero()
408+
_ = warnOnIgnorableError(ctx, row, sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)) // will always return nil
404409
}
405410
}
406411
return nil

0 commit comments

Comments
 (0)