{"action":"created","comment":{"url":"https://api.github.com/repos/apache/spark/pulls/comments/40337580","id":40337580,"diff_hunk":"@@ -52,11 +52,12 @@ abstract class LeafMathExpression(c: Double, name: String)\n  * @param f The math function.\n  * @param name The short name of the function\n  */\n-abstract class UnaryMathExpression(f: Double => Double, name: String)\n+abstract class UnaryMathExpression[T <: DataType](","path":"sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/mathExpressions.scala","position":5,"original_position":5,"commit_id":"b116def2bf89f80a9755eb318b0065099ff9bd50","original_commit_id":"b116def2bf89f80a9755eb318b0065099ff9bd50","user":{"login":"ssimeonov","id":491953,"avatar_url":"https://avatars.githubusercontent.com/u/491953?v=3","gravatar_id":"","url":"https://api.github.com/users/ssimeonov","html_url":"https://github.com/ssimeonov","followers_url":"https://api.github.com/users/ssimeonov/followers","following_url":"https://api.github.com/users/ssimeonov/following{/other_user}","gists_url":"https://api.github.com/users/ssimeonov/gists{/gist_id}","starred_url":"https://api.github.com/users/ssimeonov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ssimeonov/subscriptions","organizations_url":"https://api.github.com/users/ssimeonov/orgs","repos_url":"https://api.github.com/users/ssimeonov/repos","events_url":"https://api.github.com/users/ssimeonov/events{/privacy}","received_events_url":"https://api.github.com/users/ssimeonov/received_events","type":"User","site_admin":false},"body":"I am no Scala expert either but I think what you want is something along the lines of:\r\n\r\n```scala\r\n// Add to imports\r\nimport scala.reflect.runtime.universe.TypeTag\r\n\r\nabstract class UnaryMathExpression[T <: NumericType](\r\n  f: Double => Any, name: String)(implicit ttag: TypeTag[T], longttag: TypeTag[LongType])\r\n  extends UnaryExpression with Serializable with ImplicitCastInputTypes {\r\n\r\n  override def inputTypes: Seq[DataType] = Seq(DoubleType)\r\n  override def dataType: NumericType = if (ttag == longttag) LongType else DoubleType\r\n  override def nullable: Boolean = true\r\n  override def toString: String = s\"$name($child)\"\r\n\r\n  protected override def nullSafeEval(input: Any): Any = {\r\n    f(input.asInstanceOf[Double])\r\n  }\r\n\r\n  // name of function in java.lang.Math\r\n  def funcName: String = name.toLowerCase\r\n\r\n  override final def genCode(ctx: CodeGenContext, ev: GeneratedExpressionCode): String = {\r\n    defineCodeGen(ctx, ev, codeBuilder(ctx, ev))\r\n  }\r\n\r\n  protected def codeBuilder(ctx: CodeGenContext, ev: GeneratedExpressionCode): (String) => String =\r\n    c => s\"(${dataType.typeName})java.lang.Math.$funcName($c)\"\r\n}\r\n```\r\n\r\nNotes:\r\n\r\n- Eliminates the need for creating a subclass just for the purpose of `LongType` return specialization.\r\n\r\n- Uses tighter constraint: `NumericType` instead of `DataType`, which makes sense intuitively for math functions.\r\n\r\n- Sets up type to handle the universe of cases: `LongType` vs. `DoubleType` returns. Since we no longer subclass, that's an OK approach. It is better to use automatic companion object discovery but that would require modifying all data type case objects, which may not be a bad idea but belongs in a separate PR. See [this](http://stackoverflow.com/a/9173117/622495) for more information.\r\n\r\n- Uses `dataType.typeName` instead of hard-coding Java casts.\r\n\r\nThis would require changing all references of `UnaryMathExpression` to `UnaryMathExpression[DoubleType]` and then using `UnaryMathExpression[LongType]` for `ceil` and `floor`.\r\n\r\nThe code compiles and should behave as intended.\r\n\r\nHope this helps.\r\n","created_at":"2015-09-24T16:07:07Z","updated_at":"2015-09-24T16:07:07Z","html_url":"https://github.com/apache/spark/pull/8893#discussion_r40337580","pull_request_url":"https://api.github.com/repos/apache/spark/pulls/8893","_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/comments/40337580"},"html":{"href":"https://github.com/apache/spark/pull/8893#discussion_r40337580"},"pull_request":{"href":"https://api.github.com/repos/apache/spark/pulls/8893"}}},"pull_request":{"url":"https://api.github.com/repos/apache/spark/pulls/8893","id":45851383,"html_url":"https://github.com/apache/spark/pull/8893","diff_url":"https://github.com/apache/spark/pull/8893.diff","patch_url":"https://github.com/apache/spark/pull/8893.patch","issue_url":"https://api.github.com/repos/apache/spark/issues/8893","number":8893,"state":"open","locked":false,"title":"[SPARK-10724] [SQL] SQL's floor() returns DOUBLE","user":{"login":"navis","id":809395,"avatar_url":"https://avatars.githubusercontent.com/u/809395?v=3","gravatar_id":"","url":"https://api.github.com/users/navis","html_url":"https://github.com/navis","followers_url":"https://api.github.com/users/navis/followers","following_url":"https://api.github.com/users/navis/following{/other_user}","gists_url":"https://api.github.com/users/navis/gists{/gist_id}","starred_url":"https://api.github.com/users/navis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/navis/subscriptions","organizations_url":"https://api.github.com/users/navis/orgs","repos_url":"https://api.github.com/users/navis/repos","events_url":"https://api.github.com/users/navis/events{/privacy}","received_events_url":"https://api.github.com/users/navis/received_events","type":"User","site_admin":false},"body":"This is a change in behavior from 1.4.1 where {{floor}} returns a BIGINT. \r\n\r\n{code}\r\nscala> sql(\"select floor(1)\").printSchema\r\nroot\r\n |-- _c0: double (nullable = true)\r\n{code}\r\n\r\nIn the [Hive Language Manual|https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF] {{floor}} is defined to return BIGINT.\r\n\r\nThis is a significant issue because it changes the DataFrame schema.\r\n\r\nI wonder what caused this and whether other SQL functions are affected.","created_at":"2015-09-24T01:44:23Z","updated_at":"2015-09-24T16:07:07Z","closed_at":null,"merged_at":null,"merge_commit_sha":"07cccf865ca68ef11f5303f4590351f84c62e399","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/spark/pulls/8893/commits","review_comments_url":"https://api.github.com/repos/apache/spark/pulls/8893/comments","review_comment_url":"https://api.github.com/repos/apache/spark/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/spark/issues/8893/comments","statuses_url":"https://api.github.com/repos/apache/spark/statuses/b116def2bf89f80a9755eb318b0065099ff9bd50","head":{"label":"navis:SPARK-10724","ref":"SPARK-10724","sha":"b116def2bf89f80a9755eb318b0065099ff9bd50","user":{"login":"navis","id":809395,"avatar_url":"https://avatars.githubusercontent.com/u/809395?v=3","gravatar_id":"","url":"https://api.github.com/users/navis","html_url":"https://github.com/navis","followers_url":"https://api.github.com/users/navis/followers","following_url":"https://api.github.com/users/navis/following{/other_user}","gists_url":"https://api.github.com/users/navis/gists{/gist_id}","starred_url":"https://api.github.com/users/navis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/navis/subscriptions","organizations_url":"https://api.github.com/users/navis/orgs","repos_url":"https://api.github.com/users/navis/repos","events_url":"https://api.github.com/users/navis/events{/privacy}","received_events_url":"https://api.github.com/users/navis/received_events","type":"User","site_admin":false},"repo":{"id":36485276,"name":"spark","full_name":"navis/spark","owner":{"login":"navis","id":809395,"avatar_url":"https://avatars.githubusercontent.com/u/809395?v=3","gravatar_id":"","url":"https://api.github.com/users/navis","html_url":"https://github.com/navis","followers_url":"https://api.github.com/users/navis/followers","following_url":"https://api.github.com/users/navis/following{/other_user}","gists_url":"https://api.github.com/users/navis/gists{/gist_id}","starred_url":"https://api.github.com/users/navis/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/navis/subscriptions","organizations_url":"https://api.github.com/users/navis/orgs","repos_url":"https://api.github.com/users/navis/repos","events_url":"https://api.github.com/users/navis/events{/privacy}","received_events_url":"https://api.github.com/users/navis/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/navis/spark","description":"Mirror of Apache Spark","fork":true,"url":"https://api.github.com/repos/navis/spark","forks_url":"https://api.github.com/repos/navis/spark/forks","keys_url":"https://api.github.com/repos/navis/spark/keys{/key_id}","collaborators_url":"https://api.github.com/repos/navis/spark/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/navis/spark/teams","hooks_url":"https://api.github.com/repos/navis/spark/hooks","issue_events_url":"https://api.github.com/repos/navis/spark/issues/events{/number}","events_url":"https://api.github.com/repos/navis/spark/events","assignees_url":"https://api.github.com/repos/navis/spark/assignees{/user}","branches_url":"https://api.github.com/repos/navis/spark/branches{/branch}","tags_url":"https://api.github.com/repos/navis/spark/tags","blobs_url":"https://api.github.com/repos/navis/spark/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/navis/spark/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/navis/spark/git/refs{/sha}","trees_url":"https://api.github.com/repos/navis/spark/git/trees{/sha}","statuses_url":"https://api.github.com/repos/navis/spark/statuses/{sha}","languages_url":"https://api.github.com/repos/navis/spark/languages","stargazers_url":"https://api.github.com/repos/navis/spark/stargazers","contributors_url":"https://api.github.com/repos/navis/spark/contributors","subscribers_url":"https://api.github.com/repos/navis/spark/subscribers","subscription_url":"https://api.github.com/repos/navis/spark/subscription","commits_url":"https://api.github.com/repos/navis/spark/commits{/sha}","git_commits_url":"https://api.github.com/repos/navis/spark/git/commits{/sha}","comments_url":"https://api.github.com/repos/navis/spark/comments{/number}","issue_comment_url":"https://api.github.com/repos/navis/spark/issues/comments{/number}","contents_url":"https://api.github.com/repos/navis/spark/contents/{+path}","compare_url":"https://api.github.com/repos/navis/spark/compare/{base}...{head}","merges_url":"https://api.github.com/repos/navis/spark/merges","archive_url":"https://api.github.com/repos/navis/spark/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/navis/spark/downloads","issues_url":"https://api.github.com/repos/navis/spark/issues{/number}","pulls_url":"https://api.github.com/repos/navis/spark/pulls{/number}","milestones_url":"https://api.github.com/repos/navis/spark/milestones{/number}","notifications_url":"https://api.github.com/repos/navis/spark/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/navis/spark/labels{/name}","releases_url":"https://api.github.com/repos/navis/spark/releases{/id}","created_at":"2015-05-29T06:08:05Z","updated_at":"2015-05-29T06:08:25Z","pushed_at":"2015-09-24T07:27:05Z","git_url":"git://github.com/navis/spark.git","ssh_url":"git@github.com:navis/spark.git","clone_url":"https://github.com/navis/spark.git","svn_url":"https://github.com/navis/spark","homepage":null,"size":288726,"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":"9952217749118ae78fe794ca11e1c4a87a4ae8ba","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-09-24T11:00:28Z","pushed_at":"2015-09-24T15:31:17Z","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":1630557,"stargazers_count":5531,"watchers_count":5531,"language":"Scala","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":5113,"mirror_url":"git://git.apache.org/spark.git","open_issues_count":337,"forks":5113,"open_issues":337,"watchers":5531,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/spark/pulls/8893"},"html":{"href":"https://github.com/apache/spark/pull/8893"},"issue":{"href":"https://api.github.com/repos/apache/spark/issues/8893"},"comments":{"href":"https://api.github.com/repos/apache/spark/issues/8893/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/spark/pulls/8893/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/spark/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/spark/pulls/8893/commits"},"statuses":{"href":"https://api.github.com/repos/apache/spark/statuses/b116def2bf89f80a9755eb318b0065099ff9bd50"}}}}