lirui-apache commented on a change in pull request #10249: [FLINK-14847][hive] 
Support retrieving Hive PK constraints
URL: https://github.com/apache/flink/pull/10249#discussion_r354647830
 
 

 ##########
 File path: 
flink-connectors/flink-connector-hive/src/main/java/org/apache/flink/table/catalog/hive/client/HiveShimV210.java
 ##########
 @@ -55,4 +60,42 @@ public void alterPartition(IMetaStoreClient client, String 
databaseName, String
                }
        }
 
+       @Override
+       public UniqueConstraint getPrimaryKey(IMetaStoreClient client, String 
dbName, String tableName, byte constraintTrait) {
+               try {
+                       Class requestClz = 
Class.forName("org.apache.hadoop.hive.metastore.api.PrimaryKeysRequest");
+                       Object request = 
requestClz.getDeclaredConstructor(String.class, 
String.class).newInstance(dbName, tableName);
+                       List<?> constraints = (List<?>) 
HiveReflectionUtils.invokeMethod(client.getClass(), client,
+                                       "getPrimaryKeys", new 
Class[]{requestClz}, new Object[]{request});
+                       if (constraints.isEmpty()) {
+                               return null;
+                       }
+                       Class constraintClz = 
Class.forName("org.apache.hadoop.hive.metastore.api.SQLPrimaryKey");
+                       Method colNameMethod = 
constraintClz.getDeclaredMethod("getColumn_name");
+                       Method isEnableMethod = 
constraintClz.getDeclaredMethod("isEnable_cstr");
+                       Method isValidateMethod = 
constraintClz.getDeclaredMethod("isValidate_cstr");
+                       Method isRelyMethod = 
constraintClz.getDeclaredMethod("isRely_cstr");
+                       List<String> colNames = new ArrayList<>();
+                       for (Object constraint : constraints) {
+                               boolean add = 
!HiveTableUtil.requireEnableConstraint(constraintTrait) || (boolean) 
isEnableMethod.invoke(constraint);
+                               if (add) {
+                                       add = 
!HiveTableUtil.requireValidateConstraint(constraintTrait) || (boolean) 
isValidateMethod.invoke(constraint);
+                               }
+                               if (add) {
+                                       add = 
!HiveTableUtil.requireRelyConstraint(constraintTrait) || (boolean) 
isRelyMethod.invoke(constraint);
+                               }
 
 Review comment:
   `constaintTrait` is the desired traits that all PK columns should satisfy. 
If any trait is not satisfied by any PK column, we consider the PK as invalid 
and return null. For example, callers can specify they want a PK constraint 
that is both RELY and ENABLE. Therefore a constraint is only RELY wouldn't 
satisfy this requirement and null is returned.
   I'll rename the parameters to make it easier to understand.

----------------------------------------------------------------
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