This is an automated email from the ASF dual-hosted git repository.
ayushsaxena pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git
The following commit(s) were added to refs/heads/master by this push:
new 39c3861d666 HIVE-27563: Add typeof UDF (#4547). (John Sherman reviewed
by Ayush Saxena, okumin)
39c3861d666 is described below
commit 39c3861d66677388c94d34d56c889fabb14e04a3
Author: John Sherman <[email protected]>
AuthorDate: Mon Aug 7 08:18:01 2023 -0700
HIVE-27563: Add typeof UDF (#4547). (John Sherman reviewed by Ayush Saxena,
okumin)
---
.../hadoop/hive/ql/exec/FunctionRegistry.java | 1 +
.../hive/ql/udf/generic/GenericUDFTypeOf.java | 53 +++++++++
.../hive/ql/udf/generic/TestGenericUDFTypeOf.java | 132 +++++++++++++++++++++
ql/src/test/queries/clientpositive/udf_typeof.q | 7 ++
.../clientpositive/llap/show_functions.q.out | 2 +
.../results/clientpositive/llap/udf_typeof.q.out | 49 ++++++++
6 files changed, 244 insertions(+)
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 bed44434cb8..b7107873445 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
@@ -621,6 +621,7 @@ public final class FunctionRegistry {
system.registerGenericUDF("least", GenericUDFLeast.class);
system.registerGenericUDF("cardinality_violation",
GenericUDFCardinalityViolation.class);
system.registerGenericUDF("width_bucket", GenericUDFWidthBucket.class);
+ system.registerGenericUDF("typeof", GenericUDFTypeOf.class);
system.registerGenericUDF("from_utc_timestamp",
GenericUDFFromUtcTimestamp.class);
system.registerGenericUDF("to_utc_timestamp",
GenericUDFToUtcTimestamp.class);
diff --git
a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTypeOf.java
b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTypeOf.java
new file mode 100644
index 00000000000..d9dc0f6c84a
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTypeOf.java
@@ -0,0 +1,53 @@
+/*
+ * 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.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(runtimeConstant = true)
+@Description(name = "typeof",
+ value = "_FUNC_(x) - returns the type of the supplied argument")
+public class GenericUDFTypeOf extends GenericUDF {
+ private Text returnValue = new Text();
+
+ @Override
+ public ObjectInspector initialize(ObjectInspector[] arguments) throws
UDFArgumentException {
+ if (arguments.length != 1) {
+ throw new UDFArgumentLengthException("TYPEOF requires 1 argument, got "
+ arguments.length);
+ }
+ returnValue.set(arguments[0].getTypeName());
+ return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
+ }
+
+ @Override
+ public Object evaluate(DeferredObject[] arguments) throws HiveException {
+ return returnValue;
+ }
+
+ @Override
+ public String getDisplayString(String[] children) {
+ return getStandardDisplayString("typeof", children);
+ }
+}
diff --git
a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFTypeOf.java
b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFTypeOf.java
new file mode 100644
index 00000000000..2aee276b435
--- /dev/null
+++
b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFTypeOf.java
@@ -0,0 +1,132 @@
+/*
+ * 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 com.google.common.collect.ImmutableMap;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
+import org.apache.hadoop.io.Text;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class TestGenericUDFTypeOf {
+ private final DeferredObject[] evaluateArguments = { new
DeferredJavaObject(new Text("Test")) };
+
+ private Text executeUDF(ObjectInspector initArg) throws HiveException {
+ GenericUDFTypeOf udf = new GenericUDFTypeOf();
+ ObjectInspector[] arguments = { initArg };
+ udf.initialize(arguments);
+ return (Text) udf.evaluate(evaluateArguments);
+ }
+
+ @Test
+ public void testPrimitiveTypes() throws HiveException {
+ Map<ObjectInspector, String> oiToExpectedOutput = new
ImmutableMap.Builder<ObjectInspector, String>()
+ .put(PrimitiveObjectInspectorFactory.writableBinaryObjectInspector,
"binary")
+ .put(PrimitiveObjectInspectorFactory.writableBooleanObjectInspector,
"boolean")
+ .put(PrimitiveObjectInspectorFactory.writableByteObjectInspector,
"tinyint")
+ .put(PrimitiveObjectInspectorFactory.writableDateObjectInspector,
"date")
+ .put(PrimitiveObjectInspectorFactory.writableDoubleObjectInspector,
"double")
+ .put(PrimitiveObjectInspectorFactory.writableFloatObjectInspector,
"float")
+ .put(PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector,
"char(255)")
+
.put(PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector,
"decimal(38,18)")
+
.put(PrimitiveObjectInspectorFactory.writableHiveIntervalDayTimeObjectInspector,
"interval_day_time")
+
.put(PrimitiveObjectInspectorFactory.writableHiveIntervalYearMonthObjectInspector,
"interval_year_month")
+
.put(PrimitiveObjectInspectorFactory.writableHiveVarcharObjectInspector,
"varchar(65535)")
+ .put(PrimitiveObjectInspectorFactory.writableIntObjectInspector, "int")
+ .put(PrimitiveObjectInspectorFactory.writableLongObjectInspector,
"bigint")
+ .put(PrimitiveObjectInspectorFactory.writableShortObjectInspector,
"smallint")
+ .put(PrimitiveObjectInspectorFactory.writableStringObjectInspector,
"string")
+ .put(PrimitiveObjectInspectorFactory.writableTimestampObjectInspector,
"timestamp")
+
.put(PrimitiveObjectInspectorFactory.writableTimestampTZObjectInspector,
"timestamp with local time zone")
+ .put(PrimitiveObjectInspectorFactory.writableVoidObjectInspector,
"void")
+ .build();
+
+ for (Map.Entry<ObjectInspector, String> testCase:
oiToExpectedOutput.entrySet()) {
+ Text retValue = executeUDF(testCase.getKey());
+ assertEquals(testCase.getValue(), retValue.toString());
+ }
+ }
+
+ @Test
+ public void testListType() throws HiveException {
+ ObjectInspector oi = ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableStringObjectInspector
+ );
+ Text retValue = executeUDF(oi);
+ assertEquals("array<string>", retValue.toString());
+ }
+
+ @Test
+ public void testStructType() throws HiveException {
+ List<String> structFieldNames = Arrays.asList(
+ "str",
+ "long",
+ "strArray"
+ );
+ List<ObjectInspector> structOIs = Arrays.asList(
+ PrimitiveObjectInspectorFactory.writableStringObjectInspector,
+ PrimitiveObjectInspectorFactory.writableLongObjectInspector,
+ ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableStringObjectInspector
+ ));
+
+ ObjectInspector oi =
ObjectInspectorFactory.getStandardStructObjectInspector(structFieldNames,
structOIs);
+ Text retValue = executeUDF(oi);
+ assertEquals("struct<str:string,long:bigint,strarray:array<string>>",
retValue.toString());
+ }
+
+ @Test
+ public void testMapType() throws HiveException {
+ ObjectInspector keyOI =
PrimitiveObjectInspectorFactory.writableDateObjectInspector;
+ ObjectInspector valueOI =
ObjectInspectorFactory.getStandardListObjectInspector(
+ PrimitiveObjectInspectorFactory.writableStringObjectInspector
+ );
+ ObjectInspector oi =
ObjectInspectorFactory.getStandardMapObjectInspector(keyOI, valueOI);
+ Text retValue = executeUDF(oi);
+ assertEquals("map<date,array<string>>", retValue.toString());
+ }
+
+ @Test(expected = UDFArgumentLengthException.class)
+ public void testNotEnoughArguments() throws HiveException {
+ GenericUDFTypeOf udf = new GenericUDFTypeOf();
+ ObjectInspector[] arguments = { };
+ udf.initialize(arguments);
+ }
+
+ @Test(expected = UDFArgumentLengthException.class)
+ public void testTooManyArguments() throws HiveException {
+ GenericUDFTypeOf udf = new GenericUDFTypeOf();
+ ObjectInspector[] arguments = {
+ PrimitiveObjectInspectorFactory.writableDateObjectInspector,
+ PrimitiveObjectInspectorFactory.writableDateObjectInspector
+ };
+ udf.initialize(arguments);
+ }
+}
diff --git a/ql/src/test/queries/clientpositive/udf_typeof.q
b/ql/src/test/queries/clientpositive/udf_typeof.q
new file mode 100644
index 00000000000..8a1b135b8a8
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/udf_typeof.q
@@ -0,0 +1,7 @@
+--! qt:dataset:src
+DESCRIBE FUNCTION typeof;
+DESCRIBE FUNCTION EXTENDED typeof;
+SELECT typeof(1);
+SELECT typeof("string");
+SELECT typeof(CAST(1 as DECIMAL(4,3)));
+SELECT typeof(key) FROM src LIMIT 2;
diff --git a/ql/src/test/results/clientpositive/llap/show_functions.q.out
b/ql/src/test/results/clientpositive/llap/show_functions.q.out
index d3e74dde3b9..28c7b37481f 100644
--- a/ql/src/test/results/clientpositive/llap/show_functions.q.out
+++ b/ql/src/test/results/clientpositive/llap/show_functions.q.out
@@ -456,6 +456,7 @@ translate
trim
trunc
tumbling_window
+typeof
ucase
udftoboolean
udftobyte
@@ -1079,6 +1080,7 @@ translate
trim
trunc
tumbling_window
+typeof
ucase
udftoboolean
udftobyte
diff --git a/ql/src/test/results/clientpositive/llap/udf_typeof.q.out
b/ql/src/test/results/clientpositive/llap/udf_typeof.q.out
new file mode 100644
index 00000000000..2768d2586c3
--- /dev/null
+++ b/ql/src/test/results/clientpositive/llap/udf_typeof.q.out
@@ -0,0 +1,49 @@
+PREHOOK: query: DESCRIBE FUNCTION typeof
+PREHOOK: type: DESCFUNCTION
+POSTHOOK: query: DESCRIBE FUNCTION typeof
+POSTHOOK: type: DESCFUNCTION
+typeof(x) - returns the type of the supplied argument
+PREHOOK: query: DESCRIBE FUNCTION EXTENDED typeof
+PREHOOK: type: DESCFUNCTION
+POSTHOOK: query: DESCRIBE FUNCTION EXTENDED typeof
+POSTHOOK: type: DESCFUNCTION
+typeof(x) - returns the type of the supplied argument
+Function class:org.apache.hadoop.hive.ql.udf.generic.GenericUDFTypeOf
+Function type:BUILTIN
+PREHOOK: query: SELECT typeof(1)
+PREHOOK: type: QUERY
+PREHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+POSTHOOK: query: SELECT typeof(1)
+POSTHOOK: type: QUERY
+POSTHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+int
+PREHOOK: query: SELECT typeof("string")
+PREHOOK: type: QUERY
+PREHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+POSTHOOK: query: SELECT typeof("string")
+POSTHOOK: type: QUERY
+POSTHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+string
+PREHOOK: query: SELECT typeof(CAST(1 as DECIMAL(4,3)))
+PREHOOK: type: QUERY
+PREHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+POSTHOOK: query: SELECT typeof(CAST(1 as DECIMAL(4,3)))
+POSTHOOK: type: QUERY
+POSTHOOK: Input: _dummy_database@_dummy_table
+#### A masked pattern was here ####
+decimal(4,3)
+PREHOOK: query: SELECT typeof(key) FROM src LIMIT 2
+PREHOOK: type: QUERY
+PREHOOK: Input: default@src
+#### A masked pattern was here ####
+POSTHOOK: query: SELECT typeof(key) FROM src LIMIT 2
+POSTHOOK: type: QUERY
+POSTHOOK: Input: default@src
+#### A masked pattern was here ####
+string
+string