diff --git a/src/main/scala/com/snowflake/snowpark/functions.scala b/src/main/scala/com/snowflake/snowpark/functions.scala index 5c6f599f..2f40dfe4 100644 --- a/src/main/scala/com/snowflake/snowpark/functions.scala +++ b/src/main/scala/com/snowflake/snowpark/functions.scala @@ -3312,6 +3312,62 @@ object functions { def last(c: Column): Column = builtin("LAST_VALUE")(c) + /** + * Computes the logarithm of the given value in base 10. + * Example + * {{{ + * val df = session.createDataFrame(Seq(100)).toDF("a") + * df.select(log10(col("a"))).show() + * + * ----------- + * |"LOG10" | + * ----------- + * |2.0 | + * ----------- + * }}} + * + * @since 1.14.0 + * @param c Column to apply logarithm operation + * @return log10 of the given column + */ + def log10(c: Column): Column = builtin("LOG")(10, c) + + /** + * Computes the logarithm of the given column in base 10. + * Example + * {{{ + * val df = session.createDataFrame(Seq(100)).toDF("a") + * df.select(log10("a"))).show() + * ----------- + * |"LOG10" | + * ----------- + * |2.0 | + * ----------- + * + * }}} + * + * @since 1.14.0 + * @param columnName Column to apply logarithm operation + * @return log10 of the given column + */ + def log10(columnName: String): Column = builtin("LOG")(10, col(columnName)) + + /** + * Computes the natural logarithm of the given value plus one. + * @since 1.14.0 + * @param c the value to use + * @return the natural logarithm of the given value plus one. + */ + def log1p(c: Column): Column = callBuiltin("ln", lit(1) + c) + + /** + * Computes the natural logarithm of the given value plus one. + * @since 1.14.0 + * @param columnName the value to use + * @return the natural logarithm of the given value plus one. + */ + def log1p(columnName: String): Column = callBuiltin("ln", lit(1) + col(columnName)) + /** * Invokes a built-in snowflake function with the specified name and arguments. * Arguments can be of two types diff --git a/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala b/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala index 8a89d87b..18088f36 100644 --- a/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala @@ -2233,7 +2233,6 @@ trait FunctionSuite extends TestData { } test("last function") { - val input = Seq((5, "a", 10), (5, "b", 20), (3, "d", 15), (3, "e", 40)).toDF("grade", "name", "score") val window = Window.partitionBy(col("grade")).orderBy(col("score").desc) @@ -2245,6 +2244,48 @@ trait FunctionSuite extends TestData { sort = false) } + test("log10 Column function") { + val input = session.createDataFrame(Seq(100)).toDF("a") + val expected = Seq(2.0).toDF("log10") + + checkAnswer( + input.select(log10(col("a")).as("log10")), + expected, + sort = false) + } + + test("log10 String function") { + val input = session.createDataFrame(Seq("100")).toDF("a") + val expected = Seq(2.0).toDF("log10") + + checkAnswer( + input.select(log10("a").as("log10")), + expected, + sort = false) + } + + test("log1p Column function") { + val input = session.createDataFrame(Seq(100)).toDF("a") + + input.select(log1p(col("a"))).show() +// val expected = Seq(2.0).toDF("log10") +// +// checkAnswer( +// input.select(log10(col("a")).as("log10")), +// expected, +// sort = false) + } +// +// test("log1p String function") { +// val input = session.createDataFrame(Seq("100")).toDF("a") +// val expected = Seq(2.0).toDF("log10") +// +// checkAnswer( +// input.select(log10("a").as("log10")), +// expected, +// sort = false) +// } + } class EagerFunctionSuite extends FunctionSuite with EagerSession