pranavbhole commented on code in PR #14470:
URL: https://github.com/apache/druid/pull/14470#discussion_r1239107712
##########
server/src/main/java/org/apache/druid/metadata/SQLMetadataConnector.java:
##########
@@ -830,4 +845,73 @@ public Void withHandle(Handle handle)
log.warn(e, "Exception while deleting records from table");
}
}
+
+ /**
+ * create index on the table if not already exist with retry.
+ *
+ * @param handle Connection handle
+ * @param table name of the table to fetch the index map
+ * @return Map of the uppercase index name to List of the columns in index
+ */
+ public Map<String, List<String>> getIndexOnTable(Handle handle, String table)
+ {
+ Map<String, List<String>> res = new HashMap<>();
+ try {
+ DatabaseMetaData databaseMetaData = handle.getConnection().getMetaData();
+ ResultSet resultSet = databaseMetaData.getIndexInfo(null, null,
table.toUpperCase(Locale.ENGLISH), false, false);
+
+ while (resultSet.next()) {
+ String indexName = resultSet.getString("INDEX_NAME");
+ String columnName = resultSet.getString("COLUMN_NAME");
+ if (org.apache.commons.lang.StringUtils.isNotBlank(indexName) &&
org.apache.commons.lang.StringUtils.isNotBlank(
+ columnName)) {
+ columnName = StringUtils.toUpperCase(columnName);
+ List<String> cols = res.getOrDefault(indexName, new ArrayList<>());
+ cols.add(columnName);
+ res.put(indexName, cols);
+ }
+ }
+ }
+ catch (Exception e) {
+ log.error(e, "Exception while listing the index on table %s ", table);
+ }
+ return res;
+ }
+
+ /**
+ * create index on the table if not already exist with retry.
+ *
+ * @param tableName Name of the table to create index on
+ * @param checkString case-insensitive string which substring of index name,
it helps to check the existing index on table
+ * @param indexSQL SQL statement to create index
+ */
+ public void createIndex(final String tableName, final String checkString,
final String indexSQL)
+ {
+ try {
+ retryWithHandle(
+ new HandleCallback<Void>()
+ {
+ @Override
+ public Void withHandle(Handle handle)
+ {
+ if (tableExists(handle, tableName)) {
+ Map<String, List<String>> indexMap = getIndexOnTable(handle,
tableName);
Review Comment:
Yes, I am not using the values here excepts in tests. I just created this
map that so that we can use it for validating the indexed columns in future or
somewhere for other meta tables. We are currently just checking the index name
and creating it regardless of the indexed columns.
--
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]