Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does SparkSQL require two literal escape backslashes in the SQL query?

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.

like image 284
Bjørn Nielsen Avatar asked Jan 20 '17 13:01

Bjørn Nielsen


People also ask

Why do we use escape characters in SQL?

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: ...

How do you escape special characters in Spark SQL?

Use ` to escape special characters (e.g., ` ).

How do I escape a single quote in Spark SQL?

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.

What is literals in Spark?

A literal (also known as a constant) represents a fixed data value. Spark SQL supports the following literals: String Literal. Binary Literal.


1 Answers

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))
like image 54
Shaman Avatar answered Sep 23 '22 16:09

Shaman