-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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
[SPARK-38225][SQL] Adjust input format
of function to_binary
#35533
Changes from 5 commits
5c01f94
2869cb1
98634d7
8be5db9
4b93726
6d94e00
75494c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2545,7 +2545,7 @@ case class Encode(value: Expression, charset: Expression) | |
@ExpressionDescription( | ||
usage = """ | ||
_FUNC_(str[, fmt]) - Converts the input `str` to a binary value based on the supplied `fmt`. | ||
`fmt` can be a case-insensitive string literal of "hex", "utf-8", "base2", or "base64". | ||
`fmt` can be a case-insensitive string literal of "hex", "utf-8", or "base64". | ||
By default, the binary format for conversion is "hex" if `fmt` is omitted. | ||
The function returns NULL if at least one of the input parameters is NULL. | ||
""", | ||
|
@@ -2562,18 +2562,17 @@ case class ToBinary(expr: Expression, format: Option[Expression], child: Express | |
|
||
def this(expr: Expression, format: Expression) = this(expr, Option(format), | ||
format match { | ||
case lit if lit.foldable => | ||
case lit if (lit.foldable && Seq(StringType, NullType).contains(lit.dataType)) => | ||
val value = lit.eval() | ||
if (value == null) Literal(null, BinaryType) | ||
else { | ||
else if (value.isInstanceOf[UTF8String]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, it can only be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems we can remove this check now, as we have checked the data type ahead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! Removed. |
||
value.asInstanceOf[UTF8String].toString.toLowerCase(Locale.ROOT) match { | ||
case "hex" => Unhex(expr) | ||
case "utf-8" => Encode(expr, Literal("UTF-8")) | ||
case "base64" => UnBase64(expr) | ||
case "base2" => Cast(expr, BinaryType) | ||
case _ => lit | ||
} | ||
} | ||
} else lit | ||
|
||
case other => other | ||
} | ||
|
@@ -2589,10 +2588,11 @@ case class ToBinary(expr: Expression, format: Option[Expression], child: Express | |
|
||
override def checkInputDataTypes(): TypeCheckResult = { | ||
def checkFormat(lit: Expression) = { | ||
if (lit.foldable) { | ||
if (lit.foldable && Seq(StringType, NullType).contains(lit.dataType)) { | ||
val value = lit.eval() | ||
value == null || Seq("hex", "utf-8", "base64", "base2").contains( | ||
value.asInstanceOf[UTF8String].toString.toLowerCase(Locale.ROOT)) | ||
value == null || (value.isInstanceOf[UTF8String] && | ||
Seq("hex", "utf-8", "base64").contains( | ||
value.asInstanceOf[UTF8String].toString.toLowerCase(Locale.ROOT))) | ||
} else false | ||
} | ||
|
||
|
@@ -2601,7 +2601,7 @@ case class ToBinary(expr: Expression, format: Option[Expression], child: Express | |
} else { | ||
TypeCheckResult.TypeCheckFailure( | ||
s"Unsupported encoding format: $format. The format has to be " + | ||
s"a case-insensitive string literal of 'hex', 'utf-8', 'base2', or 'base64'") | ||
s"a case-insensitive string literal of 'hex', 'utf-8', or 'base64'") | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
-- Automatically generated by SQLQueryTestSuite | ||
-- Number of queries: 116 | ||
-- Number of queries: 117 | ||
|
||
|
||
-- !query | ||
|
@@ -850,14 +850,6 @@ struct<to_binary(abc, base64):binary> | |
i� | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 'base2') | ||
-- !query schema | ||
struct<to_binary(abc, base2):binary> | ||
-- !query output | ||
abc | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 'hex') | ||
-- !query schema | ||
|
@@ -930,10 +922,28 @@ struct<to_binary(NULL, CAST(NULL AS STRING)):binary> | |
NULL | ||
|
||
|
||
-- !query | ||
select to_binary(null, cast(null as int)) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.sql.AnalysisException | ||
cannot resolve 'to_binary(NULL, CAST(NULL AS INT))' due to data type mismatch: Unsupported encoding format: Some(ansi_cast(null as int)). The format has to be a case-insensitive string literal of 'hex', 'utf-8', or 'base64'; line 1 pos 7 | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 'invalidFormat') | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.sql.AnalysisException | ||
cannot resolve 'to_binary('abc', 'invalidFormat')' due to data type mismatch: Unsupported encoding format: Some(invalidFormat). The format has to be a case-insensitive string literal of 'hex', 'utf-8', 'base2', or 'base64'; line 1 pos 7 | ||
cannot resolve 'to_binary('abc', 'invalidFormat')' due to data type mismatch: Unsupported encoding format: Some(invalidFormat). The format has to be a case-insensitive string literal of 'hex', 'utf-8', or 'base64'; line 1 pos 7 | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 1) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.sql.AnalysisException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentionally failed query. |
||
cannot resolve 'to_binary('abc', 1)' due to data type mismatch: Unsupported encoding format: Some(1). The format has to be a case-insensitive string literal of 'hex', 'utf-8', or 'base64'; line 1 pos 7 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
-- Automatically generated by SQLQueryTestSuite | ||
-- Number of queries: 116 | ||
-- Number of queries: 117 | ||
|
||
|
||
-- !query | ||
|
@@ -846,14 +846,6 @@ struct<to_binary(abc, base64):binary> | |
i� | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 'base2') | ||
-- !query schema | ||
struct<to_binary(abc, base2):binary> | ||
-- !query output | ||
abc | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 'hex') | ||
-- !query schema | ||
|
@@ -926,10 +918,28 @@ struct<to_binary(NULL, CAST(NULL AS STRING)):binary> | |
NULL | ||
|
||
|
||
-- !query | ||
select to_binary(null, cast(null as int)) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.sql.AnalysisException | ||
cannot resolve 'to_binary(NULL, CAST(NULL AS INT))' due to data type mismatch: Unsupported encoding format: Some(cast(null as int)). The format has to be a case-insensitive string literal of 'hex', 'utf-8', or 'base64'; line 1 pos 7 | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 'invalidFormat') | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.sql.AnalysisException | ||
cannot resolve 'to_binary('abc', 'invalidFormat')' due to data type mismatch: Unsupported encoding format: Some(invalidFormat). The format has to be a case-insensitive string literal of 'hex', 'utf-8', 'base2', or 'base64'; line 1 pos 7 | ||
cannot resolve 'to_binary('abc', 'invalidFormat')' due to data type mismatch: Unsupported encoding format: Some(invalidFormat). The format has to be a case-insensitive string literal of 'hex', 'utf-8', or 'base64'; line 1 pos 7 | ||
|
||
|
||
-- !query | ||
select to_binary('abc', 1) | ||
-- !query schema | ||
struct<> | ||
-- !query output | ||
org.apache.spark.sql.AnalysisException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Intentionally failed query. |
||
cannot resolve 'to_binary('abc', 1)' due to data type mismatch: Unsupported encoding format: Some(1). The format has to be a case-insensitive string literal of 'hex', 'utf-8', or 'base64'; line 1 pos 7 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, do other systems like snowflake support
to_binary('abc', null)
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Snowflake doesn't specify that in their document, but BigQuery and Teradata support
to_binary('abc', null)
:Expressions, functions, and operators | BigQuery | Google Cloud "If an operand is NULL, the result is NULL, with the exception of the IS operator."
Teradata Online Documentation | Quick access to technical manuals "If either instring or in_encoding is NULL, the result is NULL."