Github user marmbrus commented on a diff in the pull request:

    https://github.com/apache/spark/pull/9862#discussion_r46219744
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/DataFrame.scala ---
    @@ -1241,17 +1241,32 @@ class DataFrame private[sql](
        * @since 1.4.0
        */
       def drop(colName: String): DataFrame = {
    +    drop(Seq(colName) : _*)
    +  }
    +
    +  /**
    +   * Returns a new [[DataFrame]] with columns dropped.
    +   * This is a no-op if schema doesn't contain column name(s).
    +   * @group dfops
    +   * @since 1.6.0
    +   */
    +  @scala.annotation.varargs
    +  def drop(colNames: String*): DataFrame = {
         val resolver = sqlContext.analyzer.resolver
    -    val shouldDrop = schema.exists(f => resolver(f.name, colName))
    -    if (shouldDrop) {
    -      val colsAfterDrop = schema.filter { field =>
    -        val name = field.name
    -        !resolver(name, colName)
    -      }.map(f => Column(f.name))
    -      select(colsAfterDrop : _*)
    -    } else {
    -      this
    +    val iter = colNames.iterator
    +    var df = this
    +    while (iter.hasNext) {
    +      val colName = iter.next()
    +      val shouldDrop = df.schema.exists(f => resolver(f.name, colName))
    +      if (shouldDrop) {
    +        val colsAfterDrop = df.schema.filter { field =>
    +          val name = field.name
    +          !resolver(name, colName)
    +        }.map(f => Column(f.name))
    +        df = df.select(colsAfterDrop : _*)
    +      }
         }
    +    df
    --- End diff --
    
    Two comments on the implementation here:
     - you are creating a new select for each column you are dropping, which is 
fixed by the optimizer but kind of wasteful.
    
    ```scala
    scala> val df = Seq((1,2,3,4,5)).toDF("a", "b", "c", "d", "e")
    df: org.apache.spark.sql.DataFrame = [a: int, b: int, c: int, d: int, e: 
int]
    
    scala> df.drop("a", "b", "c", "d").explain(true)
    ...
    == Analyzed Logical Plan ==
    e: int
    Project [e#9]
     Project [d#8,e#9]
      Project [c#7,d#8,e#9]
       Project [b#6,c#7,d#8,e#9]
        Project [_1#0 AS a#5,_2#1 AS b#6,_3#2 AS c#7,_4#3 AS d#8,_5#4 AS e#9]
         LocalRelation [_1#0,_2#1,_3#2,_4#3,_5#4], [[1,2,3,4,5]]
    ...
    ```
    
     - Stylistically its very imperative, where as the rest of dataframes is 
pretty functional which I find much easier to reason about.
    
    As a rough (uncompiled) sketch, I'd think about solving this as follows:
    
    ```scala
    def drop(colNames: String*): DataFrame = {
      def shouldDrop(name: String) = ...
      val remainingColumns = df.schema.filter(f => shouldDrop(f.name)).map(f => 
Column(f.name))
      if (remainingColumns.size == df.schema.size) {
        this
      } else {
        df.select(remainingColumns: _*)
      }
    }
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to