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

DDL for Table Constraints #20384

Merged
merged 6 commits into from
Feb 26, 2024

Conversation

ClarenceThreepwood
Copy link
Contributor

@ClarenceThreepwood ClarenceThreepwood commented Jul 26, 2023

Introduces table constraints to Presto (Primary Key and Unique)(#20034). Support for other types of constraints will follow.

This PR Implements

  1. creating tables with unique and PK constraints
  2. Adding unique and PK constraints to existing tables
  3. Dropping existing constraints from tables

== RELEASE NOTES ==
General Changes

  • Add framework to add and drop table constraints (Primary Key and Unique)

Hive Connector Changes

  • Add support for table constraints. Other systems that support table constraints may extend this framework in their respective connectors to add support.

@aaneja aaneja assigned aaneja and unassigned aaneja Jul 27, 2023
@aaneja aaneja self-requested a review July 27, 2023 17:32
@ClarenceThreepwood ClarenceThreepwood force-pushed the pkuniqueddl branch 3 times, most recently from 9791f60 to 2b6e3a7 Compare August 2, 2023 22:03
Copy link
Contributor

@ZacBlanco ZacBlanco left a comment

Choose a reason for hiding this comment

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

Just a few random comments. Main question was about the grammar for the constraint specification.

Copy link
Contributor

@aaneja aaneja left a comment

Choose a reason for hiding this comment

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

LGTM overall, still taking a look at some tests/other nits.

A thought I had - IMO this may be a good time to define table constraints for the TPCH and TPCDS tables, so that we can have another sample implementation that dev's can examine and play around with.
This would be very helpful to see the impact of adding constraints to plan shapes

@@ -30,6 +30,7 @@
import com.facebook.presto.spi.PrestoException;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a unit test similar to testCreateWithNotNullColumns that asserts that adding a new table constraint get's reflected in the ConnectorTableMetadata for supported connectors

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also include some negative tests

  • Multiple PRIMARY KEYs
  • Duplicate UNIQUE keys for the same named/unnamed constraint

These will be parsed by the parser, but since these are semantically incorrect, we should fail them in the CreateTableTask

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have left almost all semantic checking to the connector(s) or catalog service. It is impossible to detect all cases of semantic errors and different systems have their own rules for handling these. E.g. some systems require a globally unique name for primary keys, while others do not.
It may be possible to catch some of the common, universal errors early and return, but I'm not sure whether that is worth it. I could be convinced otherwise

Copy link
Contributor

@aaneja aaneja Sep 4, 2023

Choose a reason for hiding this comment

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

I think we should have at least the logically impossible cases (such as multiple PKs) covered in presto-main. For others, lets pick a connector (say HMS) and put some negative tests there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added some additional error checks here that basically validate whether the create table statement is internally consistent. I am reluctant to make these more exhaustive since they would include additional overhead of communicating with the remote catalog

ZacBlanco
ZacBlanco previously approved these changes Aug 28, 2023
@ClarenceThreepwood ClarenceThreepwood force-pushed the pkuniqueddl branch 6 times, most recently from 7549c29 to babfdee Compare August 30, 2023 00:59
Copy link
Contributor

@steveburnett steveburnett left a comment

Choose a reason for hiding this comment

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

LGTM! (docs)

@ClarenceThreepwood ClarenceThreepwood force-pushed the pkuniqueddl branch 3 times, most recently from 040e860 to e329df2 Compare August 31, 2023 23:50
@ClarenceThreepwood ClarenceThreepwood marked this pull request as ready for review September 11, 2023 16:31
@ClarenceThreepwood ClarenceThreepwood requested a review from a team as a code owner September 11, 2023 16:31
@github-actions
Copy link

github-actions bot commented Sep 11, 2023

Codenotify: Notifying subscribers in CODENOTIFY files for diff dccb5a4...1860a8d.

Notify File(s)
@aditi-pandit presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4
@elharo presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4
@kaikalur presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4
@rschlussel presto-parser/src/main/antlr4/com/facebook/presto/sql/parser/SqlBase.g4
@steveburnett presto-docs/src/main/sphinx/language/reserved.rst
presto-docs/src/main/sphinx/sql/alter-table.rst
presto-docs/src/main/sphinx/sql/create-table.rst

@tdcmeehan tdcmeehan self-assigned this Sep 22, 2023
@ClarenceThreepwood
Copy link
Contributor Author

Does this PR need a release note entry added in the Description?

Added - thanks for catching this

@steveburnett
Copy link
Contributor

Suggested revision of release note entry based on my possibly incomplete understanding of this PR and the release note guidelines:

== RELEASE NOTES ==
General Changes
* Add framework to add and drop table constraints (Primary Key and Unique)

Hive Connector Changes
* Add support for table constraints. Other systems that support table constraints may extend this framework in their respective connectors to add support.

Let me know if this accurately describes your work here!

Also and unrelated: pulled branch today and made a new local build to recheck the docs, everything looks good. Thanks for the doc!

@ClarenceThreepwood
Copy link
Contributor Author

@tdcmeehan - Any other feedback?

@tdcmeehan
Copy link
Contributor

LGTM. Any final thoughts @mlyublena @feilong-liu ?

tdcmeehan
tdcmeehan previously approved these changes Feb 20, 2024
steveburnett
steveburnett previously approved these changes Feb 23, 2024
Copy link
Contributor

@steveburnett steveburnett left a comment

Choose a reason for hiding this comment

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

LGTM! (docs)

Pull updated branch, new local build, everything looks good. Thanks for the doc!

Impose ordering on table columns in constraint to conform with Hive spec
Supported in the Hive connector for Hive/HMS version >= 2.1.0

ALTER TABLE DROP CONSTRAINT constraint_name
Supported in the Hive connector for Hive/HMS version >= 2.1.0

ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE/PRIMARY KEY (cols...)
Supported in the Hive connector for Hive/HMS version >= 2.1.0

CREATE TABLE table_name (column_definition, ..., CONSTRAINT constraint_name UNIQUE/PRIMAY KEY (column_name, ...), ...)
Copy link
Contributor

@steveburnett steveburnett left a comment

Choose a reason for hiding this comment

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

LGTM! (docs)

Pull updated branch, new local build.

@ClarenceThreepwood ClarenceThreepwood merged commit 492aeeb into prestodb:master Feb 26, 2024
57 checks passed
@ClarenceThreepwood ClarenceThreepwood deleted the pkuniqueddl branch February 26, 2024 17:57
@wanglinsong wanglinsong mentioned this pull request May 1, 2024
48 tasks
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.

6 participants