dolt icon indicating copy to clipboard operation
dolt copied to clipboard

subquery insert into not null column throws error

Open jycor opened this issue 1 year ago • 0 comments

repro

CREATE TABLE test (
  pk bigint NOT NULL,
  c1 bigint,
  c2 bigint,
  c3 bigint,
  c4 bigint,
  c5 bigint,
  PRIMARY KEY (pk)
);

CREATE TABLE test2 (
  pk bigint NOT NULL,
  c1 bigint NOT NULL,
  PRIMARY KEY (pk)
);

INSERT INTO TEST VALUES (1,1,1,1,1,1);
INSERT INTO TEST2 VALUES (2,2);

-- this fails with column name 'c1' is non-nullable but attempted to set a value of null
INSERT INTO TEST2 VALUES (1, (SELECT c1 FROM TEST WHERE c1=1)); 

Comes from this function in gms/sql/rowexec/insert.go

func (i *insertIter) validateNullability(ctx *sql.Context, dstSchema sql.Schema, row sql.Row) error {
	for count, col := range dstSchema {
		if !col.Nullable && row[count] == nil {
			// 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()
				_ = warnOnIgnorableError(ctx, row, sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name)) // will always return nil
			} else {
				return sql.ErrInsertIntoNonNullableProvidedNull.New(col.Name) // HERE
			}
		}
	}
	return nil
}

It is possible that this was always broken, and we were just not returning the error.

jycor avatar Oct 13 '23 23:10 jycor