When I run the below Scala code from the Spark 2.0 REPL (spark-shell), it runs as I intended it, splitting the string with a simple regular expression.
import org.apache.spark.sql.SparkSession
// Create session
val sparkSession = SparkSession.builder.master("local").getOrCreate()
// Use SparkSQL to split a string
val query = "SELECT split('What is this? A string I think', '\\\\?') AS result"
println("The query is: " + query)
val dataframe = sparkSession.sql(query)
// Show the result
dataframe.show(1, false)
giving the expected output
+---------------------------------+
|result |
+---------------------------------+
|[What is this, A string I think]|
+---------------------------------+
But I am confused about the need to escape the literal question mark with not a single, but double backslash (here represented as four backslashes since we must of course escape backslashes in Scala when not using triple-quoting).
I confirmed that some very similar code written by a colleague of mine for Spark 1.5 works just fine using a single (literal) backslash. But if I only use a single literal backslash in Spark 2.1, I get the error from the JVM's regex engine, "Dangling meta character '?' near index 0"
. I am aware this means the question mark was not escaped properly, but it smells like the backslash itself has to be escaped for first Scala and then SQL.
I'm guessing that this can be useful for inserting control characters (like newline) into the SQL query itself. I'm just confused if this has changed somewhere from Spark 1.5 to 2.1?
I have googled quite a bit for this, but didn't find anything. Either something has changed, or my colleague's code works in an unintended way.
I also tried this with Python/pyspark, and the same condition applies - double backslashes are needed in the SQL.
Can anyone explain this?
I'm running on a relatively simple setup on Windows, with Spark 2.1.0, JDK 1.8.0_111, and the Hadoop winutils.exe.
You can use the escape character with an interpreted character to make the compiler escape, or ignore, the interpreted meaning. In ANSI SQL, the backslash character (\) is the escape character. To search for data that begins with the string \abc , the WHERE clause must use an escape character as follows: ...
Use ` to escape special characters (e.g., ` ).
Use Two Single Quotes For Every One Quote To Display The simplest method to escape single quotes in SQL is to use two single quotes. For example, if you wanted to show the value O'Reilly, you would use two quotes in the middle instead of one.
A literal (also known as a constant) represents a fixed data value. Spark SQL supports the following literals: String Literal. Binary Literal.
May be because backslash is a special symbol, used to concatenate multi-line SQLs.
sql_1 = spark.sql("SELECT \
1 AS `col1`, '{0}' AS `col2`".format(var_1))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With