-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16289][SQL] Implement posexplode table generating function #13971
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
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 |
|---|---|---|
|
|
@@ -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,19 @@ 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 +136,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 +148,33 @@ 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. | ||
| */ | ||
| // 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.") | ||
|
||
| // scalastyle:on line.size.limit | ||
| case class Explode(child: Expression) | ||
| extends ExplodeBase(child, position = false) with Serializable { | ||
|
||
| } | ||
|
|
||
| /** | ||
| * Given an input array produces a sequence of rows for each position and value in the array. | ||
|
Contributor
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.
Contributor
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 |
||
| */ | ||
| // scalastyle:off line.size.limit | ||
| @ExpressionDescription( | ||
|
Contributor
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.") | ||
| // scalastyle:on line.size.limit | ||
| case class PosExplode(child: Expression) | ||
| extends ExplodeBase(child, position = true) with Serializable { | ||
|
||
| } | ||
| 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) } | ||
|
Contributor
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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,6 +129,13 @@ class ColumnExpressionSuite extends QueryTest with SharedSQLContext { | |
| Row(1) :: Row(2) :: Row(3) :: Nil) | ||
| } | ||
|
|
||
| test("single posexplode") { | ||
|
||
| val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList") | ||
| checkAnswer( | ||
| df.select(posexplode('intList)), | ||
| Row(0, 1) :: Row(1, 2) :: Row(2, 3) :: Nil) | ||
| } | ||
|
|
||
| test("explode and other columns") { | ||
| val df = Seq((1, Seq(1, 2, 3))).toDF("a", "intList") | ||
|
|
||
|
|
||
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.
maybe
so it is more clear we have three fields.