-
Notifications
You must be signed in to change notification settings - Fork 623
[client-v2] Session API with reusable Session objects #2810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chernser
wants to merge
9
commits into
main
Choose a base branch
from
03/26/26/set_session
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5634dfe
Implemented core API for sessions
chernser d937f95
Merge branch 'main' into 03/26/26/set_session
chernser f43b319
Merge branch 'main' into 03/26/26/set_session
chernser 22ab21b
added timezone method and more tests
chernser ce95250
Merge branch 'main' into 03/26/26/set_session
chernser 1d68550
Formalized session and separate object to hold session specific config
chernser 6f1ca06
Added example and updated documentation
chernser 0d0e641
Merge branch 'main' into 03/26/26/set_session
chernser 7ae5f16
Replaced remove with get when working with configuration to not chang…
chernser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
client-v2/src/main/java/com/clickhouse/client/api/Session.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package com.clickhouse.client.api; | ||
|
|
||
| import com.clickhouse.client.api.http.ClickHouseHttpProto; | ||
| import com.clickhouse.client.api.internal.ValidationUtils; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Reusable ClickHouse session configuration that can be applied to clients or operation settings. | ||
| */ | ||
| public class Session { | ||
| private String sessionId; | ||
| private Boolean sessionCheck; | ||
| private Integer sessionTimeout; | ||
| private String sessionTimezone; | ||
|
|
||
| static Session extractFrom(Map<String, Object> configuration) { | ||
chernser marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Session session = new Session(); | ||
|
|
||
| String sessionId = (String) configuration.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_ID)); | ||
| if (sessionId != null) { | ||
| session.setSessionId(sessionId); | ||
| } | ||
|
|
||
| String sessionCheck = (String) configuration.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_CHECK)); | ||
| if (sessionCheck != null) { | ||
| session.setSessionCheck("1".equals(sessionCheck) || Boolean.parseBoolean(sessionCheck)); | ||
| } | ||
|
|
||
| String sessionTimeout = (String) configuration.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_TIMEOUT)); | ||
| if (sessionTimeout != null) { | ||
| session.setSessionTimeout(Integer.parseInt(sessionTimeout)); | ||
| } | ||
|
|
||
| String sessionTimezone = (String) configuration.get(ClientConfigProperties.serverSetting(ClickHouseHttpProto.QPARAM_SESSION_TIMEZONE)); | ||
| if (sessionTimezone != null) { | ||
| session.setSessionTimezone(sessionTimezone); | ||
| } | ||
|
|
||
| return session; | ||
| } | ||
|
|
||
| public synchronized Session setSessionId(String sessionId) { | ||
| ValidationUtils.checkNonBlank(sessionId, ClickHouseHttpProto.QPARAM_SESSION_ID); | ||
| this.sessionId = sessionId; | ||
| return this; | ||
| } | ||
|
|
||
| public synchronized String getSessionId() { | ||
| return sessionId; | ||
| } | ||
|
|
||
| public synchronized Session setSessionCheck(boolean sessionCheck) { | ||
| this.sessionCheck = sessionCheck; | ||
| return this; | ||
| } | ||
|
|
||
| public synchronized Boolean getSessionCheck() { | ||
| return sessionCheck; | ||
| } | ||
|
|
||
| public synchronized Session setSessionTimeout(int timeoutInSeconds) { | ||
| ValidationUtils.checkPositive(timeoutInSeconds, ClickHouseHttpProto.QPARAM_SESSION_TIMEOUT); | ||
| this.sessionTimeout = timeoutInSeconds; | ||
| return this; | ||
| } | ||
|
|
||
| public synchronized Integer getSessionTimeout() { | ||
| return sessionTimeout; | ||
| } | ||
|
|
||
| public synchronized Session setSessionTimezone(String timezone) { | ||
| ValidationUtils.checkNonBlank(timezone, ClickHouseHttpProto.QPARAM_SESSION_TIMEZONE); | ||
| this.sessionTimezone = timezone; | ||
| return this; | ||
| } | ||
|
|
||
| public synchronized String getSessionTimezone() { | ||
| return sessionTimezone; | ||
| } | ||
|
|
||
| public synchronized void updateSessionId(String sessionId) { | ||
| setSessionId(sessionId); | ||
| } | ||
|
|
||
| public synchronized void applyTo(Map<String, Object> requestSettings) { | ||
| putIfSet(requestSettings, ClickHouseHttpProto.QPARAM_SESSION_ID, sessionId); | ||
| putIfSet(requestSettings, ClickHouseHttpProto.QPARAM_SESSION_CHECK, | ||
| sessionCheck == null ? null : (sessionCheck ? "1" : "0")); | ||
|
Check warning on line 89 in client-v2/src/main/java/com/clickhouse/client/api/Session.java
|
||
| putIfSet(requestSettings, ClickHouseHttpProto.QPARAM_SESSION_TIMEOUT, | ||
| sessionTimeout == null ? null : String.valueOf(sessionTimeout)); | ||
| putIfSet(requestSettings, ClickHouseHttpProto.QPARAM_SESSION_TIMEZONE, sessionTimezone); | ||
| } | ||
|
|
||
| private static void putIfSet(Map<String, Object> settings, String key, String value) { | ||
| if (value != null) { | ||
| settings.put(ClientConfigProperties.serverSetting(key), value); | ||
| } | ||
| } | ||
| } | ||
30 changes: 30 additions & 0 deletions
30
client-v2/src/main/java/com/clickhouse/client/api/command/CommandSettings.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,36 @@ | ||
| package com.clickhouse.client.api.command; | ||
|
|
||
| import com.clickhouse.client.api.Session; | ||
| import com.clickhouse.client.api.query.QuerySettings; | ||
|
|
||
| public class CommandSettings extends QuerySettings { | ||
| @Override | ||
| public CommandSettings setSessionId(String sessionId) { | ||
| super.setSessionId(sessionId); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public CommandSettings setSessionCheck(boolean sessionCheck) { | ||
| super.setSessionCheck(sessionCheck); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public CommandSettings setSessionTimeout(int timeoutInSeconds) { | ||
| super.setSessionTimeout(timeoutInSeconds); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public CommandSettings setSessionTimezone(String timezone) { | ||
| super.setSessionTimezone(timezone); | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public CommandSettings use(Session session) { | ||
| super.use(session); | ||
| return this; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.