-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-44983][SQL] Convert binary to string by to_char for the formats: hex, base64, utf-8
#42632
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
Changes from 5 commits
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 |
|---|---|---|
|
|
@@ -157,6 +157,15 @@ private[sql] object QueryCompilationErrors extends QueryErrorsBase with Compilat | |
| "functionName" -> toSQLId("format_string"))) | ||
| } | ||
|
|
||
| def binaryFormatError(funcName: String, invalidFormat: String): Throwable = { | ||
| new AnalysisException( | ||
|
Contributor
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. My feeling is that, if the error is only thrown in one place, we don't need to add a method here, just
Member
Author
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. One of purposes of gathering exceptions to Don't think that this PR is right place to begin don't do that. |
||
| errorClass = "INVALID_PARAMETER_VALUE.BINARY_FORMAT", | ||
| messageParameters = Map( | ||
| "parameter" -> toSQLId("format"), | ||
| "functionName" -> toSQLId(funcName), | ||
| "invalidFormat" -> toSQLValue(invalidFormat, StringType))) | ||
| } | ||
|
|
||
| def unorderablePivotColError(pivotCol: Expression): Throwable = { | ||
| new AnalysisException( | ||
| errorClass = "INCOMPARABLE_PIVOT_COLUMN", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -854,28 +854,53 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession { | |
| ) | ||
| } | ||
|
|
||
| test("to_char") { | ||
| val df = Seq(78.12).toDF("a") | ||
| checkAnswer( | ||
| df.selectExpr("to_char(a, '$99.99')"), | ||
| Seq(Row("$78.12")) | ||
| ) | ||
| checkAnswer( | ||
| df.select(to_char(col("a"), lit("$99.99"))), | ||
| Seq(Row("$78.12")) | ||
| ) | ||
| } | ||
|
|
||
| test("to_varchar") { | ||
| val df = Seq(78.12).toDF("a") | ||
| checkAnswer( | ||
| df.selectExpr("to_varchar(a, '$99.99')"), | ||
| Seq(Row("$78.12")) | ||
| ) | ||
| checkAnswer( | ||
| df.select(to_varchar(col("a"), lit("$99.99"))), | ||
| Seq(Row("$78.12")) | ||
| ) | ||
| test("to_char/to_varchar") { | ||
| Seq( | ||
| "to_char" -> ((e: Column, fmt: Column) => to_char(e, fmt)), | ||
| "to_varchar" -> ((e: Column, fmt: Column) => to_varchar(e, fmt)) | ||
| ).foreach { case (funcName, func) => | ||
| val df = Seq(78.12).toDF("a") | ||
| checkAnswer(df.selectExpr(s"$funcName(a, '$$99.99')"), Seq(Row("$78.12"))) | ||
| checkAnswer(df.select(func(col("a"), lit("$99.99"))), Seq(Row("$78.12"))) | ||
|
|
||
| val df2 = Seq((Array(100.toByte), "base64")).toDF("input", "format") | ||
| checkAnswer(df2.selectExpr(s"$funcName(input, 'hex')"), Seq(Row("64"))) | ||
| checkAnswer(df2.select(func(col("input"), lit("hex"))), Seq(Row("64"))) | ||
| checkAnswer(df2.selectExpr(s"$funcName(input, 'base64')"), Seq(Row("ZA=="))) | ||
| checkAnswer(df2.select(func(col("input"), lit("base64"))), Seq(Row("ZA=="))) | ||
| checkAnswer(df2.selectExpr(s"$funcName(input, 'utf-8')"), Seq(Row("d"))) | ||
| checkAnswer(df2.select(func(col("input"), lit("utf-8"))), Seq(Row("d"))) | ||
|
|
||
| checkError( | ||
| exception = intercept[AnalysisException] { | ||
| df2.select(func(col("input"), col("format"))).collect() | ||
| }, | ||
| errorClass = "_LEGACY_ERROR_TEMP_1100", | ||
|
Member
Author
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. TODO: Open an JIRA to assign proper name for the error class.
Member
Author
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.
Member
Author
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. Here is the PR #42737 |
||
| parameters = Map( | ||
| "argName" -> "format", | ||
| "funcName" -> "to_char", | ||
| "requiredType" -> "string")) | ||
| checkError( | ||
| exception = intercept[AnalysisException] { | ||
| df2.select(func(col("input"), lit("invalid_format"))).collect() | ||
| }, | ||
| errorClass = "INVALID_PARAMETER_VALUE.BINARY_FORMAT", | ||
| parameters = Map( | ||
| "parameter" -> "`format`", | ||
| "functionName" -> "`to_char`", | ||
| "invalidFormat" -> "'invalid_format'")) | ||
| checkError( | ||
| exception = intercept[AnalysisException] { | ||
| sql(s"select $funcName('a', 'b', 'c')") | ||
| }, | ||
| errorClass = "WRONG_NUM_ARGS.WITHOUT_SUGGESTION", | ||
| parameters = Map( | ||
| "functionName" -> s"`$funcName`", | ||
| "expectedNum" -> "2", | ||
| "actualNum" -> "3", | ||
| "docroot" -> SPARK_DOC_ROOT), | ||
| context = ExpectedContext("", "", 7, 21 + funcName.length, s"$funcName('a', 'b', 'c')")) | ||
| } | ||
| } | ||
|
|
||
| test("to_number") { | ||
|
|
||
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.
this is a bit weird, we can just write 2 lines
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.
Don't think it is the right approach to split semantically one operation: "assign names".