You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by kh...@apache.org on 2016/10/06 00:18:47 UTC

[18/38] hive git commit: HIVE-14100: Adding a new logged_in_user() UDF which returns the user provided when connecting (Peter Vary, reviewed by Mohit Sabharwal)

HIVE-14100: Adding a new logged_in_user() UDF which returns the user provided when connecting (Peter Vary, reviewed by Mohit Sabharwal)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/45c1a09b
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/45c1a09b
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/45c1a09b

Branch: refs/heads/repl2
Commit: 45c1a09b7b76e41f05520de4bb0e26bb6fadc21f
Parents: 0562efc
Author: Mohit Sabharwal <mo...@cloudera.com>
Authored: Fri Sep 30 13:54:31 2016 -0400
Committer: Mohit Sabharwal <mo...@cloudera.com>
Committed: Fri Sep 30 13:57:10 2016 -0400

----------------------------------------------------------------------
 .../hadoop/hive/ql/exec/FunctionRegistry.java   |  1 +
 .../ql/udf/generic/GenericUDFLoggedInUser.java  | 82 ++++++++++++++++++++
 .../queries/clientpositive/udf_logged_in_user.q |  5 ++
 .../results/clientpositive/show_functions.q.out |  5 ++
 .../clientpositive/udf_logged_in_user.q.out     | 22 ++++++
 5 files changed, 115 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/45c1a09b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java
