From 3c128a5333f530eb4b3d8a12b93296c3c1f0f46d Mon Sep 17 00:00:00 2001 From: yujun Date: Thu, 28 May 2026 14:12:44 +0800 Subject: [PATCH] [fix](show variables) Fix changed variable output in show variables (#63734) Problem Summary: SHOW VARIABLES WHERE is evaluated through an internal schema query. During planning of that internal query, FE may call setVarOnce() and temporarily change session variables such as disable_join_reorder. The schema scan then dumps those temporary values and reports them as Changed, even though they are not user-visible session changes. This patch dumps variables from a reverted clone so one-shot internal values do not affect SHOW VARIABLES output, while preserving the existing information_schema WHERE execution behavior. --- .../nereids/parser/LogicalPlanBuilder.java | 38 +++++++++---------- .../doris/service/FrontendServiceImpl.java | 15 +++++++- .../show_variables/show_variables_command.out | 8 ++++ .../show_variables_command.groovy | 8 ++++ 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 4187eb6e681851..7e2fa20cd2b9f7 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -6524,28 +6524,28 @@ public AlterTableOp visitDropRollupClause(DorisParser.DropRollupClauseContext ct @Override public LogicalPlan visitShowVariables(ShowVariablesContext ctx) { SetType statementScope = visitStatementScope(ctx.statementScope()); - if (ctx.wildWhere() != null) { - if (ctx.wildWhere().LIKE() != null) { - return new ShowVariablesCommand(statementScope, - stripQuotes(ctx.wildWhere().STRING_LITERAL().getText())); + LogicalPlan plan; + if (ctx.wildWhere() == null) { + plan = new ShowVariablesCommand(statementScope, null); + } else if (ctx.wildWhere().LIKE() != null) { + plan = new ShowVariablesCommand(statementScope, + stripQuotes(ctx.wildWhere().STRING_LITERAL().getText())); + } else { + StringBuilder sb = new StringBuilder(); + sb.append("SELECT `VARIABLE_NAME` AS `Variable_name`, `VARIABLE_VALUE` AS `Value` FROM "); + sb.append("`").append(InternalCatalog.INTERNAL_CATALOG_NAME).append("`"); + sb.append("."); + sb.append("`").append(InfoSchemaDb.DATABASE_NAME).append("`"); + sb.append("."); + if (statementScope == SetType.GLOBAL) { + sb.append("`global_variables` "); } else { - StringBuilder sb = new StringBuilder(); - sb.append("SELECT `VARIABLE_NAME` AS `Variable_name`, `VARIABLE_VALUE` AS `Value` FROM "); - sb.append("`").append(InternalCatalog.INTERNAL_CATALOG_NAME).append("`"); - sb.append("."); - sb.append("`").append(InfoSchemaDb.DATABASE_NAME).append("`"); - sb.append("."); - if (statementScope == SetType.GLOBAL) { - sb.append("`global_variables` "); - } else { - sb.append("`session_variables` "); - } - sb.append(getOriginSql(ctx.wildWhere())); - return new NereidsParser().parseSingle(sb.toString()); + sb.append("`session_variables` "); } - } else { - return new ShowVariablesCommand(statementScope, null); + sb.append(getOriginSql(ctx.wildWhere())); + plan = new NereidsParser().parseSingle(sb.toString()); } + return plan; } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java index 99dd56cbe42cfd..3957eec447474a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java +++ b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java @@ -121,6 +121,7 @@ import org.apache.doris.qe.NereidsCoordinator; import org.apache.doris.qe.QeProcessorImpl; import org.apache.doris.qe.QueryState; +import org.apache.doris.qe.SessionVariable; import org.apache.doris.qe.StmtExecutor; import org.apache.doris.qe.VariableMgr; import org.apache.doris.service.arrowflight.FlightSqlConnectProcessor; @@ -992,7 +993,19 @@ public TShowVariableResult showVariables(TShowVariableRequest params) throws TEx if (ctx == null) { return result; } - vars = VariableMgr.dump(SetType.fromThrift(params.getVarType()), ctx.getSessionVariable(), null); + // SHOW VARIABLES can be evaluated through an internal schema query. Planning that + // internal query may call setVarOnce() and temporarily change the live session + // variable (for example disable_join_reorder). Cloning alone is not enough, + // because the clone would keep both the temporary value and its recorded origin. + // Revert only the clone so Changed reflects user-visible session settings, + // while the real session remains untouched. + SessionVariable sessionVariable = VariableMgr.cloneSessionVariable(ctx.getSessionVariable()); + try { + VariableMgr.revertSessionValue(sessionVariable); + } catch (DdlException e) { + throw new TException(e); + } + vars = VariableMgr.dump(SetType.fromThrift(params.getVarType()), sessionVariable, null); result.setVariables(vars); return result; } diff --git a/regression-test/data/nereids_p0/ddl/show_variables/show_variables_command.out b/regression-test/data/nereids_p0/ddl/show_variables/show_variables_command.out index dc009a38e77678..ef1e17cb3124f1 100644 --- a/regression-test/data/nereids_p0/ddl/show_variables/show_variables_command.out +++ b/regression-test/data/nereids_p0/ddl/show_variables/show_variables_command.out @@ -8,3 +8,11 @@ license Apache License, Version 2.0 -- !cmd -- license Apache License, Version 2.0 +-- !changed -- +enable_profile true + +-- !not_changed -- + +-- !global -- +license Apache License, Version 2.0 + diff --git a/regression-test/suites/nereids_p0/ddl/show_variables/show_variables_command.groovy b/regression-test/suites/nereids_p0/ddl/show_variables/show_variables_command.groovy index 77caf22cacd40c..8b517b6708fca6 100644 --- a/regression-test/suites/nereids_p0/ddl/show_variables/show_variables_command.groovy +++ b/regression-test/suites/nereids_p0/ddl/show_variables/show_variables_command.groovy @@ -20,7 +20,15 @@ suite("show_variables_command") { checkNereidsExecute("""show variables like 'li_ense'""") checkNereidsExecute("""show variables where variable_name like 'li_ense'""") checkNereidsExecute("""show variables where variable_name = 'license'""") + checkNereidsExecute("""show variables where changed = 1""") + checkNereidsExecute("""show global variables where variable_name = 'license'""") qt_cmd("""show variables like 'li_ense'""") qt_cmd("""show variables where variable_name like 'li_ense'""") qt_cmd("""show variables where variable_name = 'license'""") + + sql "set enable_profile = true" + qt_changed("""show variables where changed = 1 and variable_name = 'enable_profile'""") + qt_not_changed("""show variables where changed = 1 and variable_name = 'license'""") + qt_global("""show global variables where variable_name = 'license'""") + sql "UNSET VARIABLE ALL" }