-
Notifications
You must be signed in to change notification settings - Fork 28.3k
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
[SPARK-16289][SQL] Implement posexplode table generating function #13971
Changes from all commits
584eb9e
fcfccee
e255873
1cf723a
c5dee49
153e8eb
0266052
5f3a951
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 |
---|---|---|
|
@@ -234,6 +234,7 @@ exportMethods("%in%", | |
"over", | ||
"percent_rank", | ||
"pmod", | ||
"posexplode", | ||
"quarter", | ||
"rand", | ||
"randn", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,13 +94,10 @@ case class UserDefinedGenerator( | |
} | ||
|
||
/** | ||
* Given an input array produces a sequence of rows for each value in the array. | ||
* A base class for Explode and PosExplode | ||
*/ | ||
// scalastyle:off line.size.limit | ||
@ExpressionDescription( | ||
usage = "_FUNC_(a) - Separates the elements of array a into multiple rows, or the elements of a map into multiple rows and columns.") | ||
// scalastyle:on line.size.limit | ||
case class Explode(child: Expression) extends UnaryExpression with Generator with CodegenFallback { | ||
abstract class ExplodeBase(child: Expression, position: Boolean) | ||
extends UnaryExpression with Generator with CodegenFallback with Serializable { | ||
|
||
override def children: Seq[Expression] = child :: Nil | ||
|
||
|
@@ -115,9 +112,26 @@ case class Explode(child: Expression) extends UnaryExpression with Generator wit | |
|
||
// hive-compatible default alias for explode function ("col" for array, "key", "value" for map) | ||
override def elementSchema: StructType = child.dataType match { | ||
case ArrayType(et, containsNull) => new StructType().add("col", et, containsNull) | ||
case ArrayType(et, containsNull) => | ||
if (position) { | ||
new StructType() | ||
.add("pos", IntegerType, false) | ||
.add("col", et, containsNull) | ||
} else { | ||
new StructType() | ||
.add("col", et, containsNull) | ||
} | ||
case MapType(kt, vt, valueContainsNull) => | ||
new StructType().add("key", kt, false).add("value", vt, valueContainsNull) | ||
if (position) { | ||
new StructType() | ||
.add("pos", IntegerType, false) | ||
.add("key", kt, false) | ||
.add("value", vt, valueContainsNull) | ||
} else { | ||
new StructType() | ||
.add("key", kt, false) | ||
.add("value", vt, valueContainsNull) | ||
} | ||
} | ||
|
||
override def eval(input: InternalRow): TraversableOnce[InternalRow] = { | ||
|
@@ -129,7 +143,7 @@ case class Explode(child: Expression) extends UnaryExpression with Generator wit | |
} else { | ||
val rows = new Array[InternalRow](inputArray.numElements()) | ||
inputArray.foreach(et, (i, e) => { | ||
rows(i) = InternalRow(e) | ||
rows(i) = if (position) InternalRow(i, e) else InternalRow(e) | ||
}) | ||
rows | ||
} | ||
|
@@ -141,11 +155,43 @@ case class Explode(child: Expression) extends UnaryExpression with Generator wit | |
val rows = new Array[InternalRow](inputMap.numElements()) | ||
var i = 0 | ||
inputMap.foreach(kt, vt, (k, v) => { | ||
rows(i) = InternalRow(k, v) | ||
rows(i) = if (position) InternalRow(i, k, v) else InternalRow(k, v) | ||
i += 1 | ||
}) | ||
rows | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Given an input array produces a sequence of rows for each value in the array. | ||
* | ||
* {{{ | ||
* SELECT explode(array(10,20)) -> | ||
* 10 | ||
* 20 | ||
* }}} | ||
*/ | ||
// scalastyle:off line.size.limit | ||
@ExpressionDescription( | ||
usage = "_FUNC_(a) - Separates the elements of array a into multiple rows, or the elements of map a into multiple rows and columns.", | ||
extended = "> SELECT _FUNC_(array(10,20));\n 10\n 20") | ||
// scalastyle:on line.size.limit | ||
case class Explode(child: Expression) extends ExplodeBase(child, position = false) | ||
|
||
/** | ||
* Given an input array produces a sequence of rows for each position and value in the array. | ||
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. btw since the expression description might be difficult to see without line wrapping, it'd also be better to put an example here. 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. also you don't need the |
||
* | ||
* {{{ | ||
* SELECT posexplode(array(10,20)) -> | ||
* 0 10 | ||
* 1 20 | ||
* }}} | ||
*/ | ||
// scalastyle:off line.size.limit | ||
@ExpressionDescription( | ||
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. an example would be useful. |
||
usage = "_FUNC_(a) - Separates the elements of array a into multiple rows with positions, or the elements of a map into multiple rows and columns with positions.", | ||
extended = "> SELECT _FUNC_(array(10,20));\n 0\t10\n 1\t20") | ||
// scalastyle:on line.size.limit | ||
case class PosExplode(child: Expression) extends ExplodeBase(child, position = true) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* 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.InternalRow | ||
import org.apache.spark.unsafe.types.UTF8String | ||
|
||
class GeneratorExpressionSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
private def checkTuple(actual: ExplodeBase, expected: Seq[InternalRow]): Unit = { | ||
assert(actual.eval(null).toSeq === expected) | ||
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. We have 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. Oh, thank you for review, @cloud-fan , too. 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. And, how to check the zero row? At Line 39, 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.
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. Sure. @cloud-fan . In fact, I try everything you told me in many ways because I trust you. :) 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. As a evidence, let me write the results of the most simplest case. checkEvaluation(Explode(CreateArray(Seq.empty)), Seq.empty[Row])
checkEvaluation(Explode(CreateArray(Seq.empty)), Seq.empty[InternalRow])
checkEvaluation(Explode(CreateArray(Seq.empty)), Seq.empty) All the above returns the followings.
Here is the body of // 1. This makes `Seq[Any]` into `GenericArrayData` generally.
val catalystValue = CatalystTypeConverters.convertToCatalyst(expected)
checkEvaluationWithoutCodegen(expression, catalystValue, inputRow)
// 2. Here, `val actual = plan(inputRow).get(0, expression.dataType)` is called to try casting to `expression.dataType`.
checkEvaluationWithGeneratedMutableProjection(expression, catalystValue, inputRow)
if (GenerateUnsafeProjection.canSupport(expression.dataType)) {
// 3. Here, `val unsafeRow = plan(inputRow)` with one row assumption.
checkEvalutionWithUnsafeProjection(expression, catalystValue, inputRow)
}
// 4. Here, `checkResult` fails at `result == expected`.
checkEvaluationWithOptimization(expression, catalystValue, inputRow) In short, every steps of the 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. If I didn't misunderstand, it's definitely valuable issue to investigate more. If we can upgrade 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. Let's not change it for now. We also don't want test code to become so complicated that is is no longer obvious what's going on. 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. Yep. Thank you. I'll investigate it later. |
||
} | ||
|
||
private final val int_array = Seq(1, 2, 3) | ||
private final val str_array = Seq("a", "b", "c") | ||
|
||
test("explode") { | ||
val int_correct_answer = Seq(Seq(1), Seq(2), Seq(3)) | ||
val str_correct_answer = Seq( | ||
Seq(UTF8String.fromString("a")), | ||
Seq(UTF8String.fromString("b")), | ||
Seq(UTF8String.fromString("c"))) | ||
|
||
checkTuple( | ||
Explode(CreateArray(Seq.empty)), | ||
Seq.empty) | ||
|
||
checkTuple( | ||
Explode(CreateArray(int_array.map(Literal(_)))), | ||
int_correct_answer.map(InternalRow.fromSeq(_))) | ||
|
||
checkTuple( | ||
Explode(CreateArray(str_array.map(Literal(_)))), | ||
str_correct_answer.map(InternalRow.fromSeq(_))) | ||
} | ||
|
||
test("posexplode") { | ||
val int_correct_answer = Seq(Seq(0, 1), Seq(1, 2), Seq(2, 3)) | ||
val str_correct_answer = Seq( | ||
Seq(0, UTF8String.fromString("a")), | ||
Seq(1, UTF8String.fromString("b")), | ||
Seq(2, UTF8String.fromString("c"))) | ||
|
||
checkTuple( | ||
PosExplode(CreateArray(Seq.empty)), | ||
Seq.empty) | ||
|
||
checkTuple( | ||
PosExplode(CreateArray(int_array.map(Literal(_)))), | ||
int_correct_answer.map(InternalRow.fromSeq(_))) | ||
|
||
checkTuple( | ||
PosExplode(CreateArray(str_array.map(Literal(_)))), | ||
str_correct_answer.map(InternalRow.fromSeq(_))) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2721,6 +2721,14 @@ object functions { | |
*/ | ||
def explode(e: Column): Column = withExpr { Explode(e.expr) } | ||
|
||
/** | ||
* Creates a new row for each element with position in the given array or map column. | ||
* | ||
* @group collection_funcs | ||
* @since 2.1.0 | ||
*/ | ||
def posexplode(e: Column): Column = withExpr { PosExplode(e.expr) } | ||
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. need to add this to python too |
||
|
||
/** | ||
* Extracts json object from a json string based on json path specified, and returns json string | ||
* of the extracted json object. It will return null if the input json string is invalid. | ||
|
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.
cc @rxin , is
posexplode
a special hive fallback function that we need to register? other ones don't get registered infunctions
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.
For this one, I thought the reason is
explode
is already registered.posexplode
is a pair of that.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.
yea this one is probably fine.
i wouldn't register the other ones.
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.
Thank you for reconfirming!