From 78eb378eada401e89eb6235c6a4c713c8585f2b0 Mon Sep 17 00:00:00 2001 From: Hemanth Umashankar Date: Thu, 13 Aug 2026 13:00:41 +0530 Subject: [PATCH] Store typed operation state in QueryInfo using TOperationState instead of String --- .../queryhistory/TestQueryHistoryService.java | 3 +- .../org/apache/hadoop/hive/ql/QueryInfo.java | 43 +++++++++++++++++-- .../service/cli/operation/SQLOperation.java | 10 ++--- .../servlet/QueriesRESTfulAPIServlet.java | 8 ++++ 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/queryhistory/TestQueryHistoryService.java b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/queryhistory/TestQueryHistoryService.java index e618a2510521..a4d0df62fae2 100644 --- a/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/queryhistory/TestQueryHistoryService.java +++ b/itests/hive-unit/src/test/java/org/apache/hadoop/hive/ql/queryhistory/TestQueryHistoryService.java @@ -44,6 +44,7 @@ import org.apache.hadoop.hive.ql.session.SessionState.LogHelper; import org.apache.hadoop.hive.serde2.SerDeUtils; import org.apache.hadoop.hive.shims.Utils; +import org.apache.hive.service.rpc.thrift.TOperationState; import org.apache.tez.common.counters.DAGCounter; import org.apache.tez.common.counters.TaskCounter; import org.apache.tez.common.counters.TezCounters; @@ -89,7 +90,7 @@ public void testSimpleFlush() throws Exception { service.start(); // prepare the source object from which the QueryHistoryService will obtain query information - QueryInfo queryInfo = spy(new QueryInfo(DummyRecord.QUERY_STATE, DummyRecord.END_USER, + QueryInfo queryInfo = spy(new QueryInfo(TOperationState.INITIALIZED_STATE, DummyRecord.END_USER, DummyRecord.EXECUTION_ENGINE, DummyRecord.SESSION_ID, DummyRecord.OPERATION_ID)); // elapsed time is calculated from System.currentTimeMillis(), let's mock it here for unit test convenience's sake diff --git a/ql/src/java/org/apache/hadoop/hive/ql/QueryInfo.java b/ql/src/java/org/apache/hadoop/hive/ql/QueryInfo.java index 33da60c19fee..b8931d0d56cc 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/QueryInfo.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/QueryInfo.java @@ -18,6 +18,7 @@ package org.apache.hadoop.hive.ql; import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hive.service.rpc.thrift.TOperationState; /** * The class is synchronized, as WebUI may access information about a running query. @@ -31,12 +32,13 @@ public class QueryInfo { private final String operationId; private Long runtime; // tracks only running portion of the query. private Long endTime; - private String state; + private TOperationState state; private QueryDisplay queryDisplay; private String operationLogLocation; - public QueryInfo(String state, String userName, String executionEngine, String sessionId, String operationId) { + public QueryInfo(TOperationState state, String userName, String executionEngine, String sessionId, + String operationId) { this.state = state; this.userName = userName; this.executionEngine = executionEngine; @@ -46,7 +48,7 @@ public QueryInfo(String state, String userName, String executionEngine, String s } public static QueryInfo getFromConf(HiveConf conf) { - return new QueryInfo("INITIALIZED", conf.get(DriverContext.DEFAULT_USER_NAME_PROP), + return new QueryInfo(TOperationState.INITIALIZED_STATE, conf.get(DriverContext.DEFAULT_USER_NAME_PROP), conf.getVar(HiveConf.ConfVars.HIVE_EXECUTION_ENGINE), HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_SESSION_ID), conf.get(DriverContext.DEFAULT_OPERATION_ID_PROP)); } @@ -80,6 +82,10 @@ public String getExecutionEngine() { } public synchronized String getState() { + return getDisplayState(state); + } + + public synchronized TOperationState getOperationState() { return state; } @@ -91,7 +97,7 @@ public synchronized Long getEndTime() { return endTime; } - public synchronized void updateState(String state) { + public synchronized void updateState(TOperationState state) { this.state = state; } @@ -122,4 +128,33 @@ public String getOperationLogLocation() { public void setOperationLogLocation(String operationLogLocation) { this.operationLogLocation = operationLogLocation; } + + private static String getDisplayState(TOperationState state) { + if (state == null) { + return "UNKNOWN"; + } + + switch (state) { + case INITIALIZED_STATE: + return "INITIALIZED"; + case RUNNING_STATE: + return "RUNNING"; + case FINISHED_STATE: + return "FINISHED"; + case CANCELED_STATE: + return "CANCELED"; + case CLOSED_STATE: + return "CLOSED"; + case ERROR_STATE: + return "ERROR"; + case UKNOWN_STATE: + return "UNKNOWN"; + case PENDING_STATE: + return "PENDING"; + case TIMEDOUT_STATE: + return "TIMEDOUT"; + default: + return "UNKNOWN"; + } + } } diff --git a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java index 4caa963e2a6f..5636160b952c 100644 --- a/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java +++ b/service/src/java/org/apache/hive/service/cli/operation/SQLOperation.java @@ -130,7 +130,7 @@ public SQLOperation(HiveSession parentSession, String statement, Map { @Override public void serialize(