forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-47539][SQL] Make the return value of method
castToString
be …
…`Any => UTF8String` ### What changes were proposed in this pull request? The pr aims to: - make the method `castToString(from: DataType): Any => Any` to `castToString(from: DataType): Any => UTF8String` in `ToStringBase`. - Add UT for `ToPrettyString` to improve the `coverage` of UT. ### Why are the changes needed? - Let the method return a UTF8String(`Any` -> `UTF8String`), which is more intuitive. - Currently, `ToPrettyString` lacks the corresponding `UT`. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? - Add new UT. - Pass GA. ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#45688 from panbingkun/ToPrettyString_improve. Authored-by: panbingkun <[email protected]> Signed-off-by: Hyukjin Kwon <[email protected]>
- Loading branch information
1 parent
4a1f241
commit b341787
Showing
3 changed files
with
141 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...talyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ToPrettyStringSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.sql.catalyst.expressions | ||
|
||
import org.apache.spark.SparkFunSuite | ||
import org.apache.spark.sql.catalyst.util.DateTimeTestUtils.UTC_OPT | ||
import org.apache.spark.sql.types._ | ||
import org.apache.spark.unsafe.types.{UTF8String, VariantVal} | ||
|
||
class ToPrettyStringSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
|
||
test("CalendarInterval as pretty strings") { | ||
checkEvaluation( | ||
ToPrettyString(Cast(Literal("interval -3 month 1 day 7 hours"), CalendarIntervalType)), | ||
"-3 months 1 days 7 hours") | ||
} | ||
|
||
test("Binary as pretty strings") { | ||
checkEvaluation(ToPrettyString(Cast(Literal("abcdef"), BinaryType)), "[61 62 63 64 65 66]") | ||
} | ||
|
||
test("Date as pretty strings") { | ||
checkEvaluation(ToPrettyString(Cast(Literal("1980-12-17"), DateType, UTC_OPT)), "1980-12-17") | ||
} | ||
|
||
test("Timestamp as pretty strings") { | ||
checkEvaluation( | ||
ToPrettyString(Cast(Literal("2012-11-30 09:19:00"), TimestampType, UTC_OPT)), | ||
"2012-11-30 01:19:00") | ||
} | ||
|
||
test("TimestampNTZ as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal(1L, TimestampNTZType)), "1970-01-01 00:00:00.000001") | ||
} | ||
|
||
test("Array as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal.create(Array(1, 2, 3, 4, 5))), "[1, 2, 3, 4, 5]") | ||
} | ||
|
||
test("Map as pretty strings") { | ||
checkEvaluation( | ||
ToPrettyString(Literal.create(Map(1 -> "a", 2 -> "b", 3 -> "c"))), | ||
"{1 -> a, 2 -> b, 3 -> c}") | ||
} | ||
|
||
test("Struct as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal.create((1, "a", 0.1))), "{1, a, 0.1}") | ||
checkEvaluation( | ||
ToPrettyString(Literal.create(Tuple2[String, String](null, null))), | ||
"{NULL, NULL}" | ||
) | ||
} | ||
|
||
test("YearMonthInterval as pretty strings") { | ||
checkEvaluation( | ||
ToPrettyString(Cast(Literal("INTERVAL '1-0' YEAR TO MONTH"), YearMonthIntervalType())), | ||
"INTERVAL '1-0' YEAR TO MONTH") | ||
} | ||
|
||
test("DayTimeInterval as pretty strings") { | ||
checkEvaluation( | ||
ToPrettyString(Cast(Literal("INTERVAL '1 2:03:04' DAY TO SECOND"), DayTimeIntervalType())), | ||
"INTERVAL '1 02:03:04' DAY TO SECOND") | ||
} | ||
|
||
test("Decimal as pretty strings") { | ||
checkEvaluation( | ||
ToPrettyString(Cast(Literal(1234.65), DecimalType(6, 2))), "1234.65") | ||
} | ||
|
||
test("String as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal("s")), "s") | ||
} | ||
|
||
test("Char as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal.create('a', CharType(5))), "a") | ||
} | ||
|
||
test("Byte as pretty strings") { | ||
checkEvaluation(ToPrettyString(Cast(Literal(8), ByteType)), "8") | ||
} | ||
|
||
test("Short as pretty strings") { | ||
checkEvaluation(ToPrettyString(Cast(Literal(8), ShortType)), "8") | ||
} | ||
|
||
test("Int as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal(1)), "1") | ||
} | ||
|
||
test("Long as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal(1L)), "1") | ||
} | ||
|
||
test("Float as pretty strings") { | ||
checkEvaluation(ToPrettyString(Cast(Literal(8), FloatType)), "8.0") | ||
} | ||
|
||
test("Double as pretty strings") { | ||
checkEvaluation(ToPrettyString(Cast(Literal(8), DoubleType)), "8.0") | ||
} | ||
|
||
test("Boolean as pretty strings") { | ||
checkEvaluation(ToPrettyString(Literal(false)), "false") | ||
checkEvaluation(ToPrettyString(Literal(true)), "true") | ||
} | ||
|
||
test("Variant as pretty strings") { | ||
checkEvaluation( | ||
ToPrettyString(Literal(new VariantVal(Array[Byte](1, 2, 3), Array[Byte](4, 5)))), | ||
UTF8String.fromBytes(Array[Byte](1, 2, 3)).toString) | ||
} | ||
} |