Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-21957][SQL][FOLLOWUP] Support CURRENT_USER without tailing parentheses #32770

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ valueExpression

primaryExpression
: name=(CURRENT_DATE | CURRENT_TIMESTAMP) #currentDatetime
| CURRENT_USER #currentUser
yaooqinn marked this conversation as resolved.
Show resolved Hide resolved
| CASE whenClause+ (ELSE elseExpression=expression)? END #searchedCase
| CASE value=expression whenClause+ (ELSE elseExpression=expression)? END #simpleCase
| name=(CAST | TRY_CAST) '(' expression AS dataType ')' #cast
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ class Analyzer(override val catalogManager: CatalogManager)
private val literalFunctions: Seq[(String, () => Expression, Expression => String)] = Seq(
(CurrentDate().prettyName, () => CurrentDate(), toPrettySQL(_)),
(CurrentTimestamp().prettyName, () => CurrentTimestamp(), toPrettySQL(_)),
(CurrentUser().prettyName, () => CurrentUser(), toPrettySQL),
(VirtualColumn.hiveGroupingIdName, () => GroupingID(Nil), _ => VirtualColumn.hiveGroupingIdName)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,9 @@ class AstBuilder extends SqlBaseBaseVisitor[AnyRef] with SQLConfHelper with Logg
}
}

override def visitCurrentUser(ctx: CurrentUserContext): Expression = withOrigin(ctx) {
if (conf.ansiEnabled) CurrentUser() else UnresolvedAttribute.quoted(ctx.CURRENT_USER().getText)
}
/**
* Create a [[Cast]] expression.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.hive.thriftserver
import java.sql.SQLException
import java.util.concurrent.atomic.AtomicBoolean

import org.apache.hive.service.cli.HiveSQLException
import org.apache.hive.service.cli.{HiveSQLException, OperationHandle}

import org.apache.spark.TaskKilled
import org.apache.spark.scheduler.{SparkListener, SparkListenerTaskEnd}
Expand Down Expand Up @@ -125,9 +125,23 @@ trait ThriftServerWithSparkContextSuite extends SharedThriftServer {
withCLIServiceClient(clientUser) { client =>
val sessionHandle = client.openSession(clientUser, "")
val confOverlay = new java.util.HashMap[java.lang.String, java.lang.String]
val opHandle = client.executeStatement(sessionHandle, sql, confOverlay)
val rowSet = client.fetchResults(opHandle)
assert(rowSet.toTRowSet.getColumns.get(0).getStringVal.getValues.get(0) === clientUser)
val exec: String => OperationHandle = client.executeStatement(sessionHandle, _, confOverlay)

exec(s"set ${SQLConf.ANSI_ENABLED.key}=false")
cloud-fan marked this conversation as resolved.
Show resolved Hide resolved

val opHandle1 = exec("select current_user(), current_user")
val rowSet1 = client.fetchResults(opHandle1)
rowSet1.toTRowSet.getColumns.forEach { col =>
assert(col.getStringVal.getValues.get(0) === clientUser)
}

exec(s"set ${SQLConf.ANSI_ENABLED.key}=true")
val opHandle2 = exec("select current_user")
assert(client.fetchResults(opHandle2).toTRowSet.getColumns.get(0)
.getStringVal.getValues.get(0) === clientUser)

val e = intercept[HiveSQLException](exec("select current_user()"))
assert(e.getMessage.contains("current_user"))
}
}
}
Expand Down