Uh oh!
There was an error while loading. Please reload this page.
[SPARK-13320] [SQL] Support Star in CreateStruct/CreateArray and Error Handling when DataFrame/DataSet Functions using Star - #11208
Conversation
| case s: Star => s.expand(child, resolver) | ||
| case o => o :: Nil | ||
| }) | ||
| case c: CreateStruct if containsStar(c.children) => |
There was a problem hiding this comment.
Not sure if we have the other functions that can accept star as an input parameter. If so, I think we need to create a trait for all these case classes. Then, we can remove the duplicate code. Any better idea? Thanks! : )
SparkQA
commented
Feb 15, 2016
Test build #51321 has finished for PR 11208 at commit
|
rxin
commented
Feb 15, 2016
cc @cloud-fan |
| case s: Star => s.expand(child, resolver) | ||
| case o => o :: Nil | ||
| }) | ||
| case c: CreateStructUnsafe if containsStar(c.children) => |
There was a problem hiding this comment.
CreateStructUnsafe only appears after unsafe projection, so I think we don't need to handle it in Analyzer
There was a problem hiding this comment.
I saw it is being used in two parts in Analyzer. Will remove them. Thanks!
cloud-fan
commented
Feb 16, 2016
The PR title looks confusing, |
gatorsmile
commented
Feb 16, 2016
So far, Spark SQL does not handle star expansion when we use Actually, I am not sure if |
cloud-fan
commented
Feb 16, 2016
Actually we do handle stars in One of my concern is: sometimes we check stars under |
gatorsmile
commented
Feb 16, 2016
uh, I see. The code you posted above is for Yeah, we need a clean and complete fix for resolving star. Let me check if can move these into |
gatorsmile
commented
Feb 19, 2016
@cloud-fan The latest commit separates star resolution from the reference resolution, since |
SparkQA
commented
Feb 19, 2016
Test build #51512 has finished for PR 11208 at commit
|
| """.stripMargin).select($"r.*"), | ||
| Row(3, 2) :: Nil) | ||
| assert(structDf.groupBy($"a").agg(min(struct($"record.*"))).first() == Row(3, Row(3, 1))) |
There was a problem hiding this comment.
We should write a new test case to test * in CreateStruct and CreateArray, not just put in existing ones.
cloud-fan
commented
Feb 19, 2016
Overall LGTM except some comments about tests, thanks for working on it! |
SparkQA
commented
Feb 19, 2016
Test build #51536 has finished for PR 11208 at commit
|
gatorsmile
commented
Feb 22, 2016
retest this please |
| val f = udf((a: String) => a) | ||
| val df = sparkContext.parallelize(Seq((1, 1))).toDF("a", "b") | ||
| df.select(struct($"a").as("s")).select(f($"s.a")).collect() | ||
| df.select(struct($"*").as("s")).select(f($"s.a")).collect() |
SparkQA
commented
Feb 23, 2016
Test build #51701 has finished for PR 11208 at commit
|
SparkQA
commented
Feb 23, 2016
Test build #51729 has finished for PR 11208 at commit
|
| } | ||
| ) | ||
| case g: Generate if containsStar(g.generator.children) => | ||
| failAnalysis("Cannot explode *, explode can only be applied on a specific column.") |
There was a problem hiding this comment.
just realized the error message is not clear enough, Generate is not always "explode"
There was a problem hiding this comment.
do we have a test for this error message?
There was a problem hiding this comment.
True. I moved this from another rule. I will check the coverage of test cases. Thanks!
There was a problem hiding this comment.
We already have a test case: https://github.com/apache/spark/blob/master/sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala#L181-L182
How about changing the message to Invalid usage of '*' in explode/json_tuple/UDTF? Thanks!
There was a problem hiding this comment.
explode/json_tuple/UDTF LGTM
There was a problem hiding this comment.
Thanks! Let me change it now.
gatorsmile
commented
Mar 12, 2016
retest this please |
SparkQA
commented
Mar 12, 2016
Test build #53008 has finished for PR 11208 at commit
|
gatorsmile
commented
Mar 16, 2016
retest this please |
SparkQA
commented
Mar 17, 2016
Test build #53376 has finished for PR 11208 at commit
|
gatorsmile
commented
Mar 17, 2016
cc @yhuai |
gatorsmile
commented
Mar 19, 2016
retest this please |
SparkQA
commented
Mar 19, 2016
Test build #53620 has finished for PR 11208 at commit
|
gatorsmile
commented
Mar 19, 2016
cc @yhuai |
| case o => o :: Nil | ||
| }) | ||
| // count(*) has been replaced by count(1) | ||
| case o if containsStar(o.children) => |
There was a problem hiding this comment.
We can have a method:
private def mayContainsStar(expr: Expression): Boolean = expr.isInstnaceOf[UnresolvedFunction] || expr.isInstnaceOf[CreateStruct]...
then we can simplify this to:
expr.transformUp {
case e if mayContainsStar(e) =>
e.copy(children = ...)
}
There was a problem hiding this comment.
Tried it, but copy is unable to use here. When the type is Expression (abstract type), we are unable to use the copy function to change the children. In addition, withNewChildren requires the same number of children. Do you have any idea how to fix it? Thanks!
There was a problem hiding this comment.
oh i see, I don't have a better idea, let's just keep it this way.
cloud-fan
commented
Mar 21, 2016
Sorry for putting it here for such a long time, overall LGTM, will merge it after you address the new comments, thanks! |
gatorsmile
commented
Mar 21, 2016
@cloud-fan Thank you for your detailed reviews! I know all of you are very busy. Let me know if anything needs a change. Thanks again! |
| UnresolvedAlias(child = expandStarExpression(ua.child, p.child)) :: Nil | ||
| case a @ Alias(_: UnresolvedFunction | _: CreateArray | _: CreateStruct, _) => | ||
| Alias(child = expandStarExpression(a.child, p.child), a.name)( | ||
| isGenerated = a.isGenerated) :: Nil |
There was a problem hiding this comment.
We will lose qualifier here, how about a.withNewChildren(expandStarExpression(a.child, p.child) :: Nil)?
SparkQA
commented
Mar 21, 2016
Test build #53655 has finished for PR 11208 at commit
|
| } | ||
| } | ||
| test("Star Expansion - CreateStruct and CreateArray") { |
There was a problem hiding this comment.
Why do we put these tests in SQLQuerySuite? It looks like they are mostly testing DF APIs.
There was a problem hiding this comment.
True, let me move them to DataFrameSuite. Thanks!
SparkQA
commented
Mar 21, 2016
Test build #53661 has finished for PR 11208 at commit
|
SparkQA
commented
Mar 21, 2016
Test build #53685 has finished for PR 11208 at commit
|
cloud-fan
commented
Mar 22, 2016
thanks! merging to master! |
… Handling when DataFrame/DataSet Functions using Star
This PR resolves two issues:
First, expanding * inside aggregate functions of structs when using Dataframe/Dataset APIs. For example,
```scala
structDf.groupBy($"a").agg(min(struct($"record.*")))
```
Second, it improves the error messages when having invalid star usage when using Dataframe/Dataset APIs. For example,
```scala
pagecounts4PartitionsDS
.map(line => (line._1, line._3))
.toDF()
.groupBy($"_1")
.agg(sum("*") as "sumOccurances")
```
Before the fix, the invalid usage will issue a confusing error message, like:
```
org.apache.spark.sql.AnalysisException: cannot resolve '_1' given input columns _1, _2;
```
After the fix, the message is like:
```
org.apache.spark.sql.AnalysisException: Invalid usage of '*' in function 'sum'
```
cc: rxin nongli cloud-fan
Author: gatorsmile <gatorsmile@gmail.com>
Closesapache#11208 from gatorsmile/sumDataSetResolution.davies
commented
Mar 25, 2016
@gatorsmile@cloud-fan This PR revert the change in #3674, unfortunately the unit test in AnalysisSuite. This test break once we enforce max-iteration check in tests, see https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/54090/testReport/org.apache.spark.sql.catalyst.analysis/AnalysisSuite/union_project__/ |
davies
commented
Mar 25, 2016
@gatorsmile This PR can't be easily reverted, so could you send a PR to fix it? |
davies
commented
Mar 25, 2016
I will fix this in #11828 |
This PR resolves two issues:
First, expanding * inside aggregate functions of structs when using Dataframe/Dataset APIs. For example,
Second, it improves the error messages when having invalid star usage when using Dataframe/Dataset APIs. For example,
Before the fix, the invalid usage will issue a confusing error message, like:
After the fix, the message is like:
cc: @rxin@nongli@cloud-fan