{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/192254481","pull_request_review_id":124990912,"id":192254481,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDE5MjI1NDQ4MQ==","diff_hunk":"@@ -1882,3 +1882,311 @@ case class ArrayRepeat(left: Expression, right: Expression)\n   }\n \n }\n+\n+object ArraySetLike {\n+  val kindUnion = 1\n+\n+  private val MAX_ARRAY_LENGTH: Int = ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH\n+\n+  def toArrayDataInt(hs: OpenHashSet[Int]): ArrayData = {\n+    val array = new Array[Int](hs.size)\n+    var pos = hs.nextPos(0)\n+    var i = 0\n+    while (pos != OpenHashSet.INVALID_POS) {\n+      array(i) = hs.getValue(pos)\n+      pos = hs.nextPos(pos + 1)\n+      i += 1\n+    }\n+\n+    val numBytes = 4L * array.length\n+    val unsafeArraySizeInBytes = UnsafeArrayData.calculateHeaderPortionInBytes(array.length) +\n+      org.apache.spark.unsafe.array.ByteArrayMethods.roundNumberOfBytesToNearestWord(numBytes)\n+    // Since UnsafeArrayData.fromPrimitiveArray() uses long[], max elements * 8 bytes can be used\n+    if (unsafeArraySizeInBytes <= Integer.MAX_VALUE * 8) {\n+      UnsafeArrayData.fromPrimitiveArray(array)\n+    } else {\n+      new GenericArrayData(array)\n+    }\n+  }\n+\n+  def toArrayDataLong(hs: OpenHashSet[Long]): ArrayData = {\n+    val array = new Array[Long](hs.size)\n+    var pos = hs.nextPos(0)\n+    var i = 0\n+    while (pos != OpenHashSet.INVALID_POS) {\n+      array(i) = hs.getValue(pos)\n+      pos = hs.nextPos(pos + 1)\n+      i += 1\n+    }\n+\n+    val numBytes = 8L * array.length\n+    val unsafeArraySizeInBytes = UnsafeArrayData.calculateHeaderPortionInBytes(array.length) +\n+      org.apache.spark.unsafe.array.ByteArrayMethods.roundNumberOfBytesToNearestWord(numBytes)\n+    // Since UnsafeArrayData.fromPrimitiveArray() uses long[], max elements * 8 bytes can be used\n+    if (unsafeArraySizeInBytes <= Integer.MAX_VALUE * 8) {\n+      UnsafeArrayData.fromPrimitiveArray(array)\n+    } else {\n+      new GenericArrayData(array)\n+    }\n+  }\n+\n+  def arrayUnion(\n+      array1: ArrayData,\n+      array2: ArrayData,\n+      et: DataType,\n+      ordering: Ordering[Any]): ArrayData = {\n+    if (ordering == null) {\n+      new GenericArrayData(array1.toObjectArray(et).union(array2.toObjectArray(et))\n+        .distinct.asInstanceOf[Array[Any]])\n+    } else {\n+      val length = math.min(array1.numElements().toLong + array2.numElements().toLong,\n+        ByteArrayMethods.MAX_ROUNDED_ARRAY_LENGTH)\n+      val array = new Array[Any](length.toInt)\n+      var pos = 0\n+      var hasNull = false\n+      Seq(array1, array2).foreach(_.foreach(et, (_, v) => {\n+        var found = false\n+        if (v == null) {\n+          if (hasNull) {\n+            found = true\n+          } else {\n+            hasNull = true\n+          }\n+        } else {\n+          var j = 0\n+          while (!found && j < pos) {\n+            val va = array(j)\n+            if (va != null && ordering.equiv(va, v)) {\n+              found = true\n+            }\n+            j = j + 1\n+          }\n+        }\n+        if (!found) {\n+          if (pos > MAX_ARRAY_LENGTH) {\n+            throw new RuntimeException(s\"Unsuccessful try to union arrays with $pos\" +\n+              s\" elements due to exceeding the array size limit $MAX_ARRAY_LENGTH.\")\n+          }\n+          array(pos) = v\n+          pos = pos + 1\n+        }\n+      }))\n+      new GenericArrayData(array.slice(0, pos))\n+    }\n+  }\n+}\n+\n+abstract class ArraySetLike extends BinaryArrayExpressionWithImplicitCast {\n+  def typeId: Int\n+\n+  override def dataType: DataType = left.dataType\n+\n+  override def checkInputDataTypes(): TypeCheckResult = {\n+    val typeCheckResult = super.checkInputDataTypes()\n+    if (typeCheckResult.isSuccess) {\n+      TypeUtils.checkForOrderingExpr(dataType.asInstanceOf[ArrayType].elementType,\n+        s\"function $prettyName\")\n+    } else {\n+      typeCheckResult\n+    }\n+  }\n+\n+  private def cn = left.dataType.asInstanceOf[ArrayType].containsNull ||\n+    right.dataType.asInstanceOf[ArrayType].containsNull\n+\n+  @transient private lazy val ordering: Ordering[Any] =\n+    TypeUtils.getInterpretedOrdering(elementType)\n+\n+  @transient private lazy val elementTypeSupportEquals = elementType match {\n+    case BinaryType => false\n+    case _: AtomicType => true\n+    case _ => false\n+  }\n+\n+  def intEval(ary: ArrayData, hs2: OpenHashSet[Int]): OpenHashSet[Int]\n+  def longEval(ary: ArrayData, hs2: OpenHashSet[Long]): OpenHashSet[Long]\n+  def genericEval(ary: ArrayData, hs2: OpenHashSet[Any], et: DataType): OpenHashSet[Any]\n+  def codeGen(ctx: CodegenContext, hs2: String, hs: String, len: String, getter: String, i: String,\n+    postFix: String, newOpenHashSet: String): String\n+\n+  override def nullSafeEval(input1: Any, input2: Any): Any = {\n+    val ary1 = input1.asInstanceOf[ArrayData]\n+    val ary2 = input2.asInstanceOf[ArrayData]\n+\n+    if (!cn) {\n+      elementType match {\n+        case IntegerType =>\n+          // avoid boxing of primitive int array elements\n+          val hs2 = new OpenHashSet[Int]\n+          var i = 0\n+          while (i < ary2.numElements()) {\n+            hs2.add(ary2.getInt(i))\n+            i += 1\n+          }\n+          ArraySetLike.toArrayDataInt(intEval(ary1, hs2))\n+        case LongType =>\n+          // avoid boxing of primitive long array elements\n+          val hs2 = new OpenHashSet[Long]\n+          var i = 0\n+          while (i < ary2.numElements()) {\n+            hs2.add(ary2.getLong(i))\n+            i += 1\n+          }\n+          ArraySetLike.toArrayDataLong(longEval(ary1, hs2))\n+        case _ =>\n+          val hs2 = new OpenHashSet[Any]\n+          var i = 0\n+          while (i < ary2.numElements()) {\n+            hs2.add(ary2.get(i, elementType))\n+            i += 1\n+          }\n+          new GenericArrayData(genericEval(ary1, hs2, elementType).iterator.toArray)\n+      }\n+    } else {\n+      if (typeId == ArraySetLike.kindUnion) {","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala","position":177,"original_position":177,"commit_id":"a91d1c51747878001ab791dc6cad80fc35ef537a","original_commit_id":"a91d1c51747878001ab791dc6cad80fc35ef537a","user":{"login":"ueshin","id":506656,"node_id":"MDQ6VXNlcjUwNjY1Ng==","avatar_url":"https://avatars1.githubusercontent.com/u/506656?v=4","gravatar_id":"","url":"https://api.github.com/users/ueshin","html_url":"https://github.com/ueshin","followers_url":"https://api.github.com/users/ueshin/followers","following_url":"https://api.github.com/users/ueshin/following{/other_user}","gists_url":"https://api.github.com/users/ueshin/gists{/gist_id}","starred_url":"https://api.github.com/users/ueshin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ueshin/subscriptions","organizations_url":"https://api.github.com/users/ueshin/orgs","repos_url":"https://api.github.com/users/ueshin/repos","events_url":"https://api.github.com/users/ueshin/events{/privacy}","received_events_url":"https://api.github.com/users/ueshin/received_events","type":"User","site_admin":false},"body":"How about moving to concrete class without using `typeId`?","created_at":"2018-05-31T22:22:02Z","updated_at":"2018-05-31T22:27:55Z","html_url":"https://github.com/apache/spark/pull/21061#discussion_r192254481","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/21061","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/192254481"},"html":{"href":"https://github.com/apache/spark/pull/21061#discussion_r192254481"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/21061"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/21061","id":181395877,"node_id":"MDExOlB1bGxSZXF1ZXN0MTgxMzk1ODc3","html_url":"https://github.com/apache/spark/pull/21061","diff_url":"https://github.com/apache/spark/pull/21061.diff","patch_url":"https://github.com/apache/spark/pull/21061.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/21061","number":21061,"state":"open","locked":false,"title":"[SPARK-23914][SQL] Add array_union function","user":{"login":"kiszk","id":1315079,"node_id":"MDQ6VXNlcjEzMTUwNzk=","avatar_url":"https://avatars2.githubusercontent.com/u/1315079?v=4","gravatar_id":"","url":"https://api.github.com/users/kiszk","html_url":"https://github.com/kiszk","followers_url":"https://api.github.com/users/kiszk/followers","following_url":"https://api.github.com/users/kiszk/following{/other_user}","gists_url":"https://api.github.com/users/kiszk/gists{/gist_id}","starred_url":"https://api.github.com/users/kiszk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kiszk/subscriptions","organizations_url":"https://api.github.com/users/kiszk/orgs","repos_url":"https://api.github.com/users/kiszk/repos","events_url":"https://api.github.com/users/kiszk/events{/privacy}","received_events_url":"https://api.github.com/users/kiszk/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nThe PR adds the SQL function `array_union`. The behavior of the function is based on Presto's one.\r\n\r\nThis function returns returns an array of the elements in the union of array1 and array2.\r\n\r\nNote: The order of elements in the result is not defined.\r\n\r\n## How was this patch tested?\r\n\r\nAdded UTs\r\n","created_at":"2018-04-13T06:22:16Z","updated_at":"2018-05-31T22:27:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"53bfb9144f4ac352b5aa2489d67c36ea6f1d25c3","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/21061/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/21061/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/21061/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/a91d1c51747878001ab791dc6cad80fc35ef537a","head":{"label":"kiszk:SPARK-23914","ref":"SPARK-23914","sha":"a91d1c51747878001ab791dc6cad80fc35ef537a","user":{"login":"kiszk","id":1315079,"node_id":"MDQ6VXNlcjEzMTUwNzk=","avatar_url":"https://avatars2.githubusercontent.com/u/1315079?v=4","gravatar_id":"","url":"https://api.github.com/users/kiszk","html_url":"https://github.com/kiszk","followers_url":"https://api.github.com/users/kiszk/followers","following_url":"https://api.github.com/users/kiszk/following{/other_user}","gists_url":"https://api.github.com/users/kiszk/gists{/gist_id}","starred_url":"https://api.github.com/users/kiszk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kiszk/subscriptions","organizations_url":"https://api.github.com/users/kiszk/orgs","repos_url":"https://api.github.com/users/kiszk/repos","events_url":"https://api.github.com/users/kiszk/events{/privacy}","received_events_url":"https://api.github.com/users/kiszk/received_events","type":"User","site_admin":false},"repo":{"id":45008789,"node_id":"MDEwOlJlcG9zaXRvcnk0NTAwODc4OQ==","name":"spark","full_name":"kiszk/spark","owner":{"login":"kiszk","id":1315079,"node_id":"MDQ6VXNlcjEzMTUwNzk=","avatar_url":"https://avatars2.githubusercontent.com/u/1315079?v=4","gravatar_id":"","url":"https://api.github.com/users/kiszk","html_url":"https://github.com/kiszk","followers_url":"https://api.github.com/users/kiszk/followers","following_url":"https://api.github.com/users/kiszk/following{/other_user}","gists_url":"https://api.github.com/users/kiszk/gists{/gist_id}","starred_url":"https://api.github.com/users/kiszk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kiszk/subscriptions","organizations_url":"https://api.github.com/users/kiszk/orgs","repos_url":"https://api.github.com/users/kiszk/repos","events_url":"https://api.github.com/users/kiszk/events{/privacy}","received_events_url":"https://api.github.com/users/kiszk/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/kiszk/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/kiszk/spark","forks_url":"https://api.github.com/repos/kiszk/spark/forks","keys_url":"https://api.github.com/repos/kiszk/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/kiszk/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/kiszk/spark/teams","hooks_url":"https://api.github.com/repos/kiszk/spark/hooks","issue_events_url":"https://api.github.com/repos/kiszk/spark/issues/events{/number}","events_url":"https://api.github.com/repos/kiszk/spark/events","assignees_url":"https://api.github.com/repos/kiszk/spark/assignees{/user}","branches_url":"https://api.github.com/repos/kiszk/spark/branches{/branch}","tags_url":"https://api.github.com/repos/kiszk/spark/tags","blobs_url":"https://api.github.com/repos/kiszk/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/kiszk/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/kiszk/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/kiszk/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/kiszk/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/kiszk/spark/languages","stargazers_url":"https://api.github.com/repos/kiszk/spark/stargazers","contributors_url":"https://api.github.com/repos/kiszk/spark/contributors","subscribers_url":"https://api.github.com/repos/kiszk/spark/subscribers","subscription_url":"https://api.github.com/repos/kiszk/spark/subscription","commits_url":"https://api.github.com/repos/kiszk/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/kiszk/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/kiszk/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/kiszk/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/kiszk/spark/contents/{+path}","compare_url":"https://api.github.com/repos/kiszk/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/kiszk/spark/merges","archive_url":"https://api.github.com/repos/kiszk/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/kiszk/spark/downloads","issues_url":"https://api.github.com/repos/kiszk/spark/issues{/number}","pulls_url":"https://api.github.com/repos/kiszk/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/kiszk/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/kiszk/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/kiszk/spark/labels{/name}","releases_url":"https://api.github.com/repos/kiszk/spark/releases{/id}","deployments_url":"https://api.github.com/repos/kiszk/spark/deployments","created_at":"2015-10-27T01:25:07Z","updated_at":"2017-09-24T18:21:56Z","pushed_at":"2018-05-19T23:22:11Z","git_url":"git://github.com/kiszk/spark.git","ssh_url":"git@github.com:kiszk/spark.git","clone_url":"https://github.com/kiszk/spark.git","svn_url":"https://github.com/kiszk/spark","homepage":null,"size":263055,"stargazers_count":1,"watchers_count":1,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"a7a9b1837808b281f47643490abcf054f6de7b50","user":{"login":"apache","id":47359,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQ3MzU5","avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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,"node_id":"MDEwOlJlcG9zaXRvcnkxNzE2NTY1OA==","name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"node_id":"MDEyOk9yZ2FuaXphdGlvbjQ3MzU5","avatar_url":"https://avatars0.githubusercontent.com/u/47359?v=4","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}","deployments_url":"https://api.github.com/repos/apache/spark/deployments","created_at":"2014-02-25T08:00:08Z","updated_at":"2018-05-31T21:33:04Z","pushed_at":"2018-05-31T22:20:39Z","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":271041,"stargazers_count":17475,"watchers_count":17475,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":15978,"mirror_url":null,"archived":false,"open_issues_count":553,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":15978,"open_issues":553,"watchers":17475,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/21061"},"html":{"href":"https://github.com/apache/spark/pull/21061"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/21061"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/21061/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/21061/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/21061/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/a91d1c51747878001ab791dc6cad80fc35ef537a"}},"author_association":"CONTRIBUTOR"}}
{"actor":{"display_login":"ueshin"}}