diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java index d16a247..d207d13 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionInfo.java @@ -23,6 +23,7 @@ import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; import org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge; import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF; +import org.apache.hadoop.hive.ql.udf.ptf.TableFunctionResolver; /** * FunctionInfo. @@ -40,6 +41,8 @@ private GenericUDAFResolver genericUDAFResolver; + private Class functionResolver; + public FunctionInfo(boolean isNative, String displayName, GenericUDF genericUDF) { this.isNative = isNative; @@ -61,6 +64,14 @@ public FunctionInfo(boolean isNative, String displayName, this.genericUDTF = genericUDTF; } + public FunctionInfo(String displayName, Class tFnCls) + { + this.displayName = displayName; + this.functionResolver = tFnCls; + PartitionTableFunctionDescription def = functionResolver.getAnnotation(PartitionTableFunctionDescription.class); + this.isNative = (def == null) ? false : def.isInternal(); + } + /** * Get a new GenericUDF object for the function. */ @@ -109,7 +120,7 @@ public GenericUDAFResolver getGenericUDAFResolver() { } else if (isGenericUDTF()) { return genericUDTF.getClass(); } - return null; + return this.functionResolver; } /** diff --git ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java index b0fffd9..66ea23d 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java @@ -182,7 +182,6 @@ public static final String NOOP_TABLE_FUNCTION = "noop"; public static final String NOOP_MAP_TABLE_FUNCTION = "noopwithmap"; - static Map tableFunctions = Collections.synchronizedMap(new LinkedHashMap()); static Map windowFunctions = Collections.synchronizedMap(new LinkedHashMap()); /* @@ -1294,6 +1293,9 @@ public static boolean registerTemporaryFunction( FunctionRegistry.registerTemporaryGenericUDAF( functionName, (GenericUDAFResolver) ReflectionUtils.newInstance(udfClass, null)); + } else if(TableFunctionResolver.class.isAssignableFrom(udfClass)) { + FunctionRegistry.registerTableFunction( + functionName, (Class)udfClass); } else { return false; } @@ -1406,14 +1408,14 @@ static void registerHiveUDAFsAsWindowFunctions() public static boolean isTableFunction(String name) { - PTFFunctionInfo tFInfo = tableFunctions.get(name.toLowerCase()); - return tFInfo != null && !tFInfo.isInternal(); + FunctionInfo tFInfo = mFunctions.get(name.toLowerCase()); + return tFInfo != null && !tFInfo.isNative(); } public static TableFunctionResolver getTableFunctionResolver(String name) { - PTFFunctionInfo tfInfo = tableFunctions.get(name.toLowerCase()); - return (TableFunctionResolver) ReflectionUtils.newInstance(tfInfo.getFunctionResolver(), null); + FunctionInfo tfInfo = mFunctions.get(name.toLowerCase()); + return (TableFunctionResolver) ReflectionUtils.newInstance(tfInfo.getFunctionClass(), null); } public static TableFunctionResolver getWindowingTableFunction() @@ -1428,8 +1430,8 @@ public static TableFunctionResolver getNoopTableFunction() public static void registerTableFunction(String name, Class tFnCls) { - PTFFunctionInfo tInfo = new PTFFunctionInfo(name, tFnCls); - tableFunctions.put(name.toLowerCase(), tInfo); + FunctionInfo tInfo = new FunctionInfo(name, tFnCls); + mFunctions.put(name.toLowerCase(), tInfo); } } diff --git ql/src/test/queries/clientpositive/ptf_register_tblfn.q ql/src/test/queries/clientpositive/ptf_register_tblfn.q new file mode 100644 index 0000000..a75760f --- /dev/null +++ ql/src/test/queries/clientpositive/ptf_register_tblfn.q @@ -0,0 +1,27 @@ +DROP TABLE flights_tiny; + +create table flights_tiny ( +ORIGIN_CITY_NAME string, +DEST_CITY_NAME string, +YEAR int, +MONTH int, +DAY_OF_MONTH int, +ARR_DELAY float, +FL_NUM string +); + +LOAD DATA LOCAL INPATH '../data/files/flights_tiny.txt' OVERWRITE INTO TABLE flights_tiny; + +create temporary function npathtest as 'org.apache.hadoop.hive.ql.udf.ptf.NPath$NPathResolver'; + + +-- 1. basic Npath test +select origin_city_name, fl_num, year, month, day_of_month, sz, tpath +from npathtest(on + flights_tiny + distribute by fl_num + sort by year, month, day_of_month + arg1('LATE.LATE+'), + arg2('LATE'), arg3(arr_delay > 15), + arg4('origin_city_name, fl_num, year, month, day_of_month, size(tpath) as sz, tpath[0].day_of_month as tpath') + );