Search code examples
scalaapache-sparkapache-spark-sqlstring-formatting

Spark Scala - how to use $"col" and S string interpolation together?


I want to achieve this, with a $ instead of col()

val x = "id"
df.select(col(s"$x"))

This gives me an error

df.select($s"$x") 

But this works -- How is it working without the s prefix? And is this the correct way?

df.select($"$x") 

Thanks.


Solution

  • Yes this is correct $"$x" and will return column col("id"). It's working because the method $ is defined like this in SQLImplicits:

    implicit class StringToColumn(val sc: StringContext) {
      def $(args: Any*): ColumnName = {
        new ColumnName(sc.s(args: _*))
      }
    }
    

    As you can see, we call StringContext.sc method which does the string interpolation.