Skip to content
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

SCT-7633 Adding com.snowflake.snowpark.Session.SessionBuilder.getOrCreate #67

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/SessionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ public Session create() {
this.builder.config("snowpark_enable_closure_cleaner", "never");
return new Session(builder.create());
}

/**
* Returns the existing session if already exists or create it if not.
*
* @return A {@code Session} object
* @since 1.10.0
*/
public Session getOrCreate() {
return new Session(this.builder.getOrCreate());
}
}
10 changes: 10 additions & 0 deletions src/main/scala/com/snowflake/snowpark/Session.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,16 @@ object Session extends Logging {
createInternal(None)
}

/**
* Returns the existing session if already exists or create it if not.
*
* @return A [[Session]]
* @since 1.10.0
*/
def getOrCreate: Session = {
Session.getActiveSession.getOrElse(create)
}

private[snowpark] def createInternal(conn: Option[SnowflakeConnectionV1]): Session = {
conn match {
case Some(_) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public void flatten() {
new Row[] {Row.create("1"), Row.create("2")});
}

@Test
public void getOrCreate()
{
String expectedSessionInfo = getSession().getSessionInfo();
String actualSessionInfo = Session.builder().getOrCreate().getSessionInfo();
assert(actualSessionInfo.equals(expectedSessionInfo));
}

@Test
public void getSessionInfo() {
String result = getSession().getSessionInfo();
Expand Down
6 changes: 6 additions & 0 deletions src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class SessionSuite extends SNTestBase {
t2.run()
}

test("Test for get or create session") {
sfc-gh-jvenegasvega marked this conversation as resolved.
Show resolved Hide resolved
val session1 = Session.builder.getOrCreate
val session2 = Session.builder.getOrCreate
assert(session1 == session2)
}

test("Test for invalid configs") {
val badSessionBuilder = Session.builder
.configFile(defaultProfile)
Expand Down
Loading