index b277f5e..6870dfa 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java
@@ -344,6 +344,7 @@ public final class FunctionRegistry {
     system.registerGenericUDF("current_date", GenericUDFCurrentDate.class);
     system.registerGenericUDF("current_timestamp", GenericUDFCurrentTimestamp.class);
     system.registerGenericUDF("current_user", GenericUDFCurrentUser.class);
+    system.registerGenericUDF("logged_in_user", GenericUDFLoggedInUser.class);
 
     system.registerGenericUDF("isnull", GenericUDFOPNull.class);
     system.registerGenericUDF("isnotnull", GenericUDFOPNotNull.class);

http://git-wip-us.apache.org/repos/asf/hive/blob/45c1a09b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFLoggedInUser.java
----------------------------------------------------------------------
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFLoggedInUser.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFLoggedInUser.java
new file mode 100644
index 0000000..2915b86
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFLoggedInUser.java
@@ -0,0 +1,82 @@
+/**
+ * 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.hadoop.hive.ql.udf.generic;
+
+import org.apache.hadoop.hive.ql.exec.Description;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
+import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hadoop.hive.ql.udf.UDFType;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+
+@UDFType(deterministic = true)
+@Description(name = "logged_in_user", value = "_FUNC_() - Returns logged in user name",
+        extended = "SessionState GetUserName - the username provided at session initialization")
+@NDV(maxNdv = 1)
+public class GenericUDFLoggedInUser extends GenericUDF {
+  protected Text loggedInUser;
+
+  @Override
+  public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
+    if (arguments.length != 0) {
+      throw new UDFArgumentLengthException(
+          "The function LOGGED_IN_USER does not take any arguments, but found " + arguments.length);
+    }
+
+    if (loggedInUser == null) {
+      String loggedInUserName = SessionState.get().getUserName();
+      if (loggedInUserName != null) {
+        loggedInUser = new Text(loggedInUserName);
+      }
+    }
+
+    return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
+  }
+
+  @Override
+  public Object evaluate(DeferredObject[] arguments) throws HiveException {
+    return loggedInUser;
+  }
+
+  public Text getLoggedInUser() {
+    return loggedInUser;
+  }
+
+  public void setLoggedInUser(Text loggedInUser) {
+    this.loggedInUser = loggedInUser;
+  }
+
+  @Override
+  public String getDisplayString(String[] children) {
+    return "LOGGED_IN_USER()";
+  }
+
+  @Override
+  public void copyToNewInstance(Object newInstance) throws UDFArgumentException {
+    super.copyToNewInstance(newInstance);
+    // Need to preserve loggedInUser
+    GenericUDFLoggedInUser other = (GenericUDFLoggedInUser) newInstance;
+    if (this.loggedInUser != null) {
+      other.loggedInUser = new Text(this.loggedInUser);
+    }
+  }
+}

http://git-wip-us.apache.org/repos/asf/hive/blob/45c1a09b/ql/src/test/queries/clientpositive/udf_logged_in_user.q
----------------------------------------------------------------------
diff --git a/ql/src/test/queries/clientpositive/udf_logged_in_user.q b/ql/src/test/queries/clientpositive/udf_logged_in_user.q
new file mode 100644
index 0000000..4814c72
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/udf_logged_in_user.q
@@ -0,0 +1,5 @@
+DESCRIBE FUNCTION logged_in_user;
+DESCRIBE FUNCTION EXTENDED logged_in_user;
+
+select logged_in_user()
+FROM src tablesample (1 rows);

http://git-wip-us.apache.org/repos/asf/hive/blob/45c1a09b/ql/src/test/results/clientpositive/show_functions.q.out
----------------------------------------------------------------------
diff --git a/ql/src/test/results/clientpositive/show_functions.q.out b/ql/src/test/results/clientpositive/show_functions.q.out
index 18c7b0e..4a40094 100644
--- a/ql/src/test/results/clientpositive/show_functions.q.out
+++ b/ql/src/test/results/clientpositive/show_functions.q.out
@@ -130,6 +130,7 @@ locate
 log
 log10
 log2
+logged_in_user
 lower
 lpad
 ltrim
@@ -328,6 +329,7 @@ POSTHOOK: type: SHOWFUNCTIONS
 log
 log10
 log2
+logged_in_user
 PREHOOK: query: SHOW FUNCTIONS '.*date.*'
 PREHOOK: type: SHOWFUNCTIONS
 POSTHOOK: query: SHOW FUNCTIONS '.*date.*'
@@ -408,6 +410,7 @@ POSTHOOK: type: SHOWFUNCTIONS
 log
 log10
 log2
+logged_in_user
 PREHOOK: query: SHOW FUNCTIONS LIKE "log*"
 PREHOOK: type: SHOWFUNCTIONS
 POSTHOOK: query: SHOW FUNCTIONS LIKE "log*"
@@ -415,6 +418,7 @@ POSTHOOK: type: SHOWFUNCTIONS
 log
 log10
 log2
+logged_in_user
 PREHOOK: query: SHOW FUNCTIONS LIKE `log*`
 PREHOOK: type: SHOWFUNCTIONS
 POSTHOOK: query: SHOW FUNCTIONS LIKE `log*`
@@ -422,3 +426,4 @@ POSTHOOK: type: SHOWFUNCTIONS
 log
 log10
 log2
+logged_in_user

http://git-wip-us.apache.org/repos/asf/hive/blob/45c1a09b/ql/src/test/results/clientpositive/udf_logged_in_user.q.out
----------------------------------------------------------------------
diff --git a/ql/src/test/results/clientpositive/udf_logged_in_user.q.out b/ql/src/test/results/clientpositive/udf_logged_in_user.q.out
new file mode 100644
index 0000000..ffb19ca
--- /dev/null
+++ b/ql/src/test/results/clientpositive/udf_logged_in_user.q.out
@@ -0,0 +1,22 @@
+PREHOOK: query: DESCRIBE FUNCTION logged_in_user
+PREHOOK: type: DESCFUNCTION
+POSTHOOK: query: DESCRIBE FUNCTION logged_in_user
+POSTHOOK: type: DESCFUNCTION
+logged_in_user() - Returns logged in user name
+PREHOOK: query: DESCRIBE FUNCTION EXTENDED logged_in_user
+PREHOOK: type: DESCFUNCTION
+POSTHOOK: query: DESCRIBE FUNCTION EXTENDED logged_in_user
+POSTHOOK: type: DESCFUNCTION
+logged_in_user() - Returns logged in user name
+SessionState GetUserName - the username provided at session initialization
+PREHOOK: query: select logged_in_user()
+FROM src tablesample (1 rows)
+PREHOOK: type: QUERY
+PREHOOK: Input: default@src
+#### A masked pattern was here ####
+POSTHOOK: query: select logged_in_user()
+FROM src tablesample (1 rows)
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@src
+#### A masked pattern was here ####
+NULL