Skip to content

Commit

Permalink
SNOW-802269 To Add reverse isnull conv unixtimestamp (#147)
Browse files Browse the repository at this point in the history
* Added functions to scala and java

* Added test cases

* modified

* Modified version,comments

* modified comments and test cases

* Modified format and test case

* Modified format and test case

* Modified format and test case

* Modified format and test case

* Modified format and test case

* Modified comments

* Modified testcase

* Modified testcase result

* Removed the import string joiner
  • Loading branch information
sfc-gh-sjayabalan authored Aug 22, 2024
1 parent 172caca commit 33f9142
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
69 changes: 69 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>Example:
*
* <pre>{@code
* SELECT REVERSE('Hello, world!');
* +--------------------------+
* | REVERSE('HELLO, WORLD!') |
* |--------------------------|
* | !dlrow ,olleH |
* +--------------------------+
* }</pre>
*
* @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.
*
* <p>Example::
*
* <pre>{@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)]
* }</pre>
*
* @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
*
* <p>Example - DATE_PART( date_or_time_part ,date_or_time_expr )
*
* <pre>{@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 |
* +-------------------------+------------------------+
* }</pre>
*
* @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:
Expand Down
59 changes: 59 additions & 0 deletions src/main/scala/com/snowflake/snowpark/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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( <date_or_time_part> , <date_or_time_expr> )
* 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
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
*
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/snowflake/snowpark_test/JavaFunctionSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down Expand Up @@ -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)};
Expand Down
18 changes: 18 additions & 0 deletions src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 33f9142

Please sign in to comment.