This is an automated email from the ASF dual-hosted git repository.

morrySnow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 751bdfa7795 [fix](nereids) Fix NPE in SHOW QUERY STATS with explicit 
database (#69594)
751bdfa7795 is described below

commit 751bdfa7795995617631ef9c4b2acc96d31d74bd
Author: Aranya Singh Chauhan <[email protected]>
AuthorDate: Wed Oct 28 15:48:18 2026 +0530

    [fix](nereids) Fix NPE in SHOW QUERY STATS with explicit database (#69594)
    
    ### What problem does this PR solve?
    
    **Problem Summary:**
    
    `SHOW QUERY STATS FOR <db_name>` throws a `NullPointerException` when a
    database is explicitly specified.
    
    In `ShowQueryStatsCommand.validate()`, `ShowQueryStatsType.DATABASE` is
    assigned only when the database name is resolved from the current
    session. For an explicitly specified database, `type` remains null,
    causing `getMetaData()` to fail.
    
    **Fix:**
    
    Move the `ShowQueryStatsType.DATABASE` assignment into the
    database-level statistics handling branch, ensuring correct metadata
    initialization for both implicit and explicit database queries.
    
    Added regression coverage to verify both forms return the expected
    result schema.
    
    
    Co-authored-by: Aranya Singh Chauhan 
<[email protected]>
---
 .../plans/commands/ShowQueryStatsCommand.java      |  2 +-
 .../suites/query_p0/stats/query_stats_test.groovy  | 29 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowQueryStatsCommand.java
 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowQueryStatsCommand.java
index e65ee3f110c..db2624fa2c9 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowQueryStatsCommand.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowQueryStatsCommand.java
@@ -87,7 +87,6 @@ public class ShowQueryStatsCommand extends ShowCommand {
     public void validate(ConnectContext ctx) throws UserException {
         if (StringUtils.isEmpty(dbName)) {
             dbName = ctx.getDatabase();
-            type = ShowQueryStatsType.DATABASE;
         }
         String catalog = Env.getCurrentEnv().getCurrentCatalog().getName();
         if (tableNameInfo == null && StringUtils.isEmpty(dbName)) {
@@ -111,6 +110,7 @@ public class ShowQueryStatsCommand extends ShowCommand {
             db.getTableOrDdlException(tableNameInfo.getTbl());
         }
         if (tableNameInfo == null) {
+            type = ShowQueryStatsType.DATABASE;
             Map<String, Long> stats = 
QueryStatsUtil.getMergedDatabaseStats(catalog, dbName);
             stats.forEach((tableName, queryHit) -> {
                 if (Env.getCurrentEnv().getAccessManager()
diff --git a/regression-test/suites/query_p0/stats/query_stats_test.groovy 
b/regression-test/suites/query_p0/stats/query_stats_test.groovy
index 0cadec47cc2..e4365366b60 100644
--- a/regression-test/suites/query_p0/stats/query_stats_test.groovy
+++ b/regression-test/suites/query_p0/stats/query_stats_test.groovy
@@ -65,6 +65,35 @@ suite("query_stats_test") {
     // Counts may exceed 1 in multi-FE clusters; verify right columns hit, 
wrong ones stay 0.
     sql "select k1 from ${tbName} where k0 = 1"
     sql "select k4 from ${tbName} where k2 = 1991"
+    // Explicit database-level query stats should use the
+    // same result schema as implicit database-level stats.
+    def currentDb = sql("SELECT DATABASE()")[0][0].toString()
+
+    def implicitDbStats =
+            sql_return_maparray "SHOW QUERY STATS"
+
+    def explicitDbStats =
+            sql_return_maparray "SHOW QUERY STATS FOR ${currentDb}"
+
+    def implicitRow = implicitDbStats.find {
+        it.TableName == tbName
+    }
+    def explicitRow = explicitDbStats.find {
+        it.TableName == tbName
+    }
+
+    assertNotNull(implicitRow)
+    assertNotNull(explicitRow)
+
+    assertEquals(
+            ["TableName", "QueryCount"] as Set,
+            explicitRow.keySet() as Set
+    )
+
+    assertEquals(
+            implicitRow.keySet() as Set,
+            explicitRow.keySet() as Set
+    )
     def stats1 = sql "show query stats from ${tbName}"
     def s1k1 = stats1.find { it[0] == "k1" }
     def s1k0 = stats1.find { it[0] == "k0" }


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

Reply via email to