-
Notifications
You must be signed in to change notification settings - Fork 314
feat: support TimestampType join keys in SortMergeJoin #3986
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
Merged
+181
−11
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a7bc95
feat: support TimestampType join keys in SortMergeJoin
andygrove 399116e
test: cover TimestampType SortMergeJoin outer joins
andygrove 5ae871d
test: cover TimestampType SortMergeJoin with composite keys
andygrove b5f5643
test: cover TimestampType SortMergeJoin with nullable keys
andygrove 9624aef
refactor: drop redundant comment and turbofish in SMJ join-key rewrite
andygrove bf11369
test: cover SortMergeJoin TimestampType keys across mixed write-time …
andygrove d636926
Merge branch 'main' into feat/smj-timestamp-keys
andygrove 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ import org.scalatest.Tag | |
| import org.apache.spark.sql.CometTestBase | ||
| import org.apache.spark.sql.catalyst.TableIdentifier | ||
| import org.apache.spark.sql.catalyst.analysis.UnresolvedRelation | ||
| import org.apache.spark.sql.comet.{CometBroadcastExchangeExec, CometBroadcastHashJoinExec} | ||
| import org.apache.spark.sql.comet.{CometBroadcastExchangeExec, CometBroadcastHashJoinExec, CometSortMergeJoinExec} | ||
| import org.apache.spark.sql.execution.adaptive.AQEShuffleReadExec | ||
| import org.apache.spark.sql.internal.SQLConf | ||
|
|
||
|
|
@@ -55,21 +55,159 @@ class CometJoinSuite extends CometTestBase { | |
| .toSeq) | ||
| } | ||
|
|
||
| test("SortMergeJoin with unsupported key type should fall back to Spark") { | ||
| test("SortMergeJoin with TimestampType key runs natively") { | ||
| withSQLConf( | ||
| SQLConf.SESSION_LOCAL_TIMEZONE.key -> "Asia/Kathmandu", | ||
| SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1") { | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.PREFER_SORTMERGEJOIN.key -> "true") { | ||
| withTable("t1", "t2") { | ||
| sql("CREATE TABLE t1(name STRING, time TIMESTAMP) USING PARQUET") | ||
| sql("INSERT OVERWRITE t1 VALUES('a', timestamp'2019-01-01 11:11:11')") | ||
| sql( | ||
| "INSERT OVERWRITE t1 VALUES " + | ||
| "('a', timestamp'2019-01-01 11:11:11'), " + | ||
| "('b', timestamp'2020-05-05 05:05:05')") | ||
|
|
||
| sql("CREATE TABLE t2(name STRING, time TIMESTAMP) USING PARQUET") | ||
| sql("INSERT OVERWRITE t2 VALUES('a', timestamp'2019-01-01 11:11:11')") | ||
| sql( | ||
| "INSERT OVERWRITE t2 VALUES " + | ||
| "('a', timestamp'2019-01-01 11:11:11'), " + | ||
| "('c', timestamp'2021-07-07 07:07:07')") | ||
|
|
||
| checkSparkAnswerAndOperator( | ||
| sql("SELECT * FROM t1 JOIN t2 ON t1.time = t2.time"), | ||
| Seq(classOf[CometSortMergeJoinExec])) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val df = sql("SELECT * FROM t1 JOIN t2 ON t1.time = t2.time") | ||
| val (sparkPlan, cometPlan) = checkSparkAnswer(df) | ||
| assert(sparkPlan.canonicalized === cometPlan.canonicalized) | ||
| test("SortMergeJoin with TimestampType key supports outer joins") { | ||
| withSQLConf( | ||
| SQLConf.SESSION_LOCAL_TIMEZONE.key -> "Asia/Kathmandu", | ||
| SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.PREFER_SORTMERGEJOIN.key -> "true") { | ||
| withTable("t1", "t2") { | ||
| sql("CREATE TABLE t1(id INT, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t1 VALUES " + | ||
| "(1, timestamp'2019-01-01 11:11:11'), " + | ||
| "(2, timestamp'2020-05-05 05:05:05'), " + | ||
| "(3, timestamp'2021-07-07 07:07:07')") | ||
|
|
||
| sql("CREATE TABLE t2(id INT, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t2 VALUES " + | ||
| "(10, timestamp'2019-01-01 11:11:11'), " + | ||
| "(20, timestamp'2022-02-02 02:02:02')") | ||
|
|
||
| for (joinType <- Seq("LEFT OUTER", "RIGHT OUTER", "FULL OUTER")) { | ||
|
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. 👍 |
||
| checkSparkAnswerAndOperator( | ||
| sql(s"SELECT * FROM t1 $joinType JOIN t2 ON t1.time = t2.time"), | ||
| Seq(classOf[CometSortMergeJoinExec])) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("SortMergeJoin with composite (string, timestamp) key runs natively") { | ||
| withSQLConf( | ||
| SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.PREFER_SORTMERGEJOIN.key -> "true") { | ||
| withTable("t1", "t2") { | ||
| sql("CREATE TABLE t1(name STRING, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t1 VALUES " + | ||
| "('a', timestamp'2019-01-01 11:11:11'), " + | ||
| "('b', timestamp'2019-01-01 11:11:11'), " + | ||
| "('a', timestamp'2020-05-05 05:05:05')") | ||
|
|
||
| sql("CREATE TABLE t2(name STRING, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t2 VALUES " + | ||
| "('a', timestamp'2019-01-01 11:11:11'), " + | ||
| "('b', timestamp'2020-05-05 05:05:05'), " + | ||
| "('a', timestamp'2020-05-05 05:05:05')") | ||
|
|
||
| checkSparkAnswerAndOperator( | ||
| sql( | ||
| "SELECT * FROM t1 JOIN t2 " + | ||
| "ON t1.name = t2.name AND t1.time = t2.time"), | ||
| Seq(classOf[CometSortMergeJoinExec])) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("SortMergeJoin with nullable TimestampType key runs natively") { | ||
| withSQLConf( | ||
| SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.PREFER_SORTMERGEJOIN.key -> "true") { | ||
| withTable("t1", "t2") { | ||
| sql("CREATE TABLE t1(id INT, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t1 VALUES " + | ||
| "(1, timestamp'2019-01-01 11:11:11'), " + | ||
| "(2, CAST(NULL AS TIMESTAMP)), " + | ||
| "(3, timestamp'2020-05-05 05:05:05')") | ||
|
|
||
| sql("CREATE TABLE t2(id INT, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t2 VALUES " + | ||
| "(10, timestamp'2019-01-01 11:11:11'), " + | ||
| "(20, CAST(NULL AS TIMESTAMP)), " + | ||
| "(30, timestamp'2022-02-02 02:02:02')") | ||
|
|
||
| // Inner join: NULL = NULL must not match in Spark semantics. | ||
| checkSparkAnswerAndOperator( | ||
| sql("SELECT * FROM t1 JOIN t2 ON t1.time = t2.time"), | ||
| Seq(classOf[CometSortMergeJoinExec])) | ||
|
|
||
| // Full outer join: NULL-keyed rows from both sides surface as unmatched. | ||
| checkSparkAnswerAndOperator( | ||
| sql("SELECT * FROM t1 FULL OUTER JOIN t2 ON t1.time = t2.time"), | ||
| Seq(classOf[CometSortMergeJoinExec])) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| test("SortMergeJoin with TimestampType key across mixed write-time session timezones") { | ||
| // TimestampType is an instant (UTC microseconds); only the parsing of literal | ||
| // strings depends on the session timezone. Writing each side under a different | ||
| // session zone with wall-clock literals that resolve to the same UTC instant | ||
| // must still produce a join match. | ||
| withSQLConf( | ||
| SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", | ||
| SQLConf.PREFER_SORTMERGEJOIN.key -> "true") { | ||
| withTable("t1", "t2") { | ||
| // t1 written in America/Los_Angeles. 03:11:11 -0800 == 11:11:11 UTC. | ||
| withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "America/Los_Angeles") { | ||
| sql("CREATE TABLE t1(name STRING, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t1 VALUES " + | ||
| "('a', timestamp'2019-01-01 03:11:11'), " + | ||
| "('b', timestamp'2020-05-04 22:05:05')") | ||
| } | ||
|
|
||
| // t2 written in Asia/Tokyo. 20:11:11 +0900 == 11:11:11 UTC, so the 'a' and | ||
| // 'a2' rows share a UTC instant with t1's 'a' row. | ||
| withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "Asia/Tokyo") { | ||
| sql("CREATE TABLE t2(name STRING, time TIMESTAMP) USING PARQUET") | ||
| sql( | ||
| "INSERT OVERWRITE t2 VALUES " + | ||
| "('a', timestamp'2019-01-01 20:11:11'), " + | ||
| "('c', timestamp'2021-07-07 16:07:07')") | ||
| } | ||
|
|
||
| // Read at a third session timezone to confirm the equality is on the | ||
| // stored UTC instant rather than the displayed wall-clock value. | ||
| withSQLConf(SQLConf.SESSION_LOCAL_TIMEZONE.key -> "UTC") { | ||
| checkSparkAnswerAndOperator( | ||
| sql("SELECT * FROM t1 JOIN t2 ON t1.time = t2.time"), | ||
| Seq(classOf[CometSortMergeJoinExec])) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
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.
What happens if table t1 is written with one session timezone and table t2 is written with another session timezone.
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 added a test to cover this case