This is an automated email from the ASF dual-hosted git repository. dataroaring pushed a commit to branch branch-3.0 in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-3.0 by this push: new 55630ff2609 branch-3.0:[fix](auditlog)Set sqlHash in executeInternalQuery (#49984) (#50365) 55630ff2609 is described below commit 55630ff2609542ff84a6492a034fd3f8242eead9 Author: James <lijib...@selectdb.com> AuthorDate: Fri Apr 25 14:27:13 2025 +0800 branch-3.0:[fix](auditlog)Set sqlHash in executeInternalQuery (#49984) (#50365) backport: https://github.com/apache/doris/pull/49984 --- .../java/org/apache/doris/qe/StmtExecutor.java | 4 ++ .../org/apache/doris/statistics/AnalysisJob.java | 6 ++- .../doris/qe/StmtExecutorInternalQueryTest.java | 45 ++++++++++++++++++++++ .../apache/doris/statistics/AnalysisJobTest.java | 27 +++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index de4b47d8232..fbccd704906 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -224,6 +224,7 @@ import com.google.common.collect.Sets; import com.google.protobuf.ByteString; import com.google.protobuf.ProtocolStringList; import lombok.Setter; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -3418,6 +3419,9 @@ public class StmtExecutor { UUID uuid = UUID.randomUUID(); TUniqueId queryId = new TUniqueId(uuid.getMostSignificantBits(), uuid.getLeastSignificantBits()); context.setQueryId(queryId); + if (originStmt.originStmt != null) { + context.setSqlHash(DigestUtils.md5Hex(originStmt.originStmt)); + } try { List<ResultRow> resultRows = new ArrayList<>(); try { diff --git a/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisJob.java b/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisJob.java index 0ab1fe004f6..c7c7077f6bb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisJob.java +++ b/fe/fe-core/src/main/java/org/apache/doris/statistics/AnalysisJob.java @@ -20,11 +20,13 @@ package org.apache.doris.statistics; import org.apache.doris.catalog.Env; import org.apache.doris.qe.AuditLogHelper; import org.apache.doris.qe.AutoCloseConnectContext; +import org.apache.doris.qe.ConnectContext; import org.apache.doris.qe.QueryState; import org.apache.doris.qe.QueryState.MysqlStateType; import org.apache.doris.qe.StmtExecutor; import org.apache.doris.statistics.util.StatisticsUtil; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -127,7 +129,9 @@ public class AnalysisJob { } insertStmt += values.toString(); try (AutoCloseConnectContext r = StatisticsUtil.buildConnectContext(false)) { - stmtExecutor = new StmtExecutor(r.connectContext, insertStmt); + ConnectContext context = r.connectContext; + context.setSqlHash(DigestUtils.md5Hex(insertStmt)); + stmtExecutor = new StmtExecutor(context, insertStmt); executeWithExceptionOnFail(stmtExecutor); } catch (Exception t) { throw new RuntimeException("Failed to analyze: " + t.getMessage()); diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/StmtExecutorInternalQueryTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/StmtExecutorInternalQueryTest.java new file mode 100644 index 00000000000..5091ea20dc6 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/StmtExecutorInternalQueryTest.java @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package org.apache.doris.qe; + +import org.apache.doris.analysis.StatementBase; +import org.apache.doris.nereids.NereidsPlanner; + +import mockit.Mock; +import mockit.MockUp; +import org.junit.Assert; +import org.junit.Test; + +public class StmtExecutorInternalQueryTest { + @Test + public void testSetSqlHash() { + StmtExecutor executor = new StmtExecutor(new ConnectContext(), "select * from table1"); + new MockUp<NereidsPlanner>() { + @Mock + public void plan(StatementBase queryStmt, org.apache.doris.thrift.TQueryOptions queryOptions) { + throw new RuntimeException(); + } + }; + try { + executor.executeInternalQuery(); + } catch (Exception e) { + // do nothing + } + Assert.assertEquals("a8ec30e5ad0820f8c5bd16a82a4491ca", executor.getContext().getSqlHash()); + } +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/statistics/AnalysisJobTest.java b/fe/fe-core/src/test/java/org/apache/doris/statistics/AnalysisJobTest.java index 8a163523eeb..68ce20212a3 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/statistics/AnalysisJobTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/statistics/AnalysisJobTest.java @@ -214,4 +214,31 @@ public class AnalysisJobTest { Assertions.assertEquals(0, job.queryFinished.size()); } + @Test + public void testSetSqlHash(@Mocked AnalysisInfo info, + @Mocked OlapAnalysisTask task1, @Mocked OlapAnalysisTask task2) { + AnalysisJob job = new AnalysisJob(info, Collections.singletonList(task1)); + job.queryFinished = new HashSet<>(); + job.queryFinished.add(task2); + new MockUp<AnalysisJob>() { + @Mock + public void updateTaskState(AnalysisState state, String msg) { + } + + @Mock + protected void executeWithExceptionOnFail(StmtExecutor stmtExecutor) throws Exception { + + } + + @Mock + protected void syncLoadStats() { + } + }; + job.buf.add(new ColStatsData()); + job.flushBuffer(); + Assertions.assertEquals(0, job.queryFinished.size()); + Assertions.assertEquals(0, job.buf.size()); + Assertions.assertEquals("ffd6aa73b79f9228c737a6da0f4b2834", job.stmtExecutor.getContext().getSqlHash()); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org