InvisibleProgrammer commented on code in PR #6577:
URL: https://github.com/apache/hive/pull/6577#discussion_r3538969271


##########
standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/directsql/MetaStoreDirectSql.java:
##########
@@ -534,6 +537,151 @@ public void addPartitions(List<MPartition> parts, 
List<List<MPartitionPrivilege>
     directSqlInsertPart.addPartitions(parts, partPrivilegesList, 
partColPrivilegesList);
   }
 
+  /**
+   * Gets a suitable column descriptor for an existing table, based on the 
latest partitions
+   *
+   * @param cols column list of a partition
+   * @param tblId table id
+   * @return an existing column descriptor which columns matches with @cols.  
Null if there is no match.
+   * @throws MetaException
+   */
+  public MColumnDescriptor getColumnDescriptor(List<FieldSchema> cols, long 
tblId)
+      throws MetaException {
+
+    if (cols == null || cols.isEmpty()) {
+      return null;
+    }
+
+    // Please note! In case you modify any of those methods, run 
TestHMSColumnDescriptorReuse with all the supported databases
+    List<Long> cdCandidates = findTheLatestColumnDescriptors(tblId);
+
+    cdCandidates = filterCandidatesByColumnCount(cdCandidates, cols.size());
+
+    Long matchedColumnDescriptorId = 
matchColumnDescriptorWithActualColumns(cdCandidates, cols);
+
+    if (matchedColumnDescriptorId != null) {
+      return pm.getObjectById(MColumnDescriptor.class, 
matchedColumnDescriptorId);
+    }
+
+    return null;
+  }
+
+  private @Nullable List<Long> findTheLatestColumnDescriptors(long tblId) 
throws MetaException {
+    Query<?> query = null;
+    try {
+      String findLatestDescriptorsSql = """
+        SELECT s."CD_ID"
+        FROM "PARTITIONS" p
+        JOIN "SDS" s ON s."SD_ID" = p."SD_ID"
+        WHERE p."TBL_ID" = ?
+        GROUP BY s."CD_ID"
+        ORDER BY MAX(p."PART_ID") DESC
+      """;
+      query = pm.newQuery("javax.jdo.query.SQL", findLatestDescriptorsSql);
+
+      List<Object> sqlResult = executeWithArray(query, new Object[]{ tblId }, 
findLatestDescriptorsSql);
+      if (sqlResult == null || sqlResult.isEmpty()) {
+        return null;
+      }
+
+      List<Long> latestColumnDescriptorIds = new  ArrayList<>();
+      for (Object cdId : sqlResult) {
+        latestColumnDescriptorIds.add(extractLongValue(cdId));

Review Comment:
   @dengzhhu653 : I introduced `extractLongValue as the type of CD_ID is number 
in Oracle and executeWithArray returns with BigDecimal instead of Long. 
   
   Do we have an existing reusable code to handle the situation? 



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to