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

SNOW-802269 To Add reverse isnull conv unixtimestamp #147

Merged
Merged
Show file tree
Hide file tree
Changes from 13 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
70 changes: 70 additions & 0 deletions src/main/java/com/snowflake/snowpark_java/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.snowflake.snowpark_java.udf.*;
import java.util.List;
import java.util.function.Supplier;
import net.snowflake.client.jdbc.internal.org.checkerframework.checker.units.qual.C;
sfc-gh-sjayabalan marked this conversation as resolved.
Show resolved Hide resolved

/**
* Provides utility functions that generate Column expression that you can pass to DataFrame
Expand Down Expand Up @@ -3880,6 +3881,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) {
sfc-gh-sjayabalan marked this conversation as resolved.
Show resolved Hide resolved
return new Column(com.snowflake.snowpark.functions.unix_timestamp(C.toScalaColumn()));
}

/**
* Signature - snowflake.snowpark.functions.regexp_extract (value: Union[Column, str], regexp:
Expand Down
60 changes: 60 additions & 0 deletions src/main/scala/com/snowflake/snowpark/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.snowflake.snowpark.types.TimestampType

import scala.reflect.runtime.universe.TypeTag
import scala.util.Random
import java.util.StringJoiner

/**
* Provides utility functions that generate [[Column]] expressions that you can pass to
Expand Down Expand Up @@ -3143,6 +3144,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 +3684,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 +3812,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
21 changes: 21 additions & 0 deletions src/test/java/com/snowflake/snowpark_test/JavaFunctionSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,27 @@ public void any_value() {
}

@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);
}

public void unix_timestamp() {
sfc-gh-sjayabalan marked this conversation as resolved.
Show resolved Hide resolved
DataFrame df =
getSession().sql("select * from values('2024-04-08T23:39:20.123-07:00') as t(a)");
checkAnswer(
df.select(Functions.unix_timestamp(df.col("a"))),
new Row[] {Row.create("1712619560123")},
false);
}

public void regexp_extract() {
DataFrame df = getSession().sql("select * from values('A MAN A PLAN A CANAL') as T(a)");
Row[] expected = {Row.create("MAN")};
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
Loading