Skip to content

Commit c1b9d83

Browse files
HyukjinKwondongjoon-hyun
authored andcommitted
[SPARK-10180][SQL] JDBC datasource are not processing EqualNullSafe filter
This PR is followed by #8391. Previous PR fixes JDBCRDD to support null-safe equality comparison for JDBC datasource. This PR fixes the problem that it can actually return null as a result of the comparison resulting error as using the value of that comparison. Author: hyukjinkwon <[email protected]> Author: HyukjinKwon <[email protected]> Closes #8743 from HyukjinKwon/SPARK-10180. (cherry picked from commit 94f7a12) Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent bb9aa2c commit c1b9d83

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/jdbc/JDBCRDD.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ private[sql] object JDBCRDD extends Logging {
190190
private def compileFilter(f: Filter): Option[String] = {
191191
Option(f match {
192192
case EqualTo(attr, value) => s"$attr = ${compileValue(value)}"
193+
case EqualNullSafe(attr, value) =>
194+
s"(NOT ($attr != ${compileValue(value)} OR $attr IS NULL OR " +
195+
s"${compileValue(value)} IS NULL) OR ($attr IS NULL AND ${compileValue(value)} IS NULL))"
193196
case LessThan(attr, value) => s"$attr < ${compileValue(value)}"
194197
case GreaterThan(attr, value) => s"$attr > ${compileValue(value)}"
195198
case LessThanOrEqual(attr, value) => s"$attr <= ${compileValue(value)}"
@@ -293,7 +296,6 @@ private[sql] class JDBCRDD(
293296
if (sb.length == 0) "1" else sb.substring(1)
294297
}
295298

296-
297299
/**
298300
* `filters`, but as a WHERE clause suitable for injection into a SQL query.
299301
*/

sql/core/src/test/scala/org/apache/spark/sql/jdbc/JDBCSuite.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class JDBCSuite extends SparkFunSuite
185185
assert(stripSparkFilter(sql("SELECT * FROM foobar WHERE THEID != 2")).collect().size == 2)
186186
assert(stripSparkFilter(sql("SELECT * FROM foobar WHERE THEID = 1")).collect().size == 1)
187187
assert(stripSparkFilter(sql("SELECT * FROM foobar WHERE NAME = 'fred'")).collect().size == 1)
188+
assert(stripSparkFilter(sql("SELECT * FROM foobar WHERE NAME <=> 'fred'")).collect().size == 1)
188189
assert(stripSparkFilter(sql("SELECT * FROM foobar WHERE NAME > 'fred'")).collect().size == 2)
189190
assert(stripSparkFilter(sql("SELECT * FROM foobar WHERE NAME != 'fred'")).collect().size == 2)
190191
assert(stripSparkFilter(sql("SELECT * FROM foobar WHERE NAME IN ('mary', 'fred')"))
@@ -473,7 +474,9 @@ class JDBCSuite extends SparkFunSuite
473474
=== "(NOT (col1 IN ('mno', 'pqr')))")
474475
assert(doCompileFilter(IsNull("col1")) === "col1 IS NULL")
475476
assert(doCompileFilter(IsNotNull("col1")) === "col1 IS NOT NULL")
476-
assert(doCompileFilter(And(EqualNullSafe("col0", "abc"), EqualTo("col1", "def"))) === "")
477+
assert(doCompileFilter(And(EqualNullSafe("col0", "abc"), EqualTo("col1", "def")))
478+
=== "((NOT (col0 != 'abc' OR col0 IS NULL OR 'abc' IS NULL) "
479+
+ "OR (col0 IS NULL AND 'abc' IS NULL))) AND (col1 = 'def')")
477480
}
478481

479482
test("Dialect unregister") {

0 commit comments

Comments
 (0)