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_r354497654
##########
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:
the logic here has been a bit confusing. can you add some explanation?
seems that `constaintTrait` can override `constraint`? if so, we can just
process pk based on constraintTrait first; if constraintTrait is false, then
process pk based on reflection?
----------------------------------------------------------------
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