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

Adding LEAST UDF #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Apache licensed.
hive> CREATE TEMPORARY FUNCTION chr AS 'com.nexr.platform.hive.udf.UDFChr';
hive> CREATE TEMPORARY FUNCTION last_day AS 'com.nexr.platform.hive.udf.UDFLastDay';
hive> CREATE TEMPORARY FUNCTION greatest AS 'com.nexr.platform.hive.udf.GenericUDFGreatest';
hive> CREATE TEMPORARY FUNCTION least AS 'com.nexr.platform.hive.udf.GenericUDFLeast';
hive> CREATE TEMPORARY FUNCTION to_number AS 'com.nexr.platform.hive.udf.GenericUDFToNumber';
hive> CREATE TEMPORARY FUNCTION trunc AS 'com.nexr.platform.hive.udf.GenericUDFTrunc';
hive> CREATE TEMPORARY FUNCTION rank AS 'com.nexr.platform.hive.udf.GenericUDFRank';
Expand Down
110 changes: 110 additions & 0 deletions src/main/java/com/nexr/platform/hive/udf/GenericUDFLeast.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* 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 com.nexr.platform.hive.udf;

import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDFUtils;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
import org.apache.hive.pdk.HivePdkUnitTest;
import org.apache.hive.pdk.HivePdkUnitTests;

/**
* GenericUDF Class for SQL construct "least(value1, value2, value3, ....)".
*
*/
@Description(name = "least", value = "_FUNC_(value1, value2, value3, ....) " +
"- Returns the least value in the list.",
extended = "Example:\n" + " > SELECT _FUNC_(2, 5, 12, 3) FROM src;\n 2")
@HivePdkUnitTests(setup = "create table dual_data (i int); "
+ "insert overwrite table dual_data select 1 from dual limit 1;",
cleanup = "drop table if exists dual_data;",
cases = {
@HivePdkUnitTest(query = "SELECT nexr_least(2, 5, 12, 3) " +
"FROM dual_data;", result = "2"),
@HivePdkUnitTest(query = "SELECT nexr_least('2', '5', '12', '3') " +
"FROM dual_data;", result = "12"),
@HivePdkUnitTest(query = "SELECT nexr_least('apples', 'oranges', 'bananas') " +
"FROM dual_data;", result = "apples") })
public class GenericUDFLeast extends GenericUDF {

private ObjectInspector[] argumentOIs;
private GenericUDFUtils.ReturnObjectInspectorResolver returnOIResolver;

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {

for (int i = 0; i < arguments.length; i++) {
if (arguments[i].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentTypeException(i, "Only primitive type arguments are accepted but "
+ arguments[i].getTypeName() + " is passed.");
}
}

argumentOIs = arguments;
returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
for (int i = 0; i < arguments.length; i++) {
if (!returnOIResolver.update(arguments[i])) {
throw new UDFArgumentTypeException(i, "The value of return should have the same type: \""
+ returnOIResolver.get().getTypeName() +
"\" is expected but \"" + arguments[i].getTypeName()
+ "\" is found");
}
}

return returnOIResolver.get();
}

@Override
public Object evaluate(DeferredObject[] fields) throws HiveException {
Object leastObject = null;
ObjectInspector leastOI = null;

for (int i = 0; i < fields.length; i++) {
Object fieldObject = fields[i].get();
if (leastObject == null) {
leastObject = fieldObject;
leastOI = argumentOIs[i];
continue;
}

if (ObjectInspectorUtils.compare(leastObject, leastOI, fieldObject, argumentOIs[i]) >= 0) {
leastObject = fieldObject;
leastOI = argumentOIs[i];
}
}

return returnOIResolver.convertIfNecessary(leastObject, leastOI);
}

@Override
public String getDisplayString(String[] children) {
StringBuilder sb = new StringBuilder();
sb.append("least (");
for (int i = 0; i < children.length - 1; i++) {
sb.append(children[i]).append(", ");
}
sb.append(children[children.length - 1]).append(")");
return sb.toString();
}

}