Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,6 @@ case class StringToMap(text: Expression, pairDelim: Expression, keyValueDelim: E

override def dataType: DataType = MapType(StringType, StringType)

override def checkInputDataTypes(): TypeCheckResult = {
if (Seq(pairDelim, keyValueDelim).exists(! _.foldable)) {
TypeCheckResult.TypeCheckFailure(s"$prettyName's delimiters must be foldable.")
} else {
super.checkInputDataTypes()
}
}

private lazy val mapBuilder = new ArrayBasedMapBuilder(StringType, StringType)

override def nullSafeEval(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper {
val m5 = Map("a" -> null)
checkEvaluation(new StringToMap(s5), m5)

val s6 = Literal("a=1&b=2&c=3")
val m6 = Map("a" -> "1", "b" -> "2", "c" -> "3")
checkEvaluation(new StringToMap(s6, NonFoldableLiteral("&"), NonFoldableLiteral("=")), m6)

checkExceptionInExpression[RuntimeException](
new StringToMap(Literal("a:1,b:2,a:3")), "Duplicate map key")
withSQLConf(SQLConf.MAP_KEY_DEDUP_POLICY.key -> SQLConf.MapKeyDedupPolicy.LAST_WIN.toString) {
Expand All @@ -492,12 +496,6 @@ class ComplexTypeSuite extends SparkFunSuite with ExpressionEvalHelper {
assert(StringToMap(Literal("a:1,b:2,c:3"), Literal(null), Literal(null))
.checkInputDataTypes().isFailure)
assert(new StringToMap(Literal(null), Literal(null)).checkInputDataTypes().isFailure)

assert(new StringToMap(Literal("a:1_b:2_c:3"), NonFoldableLiteral("_"))
.checkInputDataTypes().isFailure)
assert(
new StringToMap(Literal("a=1_b=2_c=3"), Literal("_"), NonFoldableLiteral("="))
.checkInputDataTypes().isFailure)
}

test("SPARK-22693: CreateNamedStruct should not use global variables") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,36 @@ class StringFunctionsSuite extends QueryTest with SharedSparkSession {
Seq(Row(Map("a" -> "1", "b" -> "2", "c" -> "3")))
)

checkAnswer(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really a test of my change, but it was a missing test (the case where a literal pair delimiter is given but no key/value delimiter is given).

df2.selectExpr("str_to_map(a, ',')"),
Seq(Row(Map("a" -> "1", "b" -> "2", "c" -> "3")))
)

val df3 = Seq(
("a=1&b=2", "&", "="),
("k#2%v#3", "%", "#")
).toDF("str", "delim1", "delim2")

checkAnswer(
df3.selectExpr("str_to_map(str, delim1, delim2)"),
Seq(
Row(Map("a" -> "1", "b" -> "2")),
Row(Map("k" -> "2", "v" -> "3"))
)
)

val df4 = Seq(
("a:1&b:2", "&"),
("k:2%v:3", "%")
).toDF("str", "delim1")

checkAnswer(
df4.selectExpr("str_to_map(str, delim1)"),
Seq(
Row(Map("a" -> "1", "b" -> "2")),
Row(Map("k" -> "2", "v" -> "3"))
)
)
}

test("SPARK-36148: check input data types of regexp_replace") {
Expand Down