Skip to content

Commit

Permalink
sql: fix panic when evaluating check constraint during insert fast path
Browse files Browse the repository at this point in the history
Fix panic due to out of bounds error when intercepting error message in
insert_fast_path.go.

No release note since the panic is not in prod.

Release note: None
  • Loading branch information
RichardJCai committed Jul 23, 2020
1 parent 89dda79 commit 3d0af2e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pkg/sql/insert_fast_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,12 @@ func (n *insertFastPathNode) enableAutoCommit() {
func interceptAlterColumnTypeParseError(
insertCols []sqlbase.ColumnDescriptor, colNum int, err error,
) error {
// Only intercept the error if the column being inserted into
// is an actual column. This is to avoid checking on values that don't
// correspond to an actual column, for example a check constraint.
if colNum >= len(insertCols) {
return err
}
var insertCol sqlbase.ColumnDescriptor

// wrapParseError is a helper function that checks if an insertCol has the
Expand Down
8 changes: 8 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/check_constraints
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,11 @@ CREATE TABLE t46675isnotnull (k int, a int, CHECK ((k, a) IS NOT NULL))
# value.
statement error pgcode 23514 pq: failed to satisfy CHECK constraint \(\(k, a\) IS NOT NULL\)
INSERT INTO t46675isnotnull VALUES (1, NULL)

# Regression test for #51690. Make sure we don't panic when a check constraint
# errors during an insert using the fast path.
statement ok
CREATE TABLE t51690(x INT, y INT, CHECK(x / y = 1));

statement error pq: division by zero
INSERT INTO t51690 VALUES (1, 0)

0 comments on commit 3d0af2e

Please sign in to comment.