dengzhhu653 commented on code in PR #6577:
URL: https://github.com/apache/hive/pull/6577#discussion_r3540291778
##########
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));
+ }
+ return latestColumnDescriptorIds;
+
+ } finally {
+ if (query != null) {
+ query.closeAll();
+ }
+ }
+ }
+
+ private List<Long> filterCandidatesByColumnCount(List<Long> cdCandidates,
int size) throws MetaException {
+ Query<?> query = null;
+ try {
+ String placeholders = cdCandidates.stream()
+ .map(c -> "?")
+ .collect(Collectors.joining(", "));
+
+ String candidatesWithProperColumnCountSql = String.format("""
+ SELECT c."CD_ID"
+ FROM "COLUMNS_V2" c
+ WHERE c."CD_ID" IN (%s)
+ GROUP BY c."CD_ID"
+ HAVING COUNT(*) = ?
+ """, placeholders);
+
+ query = pm.newQuery("javax.jdo.query.SQL",
candidatesWithProperColumnCountSql);
+
+ Object[] params = new Object[cdCandidates.size() + 1];
+ for (int i = 0; i < cdCandidates.size(); i++) {
+ params[i] = cdCandidates.get(i);
+ }
+ params[cdCandidates.size()] = size;
+
+ List<Object> result = executeWithArray(query, params,
candidatesWithProperColumnCountSql);
+
+ if (result == null || result.isEmpty()) {
+ return null;
+ }
+
+ List<Long> candidateIds = new ArrayList<>();
+ for (Object cdId : result) {
+ candidateIds.add(extractLongValue(cdId));
+ }
+
+ return candidateIds;
+
+ } finally {
+ if (query != null) {
+ query.closeAll();
+ }
+ }
+ }
+
+ private Long matchColumnDescriptorWithActualColumns(List<Long> cdCandidates,
List<FieldSchema> cols)
+ throws MetaException {
+ String findColumnSql = "SELECT \"COLUMN_NAME\", \"TYPE_NAME\", \"COMMENT\"
FROM \"COLUMNS_V2\" "
+ + "WHERE \"CD_ID\" = ? ORDER BY \"INTEGER_IDX\"";
+
+ for (Long candidate: cdCandidates) {
+ Query query = null;
+ try {
+ query = pm.newQuery("javax.jdo.query.SQL", findColumnSql);
+ List<Object[]> rows = executeWithArray(query, new Object[]
{candidate}, findColumnSql);
+ if (rows != null && rows.size() == cols.size()) {
+ for (int i = 0; i < cols.size(); i++) {
+ Object[] row = rows.get(i);
+ FieldSchema col = new FieldSchema(String.valueOf(row[0]),
String.valueOf(row[1]), String.valueOf(row[2]));
+ if (!cols.get(i).equals(col)) {
+ break;
+ }
+ }
+ return candidate;
Review Comment:
this could return a mistaken `candidate` even if `!cols.get(i).equals(col)`
--
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]