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

Vinai/insert ignore into #381

Merged
merged 19 commits into from
Apr 29, 2021
Merged

Vinai/insert ignore into #381

merged 19 commits into from
Apr 29, 2021

Conversation

VinaiRachakonda
Copy link
Contributor

@VinaiRachakonda VinaiRachakonda commented Apr 22, 2021

This PR adds support for INSERT IGNORE INTO. It is currently missing functionality for ignoring typer errors. After scoping the type errors component of this it looked to a lot of work to capture all of them with the correct MySQL notation

I also noticed a lack of partition support in MySQL terms as well so I ignored that.

@VinaiRachakonda VinaiRachakonda changed the title [WIP] Vinai/insert ignore into Vinai/insert ignore into Apr 28, 2021
Copy link
Contributor

@Hydrocharged Hydrocharged left a comment

Choose a reason for hiding this comment

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

Looks good overall!

memory/table.go Outdated
@@ -1004,7 +1002,7 @@ func (t *Table) GetIndexes(ctx *sql.Context) ([]sql.Index, error) {
return append(indexes, nonPrimaryIndexes...), nil
}

// GetForeignKeys implements sql.ForeignKeyTable
// GetForeignKeys implements sql. gnKeyTable
Copy link
Contributor

Choose a reason for hiding this comment

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

Accidental deletion?

sql/errors.go Outdated
@@ -80,7 +80,8 @@ var (
ErrDuplicateAliasOrTable = errors.NewKind("Not unique table/alias: %s")

// ErrPrimaryKeyViolation is returned when a primary key constraint is violated
ErrPrimaryKeyViolation = errors.NewKind("duplicate primary key given")
// TODO: This should be a ErDupKey instead
Copy link
Contributor

Choose a reason for hiding this comment

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

ErrDuplicateEntry? Not sure what ErDupKey is, should write the whole name.

ignore := false
if strings.Contains(strings.ToLower(i.Ignore), "ignore") {
ignore = true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be a bool in vitess rather than matching on the string. Either ignore is there or it isn't.

Change that in vitess (should be super quick), or add a TODO and get this merged in first.

for {
if err := i.replacer.Insert(i.ctx, row); err != nil {
if !sql.ErrPrimaryKeyViolation.Is(err) && !sql.ErrUniqueKeyViolation.Is(err) {
return i.ignoreOrClose(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a thing. REPLACE IGNORE does not exist AFAIK. This should be reverted to the original logic.

@@ -209,6 +209,8 @@ func (a *accumulatorIter) Next() (sql.Row, error) {
row, err := a.iter.Next()
if err == io.EOF {
return sql.NewRow(a.updateRowHandler.okResult()), nil
} else if ErrInsertIgnore.Is(err) { // return err if its not an ignorable error
continue
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Move the err != nil up to make it an else if on this chain. Or you could move both of these inside of the if err != nil, returning the error if these two cases aren't matched.

// In the case of an IGNORE we set the nil value to a default and add a warning
if i.ignore {
row[count] = col.Type.Zero()
_ = i.handleErrorAndQuitOrIgnore(sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)) // will always return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like you're using this to add a warning? I'd write a comment stating as such. Also maybe break handleErrorAndQuitOrIgnore up and have a function that only warns on ignorable errors. It isn't immediately clear what calling a function that has quit or ignore and ignoring the returned variable is for.

Maybe name it warnOnIgnorableError or something.

{sql.OkResult{RowsAffected: 0}},
},
ExpectedWarning: mysql.ERSubqueryNo1Row,
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Throw in a case with a duplicate primary key but different c1 value, so like (4, 8) or such.

@VinaiRachakonda VinaiRachakonda merged commit c9e737f into master Apr 29, 2021
@Hydrocharged Hydrocharged deleted the vinai/INSERT-IGNORE-INTO branch December 8, 2021 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants