-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16406][SQL] Improve performance of LogicalPlan.resolve #14083
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
Closed
Closed
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b4c8cdb
Add AttributeResolver
hvanhovell a8a6a83
Merge remote-tracking branch 'apache-github/master' into SPARK-16406
hvanhovell 3e39720
move refresh back
hvanhovell c75ae8d
Use Seq-based instead of List-based pattern matching
hvanhovell a5d1a4a
Rename attribute after resolution.
hvanhovell d64346b
Move resolution into AttribueSeq
hvanhovell a1e5312
Merge remote-tracking branch 'apache-github/master' into SPARK-16406
hvanhovell 85e0744
Merge remote-tracking branch 'apache/master' into SPARK-16406
hvanhovell eb6dd80
Fix compilation
hvanhovell ac4abcd
Fix UT
hvanhovell cbc164d
Appease scalastyle
hvanhovell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ package org.apache.spark.sql.catalyst | |
|
|
||
| import com.google.common.collect.Maps | ||
|
|
||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.analysis.{Resolver, UnresolvedAttribute} | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.types.{StructField, StructType} | ||
|
|
||
|
|
@@ -138,6 +140,88 @@ package object expressions { | |
| def indexOf(exprId: ExprId): Int = { | ||
| Option(exprIdToOrdinal.get(exprId)).getOrElse(-1) | ||
| } | ||
|
|
||
| private def unique[T](m: Map[T, Seq[Attribute]]): Map[T, Seq[Attribute]] = { | ||
| m.mapValues(_.distinct).map(identity) | ||
| } | ||
|
|
||
| /** Map to use for direct case insensitive attribute lookups. */ | ||
| @transient private lazy val direct: Map[String, Seq[Attribute]] = { | ||
| unique(attrs.groupBy(_.name.toLowerCase)) | ||
| } | ||
|
|
||
| /** Map to use for qualified case insensitive attribute lookups. */ | ||
| @transient private val qualified: Map[(String, String), Seq[Attribute]] = { | ||
| val grouped = attrs.filter(_.qualifier.isDefined).groupBy { a => | ||
| (a.qualifier.get.toLowerCase, a.name.toLowerCase) | ||
| } | ||
| unique(grouped) | ||
| } | ||
|
|
||
| /** Perform attribute resolution given a name and a resolver. */ | ||
| def resolve(nameParts: Seq[String], resolver: Resolver): Option[NamedExpression] = { | ||
| // Collect matching attributes given a name and a lookup. | ||
| def collectMatches(name: String, candidates: Option[Seq[Attribute]]): Seq[Attribute] = { | ||
| candidates.toSeq.flatMap(_.collect { | ||
| case a if resolver(a.name, name) => a.withName(name) | ||
| }) | ||
| } | ||
|
|
||
| // Find matches for the given name assuming that the 1st part is a qualifier (i.e. table name, | ||
| // alias, or subquery alias) and the 2nd part is the actual name. This returns a tuple of | ||
| // matched attributes and a list of parts that are to be resolved. | ||
| // | ||
| // For example, consider an example where "a" is the table name, "b" is the column name, | ||
| // and "c" is the struct field name, i.e. "a.b.c". In this case, Attribute will be "a.b", | ||
| // and the second element will be List("c"). | ||
| val matches = nameParts match { | ||
| case qualifier +: name +: nestedFields => | ||
| val key = (qualifier.toLowerCase, name.toLowerCase) | ||
| val attributes = collectMatches(name, qualified.get(key)).filter { a => | ||
| resolver(qualifier, a.qualifier.get) | ||
| } | ||
| (attributes, nestedFields) | ||
| case all => | ||
| (Nil, all) | ||
| } | ||
|
|
||
| // If none of attributes match `table.column` pattern, we try to resolve it as a column. | ||
| val (candidates, nestedFields) = matches match { | ||
| case (Seq(), _) => | ||
| val name = nameParts.head | ||
| val attributes = collectMatches(name, direct.get(name.toLowerCase)) | ||
| (attributes, nameParts.tail) | ||
| case _ => matches | ||
| } | ||
|
|
||
| def name = UnresolvedAttribute(nameParts).name | ||
| candidates match { | ||
| case Seq(a) if nestedFields.nonEmpty => | ||
| // One match, but we also need to extract the requested nested field. | ||
| // The foldLeft adds ExtractValues for every remaining parts of the identifier, | ||
| // and aliased it with the last part of the name. | ||
| // For example, consider "a.b.c", where "a" is resolved to an existing attribute. | ||
| // Then this will add ExtractValue("c", ExtractValue("b", a)), and alias the final | ||
| // expression as "c". | ||
| val fieldExprs = nestedFields.foldLeft(a: Expression) { (e, name) => | ||
| ExtractValue(e, Literal(name), resolver) | ||
| } | ||
| Some(Alias(fieldExprs, nestedFields.last)()) | ||
|
|
||
| case Seq(a) => | ||
| // One match, no nested fields, use it. | ||
| Some(a) | ||
|
|
||
| case Seq() => | ||
| // No matches. | ||
| None | ||
|
|
||
| case ambiguousReferences => | ||
| // More than one match. | ||
| val referenceNames = ambiguousReferences.mkString(", ") | ||
|
||
| throw new AnalysisException(s"Reference '$name' is ambiguous, could be: $referenceNames.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ExtractValuehas the same perf problem, but this can be fixed in a follow upThere 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.
Is there an issue for the follow up?
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.
@heuermh I have not filed the issue for this. Do you want to work on this?