{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/34891100","id":34891100,"diff_hunk":"@@ -0,0 +1,411 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one or more\n+ * contributor license agreements.  See the NOTICE file distributed with\n+ * this work for additional information regarding copyright ownership.\n+ * The ASF licenses this file to You under the Apache License, Version 2.0\n+ * (the \"License\"); you may not use this file except in compliance with\n+ * the License.  You may obtain a copy of the License at\n+ *\n+ *    http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.spark.sql.execution.joins\n+\n+import org.apache.spark.sql.catalyst.util.TypeUtils\n+import org.apache.spark.util.ThreadUtils\n+\n+import scala.annotation.tailrec\n+import scala.collection.mutable\n+import scala.concurrent._\n+import scala.concurrent.duration._\n+\n+import org.apache.spark.annotation.DeveloperApi\n+import org.apache.spark.rdd.RDD\n+import org.apache.spark.sql.Row\n+import org.apache.spark.sql.catalyst.InternalRow\n+import org.apache.spark.sql.catalyst.expressions._\n+import org.apache.spark.sql.execution.{BinaryNode, SparkPlan}\n+\n+/**\n+ * Performs an inner range join on two tables. A range join typically has the following form:\n+ *\n+ * SELECT A.*\n+ *        ,B.*\n+ * FROM   tableA A\n+ *        JOIN tableB B\n+ *         ON A.start <= B.end\n+ *          AND A.end > B.start\n+ *\n+ * The implementation builds a range index from the smaller build side, broadcasts this index\n+ * to all executors. The streaming side is then matched against the index. This reduces the number\n+ * of comparisons made by log(n) (n is the number of records in the build table) over the\n+ * typical solution (Nested Loop Join).\n+ *\n+ * TODO NaN values\n+ * TODO NULL values\n+ * TODO Outer joins? StreamSide is quite easy/BuildSide requires bookkeeping and\n+ * TODO This join will maintain sort order. The build side rows will also be added in a lower\n+ *      bound sorted fashion.\n+ */\n+@DeveloperApi\n+case class BroadcastRangeJoin(\n+    leftKeys: Seq[Expression],\n+    rightKeys: Seq[Expression],\n+    equality: Seq[Boolean],\n+    buildSide: BuildSide,\n+    left: SparkPlan,\n+    right: SparkPlan)\n+  extends BinaryNode {\n+\n+  private[this] lazy val (buildPlan, streamedPlan) = buildSide match {\n+    case BuildLeft => (left, right)\n+    case BuildRight => (right, left)\n+  }\n+\n+  private[this] lazy val (buildKeys, streamedKeys) = buildSide match {\n+    case BuildLeft => (leftKeys, rightKeys)\n+    case BuildRight => (rightKeys, leftKeys)\n+  }\n+\n+  override def output: Seq[Attribute] = left.output ++ right.output\n+\n+  @transient\n+  private[this] lazy val buildSideKeyGenerator: Projection =\n+    newProjection(buildKeys, buildPlan.output)\n+\n+  @transient\n+  private[this] lazy val streamSideKeyGenerator: () => MutableProjection =\n+    newMutableProjection(streamedKeys, streamedPlan.output)\n+\n+  private[this] val timeout: Duration = {\n+    val timeoutValue = sqlContext.conf.broadcastTimeout\n+    if (timeoutValue < 0) {\n+      Duration.Inf\n+    } else {\n+      timeoutValue.seconds\n+    }\n+  }\n+\n+  // Construct the range index.\n+  @transient\n+  private[this] val indexBroadcastFuture = future {\n+    // Deal with equality.\n+    val Seq(allowLowEqual: Boolean, allowHighEqual: Boolean) = buildSide match {\n+      case BuildLeft => equality.reverse\n+      case BuildRight => equality\n+    }\n+\n+    // Get the ordering for the datatype.\n+    val ordering = TypeUtils.getOrdering(buildKeys.head.dataType)\n+\n+    // Note that we use .execute().collect() because we don't want to convert data to Scala types\n+    // TODO find out if the result of a sort and a collect is still sorted.\n+    val eventifier = RangeIndex.toRangeEvent(buildSideKeyGenerator, ordering)\n+    val events = buildPlan.execute().map(_.copy()).collect().flatMap(eventifier)\n+\n+    // Create the index.\n+    val index = RangeIndex.build(ordering, events, allowLowEqual, allowHighEqual)\n+\n+    // Broadcast the index.\n+    sparkContext.broadcast(index)\n+  }(BroadcastRangeJoin.broadcastRangeJoinExecutionContext)\n+\n+  override def doExecute(): RDD[InternalRow] = {\n+    // Construct the range index.\n+    val indexBC = Await.result(indexBroadcastFuture, timeout)\n+\n+    // Iterate over the streaming relation.\n+    streamedPlan.execute().mapPartitions { stream =>\n+      new Iterator[InternalRow] {\n+        private[this] val index = indexBC.value\n+        private[this] val streamSideKeys = streamSideKeyGenerator()\n+        private[this] val join = new JoinedRow2 // TODO create our own join row...\n+        private[this] var row: InternalRow = EmptyRow\n+        private[this] var iterator: Iterator[InternalRow] = Iterator.empty\n+\n+        override final def hasNext: Boolean = {\n+          var result = iterator.hasNext\n+          while (!result && stream.hasNext) {\n+            row = stream.next()","path":"sql/core/src/main/scala/org/apache/spark/sql/execution/joins/BroadcastRangeJoin.scala","position":135,"original_position":135,"commit_id":"8204eaed1b9399f17415afc6ce178c845f29746f","original_commit_id":"8204eaed1b9399f17415afc6ce178c845f29746f","user":{"login":"hvanhovell","id":9616802,"avatar_url":"https://avatars.githubusercontent.com/u/9616802?v=3","gravatar_id":"","url":"https://api.github.com/users/hvanhovell","html_url":"https://github.com/hvanhovell","followers_url":"https://api.github.com/users/hvanhovell/followers","following_url":"https://api.github.com/users/hvanhovell/following{/other_user}","gists_url":"https://api.github.com/users/hvanhovell/gists{/gist_id}","starred_url":"https://api.github.com/users/hvanhovell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hvanhovell/subscriptions","organizations_url":"https://api.github.com/users/hvanhovell/orgs","repos_url":"https://api.github.com/users/hvanhovell/repos","events_url":"https://api.github.com/users/hvanhovell/events{/privacy}","received_events_url":"https://api.github.com/users/hvanhovell/received_events","type":"User","site_admin":false},"body":"Ah I see. The current (WIP) implementation only allows for inner joins, and we drop rows if they don't have a match in the index. Outer Joins are possible, however BuildSide Outerjoins will require a bit of bookkeeping.","created_at":"2015-07-17T13:58:42Z","updated_at":"2015-07-17T13:58:42Z","html_url":"https://github.com/apache/spark/pull/7379#discussion_r34891100","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/7379","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/34891100"},"html":{"href":"https://github.com/apache/spark/pull/7379#discussion_r34891100"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/7379"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/7379","id":39864667,"html_url":"https://github.com/apache/spark/pull/7379","diff_url":"https://github.com/apache/spark/pull/7379.diff","patch_url":"https://github.com/apache/spark/pull/7379.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/7379","number":7379,"state":"open","locked":false,"title":"[SPARK-8682][SQL][WIP] Range Join","user":{"login":"hvanhovell","id":9616802,"avatar_url":"https://avatars.githubusercontent.com/u/9616802?v=3","gravatar_id":"","url":"https://api.github.com/users/hvanhovell","html_url":"https://github.com/hvanhovell","followers_url":"https://api.github.com/users/hvanhovell/followers","following_url":"https://api.github.com/users/hvanhovell/following{/other_user}","gists_url":"https://api.github.com/users/hvanhovell/gists{/gist_id}","starred_url":"https://api.github.com/users/hvanhovell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hvanhovell/subscriptions","organizations_url":"https://api.github.com/users/hvanhovell/orgs","repos_url":"https://api.github.com/users/hvanhovell/repos","events_url":"https://api.github.com/users/hvanhovell/events{/privacy}","received_events_url":"https://api.github.com/users/hvanhovell/received_events","type":"User","site_admin":false},"body":"*...copied from JIRA (SPARK-8682):*\r\n\r\nCurrently Spark SQL uses a Broadcast Nested Loop join (or a filtered Cartesian Join) when it has to execute the following range query:\r\n```\r\nSELECT A.*,\r\n       B.*\r\nFROM   tableA A\r\n       JOIN tableB B\r\n        ON A.start <= B.end\r\n         AND A.end > B.start\r\n```\r\nThis is horribly inefficient. The performance of this query can be greatly improved, when one of the tables can be broadcasted, by creating a range index. A range index is basically a sorted map containing the rows of the smaller table, indexed by both the high and low keys. using this structure the complexity of the query would go from O(N * M) to O(N * 2 * LOG(M)), N = number of records in the larger table, M = number of records in the smaller (indexed) table.\r\n\r\nThis is currently a work in progress. I will be adding more tests and a small benchmark in the next couple of days. If you want to try this out, set the ```spark.sql.planner.rangeJoin``` option to ```true``` in the SQL configuration.","created_at":"2015-07-13T22:37:57Z","updated_at":"2015-07-17T13:58:42Z","closed_at":null,"merged_at":null,"merge_commit_sha":"eab5c605bb938dc6641b9fd1e4e741040d272f71","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/7379/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/7379/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/7379/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/8204eaed1b9399f17415afc6ce178c845f29746f","head":{"label":"hvanhovell:SPARK-8682","ref":"SPARK-8682","sha":"8204eaed1b9399f17415afc6ce178c845f29746f","user":{"login":"hvanhovell","id":9616802,"avatar_url":"https://avatars.githubusercontent.com/u/9616802?v=3","gravatar_id":"","url":"https://api.github.com/users/hvanhovell","html_url":"https://github.com/hvanhovell","followers_url":"https://api.github.com/users/hvanhovell/followers","following_url":"https://api.github.com/users/hvanhovell/following{/other_user}","gists_url":"https://api.github.com/users/hvanhovell/gists{/gist_id}","starred_url":"https://api.github.com/users/hvanhovell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hvanhovell/subscriptions","organizations_url":"https://api.github.com/users/hvanhovell/orgs","repos_url":"https://api.github.com/users/hvanhovell/repos","events_url":"https://api.github.com/users/hvanhovell/events{/privacy}","received_events_url":"https://api.github.com/users/hvanhovell/received_events","type":"User","site_admin":false},"repo":{"id":35900688,"name":"spark","full_name":"hvanhovell/spark","owner":{"login":"hvanhovell","id":9616802,"avatar_url":"https://avatars.githubusercontent.com/u/9616802?v=3","gravatar_id":"","url":"https://api.github.com/users/hvanhovell","html_url":"https://github.com/hvanhovell","followers_url":"https://api.github.com/users/hvanhovell/followers","following_url":"https://api.github.com/users/hvanhovell/following{/other_user}","gists_url":"https://api.github.com/users/hvanhovell/gists{/gist_id}","starred_url":"https://api.github.com/users/hvanhovell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hvanhovell/subscriptions","organizations_url":"https://api.github.com/users/hvanhovell/orgs","repos_url":"https://api.github.com/users/hvanhovell/repos","events_url":"https://api.github.com/users/hvanhovell/events{/privacy}","received_events_url":"https://api.github.com/users/hvanhovell/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hvanhovell/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/hvanhovell/spark","forks_url":"https://api.github.com/repos/hvanhovell/spark/forks","keys_url":"https://api.github.com/repos/hvanhovell/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hvanhovell/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hvanhovell/spark/teams","hooks_url":"https://api.github.com/repos/hvanhovell/spark/hooks","issue_events_url":"https://api.github.com/repos/hvanhovell/spark/issues/events{/number}","events_url":"https://api.github.com/repos/hvanhovell/spark/events","assignees_url":"https://api.github.com/repos/hvanhovell/spark/assignees{/user}","branches_url":"https://api.github.com/repos/hvanhovell/spark/branches{/branch}","tags_url":"https://api.github.com/repos/hvanhovell/spark/tags","blobs_url":"https://api.github.com/repos/hvanhovell/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hvanhovell/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hvanhovell/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/hvanhovell/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hvanhovell/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/hvanhovell/spark/languages","stargazers_url":"https://api.github.com/repos/hvanhovell/spark/stargazers","contributors_url":"https://api.github.com/repos/hvanhovell/spark/contributors","subscribers_url":"https://api.github.com/repos/hvanhovell/spark/subscribers","subscription_url":"https://api.github.com/repos/hvanhovell/spark/subscription","commits_url":"https://api.github.com/repos/hvanhovell/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/hvanhovell/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/hvanhovell/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/hvanhovell/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/hvanhovell/spark/contents/{+path}","compare_url":"https://api.github.com/repos/hvanhovell/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hvanhovell/spark/merges","archive_url":"https://api.github.com/repos/hvanhovell/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hvanhovell/spark/downloads","issues_url":"https://api.github.com/repos/hvanhovell/spark/issues{/number}","pulls_url":"https://api.github.com/repos/hvanhovell/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/hvanhovell/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/hvanhovell/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hvanhovell/spark/labels{/name}","releases_url":"https://api.github.com/repos/hvanhovell/spark/releases{/id}","created_at":"2015-05-19T18:29:31Z","updated_at":"2015-05-19T18:29:53Z","pushed_at":"2015-07-17T02:13:14Z","git_url":"git://github.com/hvanhovell/spark.git","ssh_url":"git@github.com:hvanhovell/spark.git","clone_url":"https://github.com/hvanhovell/spark.git","svn_url":"https://github.com/hvanhovell/spark","homepage":null,"size":112377,"stargazers_count":0,"watchers_count":0,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"257236c3e17906098f801cbc2059e7a9054e8cab","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":17165658,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/spark","description":"Mirror of Apache Spark","fork":false,"url":"https://api.github.com/repos/apache/spark","forks_url":"https://api.github.com/repos/apache/spark/forks","keys_url":"https://api.github.com/repos/apache/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/spark/teams","hooks_url":"https://api.github.com/repos/apache/spark/hooks","issue_events_url":"https://api.github.com/repos/apache/spark/issues/events{/number}","events_url":"https://api.github.com/repos/apache/spark/events","assignees_url":"https://api.github.com/repos/apache/spark/assignees{/user}","branches_url":"https://api.github.com/repos/apache/spark/branches{/branch}","tags_url":"https://api.github.com/repos/apache/spark/tags","blobs_url":"https://api.github.com/repos/apache/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/spark/languages","stargazers_url":"https://api.github.com/repos/apache/spark/stargazers","contributors_url":"https://api.github.com/repos/apache/spark/contributors","subscribers_url":"https://api.github.com/repos/apache/spark/subscribers","subscription_url":"https://api.github.com/repos/apache/spark/subscription","commits_url":"https://api.github.com/repos/apache/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/spark/contents/{+path}","compare_url":"https://api.github.com/repos/apache/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/spark/merges","archive_url":"https://api.github.com/repos/apache/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/spark/downloads","issues_url":"https://api.github.com/repos/apache/spark/issues{/number}","pulls_url":"https://api.github.com/repos/apache/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/spark/labels{/name}","releases_url":"https://api.github.com/repos/apache/spark/releases{/id}","created_at":"2014-02-25T08:00:08Z","updated_at":"2015-07-17T13:49:35Z","pushed_at":"2015-07-17T13:22:44Z","git_url":"git://github.com/apache/spark.git","ssh_url":"git@github.com:apache/spark.git","clone_url":"https://github.com/apache/spark.git","svn_url":"https://github.com/apache/spark","homepage":null,"size":1549141,"stargazers_count":4778,"watchers_count":4778,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4267,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":338,"forks":4267,"open_issues":338,"watchers":4778,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/7379"},"html":{"href":"https://github.com/apache/spark/pull/7379"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/7379"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/7379/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/7379/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/7379/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/8204eaed1b9399f17415afc6ce178c845f29746f"}}}}