Skip to content

Commit f58089e

Browse files
morningmanclaude
andcommitted
[fix](fe) Report the statement's pinned BE exec version to connectors
ConnectorSession#getBeExecVersion() returned the live Config.be_exec_version. Since #68837 the planner pins one version per statement, and both the query options the backends receive and the write handle carry it, so the session was the one place left that could disagree: if be_exec_version changed while a statement was planned, IcebergWritePlanProvider could choose the iceberg_transform route for a plan sent at an older version. ConnectorSessionBuilder now captures the version pinned on the statement context, read off the same context as the statement scope, and the session returns it. Without a statement context, or before the planner pins the version, the session still reports Config.be_exec_version. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a18e560 commit f58089e

3 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/connector/ConnectorSessionBuilder.java‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,25 @@ public ConnectorSession build() {
176176
}
177177
return new ConnectorSessionImpl(queryId, user, timeZone, locale,
178178
catalogId, catalogName, catalogProperties, sessionProperties, sid, cred,
179-
captureStatementScope());
179+
captureStatementScope(), captureBeExecVersion());
180+
}
181+
182+
/**
183+
* Captures the backend execution version that NereidsPlanner pinned for the current statement, which the
184+
* query options sent to the backends and the write handle carry too. It is read off the same statement
185+
* context as {@link #captureStatementScope()}. Without a statement context, or before the planner pins the
186+
* version (offline planning, commands that plan no query, tests), it is {@code Config.be_exec_version}.
187+
*/
188+
private int captureBeExecVersion() {
189+
ConnectContext ctx = connectContext != null ? connectContext : ConnectContext.get();
190+
if (ctx == null) {
191+
return Config.be_exec_version;
192+
}
193+
StatementContext sc = ctx.getStatementContext();
194+
if (sc == null || !sc.hasBeExecVersion()) {
195+
return Config.be_exec_version;
196+
}
197+
return sc.getBeExecVersion();
180198
}
181199

182200
/**

‎fe/fe-core/src/main/java/org/apache/doris/connector/ConnectorSessionImpl.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public class ConnectorSessionImpl implements ConnectorSession {
5454
// reachable) so off-thread scan pumps that reuse this one session still reach it. NONE when there is no
5555
// live statement context (offline planning, tests) -- then getStatementScope() memoizes nothing.
5656
private final ConnectorStatementScope statementScope;
57+
// The backend execution version the planner pinned for the statement, captured at construction like the
58+
// statement scope; Config.be_exec_version when the session is built outside a planned statement.
59+
private final int beExecVersion;
5760
// Otherwise-immutable session; this is bound once by the insert executor at write time
5861
// for connectors using the SPI transaction model (e.g. maxcompute), and read back by the
5962
// connector's planWrite via getCurrentTransaction(). volatile for cross-thread visibility.
@@ -63,13 +66,14 @@ public class ConnectorSessionImpl implements ConnectorSession {
6366
long catalogId, String catalogName, Map<String, String> catalogProperties,
6467
Map<String, String> sessionProperties) {
6568
this(queryId, user, timeZone, locale, catalogId, catalogName, catalogProperties, sessionProperties,
66-
null, null, ConnectorStatementScope.NONE);
69+
null, null, ConnectorStatementScope.NONE, Config.be_exec_version);
6770
}
6871

6972
ConnectorSessionImpl(String queryId, String user, String timeZone, String locale,
7073
long catalogId, String catalogName, Map<String, String> catalogProperties,
7174
Map<String, String> sessionProperties, String sessionId,
72-
ConnectorDelegatedCredential delegatedCredential, ConnectorStatementScope statementScope) {
75+
ConnectorDelegatedCredential delegatedCredential, ConnectorStatementScope statementScope,
76+
int beExecVersion) {
7377
this.queryId = queryId != null ? queryId : "";
7478
this.user = user != null ? user : "";
7579
this.timeZone = timeZone != null ? timeZone : "UTC";
@@ -83,6 +87,7 @@ public class ConnectorSessionImpl implements ConnectorSession {
8387
this.sessionId = sessionId;
8488
this.delegatedCredential = delegatedCredential;
8589
this.statementScope = statementScope != null ? statementScope : ConnectorStatementScope.NONE;
90+
this.beExecVersion = beExecVersion;
8691
}
8792

8893
@Override
@@ -168,8 +173,9 @@ public Map<String, String> getSessionProperties() {
168173

169174
@Override
170175
public int getBeExecVersion() {
171-
// Same query-wide value the write handle reports, so optimizer-time choices match planWrite.
172-
return Config.be_exec_version;
176+
// The statement's pinned version, which the write handle reports and the backends receive too, so
177+
// optimizer-time choices match planWrite and the dispatched plan.
178+
return beExecVersion;
173179
}
174180

175181
@Override

‎fe/fe-core/src/test/java/org/apache/doris/connector/ConnectorSessionImplTest.java‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,41 @@ public void testExternalScanTaskReuseFollowsTheSessionVariable() {
101101
}
102102

103103
@Test
104-
public void testBeExecVersionFollowsTheEngineConfig() {
105-
// Connectors gate optimizer-time choices on it (an Iceberg partition-hash write needs exec version 18), so
106-
// it must be the query-wide version the write handle later reports to planWrite.
104+
public void testBeExecVersionIsTheVersionPinnedForTheStatement() {
105+
// Connectors gate optimizer-time choices on it (an Iceberg partition-hash write needs exec version 18). The
106+
// planner pins one version per statement, and the query options the backends receive and the write handle
107+
// carry it, so the session must report it too: an ADMIN change of be_exec_version while the statement is
108+
// planned must not put a version-18 partition function into a plan sent at version 17. MUTATION: reading
109+
// Config.be_exec_version in getBeExecVersion() -> red.
110+
int original = Config.be_exec_version;
111+
ConnectContext ctx = new ConnectContext();
112+
ctx.setThreadLocalInfo();
113+
try {
114+
StatementContext stmtCtx = new StatementContext();
115+
ctx.setStatementContext(stmtCtx);
116+
stmtCtx.initializeBeExecVersion(17);
117+
Config.be_exec_version = 18;
118+
Assertions.assertEquals(17, ConnectorSessionBuilder.from(ctx).build().getBeExecVersion());
119+
Assertions.assertEquals(17, ConnectorSessionBuilder.create().build().getBeExecVersion(),
120+
"a session built on the statement's thread reads the thread-local statement context");
121+
} finally {
122+
Config.be_exec_version = original;
123+
ConnectContext.remove();
124+
}
125+
}
126+
127+
@Test
128+
public void testBeExecVersionFallsBackToTheEngineConfigOutsideAPlannedStatement() {
129+
// Sessions built where no statement version is pinned (background loaders, commands that plan no query,
130+
// tests) report the configured version.
107131
int original = Config.be_exec_version;
108132
try {
109133
Config.be_exec_version = 16;
110134
Assertions.assertEquals(16, ConnectorSessionBuilder.create().build().getBeExecVersion());
135+
ConnectContext ctx = new ConnectContext();
136+
ctx.setStatementContext(new StatementContext());
137+
Assertions.assertEquals(16, ConnectorSessionBuilder.from(ctx).build().getBeExecVersion(),
138+
"a statement whose version is not pinned yet");
111139
} finally {
112140
Config.be_exec_version = original;
113141
}

0 commit comments

Comments
 (0)