This is an automated email from the ASF dual-hosted git repository.

yao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new beda1a4615c7 [SPARK-47440][SQL][FOLLOWUP] Reenable predicate pushdown 
for syntax with boolean comparison in MsSqlServer
beda1a4615c7 is described below

commit beda1a4615c7f33110e360c150cb78832b0fe420
Author: Kent Yao <y...@apache.org>
AuthorDate: Fri Apr 26 23:51:58 2024 +0800

    [SPARK-47440][SQL][FOLLOWUP] Reenable predicate pushdown for syntax with 
boolean comparison in MsSqlServer
    
    ### What changes were proposed in this pull request?
    
    In https://github.com/apache/spark/pull/45564, predicate pushdown with 
boolean comparison syntax in MsSqlServer is disabled as MsSqlServer does not 
support such a feature.
    
    In this PR, we reenable this feature by converting the boolean comparison 
to an equivalent 1&0 comparison.
    
    ### Why are the changes needed?
    
    Avoid performance regressions
    
    ### Does this PR introduce _any_ user-facing change?
    
    no
    
    ### How was this patch tested?
    
    existing test
    
    ```
    [info] MsSqlServerIntegrationSuite:
    [info] - SPARK-47440: SQLServer does not support boolean expression in 
binary comparison (2 seconds, 206 milliseconds)
    ```
    
    ### Was this patch authored or co-authored using generative AI tooling?
    
    no
    
    Closes #46236 from yaooqinn/SPARK-47440.
    
    Authored-by: Kent Yao <y...@apache.org>
    Signed-off-by: Kent Yao <y...@apache.org>
---
 .../apache/spark/sql/connector/util/V2ExpressionSQLBuilder.java  | 2 +-
 .../scala/org/apache/spark/sql/jdbc/MsSqlServerDialect.scala     | 9 ++++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git 
a/sql/catalyst/src/main/java/org/apache/spark/sql/connector/util/V2ExpressionSQLBuilder.java
 
b/sql/catalyst/src/main/java/org/apache/spark/sql/connector/util/V2ExpressionSQLBuilder.java
index fd1b8f5dd1ee..61d68d4a3e88 100644
--- 
a/sql/catalyst/src/main/java/org/apache/spark/sql/connector/util/V2ExpressionSQLBuilder.java
+++ 
b/sql/catalyst/src/main/java/org/apache/spark/sql/connector/util/V2ExpressionSQLBuilder.java
@@ -212,7 +212,7 @@ public class V2ExpressionSQLBuilder {
     return l + " LIKE '%" + escapeSpecialCharsForLikePattern(value) + "%' 
ESCAPE '\\'";
   }
 
-  private String inputToSQL(Expression input) {
+  protected String inputToSQL(Expression input) {
     if (input.children().length > 1) {
       return "(" + build(input) + ")";
     } else {
diff --git 
a/sql/core/src/main/scala/org/apache/spark/sql/jdbc/MsSqlServerDialect.scala 
b/sql/core/src/main/scala/org/apache/spark/sql/jdbc/MsSqlServerDialect.scala
index a1492d81bf53..5535545efba8 100644
--- a/sql/core/src/main/scala/org/apache/spark/sql/jdbc/MsSqlServerDialect.scala
+++ b/sql/core/src/main/scala/org/apache/spark/sql/jdbc/MsSqlServerDialect.scala
@@ -86,9 +86,12 @@ private case class MsSqlServerDialect() extends JdbcDialect {
       // We shouldn't propagate these queries to MsSqlServer
       expr match {
         case e: Predicate => e.name() match {
-          case "=" | "<>" | "<=>" | "<" | "<=" | ">" | ">="
-              if e.children().exists(_.isInstanceOf[Predicate]) =>
-            super.visitUnexpectedExpr(expr)
+          case "=" | "<>" | "<=>" | "<" | "<=" | ">" | ">=" =>
+            val Array(l, r) = e.children().map {
+              case p: Predicate => s"CASE WHEN ${inputToSQL(p)} THEN 1 ELSE 0 
END"
+              case o => inputToSQL(o)
+            }
+            visitBinaryComparison(e.name(), l, r)
           case _ => super.build(expr)
         }
         case _ => super.build(expr)


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org
For additional commands, e-mail: commits-h...@spark.apache.org

Reply via email to