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

schemadiff: normalize PRIMARY KEY definition #12016

Merged

Conversation

shlomi-noach
Copy link
Contributor

@shlomi-noach shlomi-noach commented Dec 27, 2022

Description

In this PR we normalize and validate PRIMARY KEY definitions:

1. Normalize and validate CREATE TABLE

This statement:

create table t (id int primary key)

is normalized into:

create table t (id int, primary key(id))

Table definition is validated to have no more than one PRIMARY KEY.

2. Validate ADD COLUMN

In this statement:

alter table t add column i int primary key

We validate that the table does not already have a PRIMARY KEY. Once applied, the table definition is normalized as per the above.

3. Validate MODIFY COLUMN

In this statement:

alter table t modify column i int primary key

We validate that the table either:

  • does not already have a PRIMARY KEY, or
  • already has a PRIMARY KEY (i), ie no change to the PRIMARY KEY itself.

Once applied, the table definition is normalized as per the above.

4. Table diffs change

The diff of these two tables:

create table t (id int primary key);
create table t (id bigint primary key);

is:

alter table t modify column id bigint

Note that there is nothing to say that id is a PRIMARY KEY even though the original definitions included that information as part of column definition options. The reason is that internally the tables are normalized as per (1) above.


Several tests are added to validate all the above. Otherwise there's been a massive impact to unit tests because we used the id int primary key generously throughout the tests, and had to rewrite dozens of those.

Related Issue(s)

tracking: #10203

This work is needed for better foreign key support in schemadiff: it needs to validate that a parent table referenced by a foreign key, has the appropriate indexes over the referenced columns. It's then important to have the PRIMARY KEY normalized as this PR does. See #11975 and #11944

Otherwise, this is just the right way of normalizing a table. SHOW CREATE TABLE in MySQL always extracts PRIMARY KEY information outside the column definition.

Checklist

  • "Backport to:" labels have been added if this change should be back-ported
  • Tests were added or are not required
  • Documentation was added or is not required

Deployment Notes

Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
…ition splits into 'id int' and 'primary key (id)' parts. Primary key is always the first key in the list of keys. Handle queries such as 'modify id int primary key' (validate there isn't already a primary key, or that the existing primary key is over 'id'. add column with primary key definition...

Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
@shlomi-noach shlomi-noach added Type: Enhancement Logical improvement (somewhere between a bug and feature) Component: Query Serving labels Dec 27, 2022
@vitess-bot
Copy link
Contributor

vitess-bot bot commented Dec 27, 2022

Review Checklist

Hello reviewers! 👋 Please follow this checklist when reviewing this Pull Request.

General

  • Ensure that the Pull Request has a descriptive title.
  • If this is a change that users need to know about, please apply the release notes (needs details) label so that merging is blocked unless the summary release notes document is included.
  • If a test is added or modified, there should be a documentation on top of the test to explain what the expected behavior is what the test does.

If a new flag is being introduced:

  • Is it really necessary to add this flag?
  • Flag names should be clear and intuitive (as far as possible)
  • Help text should be descriptive.
  • Flag names should use dashes (-) as word separators rather than underscores (_).

If a workflow is added or modified:

  • Each item in Jobs should be named in order to mark it as required.
  • If the workflow should be required, the maintainer team should be notified.

Bug fixes

  • There should be at least one unit or end-to-end test.
  • The Pull Request description should include a link to an issue that describes the bug.

Non-trivial changes

  • There should be some code comments as to why things are implemented the way they are.

New/Existing features

  • Should be documented, either by modifying the existing documentation or creating new documentation.
  • New features should have a link to a feature request issue or an RFC that documents the use cases, corner cases and test cases.

Backward compatibility

  • Protobuf changes should be wire-compatible.
  • Changes to _vt tables and RPCs need to be backward compatible.
  • vtctl command output order should be stable and awk-able.
  • RPC changes should be compatible with vitess-operator
  • If a flag is removed, then it should also be removed from VTop, if used there.

Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Comment on lines +109 to +115
ColKeyNone ColumnKeyOption = iota
ColKeyPrimary
ColKeySpatialKey
ColKeyFulltextKey
ColKeyUnique
ColKeyUniqueKey
ColKey
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The change in go/vt/sqlparser is to make the above constants public, so that we can introspect a ColumnDefinition and see whether it has a specific key option.

Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
Copy link
Member

@deepthi deepthi left a comment

Choose a reason for hiding this comment

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

LGTM

@deepthi deepthi merged commit ff64a3b into vitessio:main Jan 9, 2023
@deepthi deepthi deleted the schema-diff-normalize-primary-key branch January 9, 2023 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Query Serving Type: Enhancement Logical improvement (somewhere between a bug and feature)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants