rednaxelafx opened a new pull request #27544: [SPARK-30795][SQL] Spark SQL 
codegen's code() interpolator should treat escapes like Scala's 
StringContext.s()
URL: https://github.com/apache/spark/pull/27544
 
 
   ### What changes were proposed in this pull request?
   
   This PR proposes to make the `code` string interpolator treat escapes the 
same way as Scala's builtin `StringContext.s()` string interpolator. This will 
remove the need for an ugly workaround in `Like` expression's codegen.
   
   ### Why are the changes needed?
   
   The `code()` string interpolator in Spark SQL's code generator should treat 
escapes like Scala's builtin `StringContext.s()` interpolator, i.e. it should 
treat escapes in the code parts, and should not treat escapes in the input 
arguments.
   
   For example,
   ```scala
   val arg = "This is an argument."
   val str = s"This is string part 1. $arg This is string part 2."
   val code = code"This is string part 1. $arg This is string part 2."
   assert(code.toString == str)
   ```
   We should expect the `code()` interpolator produce the same thing as the 
`StringContext.s()` interpolator, where only escapes in the string parts should 
be treated, while the args should be kept verbatim.
   
   But in the current implementation, due to the eager folding of code parts 
and literal input args, the escape treatment is incorrectly done on both code 
parts and literal args.
   That causes a problem when an arg contains escape sequences and wants to 
preserve that in the final produced code string. For example, in `Like` 
expression's codegen, there's an ugly workaround for this bug:
   ```scala
         // We need double escape to avoid 
org.codehaus.commons.compiler.CompileException.
         // '\\' will cause exception 'Single quote must be backslash-escaped 
in character literal'.
         // '\"' will cause exception 'Line break in literal not allowed'.
         val newEscapeChar = if (escapeChar == '\"' || escapeChar == '\\') {
           s"""\\\\\\$escapeChar"""
         } else {
           escapeChar
         }
   ```
   
   ### Does this PR introduce any user-facing change?
   
   No.
   
   ### How was this patch tested?
   
   Added a new unit test case in `CodeBlockSuite`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to