KurtYoung commented on a change in pull request #10340: 
[FLINK-14978][table-api] Introduce constraint class hierarchy required for 
primary keys
URL: https://github.com/apache/flink/pull/10340#discussion_r352409466
 
 

 ##########
 File path: 
flink-table/flink-table-common/src/main/java/org/apache/flink/table/api/TableSchema.java
 ##########
 @@ -530,12 +549,90 @@ public Builder watermark(String rowtimeAttribute, String 
watermarkExpressionStri
                        return this;
                }
 
+               /**
+                * Creates a primary key constraint for a set of given columns. 
The primary key is informational only.
+                * It will not be enforced. It can be used for optimizations. 
It is the owner's of the data responsibility
+                * to ensure uniqueness of the data.
+                *
+                * <p>The primary key will be assigned a random name.
+                *
+                * @param columns array of columns that form a unique primary 
key
+                */
+               public Builder primaryKey(String... columns) {
+                       return primaryKey(UUID.randomUUID().toString(), 
columns);
+               }
+
+               /**
+                * Creates a primary key constraint for a set of given columns. 
The primary key is informational only.
+                * It will not be enforced. It can be used for optimizations. 
It is the owner's of the data responsibility
+                * to ensure
+                *
+                * @param columns array of columns that form a unique primary 
key
+                * @param name name for the primary key, can be used to 
reference the constraint
+                */
+               public Builder primaryKey(String name, String[] columns) {
+                       if (this.primaryKeyInfo != null) {
+                               throw new ValidationException("Can not create 
multiple PRIMARY keys.");
+                       }
+                       if (StringUtils.isNullOrWhitespaceOnly(name)) {
+                               throw new ValidationException("PRIMARY KEY's 
name can not be null or empty.");
+                       }
+                       if (columns == null || columns.length == 0) {
+                               throw new ValidationException("PRIMARY KEY 
constraint must be defined for at least a single column.");
+                       }
+                       this.primaryKeyInfo = new KeyConstraintInfo(name, 
Arrays.asList(columns));
+                       return this;
+               }
+
+               private static final class KeyConstraintInfo {
 
 Review comment:
   we can avoid introducing this intermediate class if we don't use 
`FieldReferenceExpression` in `KeyConstraint`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to