Skip to content

Commit

Permalink
Added testcases for date_sub,try_cast,json_tupple
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-sjayabalan committed Aug 2, 2024
1 parent b71c43c commit 200361e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/main/scala/com/snowflake/snowpark/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3256,21 +3256,43 @@ object functions {
def from_json(e: Column): Column = {
builtin("TRY_PARSE_JSON")(e)
}

/**
* Returns the value of sourceExpr cast to data type
* targetType if possible, or NULL if not possible.
* @since 1.5.0
* @param source Any castable expression
* @param Target The type of the result
* @return The result is of type targetType.
* This function is a more relaxed variant of castfunction
* which includes a detailed description.
* try_cast differs from cast function by tolerating
* the following conditions as long as the cast
* from the type of expr to type is supported:
* If a sourceExpr value cannot fit within the domain of
* targetType the result is NULL instead of an overflow error.
* If a sourceExpr value is not well formed or contains
* invalid characters the result is NULL instead of
* an invalid data error.Exception to the above are:
*Casting to a STRUCT field with NOT NULL property.
*Casting a MAP key.
*/
def try_cast(sourceExpr : Column,targetType: datatype): Column = {
try_cast(sourceExpr AS targetType)
}
/**
* This function receives a date or timestamp, as well as a
* properly formatted string and subtracts the specified
* amount of days from it. If receiving a string, this string is
* casted to date using try_cast and if it's not possible to cast,
* returns null. If receiving
* a timestamp it will be casted to date (removing its time).
* @since 1.10.0
* @since 1.5.0
* @param start Date, Timestamp or String column to subtract days from.
* @param days Days to subtract.
* @return Column object.
*/
def date_sub(start: Column, days: Int): Column = {
dateadd("DAY", lit(days * -1), sqlExpr(s"try_cast(${start.getName.get} :: STRING as DATE)"))
dateadd("DAY", lit(days * -1), try_cast(start as DATE))
}
/**
* Invokes a built-in snowflake function with the specified name and arguments.
Expand Down
28 changes: 28 additions & 0 deletions src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2191,6 +2191,34 @@ trait FunctionSuite extends TestData {
expected,
sort = false)
}
test("json_tuple") {
var expected = Seq(("21", "Joe"),("26","Jay")).toDF("age","name")
checkAnswer(
object2
.select(json_tuple(col("obj"),"age","name"):_*),
expected,
sort = false)
}

test("date_sub") {
var expected = Seq(("2020-04-30 13:11:20.000"),("2020-08-20 01:30:05.000")).toDF()
checkAnswer(
timestamp1
.select(date_sub(col("a"),lit(1)),
expected,
sort = false
))
}

test("try_cast") {
var expected = Seq(("2020-08-01"),("2010-12-01")).toDF()
checkAnswer(
date1
.select(try_cast(col("a") as String),
expected,
sort = false
))
}

}

Expand Down

0 comments on commit 200361e

Please sign in to comment.