Skip to content

Commit

Permalink
SCT-2668 Add appName method to SessionBuilder to register in query_ta…
Browse files Browse the repository at this point in the history
…g. (#85)

* Add appName methods to SessionBuilder to register in query_tag.

* Add tests

* Update appName function documentation

* resolve comments

* update functions version
  • Loading branch information
sfc-gh-dchaconbarrantes authored Mar 4, 2024
1 parent e46f644 commit e968927
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/SessionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,17 @@ public Session create() {
public Session getOrCreate() {
return new Session(this.builder.getOrCreate());
}

/**
* Adds the app name to set in the query_tag after session creation.
* The query tag will be set with this format 'APPNAME=${appName}'.
*
* @param appName Name of the app.
* @return A {@code SessionBuilder} object
* @since 1.12.0
*/
public SessionBuilder appName(String appName) {
this.builder.appName(appName);
return this;
}
}
22 changes: 21 additions & 1 deletion src/main/scala/com/snowflake/snowpark/Session.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,7 @@ object Session extends Logging {
private var options: Map[String, String] = Map()

private var isScalaAPI: Boolean = true
private var appName: Option[String] = None

// used by Java API only
private[snowpark] def setJavaAPI(): SessionBuilder = {
Expand All @@ -1407,6 +1408,20 @@ object Session extends Logging {
this
}


/**
* Adds the app name to set in the query_tag after session creation.
* The query tag will be set with this format 'APPNAME=${appName}'.
*
* @param appName Name of the app.
* @return A [[SessionBuilder]]
* @since 1.12.0
*/
def appName(appName: String): SessionBuilder = {
this.appName = Some(appName)
this
}

/**
* Adds the configuration properties in the specified file to the SessionBuilder configuration.
*
Expand Down Expand Up @@ -1467,7 +1482,12 @@ object Session extends Logging {
* @since 0.1.0
*/
def create: Session = {
createInternal(None)
val session = createInternal(None)
val appName = this.appName
if (appName.isDefined) {
session.setQueryTag(s"APPNAME=${appName.get}")
}
session
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public void getOrCreate() {
assert (actualSessionInfo.equals(expectedSessionInfo));
}

@Test
public void appName() {
String appName = "my-app";
String expectedAppName = String.format("APPNAME=%s", appName);
Session session = Session.builder().configFile(defaultProfile).appName(appName).create();
assert (expectedAppName.equals(session.getQueryTag().get()));
}

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

}

test("Set an app name in the query tag") {
val appName = "my_app"
val expectedAppName = s"APPNAME=$appName"
val newSession = Session.builder.appName(appName).configFile(defaultProfile).create
assert(getParameterValue("query_tag", newSession) == expectedAppName)
}

test("The app name is not defined") {
val newSession = Session.builder.configFile(defaultProfile).create
assert(getParameterValue("query_tag", newSession) == "")
}

test("generator") {
checkAnswer(
session.generator(3, Seq(lit(1).as("a"), lit(2).as("b"))),
Expand Down

0 comments on commit e968927

Please sign in to comment.