Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-20912

map function with columns as strings

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Resolved
    • Trivial
    • Resolution: Won't Fix
    • 2.3.0
    • None
    • SQL
    • None

    Description

      There's only map function that accepts Column values only. It'd be very helpful to have a variant that accepted String for columns like array or struct.

      scala> val kvs = Seq(("key", "value")).toDF("k", "v")
      kvs: org.apache.spark.sql.DataFrame = [k: string, v: string]
      
      scala> kvs.printSchema
      root
       |-- k: string (nullable = true)
       |-- v: string (nullable = true)
      
      
      scala> kvs.withColumn("map", map("k", "v")).show
      <console>:26: error: type mismatch;
       found   : String("k")
       required: org.apache.spark.sql.Column
             kvs.withColumn("map", map("k", "v")).show
                                       ^
      <console>:26: error: type mismatch;
       found   : String("v")
       required: org.apache.spark.sql.Column
             kvs.withColumn("map", map("k", "v")).show
                                            ^
      
      // note $ to create Columns per string
      // not very dev-friendly
      scala> kvs.withColumn("map", map($"k", $"v")).show
      +---+-----+-----------------+
      |  k|    v|              map|
      +---+-----+-----------------+
      |key|value|Map(key -> value)|
      +---+-----+-----------------+
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            jlaskowski Jacek Laskowski
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: