Skip to content

Commit

Permalink
[BugFix] fix monitoring metrics for execute stmt (backport #52768) (#…
Browse files Browse the repository at this point in the history
…53136)

Co-authored-by: ShaoxunLi <[email protected]>
  • Loading branch information
mergify[bot] and ShaoxunLi authored Nov 22, 2024
1 parent 792bcd2 commit ec0e7c2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
16 changes: 16 additions & 0 deletions fe/fe-core/src/main/java/com/starrocks/qe/ConnectContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
import com.starrocks.sql.analyzer.Authorizer;
import com.starrocks.sql.analyzer.SemanticException;
import com.starrocks.sql.ast.CleanTemporaryTableStmt;
import com.starrocks.sql.ast.ExecuteStmt;
import com.starrocks.sql.ast.QueryStatement;
import com.starrocks.sql.ast.SetListItem;
import com.starrocks.sql.ast.SetStmt;
import com.starrocks.sql.ast.SetType;
Expand Down Expand Up @@ -256,6 +258,20 @@ public static void remove() {
threadLocalInfo.remove();
}

public boolean isQueryStmt(StatementBase statement) {
if (statement instanceof QueryStatement) {
return true;
}
if (statement instanceof ExecuteStmt) {
ExecuteStmt executeStmt = (ExecuteStmt) statement;
PrepareStmtContext prepareStmtContext = getPreparedStmt(executeStmt.getStmtName());
if (prepareStmtContext != null) {
return prepareStmtContext.getStmt().getInnerStmt() instanceof QueryStatement;
}
}
return false;
}

public boolean isSend() {
return this.isSend;
}
Expand Down
12 changes: 11 additions & 1 deletion fe/fe-core/src/main/java/com/starrocks/qe/ConnectProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,17 @@ private void handleExecute() {

executor = new StmtExecutor(ctx, executeStmt);
ctx.setExecutor(executor);
executor.execute();

boolean isQuery = ctx.isQueryStmt(executeStmt);
ctx.getState().setIsQuery(isQuery);

if (enableAudit && isQuery) {
executor.addRunningQueryDetail(executeStmt);
executor.execute();
executor.addFinishedQueryDetail();
} else {
executor.execute();
}

if (enableAudit) {
auditAfterExec(originStmt, executor.getParsedStmt(), executor.getQueryStatisticsForAuditLog());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2681,7 +2681,7 @@ public void addRunningQueryDetail(StatementBase parsedStmt) {
sql = parsedStmt.getOrigStmt().originStmt;
}

boolean isQuery = parsedStmt instanceof QueryStatement;
boolean isQuery = context.isQueryStmt(parsedStmt);
QueryDetail queryDetail = new QueryDetail(
DebugUtil.printId(context.getQueryId()),
isQuery,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.starrocks.qe.ConnectContext;
import com.starrocks.qe.PrepareStmtContext;
import com.starrocks.qe.StmtExecutor;
import com.starrocks.sql.ast.ExecuteStmt;
import com.starrocks.sql.ast.PrepareStmt;
import com.starrocks.sql.ast.QueryStatement;
import com.starrocks.sql.ast.SelectRelation;
Expand Down Expand Up @@ -83,6 +84,21 @@ public void testParser() throws Exception {
Assert.assertThrows(AnalysisException.class, () -> UtFrameUtils.parseStmtWithNewParser(sql4, ctx));
}

@Test
public void testIsQuery() throws Exception {
String selectSql = "select * from demo.prepare_stmt";
QueryStatement queryStatement = (QueryStatement) UtFrameUtils.parseStmtWithNewParser(selectSql, ctx);
Assert.assertEquals(true, ctx.isQueryStmt(queryStatement));

String prepareSql = "PREPARE stmt FROM select * from demo.prepare_stmt";
PrepareStmt prepareStmt = (PrepareStmt) UtFrameUtils.parseStmtWithNewParser(prepareSql, ctx);
Assert.assertEquals(false, ctx.isQueryStmt(prepareStmt));

ctx.putPreparedStmt("stmt", new PrepareStmtContext(prepareStmt, ctx, null));
Assert.assertEquals(true, ctx.isQueryStmt(new ExecuteStmt("stmt", null)));
Assert.assertEquals(false, ctx.isQueryStmt(new ExecuteStmt("stmt1", null)));
}

@Test
public void testPrepareEnable() {
ctx.getSessionVariable().setEnablePrepareStmt(false);
Expand Down

0 comments on commit ec0e7c2

Please sign in to comment.