0

So here is my code block

val cols = df.columns
val w = cols(0)
val query1  = s"select $cols(0), square(age) as age, age as age2, first_name, last_name from test"
val query2  = s"select $w, square(age) as age, age as age2, first_name, last_name from test"

Query 2 works just fine, query 1 throws the following error

no viable alternative at input 'select ['(line 1, pos 7)
== SQL ==
select [Ljava.lang.String;@7988d54(0), square(age) as age, age as age2, first_name, last_name from test

Is there anyway to accomplish query1 without creating a temp variable? I want to acces the indexed cols datatype directly.

1
  • I addressed your question directly but I just wanted to follow up after reading this again to tell you that if you're doing this as part of a SQL query, you're bypassing the safety of the JDBC driver. You're inviting SQL injection attacks. Commented Aug 23, 2018 at 18:49

1 Answer 1

3

You can do it by nesting an expression within the string you're interpolating:

s"select ${col(0)} and then..."
s"select ${df.columns(0)} and then..."

You use the ${expr} to hold some expr which is valid code. The single variable version of the interpolation phrase $foo is actually short hand for ${foo}.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you have any recommendations on the proper way to do this without being vulnerable to injection? I am using this query with spark, so df = sqlContext.sql(query)
Unfortunately, I can't give you a good recommendation as I have never used the SQL dataframe version. The JDBC drivers use prepared statements but I doubt there is the equivalent in Spark (I could be wrong.) There's probably a question on SO with the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.