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

 ##########
 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);
+                               }
+                               if (add) {
+                                       colNames.add((String) 
colNameMethod.invoke(constraint));
+                               } else {
+                                       return null;
 
 Review comment:
   minor: the signature is better to return an optional if value is nullable.

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