Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ object FunctionRegistry {
expression[NullIf]("nullif"),
expression[Nvl]("nvl"),
expression[Nvl2]("nvl2"),
expression[PosExplode]("posexplode"),
expression[Rand]("rand"),
expression[Randn]("randn"),
expression[CreateStruct]("struct"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

new StructType()
  .add("pos", IntegerType, nullable = false)
  .add("key", kt, nullable = false)
  .add("value", vt, valueContainsNull)

so it is more clear we have three fields.

.add("value", vt, valueContainsNull)
} else {
new StructType().add("key", kt, false).add("value", vt, valueContainsNull)
}
}

override def eval(input: InternalRow): TraversableOnce[InternalRow] = {
Expand All @@ -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
}
Expand All @@ -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.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an example would be useful.

// scalastyle:on line.size.limit
case class Explode(child: Expression)
extends ExplodeBase(child, position = false) with Serializable {
Copy link
Contributor

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

Copy link
Contributor

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 { }

Copy link
Member Author

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.

}

/**
* Given an input array produces a sequence of rows for each position and value in the array.
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

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 { }

*/
// scalastyle:off line.size.limit
@ExpressionDescription(
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this is needed. Without this, there occurs runtime errors. This happens usually when extending abstract base classes.

}
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ class ExpressionTypeCheckingSuite extends SparkFunSuite {
assertError(new Murmur3Hash(Nil), "function hash requires at least one argument")
assertError(Explode('intField),
"input to function explode should be array or map type")
assertError(PosExplode('intField),
"input to function explode should be array or map type")
}

test("check types for CreateNamedStruct") {
Expand Down
1 change: 1 addition & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/Column.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ class Column(protected[sql] val expr: Expression) extends Logging {
// Leave an unaliased generator with an empty list of names since the analyzer will generate
// the correct defaults after the nested expression's type has been resolved.
case explode: Explode => MultiAlias(explode, Nil)
case explode: PosExplode => MultiAlias(explode, Nil)

case jt: JsonTuple => MultiAlias(jt, Nil)

Expand Down
8 changes: 8 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Copy link
Contributor

Choose a reason for hiding this comment

The 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ class ColumnExpressionSuite extends QueryTest with SharedSQLContext {
Row(1) :: Row(2) :: Row(3) :: Nil)
}

test("single posexplode") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while you are at it, let's create a new suite for end-to-end generators

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and move the existing ones over there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is sql.GeneratorSuite.scala okay? The package is different from expression.GeneratorSuite.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GeneratorFunctionsSuite to match the rest

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry. What do you mean by the rest for GeneratorFunctionsSuite?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, instead of sql.GeneratorSuite. I see.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rest of the test cases that use explode in this file

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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,6 @@ private[sql] class HiveSessionCatalog(
"xpath_number", "xpath_short", "xpath_string",

// table generating function
"inline", "posexplode"
"inline"
)
}