Skip to content

Commit 40323db

Browse files
committed
[BugFix] Guard sync MV fallback with isSyncMVQuery() hint check
Move the getMaterializedViewIndex() fallback from resolveTable() into visitTable(), gated by TableRelation.isSyncMVQuery(). This avoids the expensive linear scan for non-existent table names that are not sync MV queries, matching the pattern used in QueryAnalyzer. Also add missing test_table cleanup in PlannerMetaLockerTest.afterEach() to prevent test pollution. Signed-off-by: rickxz <herick.victor.rodrigues@hotmail.com>
1 parent 6909362 commit 40323db

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

fe/fe-core/src/main/java/com/starrocks/sql/analyzer/PlannerMetaLocker.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -176,24 +176,36 @@ private static Pair<Database, Table> resolveTable(ConnectContext session, TableN
176176

177177
Table table = metadataMgr.getTable(session, catalogName, dbName, tbName);
178178
if (table == null) {
179-
// Fallback: Sync MV names are index names, not table names.
180-
// When querying via [_SYNC_MV_] hint, we need to resolve through the
181-
// materialized index to find the base table and its database.
182-
// This ensures the database is registered in currentSqlDbIds for
183-
// resource group classifier matching.
184-
Pair<Table, MaterializedIndexMeta> mvIndex =
185-
GlobalStateMgr.getCurrentState().getLocalMetastore()
186-
.getMaterializedViewIndex(dbName, tbName);
187-
if (mvIndex != null) {
188-
table = mvIndex.first;
189-
}
179+
return null;
190180
}
191181

192-
if (table == null) {
182+
return new Pair<>(db, table);
183+
}
184+
185+
private static Pair<Database, Table> resolveSyncMV(ConnectContext session, TableName tableName) {
186+
String dbName = tableName.getDb();
187+
String tbName = tableName.getTbl();
188+
189+
if (Strings.isNullOrEmpty(dbName)) {
190+
dbName = session.getDatabase();
191+
}
192+
if (Strings.isNullOrEmpty(dbName) || Strings.isNullOrEmpty(tbName)) {
193193
return null;
194194
}
195195

196-
return new Pair<>(db, table);
196+
Pair<Table, MaterializedIndexMeta> mvIndex =
197+
GlobalStateMgr.getCurrentState().getLocalMetastore()
198+
.getMaterializedViewIndex(dbName, tbName);
199+
if (mvIndex == null) {
200+
return null;
201+
}
202+
203+
Database db = GlobalStateMgr.getCurrentState().getLocalMetastore().getDb(dbName);
204+
if (db == null) {
205+
return null;
206+
}
207+
208+
return new Pair<>(db, mvIndex.first);
197209
}
198210

199211
private static class TableCollector extends AstTraverser<Void, Void> {
@@ -262,6 +274,9 @@ public Void visitAlterMaterializedViewStatement(AlterMaterializedViewStmt statem
262274
@Override
263275
public Void visitTable(TableRelation node, Void context) {
264276
Pair<Database, Table> dbAndTable = resolveTable(session, node.getName());
277+
if (dbAndTable == null && node.isSyncMVQuery()) {
278+
dbAndTable = resolveSyncMV(session, node.getName());
279+
}
265280
put(dbAndTable);
266281
return null;
267282
}

fe/fe-core/src/test/java/com/starrocks/sql/analyzer/PlannerMetaLockerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public void afterEach() throws Exception {
5858
starRocksAssert.dropMaterializedView("test_mv");
5959
} catch (Exception ignored) {
6060
}
61+
try {
62+
starRocksAssert.dropTable("test_table");
63+
} catch (Exception ignored) {
64+
}
6165
try {
6266
DDLStmtExecutor.execute(
6367
UtFrameUtils.parseStmtWithNewParser("drop resource group test_rg", connectContext),

0 commit comments

Comments
 (0)