Skip to content

Commit

Permalink
[SPARK-34881][SQL][FOLLOWUP] Implement toString() and sql() methods f…
Browse files Browse the repository at this point in the history
…or TRY_CAST

### What changes were proposed in this pull request?

Implement toString() and sql() methods for TRY_CAST

### Why are the changes needed?

The new expression should have a different name from `CAST` in SQL/String representation.

### Does this PR introduce _any_ user-facing change?

Yes, in the result of `explain()`, users can see try_cast if the new expression is used.

### How was this patch tested?

Unit tests.

Closes #32098 from gengliangwang/tryCastString.

Authored-by: Gengliang Wang <ltnwgl@gmail.com>
Signed-off-by: Gengliang Wang <ltnwgl@gmail.com>
  • Loading branch information
gengliangwang committed Apr 9, 2021
1 parent 3af2c1b commit bfba7fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,10 @@ case class TryCast(child: Expression, dataType: DataType, timeZoneId: Option[Str

override def typeCheckFailureMessage: String =
AnsiCast.typeCheckFailureMessage(child.dataType, dataType, None, None)

override def toString: String = {
s"try_cast($child as ${dataType.simpleString})"
}

override def sql: String = s"TRY_CAST(${child.sql} AS ${dataType.sql})"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.catalyst.expressions
import scala.reflect.ClassTag

import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.types.DataType
import org.apache.spark.sql.types.{DataType, IntegerType}

class TryCastSuite extends AnsiCastSuiteBase {
override protected def cast(v: Any, targetType: DataType, timeZoneId: Option[String]) = {
Expand Down Expand Up @@ -48,4 +48,8 @@ class TryCastSuite extends AnsiCastSuiteBase {
override def checkCastToNumericError(l: Literal, to: DataType, tryCastResult: Any): Unit = {
checkEvaluation(cast(l, to), tryCastResult, InternalRow(l.value))
}

test("try_cast: to_string") {
assert(TryCast(Literal("1"), IntegerType).toString == "try_cast(1 as int)")
}
}
58 changes: 29 additions & 29 deletions sql/core/src/test/resources/sql-tests/results/try_cast.sql.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,230 +5,230 @@
-- !query
SELECT TRY_CAST('1.23' AS int)
-- !query schema
struct<CAST(1.23 AS INT):int>
struct<TRY_CAST(1.23 AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST('1.23' AS long)
-- !query schema
struct<CAST(1.23 AS BIGINT):bigint>
struct<TRY_CAST(1.23 AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST('-4.56' AS int)
-- !query schema
struct<CAST(-4.56 AS INT):int>
struct<TRY_CAST(-4.56 AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST('-4.56' AS long)
-- !query schema
struct<CAST(-4.56 AS BIGINT):bigint>
struct<TRY_CAST(-4.56 AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST('abc' AS int)
-- !query schema
struct<CAST(abc AS INT):int>
struct<TRY_CAST(abc AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST('abc' AS long)
-- !query schema
struct<CAST(abc AS BIGINT):bigint>
struct<TRY_CAST(abc AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST('' AS int)
-- !query schema
struct<CAST( AS INT):int>
struct<TRY_CAST( AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST('' AS long)
-- !query schema
struct<CAST( AS BIGINT):bigint>
struct<TRY_CAST( AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST(NULL AS int)
-- !query schema
struct<CAST(NULL AS INT):int>
struct<TRY_CAST(NULL AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST(NULL AS long)
-- !query schema
struct<CAST(NULL AS BIGINT):bigint>
struct<TRY_CAST(NULL AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST('123.a' AS int)
-- !query schema
struct<CAST(123.a AS INT):int>
struct<TRY_CAST(123.a AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST('123.a' AS long)
-- !query schema
struct<CAST(123.a AS BIGINT):bigint>
struct<TRY_CAST(123.a AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST('-2147483648' AS int)
-- !query schema
struct<CAST(-2147483648 AS INT):int>
struct<TRY_CAST(-2147483648 AS INT):int>
-- !query output
-2147483648


-- !query
SELECT TRY_CAST('-2147483649' AS int)
-- !query schema
struct<CAST(-2147483649 AS INT):int>
struct<TRY_CAST(-2147483649 AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST('2147483647' AS int)
-- !query schema
struct<CAST(2147483647 AS INT):int>
struct<TRY_CAST(2147483647 AS INT):int>
-- !query output
2147483647


-- !query
SELECT TRY_CAST('2147483648' AS int)
-- !query schema
struct<CAST(2147483648 AS INT):int>
struct<TRY_CAST(2147483648 AS INT):int>
-- !query output
NULL


-- !query
SELECT TRY_CAST('-9223372036854775808' AS long)
-- !query schema
struct<CAST(-9223372036854775808 AS BIGINT):bigint>
struct<TRY_CAST(-9223372036854775808 AS BIGINT):bigint>
-- !query output
-9223372036854775808


-- !query
SELECT TRY_CAST('-9223372036854775809' AS long)
-- !query schema
struct<CAST(-9223372036854775809 AS BIGINT):bigint>
struct<TRY_CAST(-9223372036854775809 AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST('9223372036854775807' AS long)
-- !query schema
struct<CAST(9223372036854775807 AS BIGINT):bigint>
struct<TRY_CAST(9223372036854775807 AS BIGINT):bigint>
-- !query output
9223372036854775807


-- !query
SELECT TRY_CAST('9223372036854775808' AS long)
-- !query schema
struct<CAST(9223372036854775808 AS BIGINT):bigint>
struct<TRY_CAST(9223372036854775808 AS BIGINT):bigint>
-- !query output
NULL


-- !query
SELECT TRY_CAST('interval 3 month 1 hour' AS interval)
-- !query schema
struct<CAST(interval 3 month 1 hour AS INTERVAL):interval>
struct<TRY_CAST(interval 3 month 1 hour AS INTERVAL):interval>
-- !query output
3 months 1 hours


-- !query
SELECT TRY_CAST('abc' AS interval)
-- !query schema
struct<CAST(abc AS INTERVAL):interval>
struct<TRY_CAST(abc AS INTERVAL):interval>
-- !query output
NULL


-- !query
select TRY_CAST('true' as boolean)
-- !query schema
struct<CAST(true AS BOOLEAN):boolean>
struct<TRY_CAST(true AS BOOLEAN):boolean>
-- !query output
true


-- !query
select TRY_CAST('false' as boolean)
-- !query schema
struct<CAST(false AS BOOLEAN):boolean>
struct<TRY_CAST(false AS BOOLEAN):boolean>
-- !query output
false


-- !query
select TRY_CAST('abc' as boolean)
-- !query schema
struct<CAST(abc AS BOOLEAN):boolean>
struct<TRY_CAST(abc AS BOOLEAN):boolean>
-- !query output
NULL


-- !query
SELECT TRY_CAST("2021-01-01" AS date)
-- !query schema
struct<CAST(2021-01-01 AS DATE):date>
struct<TRY_CAST(2021-01-01 AS DATE):date>
-- !query output
2021-01-01


-- !query
SELECT TRY_CAST("2021-101-01" AS date)
-- !query schema
struct<CAST(2021-101-01 AS DATE):date>
struct<TRY_CAST(2021-101-01 AS DATE):date>
-- !query output
NULL


-- !query
SELECT TRY_CAST("2021-01-01 00:00:00" AS timestamp)
-- !query schema
struct<CAST(2021-01-01 00:00:00 AS TIMESTAMP):timestamp>
struct<TRY_CAST(2021-01-01 00:00:00 AS TIMESTAMP):timestamp>
-- !query output
2021-01-01 00:00:00


-- !query
SELECT TRY_CAST("2021-101-01 00:00:00" AS timestamp)
-- !query schema
struct<CAST(2021-101-01 00:00:00 AS TIMESTAMP):timestamp>
struct<TRY_CAST(2021-101-01 00:00:00 AS TIMESTAMP):timestamp>
-- !query output
NULL

0 comments on commit bfba7fa

Please sign in to comment.