-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-50087] Robust handling of boolean expressions in CASE WHEN for MsSqlServer and future connectors #48621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
7d78be3
core
andrej-db 7839e1d
revert MsSqlServerDialect build method
andrej-db 0172c49
remove visitCaseWhen, more intuitive predicate wrapping
andrej-db 7ae7397
imports
andrej-db 49d742e
fix
andrej-db c2001d9
MsSqlServerDialect: comment
andrej-db 8b1b2da
JdbcDialects: move aux here
andrej-db 1f01b77
V2ExpressionBuilder: refactor
andrej-db 7e36029
nit
andrej-db ab79afe
nit
andrej-db de38a8c
Update JdbcDialects.scala
cloud-fan 56cea3c
Update MsSqlServerDialect.scala
cloud-fan 21ec622
Update sql/core/src/main/scala/org/apache/spark/sql/jdbc/JdbcDialects…
cloud-fan 468eb89
Update JdbcDialects.scala
cloud-fan 1bc39ee
Update sql/core/src/main/scala/org/apache/spark/sql/jdbc/MsSqlServerD…
cloud-fan 6a221c6
Update JdbcDialects.scala
cloud-fan ef1bcc8
Update MsSqlServerDialect.scala
cloud-fan 5d94fa1
Update JdbcDialects.scala
andrej-db f94f8e5
Update MsSqlServerDialect.scala
andrej-db 55084a3
Update MsSqlServerIntegrationSuite.scala
andrej-db 7fb6b29
Update MsSqlServerDialect.scala
andrej-db ca0545a
Update MsSqlServerIntegrationSuite.scala
andrej-db df2fe41
Apply suggestions from code review
cloud-fan 53a4220
Update connector/docker-integration-tests/src/test/scala/org/apache/s…
cloud-fan cbef9a5
Update MsSqlServerIntegrationSuite.scala
cloud-fan c990ec6
Update JdbcDialects.scala
cloud-fan ee4d4fb
Update sql/core/src/main/scala/org/apache/spark/sql/jdbc/MsSqlServerD…
cloud-fan 6cff9f5
Update MsSqlServerIntegrationSuite.scala
andrej-db bca7ce8
Merge branch 'apache:master' into SPARK-50087-CaseWhen
andrej-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,11 @@ package org.apache.spark.sql.jdbc.v2 | |
| import java.sql.Connection | ||
|
|
||
| import org.apache.spark.{SparkConf, SparkSQLFeatureNotSupportedException} | ||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.AnalysisException | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.execution.{RowDataSourceScanExec, SparkPlan} | ||
| import org.apache.spark.sql.execution.datasources.jdbc.JDBCRDD | ||
| import org.apache.spark.sql.execution.datasources.v2.jdbc.JDBCTableCatalog | ||
| import org.apache.spark.sql.jdbc.MsSQLServerDatabaseOnDocker | ||
| import org.apache.spark.sql.types._ | ||
|
|
@@ -37,6 +41,17 @@ import org.apache.spark.tags.DockerTest | |
| @DockerTest | ||
| class MsSqlServerIntegrationSuite extends DockerJDBCIntegrationV2Suite with V2JDBCTest { | ||
|
|
||
| def getExternalEngineQuery(executedPlan: SparkPlan): String = { | ||
| getExternalEngineRdd(executedPlan).asInstanceOf[JDBCRDD].getExternalEngineQuery | ||
| } | ||
|
|
||
| def getExternalEngineRdd(executedPlan: SparkPlan): RDD[InternalRow] = { | ||
| val queryNode = executedPlan.collect { case r: RowDataSourceScanExec => | ||
| r | ||
| }.head | ||
| queryNode.rdd | ||
| } | ||
|
|
||
| override def excluded: Seq[String] = Seq( | ||
| "simple scan with OFFSET", | ||
| "simple scan with LIMIT and OFFSET", | ||
|
|
@@ -146,4 +161,68 @@ class MsSqlServerIntegrationSuite extends DockerJDBCIntegrationV2Suite with V2JD | |
| |""".stripMargin) | ||
| assert(df.collect().length == 2) | ||
| } | ||
|
|
||
| test("SPARK-50087: SqlServer handle booleans in CASE WHEN test") { | ||
| val df = sql( | ||
| s"""|SELECT * FROM $catalogName.employee | ||
| |WHERE CASE WHEN name = 'Legolas' THEN name = 'Elf' ELSE NOT (name = 'Wizard') END | ||
| |""".stripMargin | ||
| ) | ||
|
|
||
| // scalastyle:off | ||
| assert(getExternalEngineQuery(df.queryExecution.executedPlan) == | ||
| """SELECT "dept","name","salary","bonus" FROM "employee" WHERE (CASE WHEN ("name" = 'Legolas') THEN IIF(("name" = 'Elf'), 1, 0) ELSE IIF(("name" <> 'Wizard'), 1, 0) END = 1) """ | ||
| ) | ||
| // scalastyle:on | ||
| df.collect() | ||
| } | ||
|
|
||
| test("SPARK-50087: SqlServer handle booleans in CASE WHEN with always true test") { | ||
| val df = sql( | ||
| s"""|SELECT * FROM $catalogName.employee | ||
| |WHERE CASE WHEN (name = 'Legolas') THEN (name = 'Elf') ELSE (1=1) END | ||
| |""".stripMargin | ||
| ) | ||
|
|
||
| // scalastyle:off | ||
| assert(getExternalEngineQuery(df.queryExecution.executedPlan) == | ||
| """SELECT "dept","name","salary","bonus" FROM "employee" WHERE (CASE WHEN ("name" = 'Legolas') THEN IIF(("name" = 'Elf'), 1, 0) ELSE 1 END = 1) """ | ||
| ) | ||
| // scalastyle:on | ||
| df.collect() | ||
| } | ||
|
|
||
| test("SPARK-50087: SqlServer handle booleans in nested CASE WHEN test") { | ||
| val df = sql( | ||
| s"""|SELECT * FROM $catalogName.employee | ||
| |WHERE CASE WHEN (name = 'Legolas') THEN | ||
| | CASE WHEN (name = 'Elf') THEN (name = 'Elrond') ELSE (name = 'Gandalf') END | ||
| | ELSE (name = 'Sauron') END | ||
| |""".stripMargin | ||
| ) | ||
|
|
||
| // scalastyle:off | ||
| assert(getExternalEngineQuery(df.queryExecution.executedPlan) == | ||
| """SELECT "dept","name","salary","bonus" FROM "employee" WHERE (CASE WHEN ("name" = 'Legolas') THEN IIF((CASE WHEN ("name" = 'Elf') THEN IIF(("name" = 'Elrond'), 1, 0) ELSE IIF(("name" = 'Gandalf'), 1, 0) END = 1), 1, 0) ELSE IIF(("name" = 'Sauron'), 1, 0) END = 1) """ | ||
| ) | ||
| // scalastyle:on | ||
| df.collect() | ||
| } | ||
|
|
||
| test("SPARK-50087: SqlServer handle non-booleans in nested CASE WHEN test") { | ||
| val df = sql( | ||
| s"""|SELECT * FROM $catalogName.employee | ||
| |WHERE CASE WHEN (name = 'Legolas') THEN | ||
| | CASE WHEN (name = 'Elf') THEN 'Elf' ELSE 'Wizard' END | ||
| | ELSE 'Sauron' END = name | ||
| |""".stripMargin | ||
| ) | ||
|
|
||
| // scalastyle:off | ||
| assert(getExternalEngineQuery(df.queryExecution.executedPlan) == | ||
| """SELECT "dept","name","salary","bonus" FROM "employee" WHERE ("name" IS NOT NULL) AND ((CASE WHEN "name" = 'Legolas' THEN CASE WHEN "name" = 'Elf' THEN 'Elf' ELSE 'Wizard' END ELSE 'Sauron' END) = "name") """ | ||
| ) | ||
| // scalastyle:on | ||
| df.collect() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. im not sure but do we have test case for when type is not boolean ? |
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we have some pushdown check ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add an assert with External query check