deniskuzZ commented on code in PR #6322:
URL: https://github.com/apache/hive/pull/6322#discussion_r2818961190
##########
ql/src/java/org/apache/hadoop/hive/ql/metadata/HiveMaterializedViewsRegistry.java:
##########
@@ -168,44 +171,60 @@ private Loader(Hive db) {
@Override
public void run() {
- PerfLogger perfLogger = SessionState.getPerfLogger();
- try {
- DriverUtils.setUpAndStartSessionState(db.getConf());
- perfLogger.perfLogBegin(CLASS_NAME,
PerfLogger.MATERIALIZED_VIEWS_REGISTRY_REFRESH);
- if (initialized.get()) {
- for (Table mvTable : db.getAllMaterializedViewObjectsForRewriting())
{
- RelOptMaterialization existingMV = getRewritingMaterializedView(
- mvTable.getDbName(), mvTable.getTableName(), ALL);
- if (existingMV != null) {
- // We replace if the existing MV is not newer
- Table existingMVTable =
HiveMaterializedViewUtils.extractTable(existingMV);
- if (existingMVTable.getCreateTime() < mvTable.getCreateTime() ||
- (existingMVTable.getCreateTime() == mvTable.getCreateTime()
&&
- existingMVTable.getMVMetadata().getMaterializationTime()
<= mvTable.getMVMetadata().getMaterializationTime())) {
- refreshMaterializedView(db.getConf(), existingMVTable,
mvTable);
- }
- } else {
- // Simply replace if it still does not exist
- refreshMaterializedView(db.getConf(), null, mvTable);
+ DriverUtils.setUpAndStartSessionState(db.getConf());
+ refresh(db);
+ }
+ }
+
+ public void refresh(Hive db) {
+ PerfLogger perfLogger = SessionState.getPerfLogger();
+ perfLogger.perfLogBegin(CLASS_NAME,
PerfLogger.MATERIALIZED_VIEWS_REGISTRY_REFRESH);
+ try {
+ if (initialized.get()) {
+ Set<TableName> remaining = getRewritingMaterializedViews().stream()
+ .map(materialization -> new TableName(
+ "hive", materialization.qualifiedTableName.get(0),
materialization.qualifiedTableName.get(1)))
+ .collect(Collectors.toSet());
+
+ for (Table mvTable : db.getAllMaterializedViewObjectsForRewriting()) {
+ RelOptMaterialization existingMV = getRewritingMaterializedView(
+ mvTable.getDbName(), mvTable.getTableName(), ALL);
+ if (existingMV != null) {
+ // We replace if the existing MV is not newer
+ Table existingMVTable =
HiveMaterializedViewUtils.extractTable(existingMV);
+ remaining.remove(new TableName(
+ existingMVTable.getCatName(), existingMVTable.getDbName(),
existingMVTable.getTableName()));
+ if (existingMVTable.getCreateTime() < mvTable.getCreateTime() ||
+ (existingMVTable.getCreateTime() ==
mvTable.getCreateTime() &&
+
existingMVTable.getMVMetadata().getMaterializationTime() <=
mvTable.getMVMetadata().getMaterializationTime())) {
+ refreshMaterializedView(db.getConf(), existingMVTable, mvTable);
}
- }
- LOG.info("Materialized views registry has been refreshed");
- } else {
- for (Table mvTable : db.getAllMaterializedViewObjectsForRewriting())
{
+ } else {
+ // Simply replace if it still does not exist
refreshMaterializedView(db.getConf(), null, mvTable);
}
- initialized.set(true);
- LOG.info("Materialized views registry has been initialized");
}
- } catch (HiveException e) {
- if (initialized.get()) {
- LOG.error("Problem connecting to the metastore when refreshing the
view registry", e);
- } else {
- LOG.error("Problem connecting to the metastore when initializing the
view registry", e);
+
+ for (TableName tableName : remaining) {
+ dropMaterializedView(tableName.getDb(), tableName.getTable());
+ }
+
+ LOG.info("Materialized views registry has been refreshed");
+ } else {
+ for (Table mvTable : db.getAllMaterializedViewObjectsForRewriting()) {
+ refreshMaterializedView(db.getConf(), null, mvTable);
}
+ initialized.set(true);
+ LOG.info("Materialized views registry has been initialized");
Review Comment:
try to avoid code duplication
--
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]