0

I am running into a scenario where I need to convert spark expression to sql expression, and later need to parse sql expression back to spark expression. In most of the cases it work fine, but in some cases it throws error.

For example following works fine in spark

val sqlContext = spark.sqlContext
import sqlContext.implicits._
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types._

val df = Seq("Britain", "Germany", "USA", "Russia", "Japan").toDF("Country")

val japan = 'Country === "Japan"
df.filter(japan).show 
val sqlExp = japan.expr.sql
println(sqlExp) // output: (`Country` = 'Japan')
df.filter(expr(sqlExp)).show

But when I try same with following expression it fails:

val expression = 'Country.contains("S")
println(expression.expr.sql)
df.filter(expression).show
val parsedExpression = expr(expression.expr.sql) //output: contains(`Country`, 'S')
df.filter(parsedExpression).show

It seems like it works with only standard sql syntax. When I use expr("country LIKE '%S%'") it is able to parse.

Is there a way to parse back such an sql expression (that is generated by spark) to spark expression?

1
  • what is the error message you get? also, i see no such function contains in Spark-SQL documentation Commented Dec 21, 2018 at 15:49

1 Answer 1

1

The Expression.sql method:

  • Is not a part of officially public API (as stated many times by the developers code in o.a.s.sql.catalyst should be considered "weakly" private).
  • Is not explictly intended to generate valid SQL string and can be even an arbitrary string./

    In fact contains(Country, 'S') is valid in neither sql (or spark-sql) nor expr.

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

Comments

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.