You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/04/27 10:22:02 UTC

[GitHub] [hive] simhadri-g opened a new pull request #997: Hive 23301

simhadri-g opened a new pull request #997:
URL: https://github.com/apache/hive/pull/997


   UDF to map bits to privileges.


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] maheshk114 commented on a change in pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
maheshk114 commented on a change in pull request #997:
URL: https://github.com/apache/hive/pull/997#discussion_r417811126



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFStringToPrivilege.java
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * UDFSplitMapPrivs.
+ *
+ */
+
+@Description(name = "split_map_privs", value = "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+    + "regex", extended = "Example:\n" + "  > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;\n"
+    + "  [\"UPDATE\", \"CREATE\", \"ALTER\", \"INDEX\"]") class PrivilegeMap {
+  private Map<Integer, String> privilegeMap = new HashMap<Integer, String>();
+
+  Map<Integer, String> getPrivilegeMap() {
+
+    privilegeMap.put(0, "SELECT");
+    privilegeMap.put(1, "UPDATE");
+    privilegeMap.put(2, "CREATE");
+    privilegeMap.put(3, "DROP");
+    privilegeMap.put(4, "ALTER");
+    privilegeMap.put(5, "INDEX");
+    privilegeMap.put(6, "LOCK");
+    privilegeMap.put(7, "READ");
+    privilegeMap.put(8, "WRITE");
+    privilegeMap.put(9, "ALL");
+
+    return privilegeMap;
+  }
+}
+
+/**
+ * UDFSplitMapPrivs.
+ * "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+ *      "Example: > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;"
+ *     output: "  ["UPDATE", "CREATE", "ALTER", "INDEX"]"
+ */
+public class GenericUDFStringToPrivilege extends GenericUDF {
+  private transient ObjectInspectorConverters.Converter[] converters;
+  private transient Pattern constPattern;
+
+  private PrivilegeMap privsMap = new PrivilegeMap();
+
+  @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
+    if (arguments.length != 2) {

Review comment:
       You can refer public class UDFYear extends GenericUDF { and use the same validation. like checkArgsSize(arguments, 1, 1);
       checkArgPrimitive(arguments, 0);

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFStringToPrivilege.java
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * UDFSplitMapPrivs.
+ *
+ */
+
+@Description(name = "split_map_privs", value = "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+    + "regex", extended = "Example:\n" + "  > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;\n"
+    + "  [\"UPDATE\", \"CREATE\", \"ALTER\", \"INDEX\"]") class PrivilegeMap {
+  private Map<Integer, String> privilegeMap = new HashMap<Integer, String>();
+
+  Map<Integer, String> getPrivilegeMap() {
+
+    privilegeMap.put(0, "SELECT");
+    privilegeMap.put(1, "UPDATE");
+    privilegeMap.put(2, "CREATE");
+    privilegeMap.put(3, "DROP");
+    privilegeMap.put(4, "ALTER");
+    privilegeMap.put(5, "INDEX");
+    privilegeMap.put(6, "LOCK");
+    privilegeMap.put(7, "READ");
+    privilegeMap.put(8, "WRITE");
+    privilegeMap.put(9, "ALL");
+
+    return privilegeMap;
+  }
+}
+
+/**
+ * UDFSplitMapPrivs.
+ * "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+ *      "Example: > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;"
+ *     output: "  ["UPDATE", "CREATE", "ALTER", "INDEX"]"
+ */
+public class GenericUDFStringToPrivilege extends GenericUDF {
+  private transient ObjectInspectorConverters.Converter[] converters;
+  private transient Pattern constPattern;
+
+  private PrivilegeMap privsMap = new PrivilegeMap();
+
+  @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
+    if (arguments.length != 2) {
+      throw new UDFArgumentLengthException("The function split_map_privs(s, ' ') takes exactly 2 arguments.");
+    }
+
+    converters = new ObjectInspectorConverters.Converter[arguments.length];
+    for (int i = 0; i < arguments.length; i++) {
+      converters[i] = ObjectInspectorConverters
+          .getConverter(arguments[i], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+    }
+
+    ObjectInspector rightArg = arguments[1];
+    if (rightArg instanceof ConstantObjectInspector) {
+      constPattern = Pattern.compile(((ConstantObjectInspector) rightArg).
+          getWritableConstantValue().toString());
+    }
+
+    return ObjectInspectorFactory
+        .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+  }
+
+  @Override public Object evaluate(DeferredObject[] arguments) throws HiveException {
+    assert (arguments.length == 2);

Review comment:
       if its a constant ..then user may not give the second arguemnt

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFStringToPrivilege.java
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * UDFSplitMapPrivs.
+ *
+ */
+
+@Description(name = "split_map_privs", value = "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+    + "regex", extended = "Example:\n" + "  > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;\n"
+    + "  [\"UPDATE\", \"CREATE\", \"ALTER\", \"INDEX\"]") class PrivilegeMap {
+  private Map<Integer, String> privilegeMap = new HashMap<Integer, String>();
+
+  Map<Integer, String> getPrivilegeMap() {
+
+    privilegeMap.put(0, "SELECT");
+    privilegeMap.put(1, "UPDATE");
+    privilegeMap.put(2, "CREATE");
+    privilegeMap.put(3, "DROP");
+    privilegeMap.put(4, "ALTER");
+    privilegeMap.put(5, "INDEX");
+    privilegeMap.put(6, "LOCK");
+    privilegeMap.put(7, "READ");
+    privilegeMap.put(8, "WRITE");
+    privilegeMap.put(9, "ALL");
+
+    return privilegeMap;
+  }
+}
+
+/**
+ * UDFSplitMapPrivs.
+ * "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+ *      "Example: > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;"
+ *     output: "  ["UPDATE", "CREATE", "ALTER", "INDEX"]"
+ */
+public class GenericUDFStringToPrivilege extends GenericUDF {
+  private transient ObjectInspectorConverters.Converter[] converters;
+  private transient Pattern constPattern;
+
+  private PrivilegeMap privsMap = new PrivilegeMap();
+
+  @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
+    if (arguments.length != 2) {
+      throw new UDFArgumentLengthException("The function split_map_privs(s, ' ') takes exactly 2 arguments.");
+    }
+
+    converters = new ObjectInspectorConverters.Converter[arguments.length];
+    for (int i = 0; i < arguments.length; i++) {
+      converters[i] = ObjectInspectorConverters
+          .getConverter(arguments[i], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+    }
+
+    ObjectInspector rightArg = arguments[1];
+    if (rightArg instanceof ConstantObjectInspector) {
+      constPattern = Pattern.compile(((ConstantObjectInspector) rightArg).
+          getWritableConstantValue().toString());
+    }
+
+    return ObjectInspectorFactory
+        .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+  }
+
+  @Override public Object evaluate(DeferredObject[] arguments) throws HiveException {
+    assert (arguments.length == 2);
+
+    if (arguments[0].get() == null || arguments[1].get() == null) {
+      return null;
+    }
+
+    Text s = (Text) converters[0].convert(arguments[0].get());

Review comment:
       The converter is based on user argument to initialize ..and here the conversion is always to text ..that may cause issue 

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFStringToPrivilege.java
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * UDFSplitMapPrivs.
+ *
+ */
+
+@Description(name = "split_map_privs", value = "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+    + "regex", extended = "Example:\n" + "  > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;\n"
+    + "  [\"UPDATE\", \"CREATE\", \"ALTER\", \"INDEX\"]") class PrivilegeMap {
+  private Map<Integer, String> privilegeMap = new HashMap<Integer, String>();
+
+  Map<Integer, String> getPrivilegeMap() {
+
+    privilegeMap.put(0, "SELECT");
+    privilegeMap.put(1, "UPDATE");
+    privilegeMap.put(2, "CREATE");
+    privilegeMap.put(3, "DROP");
+    privilegeMap.put(4, "ALTER");
+    privilegeMap.put(5, "INDEX");
+    privilegeMap.put(6, "LOCK");
+    privilegeMap.put(7, "READ");
+    privilegeMap.put(8, "WRITE");
+    privilegeMap.put(9, "ALL");
+
+    return privilegeMap;
+  }
+}
+
+/**
+ * UDFSplitMapPrivs.
+ * "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+ *      "Example: > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;"

Review comment:
       i think ..make it simple ..no need to take any converter ...just accept string and split is using space ..

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFStringToPrivilege.java
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * UDFSplitMapPrivs.
+ *
+ */
+
+@Description(name = "split_map_privs", value = "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+    + "regex", extended = "Example:\n" + "  > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;\n"
+    + "  [\"UPDATE\", \"CREATE\", \"ALTER\", \"INDEX\"]") class PrivilegeMap {
+  private Map<Integer, String> privilegeMap = new HashMap<Integer, String>();
+
+  Map<Integer, String> getPrivilegeMap() {
+
+    privilegeMap.put(0, "SELECT");
+    privilegeMap.put(1, "UPDATE");
+    privilegeMap.put(2, "CREATE");
+    privilegeMap.put(3, "DROP");
+    privilegeMap.put(4, "ALTER");
+    privilegeMap.put(5, "INDEX");
+    privilegeMap.put(6, "LOCK");
+    privilegeMap.put(7, "READ");
+    privilegeMap.put(8, "WRITE");
+    privilegeMap.put(9, "ALL");
+
+    return privilegeMap;
+  }
+}
+
+/**
+ * UDFSplitMapPrivs.
+ * "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+ *      "Example: > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;"
+ *     output: "  ["UPDATE", "CREATE", "ALTER", "INDEX"]"
+ */
+public class GenericUDFStringToPrivilege extends GenericUDF {
+  private transient ObjectInspectorConverters.Converter[] converters;
+  private transient Pattern constPattern;
+
+  private PrivilegeMap privsMap = new PrivilegeMap();
+
+  @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
+    if (arguments.length != 2) {
+      throw new UDFArgumentLengthException("The function split_map_privs(s, ' ') takes exactly 2 arguments.");
+    }
+
+    converters = new ObjectInspectorConverters.Converter[arguments.length];
+    for (int i = 0; i < arguments.length; i++) {
+      converters[i] = ObjectInspectorConverters
+          .getConverter(arguments[i], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+    }
+
+    ObjectInspector rightArg = arguments[1];
+    if (rightArg instanceof ConstantObjectInspector) {
+      constPattern = Pattern.compile(((ConstantObjectInspector) rightArg).
+          getWritableConstantValue().toString());
+    }
+
+    return ObjectInspectorFactory
+        .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+  }
+
+  @Override public Object evaluate(DeferredObject[] arguments) throws HiveException {
+    assert (arguments.length == 2);
+
+    if (arguments[0].get() == null || arguments[1].get() == null) {
+      return null;
+    }
+
+    Text s = (Text) converters[0].convert(arguments[0].get());
+    ArrayList<Text> result = new ArrayList<Text>();
+    int index = 0;
+    Map<Integer, String> privs = privsMap.getPrivilegeMap();
+
+    if (constPattern == null) {
+      Text regex = (Text) converters[1].convert(arguments[1].get());
+      for (String str : s.toString().split(regex.toString(), -1)) {

Review comment:
       if else can be combined to one ..after initializing the pattern before loop

##########
File path: itests/src/test/resources/testconfiguration.properties
##########
@@ -1698,6 +1698,7 @@ minillaplocal.query.files=\
   smblimit.q,\
   specialChar.q,\
   split.q,\
+  split_map_privs.q,\

Review comment:
       GenericUDFSplit can be re-used ..i think this udf is not required  

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFStringToPrivilege.java
##########
@@ -0,0 +1,134 @@
+/*
+ * 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.serde2.objectinspector.ConstantObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+
+/**
+ * UDFSplitMapPrivs.
+ *
+ */
+
+@Description(name = "split_map_privs", value = "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+    + "regex", extended = "Example:\n" + "  > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;\n"
+    + "  [\"UPDATE\", \"CREATE\", \"ALTER\", \"INDEX\"]") class PrivilegeMap {
+  private Map<Integer, String> privilegeMap = new HashMap<Integer, String>();
+
+  Map<Integer, String> getPrivilegeMap() {
+
+    privilegeMap.put(0, "SELECT");
+    privilegeMap.put(1, "UPDATE");
+    privilegeMap.put(2, "CREATE");
+    privilegeMap.put(3, "DROP");
+    privilegeMap.put(4, "ALTER");
+    privilegeMap.put(5, "INDEX");
+    privilegeMap.put(6, "LOCK");
+    privilegeMap.put(7, "READ");
+    privilegeMap.put(8, "WRITE");
+    privilegeMap.put(9, "ALL");
+
+    return privilegeMap;
+  }
+}
+
+/**
+ * UDFSplitMapPrivs.
+ * "_FUNC_(str, regex) - Splits binary str and maps to privilege type "
+ *      "Example: > SELECT _FUNC_('0 1 1 0 1 1 0 0 0', ' ') FROM src LIMIT 1;"
+ *     output: "  ["UPDATE", "CREATE", "ALTER", "INDEX"]"
+ */
+public class GenericUDFStringToPrivilege extends GenericUDF {
+  private transient ObjectInspectorConverters.Converter[] converters;
+  private transient Pattern constPattern;
+
+  private PrivilegeMap privsMap = new PrivilegeMap();
+
+  @Override public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
+    if (arguments.length != 2) {
+      throw new UDFArgumentLengthException("The function split_map_privs(s, ' ') takes exactly 2 arguments.");
+    }
+
+    converters = new ObjectInspectorConverters.Converter[arguments.length];
+    for (int i = 0; i < arguments.length; i++) {
+      converters[i] = ObjectInspectorConverters
+          .getConverter(arguments[i], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+    }
+
+    ObjectInspector rightArg = arguments[1];
+    if (rightArg instanceof ConstantObjectInspector) {
+      constPattern = Pattern.compile(((ConstantObjectInspector) rightArg).
+          getWritableConstantValue().toString());
+    }
+
+    return ObjectInspectorFactory
+        .getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableStringObjectInspector);
+  }
+
+  @Override public Object evaluate(DeferredObject[] arguments) throws HiveException {
+    assert (arguments.length == 2);
+
+    if (arguments[0].get() == null || arguments[1].get() == null) {
+      return null;
+    }
+
+    Text s = (Text) converters[0].convert(arguments[0].get());
+    ArrayList<Text> result = new ArrayList<Text>();
+    int index = 0;
+    Map<Integer, String> privs = privsMap.getPrivilegeMap();
+
+    if (constPattern == null) {
+      Text regex = (Text) converters[1].convert(arguments[1].get());
+      for (String str : s.toString().split(regex.toString(), -1)) {
+        if ("1".equals(str)) {
+          result.add(new Text(privs.get(index)));
+        }
+        index++;
+      }
+    } else {
+      for (String str : constPattern.split(s.toString(), -1)) {
+        if ("1".equals(str)) {

Review comment:
       should check for validity like getting more that 10 splits




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr closed pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
belugabehr closed pull request #997:
URL: https://github.com/apache/hive/pull/997


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] aasha commented on a change in pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
aasha commented on a change in pull request #997:
URL: https://github.com/apache/hive/pull/997#discussion_r415695373



##########
File path: .gitignore
##########
@@ -38,3 +38,4 @@ standalone-metastore/metastore-server/src/gen/version
 launch.json
 settings.json
 kafka-handler/src/test/gen
+

Review comment:
       unused can be removed




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] simhadri-g commented on a change in pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
simhadri-g commented on a change in pull request #997:
URL: https://github.com/apache/hive/pull/997#discussion_r415799079



##########
File path: .gitignore
##########
@@ -38,3 +38,4 @@ standalone-metastore/metastore-server/src/gen/version
 launch.json
 settings.json
 kafka-handler/src/test/gen
+

Review comment:
       > Is the empty udf_split_map_privs.q.out file needed?
   no, it is generated while building
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] belugabehr commented on pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
belugabehr commented on pull request #997:
URL: https://github.com/apache/hive/pull/997#issuecomment-642801183


   Marked as resolved in JIRA.  Please reopen if required.
   
   https://issues.apache.org/jira/browse/HIVE-23301


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] aasha commented on a change in pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
aasha commented on a change in pull request #997:
URL: https://github.com/apache/hive/pull/997#discussion_r415699750



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java
##########
@@ -285,6 +285,7 @@
     system.registerGenericUDF("quote", GenericUDFQuote.class);
     system.registerGenericUDF("nvl", GenericUDFCoalesce.class); //HIVE-20961
     system.registerGenericUDF("split", GenericUDFSplit.class);
+    system.registerGenericUDF("split_map_privs", UDFSplitMapPrivs.class);

Review comment:
       Rename to GenericUDFStringToPrivilege




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] simhadri-g commented on a change in pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
simhadri-g commented on a change in pull request #997:
URL: https://github.com/apache/hive/pull/997#discussion_r415799412



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java
##########
@@ -285,6 +285,7 @@
     system.registerGenericUDF("quote", GenericUDFQuote.class);
     system.registerGenericUDF("nvl", GenericUDFCoalesce.class); //HIVE-20961
     system.registerGenericUDF("split", GenericUDFSplit.class);
+    system.registerGenericUDF("split_map_privs", UDFSplitMapPrivs.class);

Review comment:
       ok
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org


[GitHub] [hive] aasha commented on a change in pull request #997: Hive 23301

Posted by GitBox <gi...@apache.org>.
aasha commented on a change in pull request #997:
URL: https://github.com/apache/hive/pull/997#discussion_r415696099



##########
File path: .gitignore
##########
@@ -38,3 +38,4 @@ standalone-metastore/metastore-server/src/gen/version
 launch.json
 settings.json
 kafka-handler/src/test/gen
+

Review comment:
       Is the empty udf_split_map_privs.q.out file needed?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscribe@hive.apache.org
For additional commands, e-mail: gitbox-help@hive.apache.org