-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[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 2 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,27 @@ 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(", ")})""") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a column name. This is called when it is required to resolve a column by its | ||
| * name only and not as a column path.. | ||
| */ | ||
| private[sql] def resolveColName(colName: String, userSuppliedName: String): Boolean = { | ||
| // First remove any user supplied quotes. | ||
| val unquotedColName = userSuppliedName.stripPrefix("`").stripSuffix("`") | ||
| sqlContext.analyzer.resolver(colName, unquotedColName) | ||
|
|
||
| } | ||
|
|
||
| protected[sql] def numericColumns: Seq[Expression] = { | ||
| schema.fields.filter(_.dataType.isInstanceOf[NumericType]).map { n => | ||
| queryExecution.analyzed.resolveQuoted(n.name, sqlContext.analyzer.resolver).get | ||
|
|
@@ -1175,12 +1189,11 @@ 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)) | ||
| val shouldReplace = output.exists(f => resolveColName(f.name, colName)) | ||
| if (shouldReplace) { | ||
| val columns = output.map { field => | ||
| if (resolver(field.name, colName)) { | ||
| if (resolveColName(field.name, colName)) { | ||
| col.as(colName) | ||
| } else { | ||
| Column(field) | ||
|
|
@@ -1196,12 +1209,11 @@ 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)) | ||
| val shouldReplace = output.exists(f => resolveColName(f.name, colName)) | ||
| if (shouldReplace) { | ||
| val columns = output.map { field => | ||
| if (resolver(field.name, colName)) { | ||
| if (resolveColName(field.name, colName)) { | ||
| col.as(colName, metadata) | ||
| } else { | ||
| Column(field) | ||
|
|
@@ -1220,12 +1232,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)) | ||
| val shouldRename = output.exists(f => resolveColName(f.name, existingName)) | ||
| if (shouldRename) { | ||
| val columns = output.map { col => | ||
| if (resolver(col.name, existingName)) { | ||
| if (resolveColName(col.name, existingName)) { | ||
| Column(col).as(newName) | ||
| } else { | ||
| Column(col) | ||
|
|
@@ -1255,9 +1266,9 @@ class DataFrame private[sql]( | |
| */ | ||
| @scala.annotation.varargs | ||
| def drop(colNames: String*): DataFrame = { | ||
| val resolver = sqlContext.analyzer.resolver | ||
| val output = queryExecution.analyzed.output | ||
| val remainingCols = | ||
| schema.filter(f => colNames.forall(n => !resolver(f.name, n))).map(f => Column(f.name)) | ||
| output.filter(f => colNames.forall(n => !resolveColName(f.name, n))).map(f => Column(f)) | ||
| if (remainingCols.size == this.schema.size) { | ||
| this | ||
| } else { | ||
|
|
@@ -1274,16 +1285,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 { | ||
| def expression(attr: Attribute): Expression = col match { | ||
| case Column(u: UnresolvedAttribute) => | ||
| queryExecution.analyzed.resolveQuoted(u.name, sqlContext.analyzer.resolver).getOrElse(u) | ||
| if (resolveColName(attr.name, u.name)) attr else u | ||
| case Column(expr: Expression) => expr | ||
| } | ||
| val attrs = this.logicalPlan.output | ||
| val colsAfterDrop = attrs.filter { attr => | ||
| attr != expression | ||
| attr != expression(attr) | ||
| }.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.
how about
then we can rewrite
withColumnto:There may be better name for this, like
resolveToIndex