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-2668 Add appName method to SessionBuilder to register in query_tag. #85

Merged
merged 7 commits into from
Mar 4, 2024
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
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
Loading