-
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 2 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 |
---|---|---|
|
@@ -2565,15 +2565,15 @@ case class ToBinary(expr: Expression, format: Option[Expression], child: Express | |
case lit if lit.foldable => | ||
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 | ||
} | ||
|
@@ -2591,8 +2591,9 @@ case class ToBinary(expr: Expression, format: Option[Expression], child: Express | |
def checkFormat(lit: Expression) = { | ||
if (lit.foldable) { | ||
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", "base2").contains( | ||
value.asInstanceOf[UTF8String].toString.toLowerCase(Locale.ROOT))) | ||
} else false | ||
} | ||
|
||
|
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 | ||
|
@@ -937,3 +937,12 @@ 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 | ||
|
||
|
||
-- !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', 'base2', 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 | ||
|
@@ -933,3 +933,12 @@ 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 | ||
|
||
|
||
-- !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', 'base2', 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.
I'd put the string type check here, as
to_binary(str_col, cast(null as int))
should fail instead of returning 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.
Makes sense.
May I confirm that the case below is acceptable?
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.
yea, the type coercion can cast any atomic type to string type implicitly.