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

sql: Check CHECK constraints on upsert when updating a conflicting row #26642

Merged

Conversation

emsal0
Copy link
Contributor

@emsal0 emsal0 commented Jun 12, 2018

  • Before: On a table with a CHECK constraint, such as:

    CREATE TABLE ex(foo INT PRIMARY KEY, bar INT, CHECK (bar < 2)
    

    an upsert on the table would not properly check the CHECK constraints,
    e.g.

    INSERT INTO ex(foo, bar) VALUES(1, 2);
    INSERT INTO ex(foo, bar) VALUES(1, 2) ON CONFLICT (foo) DO UPDATE SET
        bar = 3
    

    would update the row, violating the check constraint.

  • After: On attempting such an operation, CockroachDB now throws the
    proper database error "failed to satisfy CHECK constraint ..."

Release note (sql change): CHECK constraints are now checked when
updating a conflicting row in INSERT ... ON CONFLICT DO UPDATE
statements.

closes #23699

@emsal0 emsal0 requested review from jordanlewis, knz, BramGruneir and a team June 12, 2018 16:59
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@knz
Copy link
Contributor

knz commented Jun 12, 2018

Very nice. Pleasantly suprised how little time this took you. Congratulations.

Very good commit message too. :)

Some more tests needed, see below.


Reviewed 2 of 2 files at r1.
Review status: :shipit: complete! 0 of 0 LGTMs obtained


pkg/sql/logictest/testdata/logic_test/upsert, line 546 at r1 (raw file):


statement error pq: failed to satisfy CHECK constraint \(c > 1\)
INSERT INTO abc_check(a, b, c) VALUES (1, 0, 2) ON CONFLICT(a) DO UPDATE SET (b, c) = (-1, 1);

Right now your code is lucky that the insert column order is the same as the table column order. So this does not verify that column ordering is handled properly.

  • Add tests that insert into the table with a different column ordering.
  • Add tests that insert into the table with only a subset of the columns defined in the table.
  • Mix both effects.

Comments from Reviewable

@knz
Copy link
Contributor

knz commented Jun 12, 2018

Another thing: when you create a PR which, when merges, causes an existing issue to be resolved, write "Fixes #NNN." somewhere in the PR description. This will make github do the right thing automatically when the PR merges.


Review status: :shipit: complete! 0 of 0 LGTMs obtained


Comments from Reviewable

@emsal0
Copy link
Contributor Author

emsal0 commented Jun 12, 2018

are "closes" and "fixes" functionally equivalent?


Review status: :shipit: complete! 0 of 0 LGTMs obtained


pkg/sql/logictest/testdata/logic_test/upsert, line 546 at r1 (raw file):

Previously, knz (kena) wrote…

Right now your code is lucky that the insert column order is the same as the table column order. So this does not verify that column ordering is handled properly.

  • Add tests that insert into the table with a different column ordering.
  • Add tests that insert into the table with only a subset of the columns defined in the table.
  • Mix both effects.

Done


Comments from Reviewable

@BramGruneir
Copy link
Member

are "closes" and "fixes" functionally equivalent?

Yep.


Comments from Reviewable

@knz
Copy link
Contributor

knz commented Jun 12, 2018

:lgtm: very good.

As usual you'll need to squash this. This fix makes me happy, this was a high severity bug.

Also because this was a high severity bug, after this merges you'll need to back-port your fix to our 2.0 release branch. Please refer to the wiki for details (I think it's documented here). Do not forget about Nikhil's backport tool which mostly automates the process.


Reviewed 1 of 1 files at r2.
Review status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale)


Comments from Reviewable

* Before: On a table with a CHECK constraint, such as:

    ```
    CREATE TABLE ex(foo INT PRIMARY KEY, bar INT, CHECK (bar < 2)
    ```

    an upsert on the table would not properly check the CHECK constraints,
    e.g.

    ```
    INSERT INTO ex(foo, bar) VALUES(1, 2);
    INSERT INTO ex(foo, bar) VALUES(1, 2) ON CONFLICT (foo) DO UPDATE SET
        bar = 3
    ```

    would update the row, violating the check constraint.

* After: On attempting such an operation, CockroachDB now throws the
proper database error "failed to satisfy CHECK constraint ..."

Release note (sql change): CHECK constraints are now checked when
updating a conflicting row in INSERT ... ON CONFLICT DO UPDATE
statements.
@emsal0 emsal0 force-pushed the upsert-on-conflict-check-constraint branch from a478f39 to 8bd51b6 Compare June 13, 2018 13:39
@emsal0
Copy link
Contributor Author

emsal0 commented Jun 13, 2018

bors r+

craig bot pushed a commit that referenced this pull request Jun 13, 2018
26642: sql: Check CHECK constraints on upsert when updating a conflicting row r=emsal1863 a=emsal1863

* Before: On a table with a CHECK constraint, such as:

    ```
    CREATE TABLE ex(foo INT PRIMARY KEY, bar INT, CHECK (bar < 2)
    ```

    an upsert on the table would not properly check the CHECK constraints,
    e.g.

    ```
    INSERT INTO ex(foo, bar) VALUES(1, 2);
    INSERT INTO ex(foo, bar) VALUES(1, 2) ON CONFLICT (foo) DO UPDATE SET
        bar = 3
    ```

    would update the row, violating the check constraint.

* After: On attempting such an operation, CockroachDB now throws the
proper database error "failed to satisfy CHECK constraint ..."

Release note (sql change): CHECK constraints are now checked when
updating a conflicting row in INSERT ... ON CONFLICT DO UPDATE
statements.

closes #23699 

Co-authored-by: Emmanuel <[email protected]>
@craig
Copy link
Contributor

craig bot commented Jun 13, 2018

Build succeeded

@craig craig bot merged commit 8bd51b6 into cockroachdb:master Jun 13, 2018
craig bot pushed a commit that referenced this pull request Jun 13, 2018
26699: release-2.0: sql: Check CHECK constraints on upsert when updating a conflicting row r=emsal1863 a=emsal1863

Backport 1/1 commits from #26642.

/cc @cockroachdb/release

---

* Before: On a table with a CHECK constraint, such as:

    ```
    CREATE TABLE ex(foo INT PRIMARY KEY, bar INT, CHECK (bar < 2)
    ```

    an upsert on the table would not properly check the CHECK constraints,
    e.g.

    ```
    INSERT INTO ex(foo, bar) VALUES(1, 2);
    INSERT INTO ex(foo, bar) VALUES(1, 2) ON CONFLICT (foo) DO UPDATE SET
        bar = 3
    ```

    would update the row, violating the check constraint.

* After: On attempting such an operation, CockroachDB now throws the
proper database error "failed to satisfy CHECK constraint ..."

Release note (sql change): CHECK constraints are now checked when
updating a conflicting row in INSERT ... ON CONFLICT DO UPDATE
statements.

closes #23699 


Co-authored-by: Emmanuel <[email protected]>
@knz knz added the docs-todo label Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: the updated values on ON CONFLICT do not get constraint-checked properly
4 participants