how can we test HIVE functions without referencing a table

5.2k Views Asked by At

I wanted to understand the UDF WeekOfYear and how it starts the first week. I had to artifically hit a table and run the query . I wanted to not hit the table and compute the values. Secondly can I look at the UDF source code?

SELECT weekofyear
('12-31-2013')
from a;
4

There are 4 best solutions below

0
On BEST ANSWER

You do not need table to test UDF since Hive 0.13.0.

See this Jira: HIVE-178 SELECT without FROM should assume a one-row table with no columns

Test:

hive> SELECT weekofyear('2013-12-31');

Result:

1

The source code (master branch) is here: UDFWeekOfYear.java

0
On

I don't think executing UDF without hitting the tables is possible in Hive. Even Hive developers hit the table in UDF testing.

To make query run faster you can:

  1. Create table with only one row and run UDF queries on this table
  2. Run Hive in local mode.

Hive source code is located here. UDFWeekOfYear source is here.

0
On

If you are Java developer, you can write Junit Test cases and test the UDFs..

you can search the source code of all hive built in functions in grepcode.

http://grepcode.com/file/repo1.maven.org/maven2/org.apache.hive/hive-exec/1.0.0/org/apache/hadoop/hive/ql/udf/UDFWeekOfYear.java

0
On

You should be able to use any table with at least one row to test functions. Here is an example using a few custom functions that perform work and output a string result.

Replace anytable with an actual table.

SELECT ST_AsText(ST_Intersection(ST_Polygon(2,0, 2,3, 3,0), ST_Polygon(1,1, 4,1, 4,4, 1,4))) FROM anytable LIMIT 1;

HIVE Resuluts:

OK

POLYGON ((2 1, 2.6666666666666665 1, 2 3, 2 1))

Time taken: 0.191 seconds, Fetched: 1 row(s)

hive>