-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23939][SQL] Add transform_keys function #22013
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 1 commit
0a19cc4
12e0259
5806ac4
150a6a5
9f6a8ab
6526630
f7fd231
1cbaf0c
bb52630
621213d
5db526b
e5d9b05
fb885f4
58b60b2
2f4943f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -523,14 +523,14 @@ case class TransformKeys( | |
| MapType(function.dataType, map.valueType, map.valueContainsNull) | ||
| } | ||
|
|
||
| @transient val MapType(keyType, valueType, valueContainsNull) = argument.dataType | ||
| @transient lazy val MapType(keyType, valueType, valueContainsNull) = argument.dataType | ||
|
|
||
| override def bind(f: (Expression, Seq[(DataType, Boolean)]) => LambdaFunction): TransformKeys = { | ||
| copy(function = f(function, (keyType, false) :: (valueType, valueContainsNull) :: Nil)) | ||
| } | ||
|
|
||
| @transient lazy val (keyVar, valueVar) = { | ||
| val LambdaFunction( | ||
| @transient lazy val LambdaFunction( | ||
| _, (keyVar: NamedLambdaVariable) :: (valueVar: NamedLambdaVariable) :: Nil, _) = function | ||
| (keyVar, valueVar) | ||
| } | ||
|
||
|
|
@@ -544,7 +544,7 @@ case class TransformKeys( | |
| keyVar.value.set(map.keyArray().get(i, keyVar.dataType)) | ||
| valueVar.value.set(map.valueArray().get(i, valueVar.dataType)) | ||
| val result = f.eval(inputRow) | ||
| if (result == null) { | ||
| if (result == null) { | ||
| throw new RuntimeException("Cannot use null as map key!") | ||
| } | ||
| resultKeys.update(i, result) | ||
|
|
@@ -554,7 +554,7 @@ case class TransformKeys( | |
| } | ||
|
|
||
| override def prettyName: String = "transform_keys" | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Merges two given maps into a single map by applying function to the pair of values with | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2302,13 +2302,13 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
| assert(ex5.getMessage.contains("function map_zip_with does not support ordering on type map")) | ||
| } | ||
|
|
||
| test("transform keys function - test various primitive data types combinations") { | ||
| test("transform keys function - primitive data types") { | ||
| val dfExample1 = Seq( | ||
| Map[Int, Int](1 -> 1, 9 -> 9, 8 -> 8, 7 -> 7) | ||
| ).toDF("i") | ||
|
|
||
| val dfExample2 = Seq( | ||
| Map[Int, Double](1 -> 1.0E0, 2 -> 1.4E0, 3 -> 1.7E0) | ||
| Map[Int, Double](1 -> 1.0, 2 -> 1.40, 3 -> 1.70) | ||
| ).toDF("j") | ||
|
|
||
| val dfExample3 = Seq( | ||
|
|
@@ -2357,34 +2357,37 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
| } | ||
|
|
||
| test("transform keys function - Invalid lambda functions and exceptions") { | ||
|
|
||
| val dfExample1 = Seq( | ||
| Map[Int, Int](1 -> 1, 9 -> 9, 8 -> 8, 7 -> 7) | ||
| Map[String, String]("a" -> null) | ||
| ).toDF("i") | ||
|
|
||
| val dfExample2 = Seq( | ||
| Map[String, String]("a" -> "b") | ||
| Seq(1, 2, 3, 4) | ||
| ).toDF("j") | ||
|
|
||
| val dfExample3 = Seq( | ||
| Map[String, String]("a" -> null) | ||
| ).toDF("x") | ||
|
|
||
| def testInvalidLambdaFunctions(): Unit = { | ||
| val ex1 = intercept[AnalysisException] { | ||
| dfExample1.selectExpr("transform_keys(i, k -> k )") | ||
| dfExample1.selectExpr("transform_keys(i, k -> k)") | ||
| } | ||
| assert(ex1.getMessage.contains("The number of lambda function arguments '1' does not match")) | ||
|
|
||
| val ex2 = intercept[AnalysisException] { | ||
| dfExample2.selectExpr("transform_keys(j, (k, v, x) -> k + 1)") | ||
| dfExample1.selectExpr("transform_keys(i, (k, v, x) -> k + 1)") | ||
| } | ||
| assert(ex2.getMessage.contains( | ||
| "The number of lambda function arguments '3' does not match")) | ||
| "The number of lambda function arguments '3' does not match")) | ||
|
|
||
| val ex3 = intercept[RuntimeException] { | ||
| dfExample3.selectExpr("transform_keys(x, (k, v) -> v)").show() | ||
| dfExample1.selectExpr("transform_keys(i, (k, v) -> v)").show() | ||
| } | ||
| assert(ex3.getMessage.contains("Cannot use null as map key!")) | ||
|
||
|
|
||
| val ex4 = intercept[AnalysisException] { | ||
| dfExample2.selectExpr("transform_keys(j, (k, v) -> k + 1)") | ||
| } | ||
| assert(ex4.getMessage.contains( | ||
| "data type mismatch: argument 1 requires map type")) | ||
| } | ||
|
|
||
| testInvalidLambdaFunctions() | ||
|
|
||
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.
We can use
valueTypeandvalueContainsNullfrom the following val?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.
What about this?