diff --git a/src/main/java/com/snowflake/snowpark_java/Functions.java b/src/main/java/com/snowflake/snowpark_java/Functions.java index 78c64418..1ca240fc 100644 --- a/src/main/java/com/snowflake/snowpark_java/Functions.java +++ b/src/main/java/com/snowflake/snowpark_java/Functions.java @@ -3880,6 +3880,75 @@ public static Column listagg(Column col, String delimiter) { public static Column listagg(Column col) { return new Column(com.snowflake.snowpark.functions.listagg(col.toScalaColumn())); } + /** + * Wrapper for Snowflake built-in reverse function. Gets the reversed string. Reverses the order + * of characters in a string, or of bytes in a binary value. The returned value is the same length + * as the input, but with the characters/bytes in reverse order. If subject is NULL, the result is + * also NULL. + * + *

Example: + * + *

{@code
+   * SELECT REVERSE('Hello, world!');
+   *   +--------------------------+
+   *   | REVERSE('HELLO, WORLD!') |
+   *   |--------------------------|
+   *   | !dlrow ,olleH            |
+   *   +--------------------------+
+   * }
+ * + * @since 1.14.0 + * @param name Column to be reverse. + * @return Column object. + */ + public static Column reverse(Column name) { + return new Column(com.snowflake.snowpark.functions.reverse(name.toScalaColumn())); + } + + /** + * Wrapper for Snowflake built-in isnull function. Gets a boolean depending if value is NULL or + * not. Return true if the value in the column is null. + * + *

Example:: + * + *

{@code
+   * >>> from snowflake.snowpark.functions import is_null >>> df = session.create_dataframe([1.2,
+   * float("nan"), None, 1.0], schema=["a"]) >>> df.select(is_null("a").as_("a")).collect()
+   * [Row(A=False), Row(A=False), Row(A=True), Row(A=False)]
+   * }
+ * + * @since 1.14.0 + * @param c Column to analyze if it is null value. + * @return Column object. + */ + public static Column isnull(Column c) { + return new Column(com.snowflake.snowpark.functions.isnull(c.toScalaColumn())); + } + + /** + * Returns the current Unix timestamp (in seconds) as a long. Extracts a specified date or time + * portion from a date, time, or timestamp. All calls of `unix_timestamp` within the same query + * return the same value + * + *

Example - DATE_PART( date_or_time_part ,date_or_time_expr ) + * + *

{@code
+   * SELECT TO_TIMESTAMP('2013-05-08T23:39:20.123-07:00') AS "TIME_STAMP1",
+   *  DATE_PART(EPOCH_SECOND, "TIME_STAMP1") AS "EXTRACTED EPOCH SECOND";
+   * +-------------------------+------------------------+
+   * | TIME_STAMP1             | EXTRACTED EPOCH SECOND |
+   * |-------------------------+------------------------|
+   * | 2013-05-08 23:39:20.123 |             1368056360 |
+   * +-------------------------+------------------------+
+   * }
+ * + * @since 1.14.0 + * @param c Column to be converted. + * @return Column object. + */ + public static Column unix_timestamp(Column c) { + return new Column(com.snowflake.snowpark.functions.unix_timestamp(c.toScalaColumn())); + } /** * Signature - snowflake.snowpark.functions.regexp_extract (value: Union[Column, str], regexp: diff --git a/src/main/scala/com/snowflake/snowpark/functions.scala b/src/main/scala/com/snowflake/snowpark/functions.scala index 8526805c..fd8b7ab1 100644 --- a/src/main/scala/com/snowflake/snowpark/functions.scala +++ b/src/main/scala/com/snowflake/snowpark/functions.scala @@ -3143,6 +3143,63 @@ object functions { /** + * Wrapper for Snowflake built-in reverse function. Gets the reversed string. + * Reverses the order of characters in a string, or of bytes in a binary value. + * The returned value is the same length as the input, but with the characters/bytes + * in reverse order. If subject is NULL, the result is also NULL. + * Example: SELECT REVERSE('Hello, world!'); + *+--------------------------+ + *| REVERSE('HELLO, WORLD!') | + *|--------------------------| + *| !dlrow ,olleH | + *+--------------------------+ + + * @since 1.14.0 + * @param c Column to be reverse. + * @return Column object. + */ + def reverse(c: Column): Column = + builtin("reverse")(c) + + /** + * Wrapper for Snowflake built-in isnull function. Gets a boolean + * depending if value is NULL or not. + * Return true if the value in the column is null. + *Example:: + * >>> from snowflake.snowpark.functions import is_null + * >>> df = session.create_dataframe([1.2, float("nan"), None, 1.0], + * schema=["a"]) + * >>> df.select(is_null("a").as_("a")).collect() + * [Row(A=False), Row(A=False), Row(A=True), Row(A=False)] + * @since 1.14.0 + * @param c Column to qnalize if it is null value. + * @return Column object. + */ + def isnull(c: Column): Column = is_null(c) + + /** + * Returns the current Unix timestamp (in seconds) as a long. + * Extracts a specified date or time portion from a date, time, or timestamp. + * how: + * EXTRACT , HOUR / MINUTE / SECOND , YEAR* / DAY* / WEEK* / MONTH / QUARTER + * Construction - DATE_PART( , ) + * SELECT TO_TIMESTAMP('2013-05-08T23:39:20.123-07:00') AS "TIME_STAMP1", + * DATE_PART(EPOCH_SECOND, "TIME_STAMP1") AS "EXTRACTED EPOCH SECOND"; + * +-------------------------+------------------------+ + * | TIME_STAMP1 | EXTRACTED EPOCH SECOND | + * |-------------------------+------------------------| + * | 2013-05-08 23:39:20.123 | 1368056360 | + * +-------------------------+------------------------+ + * @since 1.14.0 + * @note All calls of `unix_timestamp` within the same query return the same value + */ + def unix_timestamp(c: Column): Column = { + builtin("date_part")("epoch_second", c) + } + + /** + + * Signature - snowflake.snowpark.functions.regexp_extract * (value: Union[Column, str], regexp: Union[Column, str], idx: int) * Column @@ -3626,6 +3683,7 @@ object functions { def unbase64(col: Column): Column = callBuiltin("BASE64_DECODE_STRING", col) /** + * * Locate the position of the first occurrence of substr in a string column, after position pos. * @@ -3753,6 +3811,7 @@ object functions { builtin("RANDOM")(seed) /** + * Invokes a built-in snowflake function with the specified name and arguments. * Arguments can be of two types * diff --git a/src/test/java/com/snowflake/snowpark_test/JavaFunctionSuite.java b/src/test/java/com/snowflake/snowpark_test/JavaFunctionSuite.java index bdd14337..8b3369f2 100644 --- a/src/test/java/com/snowflake/snowpark_test/JavaFunctionSuite.java +++ b/src/test/java/com/snowflake/snowpark_test/JavaFunctionSuite.java @@ -2765,6 +2765,31 @@ public void any_value() { assert result[0].getInt(0) == 1 || result[0].getInt(0) == 2 || result[0].getInt(0) == 3; } + @Test + public void reverse() { + DataFrame df = getSession().sql("select * from values('cat') as t(a)"); + checkAnswer(df.select(Functions.reverse(df.col("a"))), new Row[] {Row.create("tac")}, false); + } + + @Test + public void isnull() { + DataFrame df = getSession().sql("select * from values(1.2),(null),(2.3) as T(a)"); + Row[] expected = {Row.create(false), Row.create(true), Row.create(false)}; + checkAnswer(df.select(Functions.isnull(df.col("a"))), expected, false); + } + + @Test + public void unix_timestamp() { + DataFrame df = + getSession() + .sql( + "select to_timestamp('2013-05-08 23:39:20.123') as a from values('2013-05-08 23:39:20.123') as t(a)"); + checkAnswer( + df.select(Functions.unix_timestamp(df.col("a"))), + new Row[] {Row.create(1368056360)}, + false); + } + @Test public void regexp_extract() { DataFrame df = getSession().sql("select * from values('A MAN A PLAN A CANAL') as T(a)"); @@ -2819,6 +2844,7 @@ public void substring_index() { false); } + @Test public void test_asc() { DataFrame df = getSession().sql("select * from values(3),(1),(2) as t(a)"); Row[] expected = {Row.create(1), Row.create(2), Row.create(3)}; diff --git a/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala b/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala index 4f31f201..b2d21880 100644 --- a/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala @@ -2325,6 +2325,24 @@ trait FunctionSuite extends TestData { checkAnswer(input.select(unbase64(col("a")).as("unbase64")), expected, sort = false) } + test("reverse") { + val data = Seq("cat").toDF("a") + checkAnswer(data.select(reverse(col("a"))), Seq(Row("tac")), sort = false) + } + + test("isnull") { + checkAnswer( + nanData1.select(equal_nan(col("A")), isnull(col("A"))), + Seq(Row(false, false), Row(true, false), Row(null, true), Row(false, false)), + sort = false) + } + + test("unix_timestamp") { + val expected = Seq(1368056360).toDF("epoch") + val data = Seq(Timestamp.valueOf("2013-05-08 23:39:20.123")).toDF("a") + checkAnswer(data.select(unix_timestamp(col("a"))), expected, sort = false) + } + test("locate Column function") { val input = session.createDataFrame(Seq(("scala", "java scala python"), ("b", "abcd"))).toDF("a", "b")