-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-32163][SQL] Nested pruning should work even with cosmetic variations #28988
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
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 |
|---|---|---|
|
|
@@ -149,10 +149,12 @@ object NestedColumnAliasing { | |
| case _ => false | ||
| } | ||
|
|
||
| // Note that when we group by extractors with their references, we should remove | ||
| // cosmetic variations. | ||
| val exclusiveAttrSet = AttributeSet(exclusiveAttrs ++ otherRootReferences) | ||
| val aliasSub = nestedFieldReferences.asInstanceOf[Seq[ExtractValue]] | ||
| .filter(!_.references.subsetOf(exclusiveAttrSet)) | ||
| .groupBy(_.references.head) | ||
| .groupBy(_.references.head.canonicalized.asInstanceOf[Attribute]) | ||
| .flatMap { case (attr, nestedFields: Seq[ExtractValue]) => | ||
| // Remove redundant `ExtractValue`s if they share the same parent nest field. | ||
| // For example, when `a.b` and `a.b.c` are in project list, we only need to alias `a.b`. | ||
|
|
@@ -174,9 +176,12 @@ object NestedColumnAliasing { | |
|
|
||
| // If all nested fields of `attr` are used, we don't need to introduce new aliases. | ||
| // By default, ColumnPruning rule uses `attr` already. | ||
| // Note that we need to remove cosmetic variations first, so we only count a | ||
| // nested field once. | ||
| if (nestedFieldToAlias.nonEmpty && | ||
| nestedFieldToAlias | ||
| .map { case (nestedField, _) => totalFieldNum(nestedField.dataType) } | ||
| dedupNestedFields.map(_.canonicalized.asInstanceOf[ExtractValue]) | ||
|
Member
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. nit: we don't need the cast here?
Member
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. This part is related to the query failure in the test? Looks it is just an optimization?
Member
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, we cannot avoid the generating duplicated aliases (
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. Due to cosmetic variations, Extractors with different qualifiers, for example, will cause incorrect total number of fields.
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. For the duplicated aliases, we can avoid it. We can work on canonicalized extractors when generating aliases. But we also need to convert coming extractors to canonicalized versions when we look up into the alias map. Currently the code looks clear. And it seems not a big deal, and I think it is rare case that there are multiple extractors with cosmetic difference. So currently I don't try to do that.
Member
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.
Thanks for the explanation. Looks okay. |
||
| .distinct | ||
| .map { nestedField => totalFieldNum(nestedField.dataType) } | ||
| .sum < totalFieldNum(attr.dataType)) { | ||
| Some(attr.exprId -> nestedFieldToAlias) | ||
| } else { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -497,6 +497,18 @@ abstract class SchemaPruningSuite | |||||||||||||
| Row(Row("Janet", null, "Jones"), "Jones") ::Nil) | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| testSchemaPruning("SPARK-32163: nested pruning should work even with cosmetic variations") { | ||||||||||||||
| withTempView("contact_alias") { | ||||||||||||||
| sql("select * from contacts") | ||||||||||||||
| .repartition(100, col("name.first"), col("name.last")) | ||||||||||||||
|
Member
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. This issue cannot happen in branch-3.0? spark/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala Lines 80 to 85 in fc2660c
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. This test case is only for master branch. However this issue can happen in branch-3.0 too. I added another new test here, which is for branch-3.0. But when we backport this to branch-3.0, we need to remove first test case as it will fail on
Member
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. Yea, thanks for adding the test. |
||||||||||||||
| .selectExpr("name").createOrReplaceTempView("contact_alias") | ||||||||||||||
|
|
||||||||||||||
| val query = sql("select name.first from contact_alias") | ||||||||||||||
| checkScan(query, "struct<name:struct<first:string,last:string>>") | ||||||||||||||
| checkAnswer(query, Row("Jane") :: Row("John") :: Row("Jim") :: Row("Janet") ::Nil) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| protected def testSchemaPruning(testName: String)(testThunk: => Unit): Unit = { | ||||||||||||||
| test(s"Spark vectorized reader - without partition data column - $testName") { | ||||||||||||||
| withSQLConf(vectorizedReaderEnabledKey -> "true") { | ||||||||||||||
|
|
||||||||||||||
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.
Nice catch, @viirya @frankyin-factual !