-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16288][SQL] Implement inline table generating function #13976
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 2 commits
9382f64
c43a187
ebd9cfa
31ffa75
fed3ba2
e260359
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 |
|---|---|---|
|
|
@@ -195,3 +195,41 @@ case class Explode(child: Expression) extends ExplodeBase(child, position = fals | |
| 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) | ||
|
|
||
| /** | ||
| * Explodes an array of structs into a table. | ||
| */ | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(a) - Explodes an array of structs into a table.", | ||
| extended = "> SELECT _FUNC_(array(struct(1, 'a'), struct(2, 'b')));\n [1,a]\n[2,b]") | ||
| case class Inline(child: Expression) extends UnaryExpression with Generator with CodegenFallback { | ||
|
|
||
| override def children: Seq[Expression] = child :: Nil | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = child.dataType match { | ||
| case ArrayType(et, _) if et.isInstanceOf[StructType] => | ||
| TypeCheckResult.TypeCheckSuccess | ||
| case _ => | ||
| TypeCheckResult.TypeCheckFailure( | ||
| s"input to function inline should be array of struct type, not ${child.dataType}") | ||
| } | ||
|
|
||
| override def elementSchema: StructType = child.dataType match { | ||
| case ArrayType(et : StructType, _) => | ||
| StructType(et.fields.zipWithIndex.map { | ||
| case (field, index) => StructField(field.name, field.dataType, nullable = field.nullable) | ||
| }) | ||
| } | ||
|
|
||
| private lazy val numFields = elementSchema.fields.length | ||
|
|
||
| override def eval(input: InternalRow): TraversableOnce[InternalRow] = { | ||
| val inputArray = child.eval(input).asInstanceOf[ArrayData] | ||
| if (inputArray == null) { | ||
| Nil | ||
| } else { | ||
| for (i <- 0 until inputArray.numElements()) | ||
| yield inputArray.getStruct(i, numFields) | ||
|
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. I'm not sure how is the performance of
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. Thank you, @cloud-fan . By the way, for about this, @rxin gave me an advice at the first commit of this PR.
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. ah i see, |
||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,4 +89,32 @@ class GeneratorFunctionSuite extends QueryTest with SharedSQLContext { | |
| exploded.join(exploded, exploded("i") === exploded("i")).agg(count("*")), | ||
| Row(3) :: Nil) | ||
| } | ||
|
|
||
| test("inline raises exception on empty array") { | ||
|
||
| val m = intercept[AnalysisException] { | ||
| spark.range(2).selectExpr("inline(array())") | ||
| }.getMessage | ||
| assert(m.contains("data type mismatch")) | ||
| } | ||
|
|
||
| test("inline with empty table") { | ||
| checkAnswer( | ||
| spark.range(0).selectExpr("inline(array(struct(10, 100)))"), | ||
| Nil) | ||
| } | ||
|
|
||
| test("inline on literal") { | ||
| checkAnswer( | ||
| spark.range(2).selectExpr("inline(array(struct(10, 100), struct(20, 200), struct(30, 300)))"), | ||
| Row(10, 100) :: Row(20, 200) :: Row(30, 300) :: | ||
| Row(10, 100) :: Row(20, 200) :: Row(30, 300) :: Nil) | ||
| } | ||
|
|
||
| test("inline on column") { | ||
| val df = Seq((1, 2)).toDF("a", "b") | ||
|
|
||
| checkAnswer( | ||
| df.selectExpr("inline(array(struct(a, b), struct(a, b)))"), | ||
| Row(1, 2) :: Row(1, 2) :: Nil) | ||
| } | ||
| } | ||
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, so it's just
etnow?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.
Yep. Currently, our type checker ensures that homogeneous StructType array.
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.
why not return
etdirectly?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.
Oh, my god. I was too naive, here.
Thank you!