Skip to content

Commit

Permalink
add java log function and test case
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-gmahadevan committed Aug 14, 2024
1 parent ca0ad9d commit c652f12
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
84 changes: 84 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4053,6 +4053,90 @@ public static Column last(Column col) {
return new Column(functions.last(col.toScalaColumn()));
}

/**
* Computes the logarithm of the given value in base 10.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values (100) as T(a)");
* df.select(Functions.log10(df.col("a")).as("log10")).show();
* -----------
* |"LOG10" |
* -----------
* |2.0 |
* -----------
* }</pre>
*
* @since 1.14.0
* @param col The input column to get last value
* @return column object from last function.
*/
public static Column log10(Column col) {
return new Column(functions.log10(col.toScalaColumn()));
}

/**
* Computes the logarithm of the given value in base 10.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values (100) as T(a)");
* df.select(Functions.log10("a").as("log10")).show();
* -----------
* |"LOG10" |
* -----------
* |2.0 |
* -----------
* }</pre>
*
* @since 1.14.0
* @param s The input column to get last value
* @return column object from last function.
*/
public static Column log10(String s) {
return new Column(functions.log10(s));
}

/**
* Computes the logarithm of the given value in base 10.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values (0.1) as T(a)");
* df.select(Functions.log1p(df.col("a")).as("log1p")).show();
* -----------------------
* |"LOG1P" |
* -----------------------
* |0.09531017980432493 |
* -----------------------
* }</pre>
*
* @since 1.14.0
* @param col The input column to get last value
* @return column object from last function.
*/
public static Column log1p(Column col) {
return new Column(functions.log1p(col.toScalaColumn()));
}

/**
* Computes the logarithm of the given value in base 10.
*
* <pre>{@code
* DataFrame df = getSession().sql("select * from values (0.1) as T(a)");
* df.select(Functions.log1p("a").as("log1p")).show();
* -----------------------
* |"LOG1P" |
* -----------------------
* |0.09531017980432493 |
* -----------------------
* }</pre>
*
* @since 1.14.0
* @param s The input column to get last value
* @return column object from last function.
*/
public static Column log1p(String s) {
return new Column(functions.log1p(s));
}

/**
* Calls a user-defined function (UDF) by name.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/com/snowflake/snowpark/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3357,7 +3357,7 @@ object functions {
*Example
* {{{
* val df = session.createDataFrame(Seq(0.1)).toDF("a")
* df.select(log1p(col("a"))).show()
* df.select(log1p(col("a")).as("log1p")).show()
* -----------------------
* |"LOG1P" |
* -----------------------
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/snowflake/snowpark_test/JavaFunctionSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -2828,4 +2828,36 @@ public void last() {
expected,
false);
}

@Test
public void log10_col() {
DataFrame df = getSession().sql("select * from values (100) as T(a)");
Row[] expected = {Row.create(2.0)};

checkAnswer(df.select(Functions.log10(df.col("a"))), expected, false);
}

@Test
public void log10_str() {
DataFrame df = getSession().sql("select * from values (100) as T(a)");
Row[] expected = {Row.create(2.0)};

checkAnswer(df.select(Functions.log10("a")), expected, false);
}

@Test
public void log1p_col() {
DataFrame df = getSession().sql("select * from values (0.1) as T(a)");
Row[] expected = {Row.create(0.09531017980432493)};

checkAnswer(df.select(Functions.log1p(df.col("a"))), expected, false);
}

@Test
public void log1p_str() {
DataFrame df = getSession().sql("select * from values (0.1) as T(a)");
Row[] expected = {Row.create(0.09531017980432493)};

checkAnswer(df.select(Functions.log1p("a")), expected, false);
}
}

0 comments on commit c652f12

Please sign in to comment.