You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by petermaxlee <gi...@git.apache.org> on 2016/07/11 18:05:15 UTC

[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

GitHub user petermaxlee opened a pull request:

    https://github.com/apache/spark/pull/14138

    [SPARK-16284][SQL] Implement reflect SQL function

    ## What changes were proposed in this pull request?
    This patch implements reflect SQL function, which can be used to invoke a Java method in SQL. Slightly different from Hive, this implementation requires the class name and the method name to be literals. This implementation also supports only a smaller number of data types, and requires the function to be static, as suggested by @rxin in #13969.
    
    java_method is an alias for reflect, so this should also resolve SPARK-16277.
    
    ## How was this patch tested?
    Added expression unit tests and an end-to-end test.


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/petermaxlee/spark reflect-static

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/14138.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #14138
    
----
commit 4c9b975536877a4649706ef5e4e87a48a292668a
Author: petermaxlee <pe...@gmail.com>
Date:   2016-07-08T16:57:16Z

    Rebase

commit 3fec1610c855b7595489ec3a635f0987dbb4701b
Author: petermaxlee <pe...@gmail.com>
Date:   2016-07-07T06:50:46Z

    code review

commit 1c7bcbe4e3ab686bb581d5abe177ca1d1c66698d
Author: petermaxlee <pe...@gmail.com>
Date:   2016-07-08T18:19:58Z

    code review

commit d24ca97a0ac85b54ea64b017343fe9eb51ecc02e
Author: petermaxlee <pe...@gmail.com>
Date:   2016-07-08T21:40:55Z

    Merge remote-tracking branch 'apache/master' into reflect

commit 0ccade2485c7bb4bc767aab3f46e9f9e76eb2db8
Author: petermaxlee <pe...@gmail.com>
Date:   2016-07-11T17:50:12Z

    Merge remote-tracking branch 'apache/master' into reflect-static

commit 48172d1be903ab5760aec2e76c69dd17166c9660
Author: petermaxlee <pe...@gmail.com>
Date:   2016-07-11T18:03:56Z

    Handle only static functions.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62107 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62107/consoleFull)** for PR 14138 at commit [`48172d1`](https://github.com/apache/spark/commit/48172d1be903ab5760aec2e76c69dd17166c9660).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70383800
  
    --- Diff: sql/core/src/test/scala/org/apache/spark/sql/MiscFunctionsSuite.scala ---
    @@ -0,0 +1,35 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql
    +
    +import org.apache.spark.sql.test.SharedSQLContext
    +
    +class MiscFunctionsSuite extends QueryTest with SharedSQLContext {
    +  import testImplicits._
    +
    +  test("reflect and java_method") {
    +    val df = Seq((1, "one")).toDF("a", "b")
    +    checkAnswer(
    +      df.selectExpr("reflect('org.apache.spark.sql.ReflectClass', 'method1', a, b)"),
    --- End diff --
    
    we should use `classOf[ReflectClass]...` instead of hardcode the class name. And also test `java_method` to match the test name.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/spark/pull/14138


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70366108
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala ---
    @@ -0,0 +1,166 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import java.lang.reflect.{Method, Modifier}
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
    +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +import org.apache.spark.util.Utils
    +
    +/**
    + * An expression that invokes a method on a class via reflection.
    + *
    + * For now, only types defined in `Reflect.typeMapping` are supported (basically primitives
    + * and string) as input types, and the output is turned automatically to a string.
    + *
    + * Note that unlike Hive's reflect function, this expression calls only static methods
    + * (i.e. does not support calling non-static methods).
    + *
    + * We should also look into how to consolidate this expression with
    + * [[org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke]] in the future.
    + *
    + * @param children the first element should be a literal string for the class name,
    + *                 and the second element should be a literal string for the method name,
    + *                 and the remaining are input arguments to the Java method.
    + */
    +// scalastyle:off line.size.limit
    +@ExpressionDescription(
    +  usage = "_FUNC_(class,method[,arg1[,arg2..]]) calls method with reflection",
    +  extended = "> SELECT _FUNC_('java.util.UUID', 'randomUUID');\n c33fb387-8500-4bfa-81d2-6e0e3e930df2")
    +// scalastyle:on line.size.limit
    +case class CallMethodViaReflection(children: Seq[Expression])
    +  extends Expression with CodegenFallback {
    +
    +  override def prettyName: String = "reflect"
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (children.size < 2) {
    +      TypeCheckFailure("requires at least two arguments")
    +    } else if (!children.take(2).forall(e => e.dataType == StringType && e.foldable)) {
    +      // The first two arguments must be string type.
    +      TypeCheckFailure("first two arguments should be string literals")
    +    } else if (!classExists) {
    +      TypeCheckFailure(s"class $className not found")
    +    } else if (method == null) {
    +      TypeCheckFailure(s"cannot find a static method that matches the argument types in $className")
    +    } else {
    +      TypeCheckSuccess
    +    }
    +  }
    +
    +  override def deterministic: Boolean = false
    +  override def nullable: Boolean = true
    +  override val dataType: DataType = StringType
    +
    +  override def eval(input: InternalRow): Any = {
    +    var i = 0
    +    while (i < argExprs.length) {
    +      buffer(i) = argExprs(i).eval(input).asInstanceOf[Object]
    +      // Convert if necessary. Based on the types defined in typeMapping, string is the only
    +      // type that needs conversion. If we support timestamps, dates, decimals, arrays, or maps
    +      // in the future, proper conversion needs to happen here too.
    +      if (buffer(i).isInstanceOf[UTF8String]) {
    +        buffer(i) = buffer(i).toString
    +      }
    +      i += 1
    +    }
    +    val ret = method.invoke(null, buffer : _*)
    +    UTF8String.fromString(String.valueOf(ret))
    +  }
    +
    +  @transient private lazy val argExprs: Array[Expression] = children.drop(2).toArray
    +
    +  /** Name of the class -- this has to be called after we verify children has at least two exprs. */
    +  @transient private lazy val className = children(0).eval().asInstanceOf[UTF8String].toString
    --- End diff --
    
    can we just write something like this?
    ```
    @transient private lazy val cls: Class[_] = ... // null if not found
    
    @transient lazy val method: Method = ...
    ```
    This looks more straightforward and I don't think it's hard to test.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/62115/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62183 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62183/consoleFull)** for PR 14138 at commit [`b574c5d`](https://github.com/apache/spark/commit/b574c5d8abf93fcb34e7a2f88af14fdeb7d1bc66).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62183 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62183/consoleFull)** for PR 14138 at commit [`b574c5d`](https://github.com/apache/spark/commit/b574c5d8abf93fcb34e7a2f88af14fdeb7d1bc66).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by petermaxlee <gi...@git.apache.org>.
Github user petermaxlee commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70366868
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala ---
    @@ -0,0 +1,166 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import java.lang.reflect.{Method, Modifier}
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
    +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +import org.apache.spark.util.Utils
    +
    +/**
    + * An expression that invokes a method on a class via reflection.
    + *
    + * For now, only types defined in `Reflect.typeMapping` are supported (basically primitives
    + * and string) as input types, and the output is turned automatically to a string.
    + *
    + * Note that unlike Hive's reflect function, this expression calls only static methods
    + * (i.e. does not support calling non-static methods).
    + *
    + * We should also look into how to consolidate this expression with
    + * [[org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke]] in the future.
    + *
    + * @param children the first element should be a literal string for the class name,
    + *                 and the second element should be a literal string for the method name,
    + *                 and the remaining are input arguments to the Java method.
    + */
    +// scalastyle:off line.size.limit
    +@ExpressionDescription(
    +  usage = "_FUNC_(class,method[,arg1[,arg2..]]) calls method with reflection",
    +  extended = "> SELECT _FUNC_('java.util.UUID', 'randomUUID');\n c33fb387-8500-4bfa-81d2-6e0e3e930df2")
    +// scalastyle:on line.size.limit
    +case class CallMethodViaReflection(children: Seq[Expression])
    +  extends Expression with CodegenFallback {
    +
    +  override def prettyName: String = "reflect"
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (children.size < 2) {
    +      TypeCheckFailure("requires at least two arguments")
    +    } else if (!children.take(2).forall(e => e.dataType == StringType && e.foldable)) {
    +      // The first two arguments must be string type.
    +      TypeCheckFailure("first two arguments should be string literals")
    +    } else if (!classExists) {
    +      TypeCheckFailure(s"class $className not found")
    +    } else if (method == null) {
    +      TypeCheckFailure(s"cannot find a static method that matches the argument types in $className")
    +    } else {
    +      TypeCheckSuccess
    +    }
    +  }
    +
    +  override def deterministic: Boolean = false
    +  override def nullable: Boolean = true
    +  override val dataType: DataType = StringType
    +
    +  override def eval(input: InternalRow): Any = {
    +    var i = 0
    +    while (i < argExprs.length) {
    +      buffer(i) = argExprs(i).eval(input).asInstanceOf[Object]
    +      // Convert if necessary. Based on the types defined in typeMapping, string is the only
    +      // type that needs conversion. If we support timestamps, dates, decimals, arrays, or maps
    +      // in the future, proper conversion needs to happen here too.
    +      if (buffer(i).isInstanceOf[UTF8String]) {
    +        buffer(i) = buffer(i).toString
    +      }
    +      i += 1
    +    }
    +    val ret = method.invoke(null, buffer : _*)
    +    UTF8String.fromString(String.valueOf(ret))
    +  }
    +
    +  @transient private lazy val argExprs: Array[Expression] = children.drop(2).toArray
    +
    +  /** Name of the class -- this has to be called after we verify children has at least two exprs. */
    +  @transient private lazy val className = children(0).eval().asInstanceOf[UTF8String].toString
    --- End diff --
    
    Are you suggesting removing className? That would make error reporting harder.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/62133/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    thanks, merging to master and 2.0!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by petermaxlee <gi...@git.apache.org>.
Github user petermaxlee commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70366073
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala ---
    @@ -0,0 +1,166 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import java.lang.reflect.{Method, Modifier}
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
    +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +import org.apache.spark.util.Utils
    +
    +/**
    + * An expression that invokes a method on a class via reflection.
    + *
    + * For now, only types defined in `Reflect.typeMapping` are supported (basically primitives
    + * and string) as input types, and the output is turned automatically to a string.
    + *
    + * Note that unlike Hive's reflect function, this expression calls only static methods
    + * (i.e. does not support calling non-static methods).
    + *
    + * We should also look into how to consolidate this expression with
    + * [[org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke]] in the future.
    + *
    + * @param children the first element should be a literal string for the class name,
    + *                 and the second element should be a literal string for the method name,
    + *                 and the remaining are input arguments to the Java method.
    + */
    +// scalastyle:off line.size.limit
    +@ExpressionDescription(
    +  usage = "_FUNC_(class,method[,arg1[,arg2..]]) calls method with reflection",
    +  extended = "> SELECT _FUNC_('java.util.UUID', 'randomUUID');\n c33fb387-8500-4bfa-81d2-6e0e3e930df2")
    +// scalastyle:on line.size.limit
    +case class CallMethodViaReflection(children: Seq[Expression])
    +  extends Expression with CodegenFallback {
    +
    +  override def prettyName: String = "reflect"
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (children.size < 2) {
    +      TypeCheckFailure("requires at least two arguments")
    +    } else if (!children.take(2).forall(e => e.dataType == StringType && e.foldable)) {
    +      // The first two arguments must be string type.
    +      TypeCheckFailure("first two arguments should be string literals")
    +    } else if (!classExists) {
    +      TypeCheckFailure(s"class $className not found")
    +    } else if (method == null) {
    +      TypeCheckFailure(s"cannot find a static method that matches the argument types in $className")
    +    } else {
    +      TypeCheckSuccess
    +    }
    +  }
    +
    +  override def deterministic: Boolean = false
    +  override def nullable: Boolean = true
    +  override val dataType: DataType = StringType
    +
    +  override def eval(input: InternalRow): Any = {
    +    var i = 0
    +    while (i < argExprs.length) {
    +      buffer(i) = argExprs(i).eval(input).asInstanceOf[Object]
    +      // Convert if necessary. Based on the types defined in typeMapping, string is the only
    +      // type that needs conversion. If we support timestamps, dates, decimals, arrays, or maps
    +      // in the future, proper conversion needs to happen here too.
    +      if (buffer(i).isInstanceOf[UTF8String]) {
    +        buffer(i) = buffer(i).toString
    +      }
    +      i += 1
    +    }
    +    val ret = method.invoke(null, buffer : _*)
    +    UTF8String.fromString(String.valueOf(ret))
    +  }
    +
    +  @transient private lazy val argExprs: Array[Expression] = children.drop(2).toArray
    +
    +  /** Name of the class -- this has to be called after we verify children has at least two exprs. */
    +  @transient private lazy val className = children(0).eval().asInstanceOf[UTF8String].toString
    +
    +  /** True if the class exists and can be loaded. */
    +  @transient private lazy val classExists = CallMethodViaReflection.classExists(className)
    +
    +  /** The reflection method. */
    +  @transient lazy val method: Method = {
    +    val methodName = children(1).eval(null).asInstanceOf[UTF8String].toString
    +    CallMethodViaReflection.findMethod(className, methodName, argExprs.map(_.dataType)).orNull
    +  }
    +
    +  /** A temporary buffer used to hold intermediate results returned by children. */
    +  @transient private lazy val buffer = new Array[Object](argExprs.length)
    +}
    +
    +object CallMethodViaReflection {
    +  /** Mapping from Spark's type to acceptable JVM types. */
    +  val typeMapping = Map[DataType, Seq[Class[_]]](
    +    BooleanType -> Seq(classOf[java.lang.Boolean], classOf[Boolean]),
    +    ByteType -> Seq(classOf[java.lang.Byte], classOf[Byte]),
    +    ShortType -> Seq(classOf[java.lang.Short], classOf[Short]),
    +    IntegerType -> Seq(classOf[java.lang.Integer], classOf[Int]),
    +    LongType -> Seq(classOf[java.lang.Long], classOf[Long]),
    +    FloatType -> Seq(classOf[java.lang.Float], classOf[Float]),
    +    DoubleType -> Seq(classOf[java.lang.Double], classOf[Double]),
    +    StringType -> Seq(classOf[String])
    +  )
    +
    +  /**
    +   * Returns true if the class can be found and loaded.
    +   */
    +  private def classExists(className: String): Boolean = {
    +    try {
    +      Utils.classForName(className)
    +      true
    +    } catch {
    +      case e: ClassNotFoundException => false
    +    }
    +  }
    +
    +  /**
    +   * Finds a Java static method using reflection that matches the given argument types,
    +   * and whose return type is string.
    +   *
    +   * The types sequence must be the valid types defined in [[typeMapping]].
    +   *
    +   * This is made public for unit testing.
    +   */
    +  def findMethod(className: String, methodName: String, argTypes: Seq[DataType]): Option[Method] = {
    +    val clazz: Class[_] = Utils.classForName(className)
    +    clazz.getMethods.find { method =>
    +      val candidateTypes = method.getParameterTypes
    +      if (method.getName != methodName) {
    +        // Name must match
    +        false
    +      } else if (!Modifier.isStatic(method.getModifiers)) {
    +        // Method must be static
    +        false
    +      } else if (candidateTypes.length != argTypes.length) {
    +        // Argument length must match
    +        false
    +      } else {
    +        // Argument type must match. That is, either the method's argument type matches one of the
    +        // acceptable types defined in typeMapping, or it is a super type of the acceptable types.
    +        candidateTypes.zip(argTypes).forall { case (candidateType, argType) =>
    +          typeMapping(argType).exists { acceptableType =>
    +            acceptableType == candidateType || candidateType.isAssignableFrom(acceptableType)
    --- End diff --
    
    updated.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Merged build finished. Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by petermaxlee <gi...@git.apache.org>.
Github user petermaxlee commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70366065
  
    --- Diff: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflectionSuite.scala ---
    @@ -0,0 +1,102 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import org.apache.spark.SparkFunSuite
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure
    +import org.apache.spark.sql.types.{IntegerType, StringType}
    +
    +/** A static class for testing purpose. */
    +object ReflectStaticClass {
    +  def method1(): String = "m1"
    +  def method2(v1: Int): String = "m" + v1
    +  def method3(v1: java.lang.Integer): String = "m" + v1
    +  def method4(v1: Int, v2: String): String = "m" + v1 + v2
    +}
    +
    +/** A non-static class for testing purpose. */
    +class ReflectDynamicClass {
    --- End diff --
    
    This is used to make sure we report the correct error message.



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Test FAILed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/62107/
    Test FAILed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70365738
  
    --- Diff: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflectionSuite.scala ---
    @@ -0,0 +1,102 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import org.apache.spark.SparkFunSuite
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure
    +import org.apache.spark.sql.types.{IntegerType, StringType}
    +
    +/** A static class for testing purpose. */
    +object ReflectStaticClass {
    +  def method1(): String = "m1"
    +  def method2(v1: Int): String = "m" + v1
    +  def method3(v1: java.lang.Integer): String = "m" + v1
    +  def method4(v1: Int, v2: String): String = "m" + v1 + v2
    +}
    +
    +/** A non-static class for testing purpose. */
    +class ReflectDynamicClass {
    --- End diff --
    
    no need to test it?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70383507
  
    --- Diff: sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflectionSuite.scala ---
    @@ -0,0 +1,102 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import org.apache.spark.SparkFunSuite
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure
    +import org.apache.spark.sql.types.{IntegerType, StringType}
    +
    +/** A static class for testing purpose. */
    +object ReflectStaticClass {
    +  def method1(): String = "m1"
    +  def method2(v1: Int): String = "m" + v1
    +  def method3(v1: java.lang.Integer): String = "m" + v1
    +  def method4(v1: Int, v2: String): String = "m" + v1 + v2
    +}
    +
    +/** A non-static class for testing purpose. */
    +class ReflectDynamicClass {
    +  def method1(): String = "m1"
    +}
    +
    +/**
    + * Test suite for [[CallMethodViaReflection]] and its companion object.
    + */
    +class CallMethodViaReflectionSuite extends SparkFunSuite with ExpressionEvalHelper {
    +
    +  import CallMethodViaReflection._
    +
    +  // Get rid of the $ so we are getting the companion object's name.
    +  private val staticClassName = ReflectStaticClass.getClass.getName.replace("$", "")
    --- End diff --
    
    nit: we can use `stripSuffix("$")`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62115 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62115/consoleFull)** for PR 14138 at commit [`365efd1`](https://github.com/apache/spark/commit/365efd1983ab2784aa22d9d80de9707e280c5141).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Test PASSed.
    Refer to this link for build results (access rights to CI server needed): 
    https://amplab.cs.berkeley.edu/jenkins//job/SparkPullRequestBuilder/62183/
    Test PASSed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62133 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62133/consoleFull)** for PR 14138 at commit [`ccfb9c4`](https://github.com/apache/spark/commit/ccfb9c4511bdd6d9655ef5ef56fb387b9fab06dd).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62107 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62107/consoleFull)** for PR 14138 at commit [`48172d1`](https://github.com/apache/spark/commit/48172d1be903ab5760aec2e76c69dd17166c9660).
     * This patch **fails Spark unit tests**.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70365507
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala ---
    @@ -0,0 +1,166 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import java.lang.reflect.{Method, Modifier}
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
    +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +import org.apache.spark.util.Utils
    +
    +/**
    + * An expression that invokes a method on a class via reflection.
    + *
    + * For now, only types defined in `Reflect.typeMapping` are supported (basically primitives
    + * and string) as input types, and the output is turned automatically to a string.
    + *
    + * Note that unlike Hive's reflect function, this expression calls only static methods
    + * (i.e. does not support calling non-static methods).
    + *
    + * We should also look into how to consolidate this expression with
    + * [[org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke]] in the future.
    + *
    + * @param children the first element should be a literal string for the class name,
    + *                 and the second element should be a literal string for the method name,
    + *                 and the remaining are input arguments to the Java method.
    + */
    +// scalastyle:off line.size.limit
    +@ExpressionDescription(
    +  usage = "_FUNC_(class,method[,arg1[,arg2..]]) calls method with reflection",
    +  extended = "> SELECT _FUNC_('java.util.UUID', 'randomUUID');\n c33fb387-8500-4bfa-81d2-6e0e3e930df2")
    +// scalastyle:on line.size.limit
    +case class CallMethodViaReflection(children: Seq[Expression])
    +  extends Expression with CodegenFallback {
    +
    +  override def prettyName: String = "reflect"
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (children.size < 2) {
    +      TypeCheckFailure("requires at least two arguments")
    +    } else if (!children.take(2).forall(e => e.dataType == StringType && e.foldable)) {
    +      // The first two arguments must be string type.
    +      TypeCheckFailure("first two arguments should be string literals")
    +    } else if (!classExists) {
    +      TypeCheckFailure(s"class $className not found")
    +    } else if (method == null) {
    +      TypeCheckFailure(s"cannot find a static method that matches the argument types in $className")
    +    } else {
    +      TypeCheckSuccess
    +    }
    +  }
    +
    +  override def deterministic: Boolean = false
    +  override def nullable: Boolean = true
    +  override val dataType: DataType = StringType
    +
    +  override def eval(input: InternalRow): Any = {
    +    var i = 0
    +    while (i < argExprs.length) {
    +      buffer(i) = argExprs(i).eval(input).asInstanceOf[Object]
    +      // Convert if necessary. Based on the types defined in typeMapping, string is the only
    +      // type that needs conversion. If we support timestamps, dates, decimals, arrays, or maps
    +      // in the future, proper conversion needs to happen here too.
    +      if (buffer(i).isInstanceOf[UTF8String]) {
    +        buffer(i) = buffer(i).toString
    +      }
    +      i += 1
    +    }
    +    val ret = method.invoke(null, buffer : _*)
    +    UTF8String.fromString(String.valueOf(ret))
    +  }
    +
    +  @transient private lazy val argExprs: Array[Expression] = children.drop(2).toArray
    +
    +  /** Name of the class -- this has to be called after we verify children has at least two exprs. */
    +  @transient private lazy val className = children(0).eval().asInstanceOf[UTF8String].toString
    +
    +  /** True if the class exists and can be loaded. */
    +  @transient private lazy val classExists = CallMethodViaReflection.classExists(className)
    +
    +  /** The reflection method. */
    +  @transient lazy val method: Method = {
    +    val methodName = children(1).eval(null).asInstanceOf[UTF8String].toString
    +    CallMethodViaReflection.findMethod(className, methodName, argExprs.map(_.dataType)).orNull
    +  }
    +
    +  /** A temporary buffer used to hold intermediate results returned by children. */
    +  @transient private lazy val buffer = new Array[Object](argExprs.length)
    +}
    +
    +object CallMethodViaReflection {
    +  /** Mapping from Spark's type to acceptable JVM types. */
    +  val typeMapping = Map[DataType, Seq[Class[_]]](
    +    BooleanType -> Seq(classOf[java.lang.Boolean], classOf[Boolean]),
    +    ByteType -> Seq(classOf[java.lang.Byte], classOf[Byte]),
    +    ShortType -> Seq(classOf[java.lang.Short], classOf[Short]),
    +    IntegerType -> Seq(classOf[java.lang.Integer], classOf[Int]),
    +    LongType -> Seq(classOf[java.lang.Long], classOf[Long]),
    +    FloatType -> Seq(classOf[java.lang.Float], classOf[Float]),
    +    DoubleType -> Seq(classOf[java.lang.Double], classOf[Double]),
    +    StringType -> Seq(classOf[String])
    +  )
    +
    +  /**
    +   * Returns true if the class can be found and loaded.
    +   */
    +  private def classExists(className: String): Boolean = {
    +    try {
    +      Utils.classForName(className)
    +      true
    +    } catch {
    +      case e: ClassNotFoundException => false
    +    }
    +  }
    +
    +  /**
    +   * Finds a Java static method using reflection that matches the given argument types,
    +   * and whose return type is string.
    +   *
    +   * The types sequence must be the valid types defined in [[typeMapping]].
    +   *
    +   * This is made public for unit testing.
    +   */
    +  def findMethod(className: String, methodName: String, argTypes: Seq[DataType]): Option[Method] = {
    +    val clazz: Class[_] = Utils.classForName(className)
    +    clazz.getMethods.find { method =>
    +      val candidateTypes = method.getParameterTypes
    +      if (method.getName != methodName) {
    +        // Name must match
    +        false
    +      } else if (!Modifier.isStatic(method.getModifiers)) {
    +        // Method must be static
    +        false
    +      } else if (candidateTypes.length != argTypes.length) {
    +        // Argument length must match
    +        false
    +      } else {
    +        // Argument type must match. That is, either the method's argument type matches one of the
    +        // acceptable types defined in typeMapping, or it is a super type of the acceptable types.
    +        candidateTypes.zip(argTypes).forall { case (candidateType, argType) =>
    +          typeMapping(argType).exists { acceptableType =>
    +            acceptableType == candidateType || candidateType.isAssignableFrom(acceptableType)
    --- End diff --
    
    `candidateType.isAssignableFrom(acceptableType)` should already covers `acceptableType == candidateType`


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62115 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62115/consoleFull)** for PR 14138 at commit [`365efd1`](https://github.com/apache/spark/commit/365efd1983ab2784aa22d9d80de9707e280c5141).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by SparkQA <gi...@git.apache.org>.
Github user SparkQA commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    **[Test build #62133 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/62133/consoleFull)** for PR 14138 at commit [`ccfb9c4`](https://github.com/apache/spark/commit/ccfb9c4511bdd6d9655ef5ef56fb387b9fab06dd).
     * This patch passes all tests.
     * This patch merges cleanly.
     * This patch adds no public classes.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by AmplabJenkins <gi...@git.apache.org>.
Github user AmplabJenkins commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    Merged build finished. Test FAILed.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark issue #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on the issue:

    https://github.com/apache/spark/pull/14138
  
    LGTM except some style comments. Thanks for working on it!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


[GitHub] spark pull request #14138: [SPARK-16284][SQL] Implement reflect SQL function

Posted by cloud-fan <gi...@git.apache.org>.
Github user cloud-fan commented on a diff in the pull request:

    https://github.com/apache/spark/pull/14138#discussion_r70382966
  
    --- Diff: sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/CallMethodViaReflection.scala ---
    @@ -0,0 +1,166 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.sql.catalyst.expressions
    +
    +import java.lang.reflect.{Method, Modifier}
    +
    +import org.apache.spark.sql.catalyst.InternalRow
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult
    +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.{TypeCheckFailure, TypeCheckSuccess}
    +import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
    +import org.apache.spark.sql.types._
    +import org.apache.spark.unsafe.types.UTF8String
    +import org.apache.spark.util.Utils
    +
    +/**
    + * An expression that invokes a method on a class via reflection.
    + *
    + * For now, only types defined in `Reflect.typeMapping` are supported (basically primitives
    + * and string) as input types, and the output is turned automatically to a string.
    + *
    + * Note that unlike Hive's reflect function, this expression calls only static methods
    + * (i.e. does not support calling non-static methods).
    + *
    + * We should also look into how to consolidate this expression with
    + * [[org.apache.spark.sql.catalyst.expressions.objects.StaticInvoke]] in the future.
    + *
    + * @param children the first element should be a literal string for the class name,
    + *                 and the second element should be a literal string for the method name,
    + *                 and the remaining are input arguments to the Java method.
    + */
    +// scalastyle:off line.size.limit
    +@ExpressionDescription(
    +  usage = "_FUNC_(class,method[,arg1[,arg2..]]) calls method with reflection",
    +  extended = "> SELECT _FUNC_('java.util.UUID', 'randomUUID');\n c33fb387-8500-4bfa-81d2-6e0e3e930df2")
    +// scalastyle:on line.size.limit
    +case class CallMethodViaReflection(children: Seq[Expression])
    +  extends Expression with CodegenFallback {
    +
    +  override def prettyName: String = "reflect"
    +
    +  override def checkInputDataTypes(): TypeCheckResult = {
    +    if (children.size < 2) {
    +      TypeCheckFailure("requires at least two arguments")
    +    } else if (!children.take(2).forall(e => e.dataType == StringType && e.foldable)) {
    +      // The first two arguments must be string type.
    +      TypeCheckFailure("first two arguments should be string literals")
    +    } else if (!classExists) {
    +      TypeCheckFailure(s"class $className not found")
    +    } else if (method == null) {
    +      TypeCheckFailure(s"cannot find a static method that matches the argument types in $className")
    +    } else {
    +      TypeCheckSuccess
    +    }
    +  }
    +
    +  override def deterministic: Boolean = false
    +  override def nullable: Boolean = true
    +  override val dataType: DataType = StringType
    +
    +  override def eval(input: InternalRow): Any = {
    +    var i = 0
    +    while (i < argExprs.length) {
    +      buffer(i) = argExprs(i).eval(input).asInstanceOf[Object]
    +      // Convert if necessary. Based on the types defined in typeMapping, string is the only
    +      // type that needs conversion. If we support timestamps, dates, decimals, arrays, or maps
    +      // in the future, proper conversion needs to happen here too.
    +      if (buffer(i).isInstanceOf[UTF8String]) {
    +        buffer(i) = buffer(i).toString
    +      }
    +      i += 1
    +    }
    +    val ret = method.invoke(null, buffer : _*)
    +    UTF8String.fromString(String.valueOf(ret))
    +  }
    +
    +  @transient private lazy val argExprs: Array[Expression] = children.drop(2).toArray
    +
    +  /** Name of the class -- this has to be called after we verify children has at least two exprs. */
    +  @transient private lazy val className = children(0).eval().asInstanceOf[UTF8String].toString
    --- End diff --
    
    ok let's keep it


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org