spark-test commented on code in PR #56558:
URL: https://github.com/apache/spark/pull/56558#discussion_r4520539987


##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/util/V2ExpressionSQLBuilder.java:
##########
@@ -90,9 +90,9 @@ protected String escapeSpecialCharsForLikePattern(String str) 
{
    * The default returns the input unchanged: a standard SQL string literal is 
taken verbatim (the
    * single-quote doubling is already applied when the literal is rendered), 
so the {@code \} that
    * {@link #escapeSpecialCharsForLikePattern} uses as the LIKE escape 
character reaches the LIKE
-   * engine intact. A dialect whose string-literal syntax gives {@code \} a 
special meaning (e.g.
-   * MySQL, which treats {@code \} as an escape character inside string 
literals) must override this
-   * to double the backslash, so the LIKE pattern survives string-literal 
parsing unchanged.
+   * engine intact. A dialect whose string-literal syntax gives {@code \} a 
special meaning must

Review Comment:
   Thanks, that reasoning holds. A builder outside Spark's jdbc package can't 
extend JDBCSQLBuilder, which is private[jdbc], so it builds on 
V2ExpressionSQLBuilder and still reaches both hooks through the base 
visitStartsWith/visitEndsWith/visitContains. Both javadocs are accurate for 
those builders, and the public docs don't need to describe the private JDBC 
builder, so I'm withdrawing this nit.
   
   <!-- SPARK_DEV_REVIEW_REPLY 
{"feedback_id":"inline:4480202248","thread_id":"inline:4480202248","verdict_sha256":"f6d364ae1e4607846c8ff532d88eb77b407ea13c1c1bdc8cd9a49c8490bd79ff"}
 -->



##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/MySQLDialect.scala:
##########
@@ -130,6 +124,15 @@ private case class MySQLDialect() extends JdbcDialect with 
SQLConfHelper with No
     }
   }
 
+  override def compileValue(value: Any): Any = value match {
+    // '\\' is one backslash in the default sql_mode but two under 
NO_BACKSLASH_ESCAPES,
+    // so LEFT('\\', 1) yields exactly one backslash in either mode.
+    case stringValue: String if stringValue.contains('\\') =>

Review Comment:
   **Nit (P3):** This makes pushed values safe, but `getTableCommentQuery` 
(line 281) and `indexExists` (line 328) still quote the comment and the index 
name with `escapeSql` alone. In the default `sql_mode`, MySQL still reads a 
backslash there as an escape, and a backslash before a quote ends the literal 
early. The description defers DDL and metadata SQL to a follow-up, but nothing 
at those two methods says so, so a reader of that code can't tell the fix is 
partial. Could you add a TODO at each that names backslashes in comments and 
index names, noting for the comment that `COMMENT` accepts only a quoted 
literal, so this `REPLACE` form can't be reused there?
   
   **Verification:**
   - **Inspection:** Check that the TODOs at the table-comment query and the 
index-existence query name backslashes in comments and index names as still 
read as escapes in the default sql_mode, say why the comment cannot reuse the 
pushed-value form, and that the change touches only comments.



##########
sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects.scala:
##########
@@ -429,9 +429,38 @@ abstract class JdbcDialect extends Serializable with 
Logging {
               "funcName" -> e.name()))
         }
         inputToSQL(e.children()(0))
+      case e: GeneralScalarExpression if isLikeStringPredicate(e.name()) =>
+        buildLikeStringPredicate(e).getOrElse(visitUnexpectedExpr(e))

Review Comment:
   **Nit (P3):** This declines non-literal patterns for Spark's JDBC dialects, 
but `V2ExpressionSQLBuilder`'s own 
`visitStartsWith`/`visitEndsWith`/`visitContains` still strip the first and 
last characters of whatever the pattern renders to. A builder that extends 
`V2ExpressionSQLBuilder` directly therefore still pushes `startsWith(email, 
email)` as `email LIKE 'mai%' ESCAPE '\'`. That includes non-JDBC DSv2 sources 
and a `JdbcDialect` that overrides `compileExpression` with its own builder, 
since `JDBCSQLBuilder` is `private[jdbc]`. The description defers this path, 
but the only comment in those methods is the "Remove quotes at the beginning 
and end" note, so a reader of the base class can't tell the stripping is a 
known gap. Could you add a TODO there that names non-literal patterns and the 
builders that still reach these methods?
   
   **Verification:**
   - **Inspection:** Check that a TODO at each of the three base LIKE-family 
visit methods names non-literal patterns as the deferred input and the builders 
that extend V2ExpressionSQLBuilder directly as the ones still affected, and 
that the change touches only comments.



##########
connector/docker-integration-tests/src/test/scala/org/apache/spark/sql/jdbc/MySQLIntegrationSuite.scala:
##########
@@ -364,6 +376,24 @@ class MySQLIntegrationSuite extends 
SharedJDBCIntegrationSuite {
       .load()
     checkAnswer(df, Row("brown     ", "fox"))
   }
+
+  test("SPARK-57500: push down string literals containing a backslash in any 
sql_mode") {
+    Seq("", "NO_BACKSLASH_ESCAPES").foreach { mode =>
+      val df = spark.read
+        .option("sessionInitStatement", s"SET SESSION sql_mode = '$mode'")
+        .jdbc(jdbcUrl, "backslash", new Properties)
+      checkAnswer(df.filter(col("s") === "C:\\temp"), Row("C:\\temp"))

Review Comment:
   **Nit (P3):** Every backslash-bearing value these tests use has exactly one 
backslash: `C:\temp`, `C:\` and `x\' OR 1=1) #` here, and `ab\`, `\ab`, `a\b` 
and `a'\b` in `JDBCSuite`. `compileValue` doubles every backslash and, under 
`NO_BACKSLASH_ESCAPES`, relies on `REPLACE` collapsing each doubled pair, so 
with one backslash per value, a regression that doubles only the first 
backslash would still pass every assertion. Reading the code, that regression 
would make a value whose second backslash precedes a quote end its literal 
early in the default `sql_mode`, which breaks the query or, for a crafted 
value, lets the rest of the value run as SQL again. A value with adjacent 
backslashes, such as the UNC path `\\server\share`, would also read back wrong.
   
   Could you add such values to the fixture and probes here, in both 
`sql_mode`s, and to the `JDBCSuite` SQL assertions?
   
   **Verification:**
   - **Regression:** Show that the generated MySQL SQL for a value with 
several, including adjacent, backslashes doubles each of them, so that a change 
that doubles only the first backslash fails the check.
   - **Behavior:** Show that, against a MySQL server in both the default 
sql_mode and NO_BACKSLASH_ESCAPES, equality and startsWith filters on 
multi-backslash values return exactly the expected rows, and that an 
injection-shaped value whose second backslash precedes a single quote returns 
no rows.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to