Gadam
Gadam

Reputation: 3024

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.

Upvotes: 1

Views: 669

Answers (1)

blackbishop
blackbishop

Reputation: 32710

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.

Upvotes: 4

Related Questions