-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-12988][SQL] Can't drop columns that contain dots #10943
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -143,13 +143,24 @@ class DataFrame private[sql]( | |
| queryExecution.analyzed | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a column path i.e column name may contain "." or "`". . | ||
| */ | ||
| protected[sql] def resolve(colName: String): NamedExpression = { | ||
| queryExecution.analyzed.resolveQuoted(colName, sqlContext.analyzer.resolver).getOrElse { | ||
| throw new AnalysisException( | ||
| s"""Cannot resolve column name "$colName" among (${schema.fieldNames.mkString(", ")})""") | ||
| } | ||
| } | ||
|
|
||
| private[sql] def resolveToIndex(colName: String): Option[Int] = { | ||
| val resolver = sqlContext.analyzer.resolver | ||
| // First remove any user supplied quotes. | ||
| val unquotedColName = colName.stripPrefix("`").stripSuffix("`") | ||
| val index = queryExecution.analyzed.output.indexWhere(f => resolver(f.name, unquotedColName)) | ||
| if (index >= 0) Some(index) else None | ||
| } | ||
|
|
||
| protected[sql] def numericColumns: Seq[Expression] = { | ||
| schema.fields.filter(_.dataType.isInstanceOf[NumericType]).map { n => | ||
| queryExecution.analyzed.resolveQuoted(n.name, sqlContext.analyzer.resolver).get | ||
|
|
@@ -1175,19 +1186,10 @@ class DataFrame private[sql]( | |
| * @since 1.3.0 | ||
| */ | ||
| def withColumn(colName: String, col: Column): DataFrame = { | ||
| val resolver = sqlContext.analyzer.resolver | ||
| val output = queryExecution.analyzed.output | ||
| val shouldReplace = output.exists(f => resolver(f.name, colName)) | ||
| if (shouldReplace) { | ||
| val columns = output.map { field => | ||
| if (resolver(field.name, colName)) { | ||
| col.as(colName) | ||
| } else { | ||
| Column(field) | ||
| } | ||
| } | ||
| select(columns : _*) | ||
| } else { | ||
| resolveToIndex(colName).map { index => | ||
| select(output.map(attr => Column(attr)).updated(index, col.as(colName)) : _*) | ||
| }.getOrElse { | ||
| select(Column("*"), col.as(colName)) | ||
| } | ||
| } | ||
|
|
@@ -1196,19 +1198,10 @@ class DataFrame private[sql]( | |
| * Returns a new [[DataFrame]] by adding a column with metadata. | ||
| */ | ||
| private[spark] def withColumn(colName: String, col: Column, metadata: Metadata): DataFrame = { | ||
| val resolver = sqlContext.analyzer.resolver | ||
| val output = queryExecution.analyzed.output | ||
| val shouldReplace = output.exists(f => resolver(f.name, colName)) | ||
| if (shouldReplace) { | ||
| val columns = output.map { field => | ||
| if (resolver(field.name, colName)) { | ||
| col.as(colName, metadata) | ||
| } else { | ||
| Column(field) | ||
| } | ||
| } | ||
| select(columns : _*) | ||
| } else { | ||
| resolveToIndex(colName).map {index => | ||
| select(output.map(attr => Column(attr)).updated(index, col.as(colName, metadata)) : _*) | ||
| }.getOrElse { | ||
| select(Column("*"), col.as(colName, metadata)) | ||
| } | ||
| } | ||
|
|
@@ -1220,19 +1213,11 @@ class DataFrame private[sql]( | |
| * @since 1.3.0 | ||
| */ | ||
| def withColumnRenamed(existingName: String, newName: String): DataFrame = { | ||
| val resolver = sqlContext.analyzer.resolver | ||
| val output = queryExecution.analyzed.output | ||
| val shouldRename = output.exists(f => resolver(f.name, existingName)) | ||
| if (shouldRename) { | ||
| val columns = output.map { col => | ||
| if (resolver(col.name, existingName)) { | ||
| Column(col).as(newName) | ||
| } else { | ||
| Column(col) | ||
| } | ||
| } | ||
| select(columns : _*) | ||
| } else { | ||
| resolveToIndex(existingName).map {index => | ||
| val renamed = Column(output(index)).as(newName) | ||
| select(output.map(attr => Column(attr)).updated(index, renamed) : _*) | ||
| }.getOrElse { | ||
| this | ||
| } | ||
| } | ||
|
|
@@ -1255,13 +1240,13 @@ class DataFrame private[sql]( | |
| */ | ||
| @scala.annotation.varargs | ||
| def drop(colNames: String*): DataFrame = { | ||
| val resolver = sqlContext.analyzer.resolver | ||
| val remainingCols = | ||
| schema.filter(f => colNames.forall(n => !resolver(f.name, n))).map(f => Column(f.name)) | ||
| if (remainingCols.size == this.schema.size) { | ||
| val indexesToDrop = colNames.flatMap(resolveToIndex) | ||
| if (indexesToDrop.isEmpty) { | ||
| this | ||
| } else { | ||
| this.select(remainingCols: _*) | ||
| val output = queryExecution.analyzed.output | ||
| val remainingCols = output.indices.diff(indexesToDrop).map(index => Column(output(index))) | ||
| select(remainingCols: _*) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1274,16 +1259,20 @@ class DataFrame private[sql]( | |
| * @since 1.4.1 | ||
| */ | ||
| def drop(col: Column): DataFrame = { | ||
|
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. |
||
| val expression = col match { | ||
| val expression: Expression = col match { | ||
| case Column(u: UnresolvedAttribute) => | ||
| queryExecution.analyzed.resolveQuoted(u.name, sqlContext.analyzer.resolver).getOrElse(u) | ||
| resolveToIndex(u.name).map(this.logicalPlan.output).getOrElse(u) | ||
| case Column(expr: Expression) => expr | ||
| } | ||
| val attrs = this.logicalPlan.output | ||
| val colsAfterDrop = attrs.filter { attr => | ||
| attr != expression | ||
| }.map(attr => Column(attr)) | ||
| select(colsAfterDrop : _*) | ||
| if (colsAfterDrop.size == this.schema.size) { | ||
| this | ||
| } else { | ||
| select(colsAfterDrop: _*) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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.
do we need to do this? I think for these methods that require column name, user should just pass in an exact column name string, and we don't need to do any extra parsing here, i.e. no resolver, no strip for "`"
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.
for example, what if a column is named
a`a? User should be able to just pass ina`aand we shouldn't strip the "`"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.
@cloud-fan Hi Wenchen,
Can you please go through the following comment.
https://issues.apache.org/jira/browse/SPARK-12988?focusedCommentId=15118433&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-15118433
I was trying to address the 3rd bullet in the list. About your second question , per bullet one this should be disallowed ? Please let me know.