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.
CodePudding user response:
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.