3434
3535package com .starrocks .qe ;
3636
37+ import com .google .common .annotations .VisibleForTesting ;
3738import com .google .common .base .Preconditions ;
3839import com .google .common .base .Strings ;
3940import com .google .common .collect .Lists ;
@@ -387,9 +388,16 @@ private RuntimeProfile buildTopLevelProfile() {
387388 AstToSQLBuilder .toSQLOrDefault (parsedStmt , originStmt .originStmt );
388389 if (AuditEncryptionChecker .needEncrypt (parsedStmt )) {
389390 summaryProfile .addInfoString (ProfileManager .SQL_STATEMENT ,
390- AstToSQLBuilder .toSQLOrDefault (parsedStmt , originStmt .originStmt ));
391+ SqlCredentialRedactor .redact (AstToSQLBuilder .toSQLOrDefault (parsedStmt , originStmt .originStmt )));
392+ } else if (Config .enable_sql_desensitize_in_log ) {
393+ String desensitizedSql = AstToSQLBuilder .toSQL (parsedStmt , FormatOptions .allEnable ()
394+ .setColumnSimplifyTableName (false )
395+ .setEnableDigest (true ))
396+ .orElse ("this is a desensitized sql" );
397+ summaryProfile .addInfoString (ProfileManager .SQL_STATEMENT ,
398+ SqlCredentialRedactor .redact (desensitizedSql ));
391399 } else {
392- summaryProfile .addInfoString (ProfileManager .SQL_STATEMENT , sql );
400+ summaryProfile .addInfoString (ProfileManager .SQL_STATEMENT , SqlCredentialRedactor . redact ( sql ) );
393401 }
394402
395403 // Add some import variables in profile
@@ -659,7 +667,7 @@ private ExecPlan generateExecPlan() throws Exception {
659667 if (e .getType ().equals (ErrorType .USER_ERROR )) {
660668 throw e ;
661669 } else {
662- LOG .warn ("Planner error: " + originStmt . originStmt , e );
670+ LOG .warn ("Planner error: {}" , getRedactedOriginStmtInString () , e );
663671 throw e ;
664672 }
665673 } catch (Exception e ) {
@@ -743,14 +751,29 @@ public void execute() throws Exception {
743751 execPlan = generateExecPlan ();
744752 } catch (Exception e ) {
745753 LOG .warn ("Generate exec plan failed for explain stmt: {}" ,
746- parsedStmt . getOrigStmt (). originStmt , e );
754+ getRedactedOriginStmtInString () , e );
747755 }
748756 handleExplainExecPlan (execPlan );
749757 return ;
750758 }
751759
752- // execPlan is the output of planner
753- ExecPlan execPlan = generateExecPlan ();
760+ // Register as a planning query so it is visible in current_queries during optimization.
761+ // The planning entry is removed before handleQueryStmt/handleDMLStmt re-registers
762+ // with the real Coordinator, avoiding AlreadyExistsException from putIfAbsent.
763+ ExecPlan execPlan ;
764+ context .setPlanning (true );
765+ try {
766+ QeProcessorImpl .INSTANCE .registerQuery (context .getExecutionId (),
767+ QeProcessorImpl .QueryInfo .fromPlanningQuery (context , getRedactedOriginStmtInString ()));
768+ } catch (Exception e ) {
769+ LOG .warn ("Failed to register planning query: {}" , DebugUtil .printId (context .getExecutionId ()), e );
770+ }
771+ try {
772+ execPlan = generateExecPlan ();
773+ } finally {
774+ context .setPlanning (false );
775+ QeProcessorImpl .INSTANCE .unregisterQuery (context .getExecutionId ());
776+ }
754777
755778 // no need to execute http query dump request in BE
756779 if (context .isHTTPQueryDump ) {
@@ -821,7 +844,7 @@ public void execute() throws Exception {
821844 originStmt = this .originStmt .originStmt ;
822845 }
823846 needRetry = true ;
824- LOG .warn ("retry {} times. stmt: {}" , (i + 1 ), originStmt );
847+ LOG .warn ("retry {} times. stmt: {}" , (i + 1 ), SqlCredentialRedactor . redact ( originStmt ) );
825848 } else {
826849 throw e ;
827850 }
@@ -954,7 +977,7 @@ public void execute() throws Exception {
954977 String sql = originStmt != null ? originStmt .originStmt : "" ;
955978 String truncatedSql = sql .length () > 200 ? sql .substring (0 , 200 ) + "..." : sql ;
956979 LOG .error ("LargeInPredicate optimization failed, sql: {}, error: {}. Will retry with" +
957- " enable_large_in_predicate=false." , truncatedSql , e .getMessage ());
980+ " enable_large_in_predicate=false." , SqlCredentialRedactor . redact ( truncatedSql ) , e .getMessage ());
958981 throw e ;
959982 } catch (StarRocksException e ) {
960983 String sql = originStmt != null ? originStmt .originStmt : "" ;
@@ -1006,7 +1029,8 @@ public void execute() throws Exception {
10061029 context .setSessionVariable (sessionVariableBackup );
10071030
10081031 if (shouldMarkIdleCheck && originWarehouseId != null ) {
1009- WarehouseIdleChecker .decreaseRunningSQL (originWarehouseId );
1032+ WarehouseIdleChecker .decreaseRunningSQL (originWarehouseId ,
1033+ getRedactedOriginStmtInString ());
10101034 }
10111035
10121036 recordExecStatsIntoContext ();
@@ -1520,7 +1544,7 @@ private void handleQueryStmt(ExecPlan execPlan) throws Exception {
15201544 }
15211545
15221546 QeProcessorImpl .INSTANCE .registerQuery (context .getExecutionId (),
1523- new QeProcessorImpl .QueryInfo (context , originStmt . originStmt , coord ));
1547+ new QeProcessorImpl .QueryInfo (context , getRedactedOriginStmtInString () , coord ));
15241548
15251549 if (isSchedulerExplain ) {
15261550 coord .execWithoutDeploy ();
@@ -2316,7 +2340,7 @@ private void handleDdlStmt() throws DdlException {
23162340 if (sql == null ) {
23172341 sql = originStmt .originStmt ;
23182342 }
2319- LOG .warn ("DDL statement (" + sql + " ) process failed." , e );
2343+ LOG .warn ("DDL statement ({} ) process failed." , SqlCredentialRedactor . redact ( sql ) , e );
23202344 }
23212345 context .setState (e .getQueryState ());
23222346 } catch (DdlException | AlterJobException e ) {
@@ -2329,7 +2353,7 @@ private void handleDdlStmt() throws DdlException {
23292353 if (sql == null || sql .isEmpty ()) {
23302354 sql = originStmt .originStmt ;
23312355 }
2332- LOG .warn ("DDL statement (" + sql + " ) process failed." , e );
2356+ LOG .warn ("DDL statement ({} ) process failed." , SqlCredentialRedactor . redact ( sql ) , e );
23332357 context .getState ().setError (e .getMessage ());
23342358 }
23352359 }
@@ -2535,7 +2559,7 @@ public void handleDMLStmtWithProfile(ExecPlan execPlan, DmlStmt stmt) throws Exc
25352559 try {
25362560 handleDMLStmt (execPlan , stmt );
25372561 } catch (Throwable t ) {
2538- LOG .warn ("DML statement({}) process failed." , originStmt . originStmt , t );
2562+ LOG .warn ("DML statement({}) process failed." , getRedactedOriginStmtInString () , t );
25392563 throw t ;
25402564 } finally {
25412565 boolean isAsync = false ;
@@ -2589,7 +2613,7 @@ public void handleDMLStmt(ExecPlan execPlan, DmlStmt stmt) throws Exception {
25892613 context .getState ().setOk ();
25902614 } catch (QueryStateException e ) {
25912615 if (e .getQueryState ().getStateType () != MysqlStateType .OK ) {
2592- LOG .warn ("DDL statement(" + originStmt . originStmt + " ) process failed." , e );
2616+ LOG .warn ("DDL statement({} ) process failed." , getRedactedOriginStmtInString () , e );
25932617 }
25942618 context .setState (e .getQueryState ());
25952619 }
@@ -2713,8 +2737,8 @@ public void handleDMLStmt(ExecPlan execPlan, DmlStmt stmt) throws Exception {
27132737
27142738 coord .setLoadJobId (jobId );
27152739 trackingSql = "select tracking_log from information_schema.load_tracking_logs where job_id=" + jobId ;
2716-
2717- QeProcessorImpl . QueryInfo queryInfo = new QeProcessorImpl .QueryInfo (context , originStmt . originStmt , coord );
2740+ QeProcessorImpl . QueryInfo queryInfo =
2741+ new QeProcessorImpl .QueryInfo (context , getRedactedOriginStmtInString () , coord );
27182742 QeProcessorImpl .INSTANCE .registerQuery (context .getExecutionId (), queryInfo );
27192743
27202744 if (isSchedulerExplain ) {
@@ -2743,7 +2767,7 @@ public void handleDMLStmt(ExecPlan execPlan, DmlStmt stmt) throws Exception {
27432767 if (Config .log_plan_cancelled_by_crash_be && context .getQueryDetail () == null ) {
27442768 LOG .warn ("Query cancelled by crash of backends [QueryId={}] [SQL={}] [Plan={}]" ,
27452769 DebugUtil .printId (context .getExecutionId ()),
2746- originStmt == null ? "" : originStmt . originStmt ,
2770+ getRedactedOriginStmtInString () ,
27472771 execPlan .getExplainString (TExplainLevel .COSTS ));
27482772 }
27492773
@@ -2954,10 +2978,7 @@ public void handleDMLStmt(ExecPlan execPlan, DmlStmt stmt) throws Exception {
29542978 }
29552979 } catch (Throwable t ) {
29562980 // if any throwable being thrown during insert operation, first we should abort this txn
2957- String failedSql = "" ;
2958- if (originStmt != null && originStmt .originStmt != null ) {
2959- failedSql = originStmt .originStmt ;
2960- }
2981+ String failedSql = getRedactedOriginStmtInString ();
29612982 LOG .warn ("failed to handle stmt [{}] label: {}" , failedSql , label , t );
29622983 String errMsg = t .getMessage ();
29632984 if (errMsg == null ) {
@@ -3077,6 +3098,15 @@ public String getOriginStmtInString() {
30773098 return originStmt .originStmt ;
30783099 }
30793100
3101+ @ VisibleForTesting
3102+ String getRedactedOriginStmtInString () {
3103+ String sql = getOriginStmtInString ();
3104+ if (!SqlCredentialRedactor .mayNeedCredentialRedaction (sql )) {
3105+ return sql ;
3106+ }
3107+ return SqlCredentialRedactor .redact (sql );
3108+ }
3109+
30803110 public Pair <List <TResultBatch >, Status > executeStmtWithExecPlan (ConnectContext context , ExecPlan plan ) {
30813111 List <TResultBatch > sqlResult = Lists .newArrayList ();
30823112 try {
@@ -3195,11 +3225,17 @@ public void addRunningQueryDetail(StatementBase parsedStmt) {
31953225 }
31963226
31973227 try {
3198- String sql ;
3199- if (AuditEncryptionChecker .needEncrypt (parsedStmt )) {
3200- sql = AstToSQLBuilder .toSQLOrDefault (parsedStmt , parsedStmt .getOrigStmt ().originStmt );
3201- } else {
3202- sql = parsedStmt .getOrigStmt ().originStmt ;
3228+ String originSql = getQueryDetailSql (parsedStmt );
3229+ boolean needEncrypt = AuditEncryptionChecker .needEncrypt (parsedStmt );
3230+ String sql = originSql ;
3231+ if (needEncrypt || Config .enable_sql_desensitize_in_log ) {
3232+ sql = AstToSQLBuilder .toSQL (parsedStmt , FormatOptions .allEnable ()
3233+ .setColumnSimplifyTableName (false )
3234+ .setHideCredential (needEncrypt )
3235+ .setEnableDigest (Config .enable_sql_desensitize_in_log ))
3236+ .orElse ("this is a desensitized sql" );
3237+ } else if (SqlCredentialRedactor .mayNeedCredentialRedaction (originSql )) {
3238+ sql = SqlCredentialRedactor .redact (originSql );
32033239 }
32043240
32053241 boolean isQuery = context .isQueryStmt (parsedStmt );
0 commit comments