-
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
Changes from 3 commits
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 |
|---|---|---|
|
|
@@ -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 => | ||
| select(output.map(attr => | ||
| Column(attr)).updated(index, Column(output(index)).as(newName)) : _*) | ||
|
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. we can define a
Contributor
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. @cloud-fan Sure. Will do. |
||
| }.getOrElse { | ||
| this | ||
| } | ||
| } | ||
|
|
@@ -1255,9 +1240,9 @@ 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)) | ||
| val output = queryExecution.analyzed.output | ||
| val droppedAttrs = colNames.map(n => resolveToIndex(n)).flatten.map(output) | ||
| val remainingCols = output.filterNot(droppedAttrs.contains).map(Column(_)) | ||
|
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. An easier approach is to use the indexes:
Contributor
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. @cloud-fan Thanks.. Will do. |
||
| if (remainingCols.size == this.schema.size) { | ||
| this | ||
| } else { | ||
|
|
@@ -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: _*) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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.