From b23d06c5652084a38c5ea6ecec508d31ae7649a7 Mon Sep 17 00:00:00 2001 From: Jose Venegas Vega Date: Thu, 23 Nov 2023 11:26:40 -0600 Subject: [PATCH 1/5] SCT-7633 Adding com.snowflake.snowpark.Session.SessionBuilder.GetOrCreate --- .../com/snowflake/snowpark_java/SessionBuilder.java | 10 ++++++++++ src/main/scala/com/snowflake/snowpark/Session.scala | 10 ++++++++++ .../com/snowflake/snowpark_test/JavaSessionSuite.java | 8 ++++++++ .../com/snowflake/snowpark_test/SessionSuite.scala | 5 +++++ 4 files changed, 33 insertions(+) diff --git a/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java b/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java index 344266c6..210a426e 100644 --- a/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java +++ b/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java @@ -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()); + } } diff --git a/src/main/scala/com/snowflake/snowpark/Session.scala b/src/main/scala/com/snowflake/snowpark/Session.scala index ea1e5913..aa8058d0 100644 --- a/src/main/scala/com/snowflake/snowpark/Session.scala +++ b/src/main/scala/com/snowflake/snowpark/Session.scala @@ -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(_) => diff --git a/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java index a0d7de57..068e9f5e 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java @@ -81,6 +81,14 @@ public void flatten() { new Row[] {Row.create("1"), Row.create("2")}); } + @Test + public void getOrCreate() + { + String expectedSession = getSession().getSessionInfo(); + String actualSession = Session.builder().getOrCreate().getSessionInfo(); + assert(expectedSession.equals(actualSession)); + } + @Test public void getSessionInfo() { String result = getSession().getSessionInfo(); diff --git a/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala b/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala index 2803378c..71ed1aac 100644 --- a/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala @@ -49,6 +49,11 @@ class SessionSuite extends SNTestBase { t2.run() } + test("Test for get or create session") { + val actualSession = Session.builder.getOrCreate + assert(actualSession == session) + } + test("Test for invalid configs") { val badSessionBuilder = Session.builder .configFile(defaultProfile) From 3d92d37e33660a800e9bb4fdeb6102ad21631c7d Mon Sep 17 00:00:00 2001 From: Jose Venegas Vega Date: Thu, 23 Nov 2023 11:46:05 -0600 Subject: [PATCH 2/5] SCT-7633 Adding com.snowflake.snowpark.Session.SessionBuilder.GetOrCreate --- src/main/java/com/snowflake/snowpark_java/SessionBuilder.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java b/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java index 210a426e..e9675a34 100644 --- a/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java +++ b/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java @@ -78,6 +78,9 @@ public Session create() { * @since 1.10.0 */ public Session getOrCreate() { + // disable closure cleaner in Java session, + // it only works with Scala UDF. + this.builder.config("snowpark_enable_closure_cleaner", "never"); return new Session(this.builder.getOrCreate()); } } From 7dc2f1521044c710e0630d1ce24716d9bfed1696 Mon Sep 17 00:00:00 2001 From: Jose Venegas Vega Date: Thu, 23 Nov 2023 11:53:41 -0600 Subject: [PATCH 3/5] Updating java GetOrCreate test variable names --- .../java/com/snowflake/snowpark_test/JavaSessionSuite.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java index 068e9f5e..75bda890 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaSessionSuite.java @@ -84,9 +84,9 @@ public void flatten() { @Test public void getOrCreate() { - String expectedSession = getSession().getSessionInfo(); - String actualSession = Session.builder().getOrCreate().getSessionInfo(); - assert(expectedSession.equals(actualSession)); + String expectedSessionInfo = getSession().getSessionInfo(); + String actualSessionInfo = Session.builder().getOrCreate().getSessionInfo(); + assert(actualSessionInfo.equals(expectedSessionInfo)); } @Test From 48ec89723d042c2205e6ceb88e730c1ba615f58f Mon Sep 17 00:00:00 2001 From: Jose Venegas Vega Date: Thu, 23 Nov 2023 13:34:51 -0600 Subject: [PATCH 4/5] Updating getOrCreate for java --- src/main/java/com/snowflake/snowpark_java/SessionBuilder.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java b/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java index e9675a34..210a426e 100644 --- a/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java +++ b/src/main/java/com/snowflake/snowpark_java/SessionBuilder.java @@ -78,9 +78,6 @@ public Session create() { * @since 1.10.0 */ public Session getOrCreate() { - // disable closure cleaner in Java session, - // it only works with Scala UDF. - this.builder.config("snowpark_enable_closure_cleaner", "never"); return new Session(this.builder.getOrCreate()); } } From 9e52bc5db9c37d568d3195aa708c5ca7907f289f Mon Sep 17 00:00:00 2001 From: Jose Venegas Vega Date: Mon, 27 Nov 2023 14:30:17 -0600 Subject: [PATCH 5/5] Solving scala failing test --- .../scala/com/snowflake/snowpark_test/SessionSuite.scala | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala b/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala index 71ed1aac..6ab7d5db 100644 --- a/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/SessionSuite.scala @@ -50,8 +50,9 @@ class SessionSuite extends SNTestBase { } test("Test for get or create session") { - val actualSession = Session.builder.getOrCreate - assert(actualSession == session) + val session1 = Session.builder.getOrCreate + val session2 = Session.builder.getOrCreate + assert(session1 == session2) } test("Test for invalid configs") {