From 200361ec6aa5895c8bbe1a957a36be0228262f43 Mon Sep 17 00:00:00 2001 From: Shyamala Jayabalan Date: Fri, 2 Aug 2024 11:36:53 -0400 Subject: [PATCH] Added testcases for date_sub,try_cast,json_tupple --- .../com/snowflake/snowpark/functions.scala | 28 +++++++++++++++++-- .../snowpark_test/FunctionSuite.scala | 28 +++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/main/scala/com/snowflake/snowpark/functions.scala b/src/main/scala/com/snowflake/snowpark/functions.scala index 256fbf2a..b9b7f0f7 100644 --- a/src/main/scala/com/snowflake/snowpark/functions.scala +++ b/src/main/scala/com/snowflake/snowpark/functions.scala @@ -3256,7 +3256,29 @@ 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 @@ -3264,13 +3286,13 @@ object functions { * 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. diff --git a/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala b/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala index acc28270..e87ba6e1 100644 --- a/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala +++ b/src/test/scala/com/snowflake/snowpark_test/FunctionSuite.scala @@ -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 + )) + } }