{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/181351132","pull_request_review_id":111963583,"id":181351132,"diff_hunk":"@@ -287,3 +289,160 @@ case class ArrayContains(left: Expression, right: Expression)\n \n   override def prettyName: String = \"array_contains\"\n }\n+\n+/**\n+ * Transforms an array of arrays into a single array.\n+ */\n+@ExpressionDescription(\n+  usage = \"_FUNC_(arrayOfArrays) - Transforms an array of arrays into a single array.\",\n+  examples = \"\"\"\n+    Examples:\n+      > SELECT _FUNC_(array(array(1, 2), array(3, 4));\n+       [1,2,3,4]\n+  \"\"\",\n+  since = \"2.4.0\")\n+case class Flatten(child: Expression) extends UnaryExpression {\n+\n+  override def nullable: Boolean = child.nullable || dataType.containsNull\n+\n+  override def dataType: ArrayType = {\n+    child\n+      .dataType.asInstanceOf[ArrayType]\n+      .elementType.asInstanceOf[ArrayType]\n+  }\n+\n+  override def checkInputDataTypes(): TypeCheckResult = child.dataType match {\n+    case ArrayType(_: ArrayType, _) =>\n+      TypeCheckResult.TypeCheckSuccess\n+    case _ =>\n+      TypeCheckResult.TypeCheckFailure(\n+        s\"The argument should be an array of arrays, \" +\n+        s\"but '${child.sql}' is of ${child.dataType.simpleString} type.\"\n+      )\n+  }\n+\n+  override def nullSafeEval(array: Any): Any = {\n+    val elements = array.asInstanceOf[ArrayData].toObjectArray(dataType)\n+\n+    if (elements.contains(null)) {\n+      null\n+    } else {\n+      val flattened = elements.flatMap(","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/collectionOperations.scala","position":51,"original_position":51,"commit_id":"0e0def48709fd04efeee5c51cdaea641e47eae15","original_commit_id":"0e0def48709fd04efeee5c51cdaea641e47eae15","user":{"login":"kiszk","id":1315079,"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":"Do we need size check whether the total array is less than or equal to possible max array size?","created_at":"2018-04-13T10:43:49Z","updated_at":"2018-04-13T10:43:49Z","html_url":"https://github.com/apache/spark/pull/20938#discussion_r181351132","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/20938","author_association":"CONTRIBUTOR","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/181351132"},"html":{"href":"https://github.com/apache/spark/pull/20938#discussion_r181351132"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/20938"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/20938","id":178390529,"html_url":"https://github.com/apache/spark/pull/20938","diff_url":"https://github.com/apache/spark/pull/20938.diff","patch_url":"https://github.com/apache/spark/pull/20938.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/20938","number":20938,"state":"open","locked":false,"title":"[SPARK-23821][SQL] Collection function: flatten","user":{"login":"mn-mikke","id":3674621,"avatar_url":"https://avatars3.githubusercontent.com/u/3674621?v=4","gravatar_id":"","url":"https://api.github.com/users/mn-mikke","html_url":"https://github.com/mn-mikke","followers_url":"https://api.github.com/users/mn-mikke/followers","following_url":"https://api.github.com/users/mn-mikke/following{/other_user}","gists_url":"https://api.github.com/users/mn-mikke/gists{/gist_id}","starred_url":"https://api.github.com/users/mn-mikke/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mn-mikke/subscriptions","organizations_url":"https://api.github.com/users/mn-mikke/orgs","repos_url":"https://api.github.com/users/mn-mikke/repos","events_url":"https://api.github.com/users/mn-mikke/events{/privacy}","received_events_url":"https://api.github.com/users/mn-mikke/received_events","type":"User","site_admin":false},"body":"## What changes were proposed in this pull request?\r\n\r\nThis PR adds a new collection function that transforms an array of arrays into a single array. The PR comprises:\r\n- An expression for flattening array structure\r\n- Flatten function\r\n- A wrapper for PySpark\r\n\r\n## How was this patch tested?\r\n\r\nNew tests added into:\r\n- CollectionExpressionsSuite\r\n- DataFrameFunctionsSuite\r\n\r\n## Codegen examples\r\n### Primitive type\r\n```\r\nval df = Seq(\r\n  Seq(Seq(1, 2), Seq(4, 5)),\r\n  Seq(null, Seq(1))\r\n).toDF(\"i\")\r\ndf.filter($\"i\".isNotNull || $\"i\".isNull).select(flatten($\"i\")).debugCodegen\r\n```\r\nResult:\r\n```\r\n/* 047 */         ArrayData project_value = null;\r\n/* 048 */\r\n/* 049 */         if (!inputadapter_isNull) {\r\n/* 050 */           for(int z=0; z < inputadapter_value.numElements(); z++) {\r\n/* 051 */             project_isNull |= inputadapter_value.isNullAt(z);\r\n/* 052 */           }\r\n/* 053 */           if(!project_isNull) {\r\n/* 054 */             int project_numElements = 0;\r\n/* 055 */             for(int z=0; z < inputadapter_value.numElements(); z++) {\r\n/* 056 */               project_numElements += inputadapter_value.getArray(z).numElements();\r\n/* 057 */             }\r\n/* 058 */\r\n/* 059 */             int project_size = UnsafeArrayData.calculateHeaderPortionInBytes(project_numElements) +\r\n/* 060 */             org.apache.spark.unsafe.array.ByteArrayMethods.roundNumberOfBytesToNearestWord(\r\n/* 061 */               4 * project_numElements\r\n/* 062 */             );\r\n/* 063 */\r\n/* 064 */             byte[] project_array = new byte[project_size];\r\n/* 065 */             UnsafeArrayData project_tempArrayData = new UnsafeArrayData();\r\n/* 066 */             Platform.putLong(project_array, 16, project_numElements);\r\n/* 067 */             project_tempArrayData.pointTo(project_array, 16, project_size);\r\n/* 068 */             int project_counter = 0;\r\n/* 069 */             for(int k=0; k < inputadapter_value.numElements(); k++) {\r\n/* 070 */               ArrayData arr = inputadapter_value.getArray(k);\r\n/* 071 */               for(int l = 0; l < arr.numElements(); l++) {\r\n/* 072 */                 if(arr.isNullAt(l)) {\r\n/* 073 */                   project_tempArrayData.setNullAt(project_counter);\r\n/* 074 */                 } else {\r\n/* 075 */                   project_tempArrayData.setInt(\r\n/* 076 */                     project_counter,\r\n/* 077 */                     arr.getInt(l)\r\n/* 078 */                   );\r\n/* 079 */                 }\r\n/* 080 */                 project_counter++;\r\n/* 081 */               }\r\n/* 082 */             }\r\n/* 083 */             project_value = project_tempArrayData;\r\n/* 084 */\r\n/* 085 */           }\r\n/* 086 */\r\n/* 087 */         }\r\n```\r\n### Complex type\r\n```\r\nval df = Seq(\r\n  Seq(Seq(\"a\", \"b\"), Seq(null, \"d\")),\r\n  Seq(null, Seq(\"a\"))\r\n).toDF(\"s\")\r\ndf.filter($\"s\".isNotNull || $\"s\".isNull).select(flatten($\"s\")).debugCodegen\r\n```\r\nResult:\r\n```\r\n/* 047 */         ArrayData project_value = null;\r\n/* 048 */\r\n/* 049 */         if (!inputadapter_isNull) {\r\n/* 050 */           for(int z=0; z < inputadapter_value.numElements(); z++) {\r\n/* 051 */             project_isNull |= inputadapter_value.isNullAt(z);\r\n/* 052 */           }\r\n/* 053 */           if(!project_isNull) {\r\n/* 054 */             int project_numElements = 0;\r\n/* 055 */             for(int z=0; z < inputadapter_value.numElements(); z++) {\r\n/* 056 */               project_numElements += inputadapter_value.getArray(z).numElements();\r\n/* 057 */             }\r\n/* 058 */\r\n/* 059 */             Object[] project_arrayObject = new Object[project_numElements];\r\n/* 060 */             int project_counter = 0;\r\n/* 061 */             for(int k=0; k < inputadapter_value.numElements(); k++) {\r\n/* 062 */               Object[] arr = inputadapter_value.getArray(k).array();\r\n/* 063 */               for(int l = 0; l < arr.length; l++) {\r\n/* 064 */                 project_arrayObject[project_counter] = arr[l];\r\n/* 065 */                 project_counter++;\r\n/* 066 */               }\r\n/* 067 */             }\r\n/* 068 */             project_value = new org.apache.spark.sql.catalyst.util.GenericArrayData(project_arrayObject);\r\n/* 069 */\r\n/* 070 */           }\r\n/* 071 */\r\n/* 072 */         }\r\n```","created_at":"2018-03-29T17:19:39Z","updated_at":"2018-04-13T10:43:49Z","closed_at":null,"merged_at":null,"merge_commit_sha":"a5c0f7d4d36a46017ecd7a551334587b56e677fe","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/20938/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/20938/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/20938/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/0e0def48709fd04efeee5c51cdaea641e47eae15","head":{"label":"AbsaOSS:feature/array-api-flatten-to-master","ref":"feature/array-api-flatten-to-master","sha":"0e0def48709fd04efeee5c51cdaea641e47eae15","user":{"login":"AbsaOSS","id":18133719,"avatar_url":"https://avatars0.githubusercontent.com/u/18133719?v=4","gravatar_id":"","url":"https://api.github.com/users/AbsaOSS","html_url":"https://github.com/AbsaOSS","followers_url":"https://api.github.com/users/AbsaOSS/followers","following_url":"https://api.github.com/users/AbsaOSS/following{/other_user}","gists_url":"https://api.github.com/users/AbsaOSS/gists{/gist_id}","starred_url":"https://api.github.com/users/AbsaOSS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AbsaOSS/subscriptions","organizations_url":"https://api.github.com/users/AbsaOSS/orgs","repos_url":"https://api.github.com/users/AbsaOSS/repos","events_url":"https://api.github.com/users/AbsaOSS/events{/privacy}","received_events_url":"https://api.github.com/users/AbsaOSS/received_events","type":"Organization","site_admin":false},"repo":{"id":122981537,"name":"spark","full_name":"AbsaOSS/spark","owner":{"login":"AbsaOSS","id":18133719,"avatar_url":"https://avatars0.githubusercontent.com/u/18133719?v=4","gravatar_id":"","url":"https://api.github.com/users/AbsaOSS","html_url":"https://github.com/AbsaOSS","followers_url":"https://api.github.com/users/AbsaOSS/followers","following_url":"https://api.github.com/users/AbsaOSS/following{/other_user}","gists_url":"https://api.github.com/users/AbsaOSS/gists{/gist_id}","starred_url":"https://api.github.com/users/AbsaOSS/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AbsaOSS/subscriptions","organizations_url":"https://api.github.com/users/AbsaOSS/orgs","repos_url":"https://api.github.com/users/AbsaOSS/repos","events_url":"https://api.github.com/users/AbsaOSS/events{/privacy}","received_events_url":"https://api.github.com/users/AbsaOSS/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/AbsaOSS/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/AbsaOSS/spark","forks_url":"https://api.github.com/repos/AbsaOSS/spark/forks","keys_url":"https://api.github.com/repos/AbsaOSS/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/AbsaOSS/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/AbsaOSS/spark/teams","hooks_url":"https://api.github.com/repos/AbsaOSS/spark/hooks","issue_events_url":"https://api.github.com/repos/AbsaOSS/spark/issues/events{/number}","events_url":"https://api.github.com/repos/AbsaOSS/spark/events","assignees_url":"https://api.github.com/repos/AbsaOSS/spark/assignees{/user}","branches_url":"https://api.github.com/repos/AbsaOSS/spark/branches{/branch}","tags_url":"https://api.github.com/repos/AbsaOSS/spark/tags","blobs_url":"https://api.github.com/repos/AbsaOSS/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/AbsaOSS/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/AbsaOSS/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/AbsaOSS/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/AbsaOSS/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/AbsaOSS/spark/languages","stargazers_url":"https://api.github.com/repos/AbsaOSS/spark/stargazers","contributors_url":"https://api.github.com/repos/AbsaOSS/spark/contributors","subscribers_url":"https://api.github.com/repos/AbsaOSS/spark/subscribers","subscription_url":"https://api.github.com/repos/AbsaOSS/spark/subscription","commits_url":"https://api.github.com/repos/AbsaOSS/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/AbsaOSS/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/AbsaOSS/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/AbsaOSS/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/AbsaOSS/spark/contents/{+path}","compare_url":"https://api.github.com/repos/AbsaOSS/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/AbsaOSS/spark/merges","archive_url":"https://api.github.com/repos/AbsaOSS/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/AbsaOSS/spark/downloads","issues_url":"https://api.github.com/repos/AbsaOSS/spark/issues{/number}","pulls_url":"https://api.github.com/repos/AbsaOSS/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/AbsaOSS/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/AbsaOSS/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/AbsaOSS/spark/labels{/name}","releases_url":"https://api.github.com/repos/AbsaOSS/spark/releases{/id}","deployments_url":"https://api.github.com/repos/AbsaOSS/spark/deployments","created_at":"2018-02-26T14:30:33Z","updated_at":"2018-02-26T14:31:00Z","pushed_at":"2018-04-13T09:15:18Z","git_url":"git://github.com/AbsaOSS/spark.git","ssh_url":"git@github.com:AbsaOSS/spark.git","clone_url":"https://github.com/AbsaOSS/spark.git","svn_url":"https://github.com/AbsaOSS/spark","homepage":null,"size":267243,"stargazers_count":0,"watchers_count":0,"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":1,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"0b19122d434e39eb117ccc3174a0688c9c874d48","user":{"login":"apache","id":47359,"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,"name":"spark","full_name":"apache/spark","owner":{"login":"apache","id":47359,"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-04-13T09:01:42Z","pushed_at":"2018-04-13T10:41:58Z","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":267341,"stargazers_count":16890,"watchers_count":16890,"language":"Scala","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":15504,"mirror_url":null,"archived":false,"open_issues_count":514,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0"},"forks":15504,"open_issues":514,"watchers":16890,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/20938"},"html":{"href":"https://github.com/apache/spark/pull/20938"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/20938"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/20938/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/20938/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/20938/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/0e0def48709fd04efeee5c51cdaea641e47eae15"}},"author_association":"NONE"}}
{"actor":{"display_login":"kiszk"}}