Skip to content

Commit ec06096

Browse files
morningmanclaude
andcommitted
[feature](arrow-flight) Let the schema session option put the session back into no database
GetSessionOptions reports a session that is in no database -- the state it starts in and SWITCH puts it back into -- as schema = "", but that value could not be set back: SetSessionOptions refused the empty string and the empty value alike with INVALID_VALUE. The empty value is Flight's way of unsetting an option and what the ADBC driver sends on its erase path (adbc.flight.sql.session.optionerase.schema), and no statement leads to that state, there being no USE of nothing. Both now put the session back into no database, through a new ConnectContext.clearDatabase() (which SWITCH shares), without running a statement: there is nothing in it to check. catalog keeps refusing the empty value, a session always being in some catalog. Tests: FlightSessionOptionsTest.testTheEmptyValueOrStringLeavesTheDatabase (erase, the "" round trip, a user without privileges) and a step of the test_session_options regression suite (a statement needs a database again after the clear). Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Ja986K4PEm2LD8u44L9jd2
1 parent 2761502 commit ec06096

4 files changed

Lines changed: 84 additions & 13 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/arrowflight/FlightSessionOptions.java‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@
5959
* {@code SHOW VARIABLES} shows, every value as a string: the one representation {@code SET} accepts
6060
* back, whatever the Java type of the variable behind it. The names an option goes by are exactly
6161
* the names that are read back, spelled the same: a client looks an option up in the result of
62-
* GetSessionOptions by the name it set it under.
62+
* GetSessionOptions by the name it set it under. And what is read back can be set back: the empty
63+
* value, Flight's way of unsetting an option, puts a variable back to its default and the session
64+
* back into no database, as it started -- the state GetSessionOptions reports as an empty
65+
* {@code schema}, which sets it too. A session is always in some catalog, so {@code catalog} has no
66+
* empty value.
6367
*
6468
* <p>The result of setting an option is one of the three {@link ErrorValue}s per name and nothing
6569
* else, so the reason a value was refused only reaches the frontend log.
@@ -118,7 +122,8 @@ static ErrorValue setOne(ConnectContext ctx, String name, SessionOptionValue val
118122

119123
/**
120124
* The options of the session: the current catalog, the current database (an empty string when
121-
* none has been chosen) and every session variable {@code SHOW VARIABLES} would list, in its text.
125+
* the session is in none, the value that puts it back there) and every session variable
126+
* {@code SHOW VARIABLES} would list, in its text.
122127
*/
123128
public static Map<String, SessionOptionValue> get(ConnectContext ctx) {
124129
Map<String, SessionOptionValue> options = new LinkedHashMap<>();
@@ -134,6 +139,7 @@ public static Map<String, SessionOptionValue> get(ConnectContext ctx) {
134139

135140
private static ErrorValue switchCatalog(ConnectContext ctx, SessionOptionValue value) {
136141
String catalog = value.acceptVisitor(STRING_VALUE);
142+
// A session is always in some catalog, so there is no catalog the empty value could stand for.
137143
if (catalog == null || catalog.isEmpty()) {
138144
return ErrorValue.INVALID_VALUE;
139145
}
@@ -151,9 +157,17 @@ private static ErrorValue switchCatalog(ConnectContext ctx, SessionOptionValue v
151157

152158
private static ErrorValue useDatabase(ConnectContext ctx, SessionOptionValue value) {
153159
String database = value.acceptVisitor(STRING_VALUE);
154-
if (database == null || database.isEmpty()) {
160+
if (database == null) {
155161
return ErrorValue.INVALID_VALUE;
156162
}
163+
// No database: the state the session started in, which GetSessionOptions reports as the
164+
// empty string and the empty value asks for back (Flight's way of unsetting an option; what
165+
// the ADBC driver sends to erase one). No statement leads there -- there is no USE of
166+
// nothing -- so it is not one the session runs, and there is nothing in it to check.
167+
if (database.isEmpty()) {
168+
ctx.clearDatabase();
169+
return null;
170+
}
157171
return runStatement(ctx, SCHEMA, "USE " + quoteIdentifier(database), ErrorCode.ERR_BAD_DB_ERROR);
158172
}
159173

@@ -223,7 +237,10 @@ private static String quoteIdentifier(String name) {
223237
return "`" + name.replace("`", "``") + "`";
224238
}
225239

226-
/** The string of a string-valued option; null for any other kind of value. */
240+
/**
241+
* The string of a string-valued option, the empty string for the empty value (no value, which
242+
* is what the empty string says too); null for any other kind of value.
243+
*/
227244
private static final SessionOptionValueVisitor<String> STRING_VALUE = new SessionOptionValueVisitor<String>() {
228245
@Override
229246
public String visit(String value) {
@@ -252,7 +269,7 @@ public String visit(String[] value) {
252269

253270
@Override
254271
public String visit(Void value) {
255-
return null;
272+
return "";
256273
}
257274
};
258275

‎fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,15 @@ public FunctionRegistry getFunctionRegistry() {
994994

995995
public void changeDefaultCatalog(String catalogName) {
996996
defaultCatalog = catalogName;
997+
clearDatabase();
998+
}
999+
1000+
/**
1001+
* No current database: the state a session starts in and switching catalogs puts it back into.
1002+
* No statement leads back to it (there is no USE of nothing), so a client that means to be in
1003+
* none again, e.g. through the Arrow Flight SQL {@code schema} session option, comes here.
1004+
*/
1005+
public void clearDatabase() {
9971006
currentDb = "";
9981007
currentDbId = -1;
9991008
}

‎fe/fe-core/src/test/java/org/apache/doris/arrowflight/FlightSessionOptionsTest.java‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,42 @@ public void testUnknownCatalogOrDatabaseIsAnInvalidValue() {
141141
Assertions.assertEquals(ErrorValue.INVALID_VALUE, set(ctx, FlightSessionOptions.SCHEMA, str("no_such_db")));
142142
Assertions.assertEquals("", ctx.getDatabase());
143143
// A catalog or a database is named by a string, and by nothing else.
144-
Assertions.assertEquals(ErrorValue.INVALID_VALUE, set(ctx, FlightSessionOptions.CATALOG, str("")));
145-
Assertions.assertEquals(ErrorValue.INVALID_VALUE,
146-
set(ctx, FlightSessionOptions.SCHEMA, SessionOptionValueFactory.makeEmptySessionOptionValue()));
147144
Assertions.assertEquals(ErrorValue.INVALID_VALUE,
148145
set(ctx, FlightSessionOptions.SCHEMA, SessionOptionValueFactory.makeSessionOptionValue(true)));
149146
Assertions.assertEquals(ErrorValue.INVALID_VALUE, set(ctx, FlightSessionOptions.CATALOG,
150147
SessionOptionValueFactory.makeSessionOptionValue(new String[] {"internal"})));
148+
// A session is always in some catalog: there is none the empty value or string could name.
149+
Assertions.assertEquals(ErrorValue.INVALID_VALUE, set(ctx, FlightSessionOptions.CATALOG, str("")));
150+
Assertions.assertEquals(ErrorValue.INVALID_VALUE,
151+
set(ctx, FlightSessionOptions.CATALOG, SessionOptionValueFactory.makeEmptySessionOptionValue()));
152+
Assertions.assertEquals("internal", ctx.getDefaultCatalog());
153+
}
154+
155+
// What GetSessionOptions reads back can be set back, the no-database state included: the empty
156+
// value (Flight's way of unsetting an option; what the ADBC driver sends to erase one) and the
157+
// empty string GET reports for it both put the session back into no database.
158+
@Test
159+
public void testTheEmptyValueOrStringLeavesTheDatabase() {
160+
ConnectContext ctx = rootSession();
161+
Assertions.assertNull(set(ctx, FlightSessionOptions.SCHEMA, str(DB)));
162+
Assertions.assertEquals(DB, ctx.getDatabase());
163+
Assertions.assertNull(set(ctx, FlightSessionOptions.SCHEMA, SessionOptionValueFactory.makeEmptySessionOptionValue()));
164+
Assertions.assertEquals("", ctx.getDatabase());
165+
Assertions.assertEquals("", get(ctx, FlightSessionOptions.SCHEMA));
166+
Assertions.assertEquals(-1, ctx.getCurrentDbId());
167+
168+
// The round trip: what GET answered for a fresh session, set back after a database was chosen.
169+
String initial = get(rootSession(), FlightSessionOptions.SCHEMA);
170+
Assertions.assertEquals("", initial);
171+
Assertions.assertNull(set(ctx, FlightSessionOptions.SCHEMA, str(DB)));
172+
Assertions.assertNull(set(ctx, FlightSessionOptions.SCHEMA, str(initial)));
173+
Assertions.assertEquals("", ctx.getDatabase());
174+
Assertions.assertEquals("", get(ctx, FlightSessionOptions.SCHEMA));
175+
// Leaving a database the session is not in is nothing, not an error; and a user without any
176+
// privilege may leave as well: nothing is entered.
177+
Assertions.assertNull(set(ctx, FlightSessionOptions.SCHEMA, str("")));
178+
Assertions.assertNull(set(flightSession(UserIdentity.createAnalyzedUserIdentWithIp(USER, "%")),
179+
FlightSessionOptions.SCHEMA, SessionOptionValueFactory.makeEmptySessionOptionValue()));
151180
}
152181

153182
@Test

‎regression-test/suites/arrow_flight_sql_p0/test_session_options.groovy‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,23 @@ suite("test_session_options") {
123123
assertEquals("internal", options()["catalog"])
124124
assertEquals(context.dbName, options()["schema"])
125125

126-
// 4. A session variable takes a value of its own type or a string, reads back as text, and
126+
// 4. And the session can leave its database again, back to how it started: with the empty
127+
// value (Flight's way of unsetting an option, what the ADBC driver sends to erase one) or
128+
// with the empty string GetSessionOptions reported for a fresh session, set back as read.
129+
// A session is always in some catalog, so `catalog` takes no empty value.
130+
assertEquals([:], set([schema: SessionOptionValueFactory.makeEmptySessionOptionValue()]))
131+
assertEquals("", options()["schema"])
132+
statementFails("SHOW TABLES LIKE 'session_options_tbl'", "No database selected")
133+
assertEquals([:], set([schema: str(context.dbName)]))
134+
assertEquals(context.dbName, options()["schema"])
135+
assertEquals([:], set([schema: str(initial["schema"])]))
136+
assertEquals("", options()["schema"])
137+
assertEquals([catalog: "INVALID_VALUE"], set([catalog: SessionOptionValueFactory.makeEmptySessionOptionValue()]))
138+
assertEquals([catalog: "INVALID_VALUE"], set([catalog: str("")]))
139+
assertEquals("internal", options()["catalog"])
140+
assertEquals([:], set([schema: str(context.dbName)]))
141+
142+
// 5. A session variable takes a value of its own type or a string, reads back as text, and
127143
// is what the session's statements see. The empty value sets it back to its default.
128144
def defaultTimeout = rows("SHOW VARIABLES LIKE 'query_timeout'")[0][2]
129145
assertEquals([:], set([query_timeout: SessionOptionValueFactory.makeSessionOptionValue(77L)]))
@@ -135,7 +151,7 @@ suite("test_session_options") {
135151
assertEquals([:], set([query_timeout: SessionOptionValueFactory.makeEmptySessionOptionValue()]))
136152
assertEquals(defaultTimeout, options()["query_timeout"])
137153

138-
// 5. Each option of a request is set on its own and answered on its own. A variable is named
154+
// 6. Each option of a request is set on its own and answered on its own. A variable is named
139155
// as GetSessionOptions names it, and only so: not in another case, not without the prefix
140156
// an experimental one is shown with, and a hidden or a retired one is no option at all.
141157
assertEquals([no_such_variable: "INVALID_NAME"], set([no_such_variable: str("1")]))
@@ -156,12 +172,12 @@ suite("test_session_options") {
156172
set([query_timeout: str("66"), no_such_variable: str("1"), time_zone: str("Mars/Olympus_Mons")]))
157173
assertEquals("66", options()["query_timeout"])
158174

159-
// 6. A string value arrives as sent, quotes and backslashes included.
175+
// 7. A string value arrives as sent, quotes and backslashes included.
160176
String text = "it's \"quoted\" and back\\slashed"
161177
assertEquals([:], set([session_context: str(text)]))
162178
assertEquals(text, options()["session_context"])
163179

164-
// 7. CloseSession invalidates the bearer token: the session is gone, and so is the token.
180+
// 8. CloseSession invalidates the bearer token: the session is gone, and so is the token.
165181
assertEquals("CLOSED", flight.closeSession(new CloseSessionRequest(), cred).getStatus().name())
166182
try {
167183
flight.getSessionOptions(new GetSessionOptionsRequest(), cred)
@@ -174,7 +190,7 @@ suite("test_session_options") {
174190
allocator.close()
175191
}
176192

177-
// 8. What a real driver does with it: the Flight SQL JDBC driver sets the `catalog` session
193+
// 9. What a real driver does with it: the Flight SQL JDBC driver sets the `catalog` session
178194
// option while connecting when the URL names one, and refuses to connect when that fails.
179195
Class.forName("org.apache.arrow.driver.jdbc.ArrowFlightJdbcDriver")
180196
def jdbcUrl = { String catalog ->

0 commit comments

Comments
 (0)