-
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 3 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 |
|---|---|---|
|
|
@@ -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,47 @@ 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) 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 |
||
| * | ||
| * {{{ | ||
| * SELECT explode(array(10,20)) -> | ||
| * 0 10 | ||
| * 1 20 | ||
| * }}} | ||
| */ | ||
| // 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.", | ||
| 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) with Serializable { | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * 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 GeneratorSuite extends SparkFunSuite with ExpressionEvalHelper { | ||
|
||
| private def checkTuple(actual: ExplodeBase, expected: Seq[InternalRow]): Unit = { | ||
| assert(actual.eval(null).toSeq === expected) | ||
| } | ||
|
|
||
| private final val int_array = Seq(1, 2, 3) | ||
| private final val str_array = Seq("a", "b", "c") | ||
|
|
||
| test("explode") { | ||
|
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. can you add a test case for empty input?
Member
Author
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. Done. |
||
| 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(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(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) } | ||
|
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.
case classes are automatically serializable so you don't need this i think
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.
also you don't need the
{ }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.
Hmm. Okay. I will try to remove Serializable.