-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23940][SQL] Add transform_values SQL function #22045
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 6 commits
68392e3
fd05645
cdecd32
7df3ac5
b73106d
daf7935
56d08ef
9e89565
3382e1a
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 |
|---|---|---|
|
|
@@ -497,6 +497,53 @@ case class ArrayAggregate( | |
| override def prettyName: String = "aggregate" | ||
| } | ||
|
|
||
| /** | ||
| * Returns a map that applies the function to each value of the map. | ||
| */ | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(expr, func) - Transforms values in the map using the function.", | ||
| examples = """ | ||
|
||
| Examples: | ||
| > SELECT _FUNC_(map(array(1, 2, 3), array(1, 2, 3)), (k, v) -> v + 1); | ||
| map(array(1, 2, 3), array(2, 3, 4)) | ||
| > SELECT _FUNC_(map(array(1, 2, 3), array(1, 2, 3)), (k, v) -> k + v); | ||
| map(array(1, 2, 3), array(2, 4, 6)) | ||
| """, | ||
| since = "2.4.0") | ||
|
||
| case class TransformValues( | ||
| argument: Expression, | ||
| function: Expression) | ||
| extends MapBasedSimpleHigherOrderFunction with CodegenFallback { | ||
|
|
||
| override def nullable: Boolean = argument.nullable | ||
|
|
||
| @transient lazy val MapType(keyType, valueType, valueContainsNull) = argument.dataType | ||
|
|
||
| override def dataType: DataType = MapType(keyType, function.dataType, valueContainsNull) | ||
|
||
|
|
||
| override def bind(f: (Expression, Seq[(DataType, Boolean)]) => LambdaFunction) | ||
| : TransformValues = { | ||
| copy(function = f(function, (keyType, false) :: (valueType, valueContainsNull) :: Nil)) | ||
| } | ||
|
|
||
| @transient lazy val LambdaFunction( | ||
| _, (keyVar: NamedLambdaVariable) :: (valueVar: NamedLambdaVariable) :: Nil, _) = function | ||
|
||
|
|
||
| override def nullSafeEval(inputRow: InternalRow, argumentValue: Any): Any = { | ||
| val map = argumentValue.asInstanceOf[MapData] | ||
| val resultValues = new GenericArrayData(new Array[Any](map.numElements)) | ||
| var i = 0 | ||
| while (i < map.numElements) { | ||
| keyVar.value.set(map.keyArray().get(i, keyVar.dataType)) | ||
| valueVar.value.set(map.valueArray().get(i, valueVar.dataType)) | ||
| resultValues.update(i, functionForEval.eval(inputRow)) | ||
| i += 1 | ||
| } | ||
| new ArrayBasedMapData(map.keyArray(), resultValues) | ||
| } | ||
| override def prettyName: String = "transform_values" | ||
| } | ||
|
|
||
| /** | ||
| * Merges two given maps into a single map by applying function to the pair of values with | ||
| * the same key. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2302,6 +2302,177 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext { | |
| assert(ex5.getMessage.contains("function map_zip_with does not support ordering on type map")) | ||
| } | ||
|
|
||
| test("transform values function - test primitive data types") { | ||
| val dfExample1 = Seq( | ||
| Map[Int, Int](1 -> 1, 9 -> 9, 8 -> 8, 7 -> 7) | ||
| ).toDF("i") | ||
|
|
||
| val dfExample2 = Seq( | ||
| Map[Boolean, String](false -> "abc", true -> "def") | ||
| ).toDF("x") | ||
|
|
||
| val dfExample3 = Seq( | ||
| Map[String, Int]("a" -> 1, "b" -> 2, "c" -> 3) | ||
| ).toDF("y") | ||
|
|
||
| val dfExample4 = Seq( | ||
| Map[Int, Double](1 -> 1.0, 2 -> 1.40, 3 -> 1.70) | ||
| ).toDF("z") | ||
|
|
||
| val dfExample5 = Seq( | ||
| Map[Int, Array[Int]](1 -> Array(1, 2)) | ||
| ).toDF("c") | ||
|
|
||
| def testMapOfPrimitiveTypesCombination(): Unit = { | ||
| checkAnswer(dfExample1.selectExpr("transform_values(i, (k, v) -> k + v)"), | ||
| Seq(Row(Map(1 -> 2, 9 -> 18, 8 -> 16, 7 -> 14)))) | ||
|
|
||
| checkAnswer(dfExample2.selectExpr( | ||
| "transform_values(x, (k, v) -> if(k, v, CAST(k AS String)))"), | ||
| Seq(Row(Map(false -> "false", true -> "def")))) | ||
|
|
||
| checkAnswer(dfExample2.selectExpr("transform_values(x, (k, v) -> NOT k AND v = 'abc')"), | ||
| Seq(Row(Map(false -> true, true -> false)))) | ||
|
|
||
| checkAnswer(dfExample3.selectExpr("transform_values(y, (k, v) -> v * v)"), | ||
| Seq(Row(Map("a" -> 1, "b" -> 4, "c" -> 9)))) | ||
|
|
||
| checkAnswer(dfExample3.selectExpr( | ||
| "transform_values(y, (k, v) -> k || ':' || CAST(v as String))"), | ||
| Seq(Row(Map("a" -> "a:1", "b" -> "b:2", "c" -> "c:3")))) | ||
|
|
||
| checkAnswer( | ||
| dfExample3.selectExpr("transform_values(y, (k, v) -> concat(k, cast(v as String)))"), | ||
| Seq(Row(Map("a" -> "a1", "b" -> "b2", "c" -> "c3")))) | ||
|
|
||
| checkAnswer( | ||
| dfExample4.selectExpr( | ||
| "transform_values(" + | ||
| "z,(k, v) -> map_from_arrays(ARRAY(1, 2, 3), " + | ||
| "ARRAY('one', 'two', 'three'))[k] || '_' || CAST(v AS String))"), | ||
| Seq(Row(Map(1 -> "one_1.0", 2 -> "two_1.4", 3 ->"three_1.7")))) | ||
|
|
||
| checkAnswer( | ||
| dfExample4.selectExpr("transform_values(z, (k, v) -> k-v)"), | ||
| Seq(Row(Map(1 -> 0.0, 2 -> 0.6000000000000001, 3 -> 1.3)))) | ||
|
|
||
| checkAnswer( | ||
| dfExample5.selectExpr("transform_values(c, (k, v) -> k + cardinality(v))"), | ||
| Seq(Row(Map(1 -> 3)))) | ||
| } | ||
|
|
||
| // Test with local relation, the Project will be evaluated without codegen | ||
| testMapOfPrimitiveTypesCombination() | ||
| dfExample1.cache() | ||
| dfExample2.cache() | ||
| dfExample3.cache() | ||
| dfExample4.cache() | ||
| dfExample5.cache() | ||
| // Test with cached relation, the Project will be evaluated with codegen | ||
| testMapOfPrimitiveTypesCombination() | ||
| } | ||
|
|
||
| test("transform values function - test empty") { | ||
| val dfExample1 = Seq( | ||
| Map.empty[Integer, Integer] | ||
| ).toDF("i") | ||
|
|
||
| val dfExample2 = Seq( | ||
| Map.empty[BigInt, String] | ||
| ).toDF("j") | ||
|
|
||
| def testEmpty(): Unit = { | ||
| checkAnswer(dfExample1.selectExpr("transform_values(i, (k, v) -> NULL)"), | ||
| Seq(Row(Map.empty[Integer, Integer]))) | ||
|
|
||
| checkAnswer(dfExample1.selectExpr("transform_values(i, (k, v) -> k)"), | ||
| Seq(Row(Map.empty[Integer, Integer]))) | ||
|
|
||
| checkAnswer(dfExample1.selectExpr("transform_values(i, (k, v) -> v)"), | ||
| Seq(Row(Map.empty[Integer, Integer]))) | ||
|
|
||
| checkAnswer(dfExample1.selectExpr("transform_values(i, (k, v) -> 0)"), | ||
| Seq(Row(Map.empty[Integer, Integer]))) | ||
|
|
||
| checkAnswer(dfExample1.selectExpr("transform_values(i, (k, v) -> 'value')"), | ||
| Seq(Row(Map.empty[Integer, String]))) | ||
|
|
||
| checkAnswer(dfExample1.selectExpr("transform_values(i, (k, v) -> true)"), | ||
| Seq(Row(Map.empty[Integer, Boolean]))) | ||
|
|
||
| checkAnswer(dfExample2.selectExpr("transform_values(j, (k, v) -> k + cast(v as BIGINT))"), | ||
| Seq(Row(Map.empty[BigInt, BigInt]))) | ||
| } | ||
|
|
||
| testEmpty() | ||
| dfExample1.cache() | ||
| dfExample2.cache() | ||
| testEmpty() | ||
| } | ||
|
|
||
| test("transform values function - test null values") { | ||
| val dfExample1 = Seq( | ||
| Map[Int, Integer](1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4) | ||
| ).toDF("a") | ||
|
|
||
| val dfExample2 = Seq( | ||
| Map[Int, String](1 -> "a", 2 -> "b", 3 -> null) | ||
| ).toDF("b") | ||
|
|
||
| def testNullValue(): Unit = { | ||
| checkAnswer(dfExample1.selectExpr("transform_values(a, (k, v) -> null)"), | ||
| Seq(Row(Map[Int, Integer](1 -> null, 2 -> null, 3 -> null, 4 -> null)))) | ||
|
|
||
| checkAnswer(dfExample2.selectExpr( | ||
| "transform_values(b, (k, v) -> IF(v IS NULL, k + 1, k + 2))"), | ||
| Seq(Row(Map(1 -> 3, 2 -> 4, 3 -> 4)))) | ||
| } | ||
|
|
||
| testNullValue() | ||
| dfExample1.cache() | ||
| dfExample2.cache() | ||
| testNullValue() | ||
| } | ||
|
|
||
| test("transform values function - test invalid functions") { | ||
| val dfExample1 = Seq( | ||
| Map[Int, Int](1 -> 1, 9 -> 9, 8 -> 8, 7 -> 7) | ||
| ).toDF("i") | ||
|
|
||
| val dfExample2 = Seq( | ||
| Map[String, String]("a" -> "b") | ||
| ).toDF("j") | ||
|
|
||
| val dfExample3 = Seq( | ||
| Seq(1, 2, 3, 4) | ||
| ).toDF("x") | ||
|
|
||
| def testInvalidLambdaFunctions(): Unit = { | ||
|
|
||
| val ex1 = intercept[AnalysisException] { | ||
| dfExample1.selectExpr("transform_values(i, k -> k )") | ||
|
||
| } | ||
| assert(ex1.getMessage.contains("The number of lambda function arguments '1' does not match")) | ||
|
|
||
| val ex2 = intercept[AnalysisException] { | ||
| dfExample2.selectExpr("transform_values(j, (k, v, x) -> k + 1)") | ||
| } | ||
| assert(ex2.getMessage.contains("The number of lambda function arguments '3' does not match")) | ||
|
|
||
| val ex3 = intercept[AnalysisException] { | ||
| dfExample3.selectExpr("transform_values(x, (k, v) -> k + 1)") | ||
| } | ||
| assert(ex3.getMessage.contains( | ||
| "data type mismatch: argument 1 requires map type")) | ||
| } | ||
|
|
||
| testInvalidLambdaFunctions() | ||
| dfExample1.cache() | ||
| dfExample2.cache() | ||
| dfExample3.cache() | ||
| testInvalidLambdaFunctions() | ||
| } | ||
|
|
||
| private def assertValuesDoNotChangeAfterCoalesceOrUnion(v: Column): Unit = { | ||
| import DataFrameFunctionsSuite.CodegenFallbackExpr | ||
| for ((codegenFallback, wholeStage) <- Seq((true, false), (false, false), (false, true))) { | ||
|
|
||
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.
nit: indent