hello-stephen commented on code in PR #71190:
URL: https://github.com/apache/doris/pull/71190#discussion_r4749460330
##########
fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/RefreshMTMVCommand.java:
##########
@@ -157,10 +158,14 @@ private void dryRunRefresh(ConnectContext ctx,
StmtExecutor executor) throws Exc
ConnectContext createDryRunConnectContext(MTMV mtmv) throws UserException {
// Read once, so the group that is checked is the group that is bound.
Optional<String> declaredComputeGroup = mtmv.getComputeGroup();
- ConnectContext internalCtx = createRefreshContext(mtmv,
declaredComputeGroup);
- // Like MTMVTask, only a declared group is re-checked: the MV never
chose the session's group.
+ ConnectContext internalCtx = createRefreshContext(mtmv,
declaredComputeGroup,
+ refreshMTMVInfo.getSessionVariables());
Review Comment:
[P2] Use the REFRESH timeout for the outer dry-run statement
The hint changes only the internal context, while
`ConnectPoolMgr.timeoutChecker` still checks the registered caller's context.
With default user timeout properties, `SET query_timeout=1` followed by
`REFRESH /*+ SET_VAR(query_timeout=3600) */ MATERIALIZED VIEW mv INCREMENTAL
WITH DRY RUN` cancels a nonempty delta that is still running when the caller's
one-second timeout is checked. `ConnectContext.checkTimeout` uses the unchanged
caller value, and the outer executor forwards that cancellation to the internal
executor through `dryRunRefresh`'s cancel delegate. Thus the requested longer
REFRESH timeout is ineffective even though the internal coordinator receives
it. Align the outer statement's effective deadline with the REFRESH override
while preserving restoration of the caller's session, and add coverage that
invokes the timeout checker during a dry run.
##########
fe/fe-core/src/main/java/org/apache/doris/mtmv/MTMVPlanUtil.java:
##########
@@ -156,6 +163,135 @@ public static ConnectContext createMTMVContext(MTMV mtmv,
List<RuleType> disable
return ctx;
}
+ /**
+ * Create an internal MTMV context for refresh execution, dry run, or
EXPLAIN REFRESH.
+ * Explicit REFRESH SET_VAR hints are applied on top of MTMV defaults so
they take
+ * precedence over both MTMV defaults and MV query SQL SET_VAR hints.
Materialized view
+ * rewrite remains forcibly disabled to prevent self-rewrite during
refresh and keep
+ * REFRESH, DRY RUN, and EXPLAIN REFRESH behavior consistent.
+ */
+ public static ConnectContext createMTMVRefreshContext(MTMV mtmv,
List<RuleType> disableRules,
+ @Nullable Map<String, String> refreshSessionVariables) {
+ ConnectContext previousContext = ConnectContext.get();
+ ConnectContext ctx = createMTMVContext(mtmv, disableRules);
+ try {
+ applyRefreshSessionVariables(ctx, null, refreshSessionVariables);
+ ctx.getSessionVariable().setEnableMaterializedViewRewrite(false);
+
ctx.getSessionVariable().setEnableDmlMaterializedViewRewrite(false);
+ ctx.addOverriddenSessionVars(ImmutableSet.of(
+ SessionVariable.ENABLE_MATERIALIZED_VIEW_REWRITE,
+ SessionVariable.ENABLE_DML_MATERIALIZED_VIEW_REWRITE));
+ return ctx;
+ } catch (RuntimeException | Error e) {
+ if (previousContext == null) {
+ ConnectContext.remove();
+ } else {
+ previousContext.setThreadLocalInfo();
+ }
+ throw e;
+ }
+ }
+
+ /**
+ * Validate SET_VAR hint parameters from REFRESH MATERIALIZED VIEW against
a throwaway
+ * ConnectContext carrying the caller's identity, so invalid variables and
privilege-gated
+ * overrides fail fast without mutating the caller's session or query
state.
+ */
+ public static void validateRefreshSessionVariables(ConnectContext
callerCtx,
+ @Nullable Map<String, String> refreshSessionVariables) {
+ if (refreshSessionVariables == null ||
refreshSessionVariables.isEmpty()) {
+ return;
+ }
+ ConnectContext validationCtx = new ConnectContext();
+ validationCtx.setEnv(callerCtx.getEnv());
+
validationCtx.setCurrentUserIdentity(callerCtx.getCurrentUserIdentity());
+ validationCtx.setSessionVariable(VariableMgr.newSessionVariable());
+ applyRefreshSessionVariables(validationCtx, null,
refreshSessionVariables);
+ validateRefreshPrivilegedSessionVariables(callerCtx, validationCtx);
+ }
+
+ private static void
validateRefreshPrivilegedSessionVariables(ConnectContext callerCtx,
+ ConnectContext validationCtx) {
+ SessionVariable sv = validationCtx.getSessionVariable();
+ if
(validationCtx.isSessionVarOverridden(SessionVariable.CLOUD_CLUSTER) &&
Config.isCloudMode()) {
+ String targetCluster = sv.getCloudCluster();
+ if (Strings.isNullOrEmpty(targetCluster)) {
+ throw new AnalysisException(
+ "Session variable 'cloud_cluster' cannot be empty in
REFRESH MATERIALIZED VIEW");
+ }
+ try {
+
ComputeGroupBindingUtil.validateDeclaredComputeGroup(callerCtx, targetCluster);
+ } catch (UserException e) {
+ throw new AnalysisException(e.getMessage(), e);
+ }
+ }
+ if
(validationCtx.isSessionVarOverridden(SessionVariable.WORKLOAD_VARIABLE)) {
+ String targetWorkloadGroup = sv.getWorkloadGroup();
+ if (Strings.isNullOrEmpty(targetWorkloadGroup)) {
+ throw new AnalysisException(
+ "Session variable 'workload_group' cannot be empty in
REFRESH MATERIALIZED VIEW");
+ }
+ if (!Env.getCurrentEnv().getAccessManager()
+ .checkWorkloadGroupPriv(callerCtx, targetWorkloadGroup,
PrivPredicate.USAGE)) {
+ throw new AnalysisException(String.format(
+ "Access denied; you need (at least one of) the %s
privilege(s) to use workload group '%s'.",
+ "USAGE/ADMIN", targetWorkloadGroup));
+ }
+ }
+ if
(validationCtx.isSessionVarOverridden(SessionVariable.DEFAULT_AI_RESOURCE)) {
+ String targetAiResource = sv.defaultAIResource;
+ if (!Strings.isNullOrEmpty(targetAiResource)
+ && !Env.getCurrentEnv().getAccessManager()
+ .checkResourcePriv(callerCtx, targetAiResource,
PrivPredicate.USAGE)) {
+ throw new
AnalysisException(ErrorCode.ERR_RESOURCE_ACCESS_DENIED_ERROR.formatErrorMsg(
+ PrivPredicate.USAGE.getPrivs().toString(),
targetAiResource));
+ }
+ }
+ }
+
+ /**
+ * Apply REFRESH SET_VAR hints to an internal refresh ConnectContext and
mark their
+ * canonical names as overridden so MV definition SQL SET_VAR hints cannot
clobber them.
+ */
+ public static void applyRefreshSessionVariables(ConnectContext ctx,
@Nullable StatementContext statementContext,
+ @Nullable Map<String, String> refreshSessionVariables) {
+ if (refreshSessionVariables == null ||
refreshSessionVariables.isEmpty()) {
+ return;
+ }
+ ConnectContext previousContext = ConnectContext.get();
+ boolean switched = previousContext != ctx;
+ if (switched) {
+ ctx.setThreadLocalInfo();
+ }
+ try {
+ Set<String> overriddenCanonicalNames =
Sets.newHashSetWithExpectedSize(refreshSessionVariables.size());
+ SessionVariable sessionVariable = ctx.getSessionVariable();
+ for (Map.Entry<String, String> entry :
refreshSessionVariables.entrySet()) {
+ String key = entry.getKey();
+ String value = entry.getValue();
+ try {
+ VariableMgr.setVar(sessionVariable, new SetVar(key, new
StringLiteral(value)));
Review Comment:
[P2] Reject static session variables before applying REFRESH hints
A throwaway `SessionVariable` does not isolate
`enable_multi_cluster_sync_load`: its annotated backing field
(`SessionVariable.cloudEnableMultiClusterSyncLoad`) is static. For a valid MV,
`REFRESH /*+ SET_VAR(enable_multi_cluster_sync_load=true, query_timeout='bad')
*/ MATERIALIZED VIEW mv COMPLETE` writes that shared flag here before rejecting
the timeout. This path neither enables origin tracking nor restores the field,
and the outer executor has no caller-session origin value to revert. The flag
therefore stays changed for every connection on this FE, making unrelated cloud
INSERTs and broker loads synchronize tablets to other compute groups
(`OlapInsertExecutor` / `CloudBrokerLoadJob`). Reject shared/static variables
before applying hints or make their storage context-local, and cover failed
validation plus cross-session isolation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]