Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions python/pyspark/sql/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,15 @@ def hash(*cols):
return Column(jc)


@since(2.3)
def current_user():
"""
Returns current user executing the query as a :class:`StringType` column column.
"""
sc = SparkContext._active_spark_context
return Column(sc._jvm.functions.current_user())


# ---------------------- String/Binary functions ------------------------------

_string_functions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ object FunctionRegistry {
expression[Sha1]("sha"),
expression[Sha1]("sha1"),
expression[Sha2]("sha2"),
expression[CurrentUser]("current_user"),
expression[SparkPartitionID]("spark_partition_id"),
expression[InputFileName]("input_file_name"),
expression[InputFileBlockStart]("input_file_block_start"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*

@wangyum wangyum Sep 11, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* 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.SparkContext
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.codegen.CodegenFallback
import org.apache.spark.sql.types.{DataType, StringType}

/**
* Return the user executing the current query.
*
* There is no code generation since this expression should get constant folded by the optimizer.
*/
@ExpressionDescription(
usage = """
_FUNC_() - Returns the current user executing the query.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think Spark has something like SESSION_USER. Thus, this impl is wrong.

""",
since = "2.3.0")
case class CurrentUser() extends LeafExpression with CodegenFallback {
override def foldable: Boolean = true
override def nullable: Boolean = false
override def dataType: DataType = StringType

/** Returns the result of evaluating this expression on a given input Row */
override def eval(input: InternalRow): Any = {
SparkContext.getActive.map(_.sparkUser).getOrElse("")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is from Spark Context. All the sessions share the same user names. I am afraid this value is misleading to our end users. cc @rxin

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually the user which executes the query. I agree that may be misleading that a user can connect to STS with his credentials and then the queries are run with a superuser, but this is how Spark works at the moment and the user which actually runs the query is the superuser. If we don't think to STS, in the other use cases the user which runs a query is the user who started the session.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I do not think we should support it until we provide session-specific user management.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks. Then is there any plan to support session-specific user management? If so, can I help somehow? Thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far, it is not in our plan.

}

override def prettyName: String = "current_user"

}
10 changes: 10 additions & 0 deletions sql/core/src/main/scala/org/apache/spark/sql/functions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,16 @@ object functions {
new Murmur3Hash(cols.map(_.expr))
}

/**
* Returns current user executing the query as a string column.
*
* @group misc_funcs
* @since 2.3.0
*/
def current_user(): Column = withExpr {
CurrentUser()
}

//////////////////////////////////////////////////////////////////////////////////////////////
// String functions
//////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ class DataFrameFunctionsSuite extends QueryTest with SharedSQLContext {
Row(2743272264L, 2180413220L))
}

test("misc current_user function") {
checkAnswer(
spark.sql("select current_user()"),
Row(spark.sparkContext.sparkUser)
)
}

test("string function find_in_set") {
val df = Seq(("abc,b,ab,c,def", "abc,b,ab,c,def")).toDF("a", "b")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.spark.sql

import org.apache.spark.sql.functions.current_user
import org.apache.spark.sql.test.SharedSQLContext

class MiscFunctionsSuite extends QueryTest with SharedSQLContext {
Expand All @@ -31,6 +32,13 @@ class MiscFunctionsSuite extends QueryTest with SharedSQLContext {
s"java_method('$className', 'method1', a, b)"),
Row("m1one", "m1one"))
}

test("current_user") {
val df = Seq(1, 2).toDF("a")
checkAnswer(
df.select(current_user()).limit(1),
Row(spark.sparkContext.sparkUser))
}
}

object ReflectClass {
Expand Down