15 package com.cloudera.impala.hive.executor;
17 import static org.junit.Assert.assertArrayEquals;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
21 import java.lang.reflect.Method;
22 import java.net.MalformedURLException;
23 import java.util.ArrayList;
25 import org.apache.hadoop.hive.ql.udf.UDFAcos;
26 import org.apache.hadoop.hive.ql.udf.UDFAscii;
27 import org.apache.hadoop.hive.ql.udf.UDFAsin;
28 import org.apache.hadoop.hive.ql.udf.UDFAtan;
29 import org.apache.hadoop.hive.ql.udf.UDFBin;
30 import org.apache.hadoop.hive.ql.udf.UDFConv;
31 import org.apache.hadoop.hive.ql.udf.UDFCos;
32 import org.apache.hadoop.hive.ql.udf.UDFDegrees;
33 import org.apache.hadoop.hive.ql.udf.UDFE;
34 import org.apache.hadoop.hive.ql.udf.UDFExp;
35 import org.apache.hadoop.hive.ql.udf.UDFFindInSet;
36 import org.apache.hadoop.hive.ql.udf.UDFHex;
37 import org.apache.hadoop.hive.ql.udf.UDFLength;
38 import org.apache.hadoop.hive.ql.udf.UDFLn;
39 import org.apache.hadoop.hive.ql.udf.UDFLog;
40 import org.apache.hadoop.hive.ql.udf.UDFLog10;
41 import org.apache.hadoop.hive.ql.udf.UDFLog2;
42 import org.apache.hadoop.hive.ql.udf.UDFPI;
43 import org.apache.hadoop.hive.ql.udf.UDFRadians;
44 import org.apache.hadoop.hive.ql.udf.UDFRand;
45 import org.apache.hadoop.hive.ql.udf.UDFRepeat;
46 import org.apache.hadoop.hive.ql.udf.UDFReverse;
47 import org.apache.hadoop.hive.ql.udf.UDFSign;
48 import org.apache.hadoop.hive.ql.udf.UDFSin;
49 import org.apache.hadoop.hive.ql.udf.UDFSpace;
50 import org.apache.hadoop.hive.ql.udf.UDFSqrt;
51 import org.apache.hadoop.hive.ql.udf.UDFSubstr;
52 import org.apache.hadoop.hive.ql.udf.UDFTan;
53 import org.apache.hadoop.hive.ql.udf.UDFUnhex;
54 import org.apache.hadoop.io.BytesWritable;
55 import org.apache.hadoop.io.Text;
56 import org.apache.hadoop.io.Writable;
63 import com.google.common.base.Preconditions;
64 import com.google.common.collect.Lists;
66 @SuppressWarnings(
"restriction")
68 private final String HIVE_BUILTIN_JAR = System.getenv(
"HIVE_HOME") +
"/" +
69 "lib/hive-exec-" + System.getenv(
"IMPALA_HIVE_VERSION") +
".jar";
72 ArrayList<Long> allocations_ = Lists.newArrayList();
77 long ptr = UnsafeUtil.UNSAFE.allocateMemory(byteSize);
78 allocations_.add(ptr);
84 byte[] array = v.getBytes();
85 long ptr = allocate(16);
86 UnsafeUtil.UNSAFE.putInt(ptr + 8, 0);
88 sw.set(array, 0, array.length);
94 for (Long l: allocations_) {
95 UnsafeUtil.UNSAFE.freeMemory(l.longValue());
111 w.set((byte)((Integer)o).intValue());
116 w.set(((Integer)o).shortValue());
177 || w instanceof String) {
180 Preconditions.checkArgument(
false);
187 Type expectedType,
boolean validate, Object... args)
189 Type[] argTypes =
new Type[args.length];
190 for (
int i = 0; i < args.length; ++i) {
191 Preconditions.checkNotNull(args[i]);
192 argTypes[i] = getType(args[i]);
196 Method method = e.getMethod();
197 Object[] inputArgs =
new Object[args.length];
198 for (
int i = 0; i < args.length; ++i) {
199 if (args[i] instanceof String) {
202 if (method.getParameterTypes()[i] == Text.class) {
203 inputArgs[i] = createText((String)args[i]);
204 }
else if (method.getParameterTypes()[i] == BytesWritable.class) {
205 inputArgs[i] = createBytes((String)args[i]);
207 Preconditions.checkState(method.getParameterTypes()[i] == String.class);
208 inputArgs[i] = args[i];
211 inputArgs[i] = args[i];
217 for (
int i = 0; i < 10; ++i) {
218 long r = e.evaluate(inputArgs);
222 boolean v = UnsafeUtil.UNSAFE.getByte(r) != 0;
251 byte[] expectedBytes = null;
253 expectedBytes = ((ImpalaBytesWritable)expectedValue).getBytes();
255 expectedBytes = ((ImpalaTextWritable)expectedValue).getBytes();
256 }
else if (expectedValue instanceof String) {
257 expectedBytes = ((String)expectedValue).getBytes();
259 Preconditions.checkState(
false);
262 assertArrayEquals(sw.
getBytes(), expectedBytes);
266 Preconditions.checkArgument(
false);
272 void TestUdf(String jar, Class<?> c, Writable expectedValue, Object... args)
274 TestUdfImpl(jar, c, expectedValue, getType(expectedValue),
true, args);
277 void TestUdf(String jar, Class<?> c, String expectedValue, Object... args)
279 TestUdfImpl(jar, c, expectedValue, getType(expectedValue),
true, args);
282 void TestHiveUdf(Class<?> c, Writable expectedValue, Object... args)
284 TestUdfImpl(HIVE_BUILTIN_JAR, c, expectedValue, getType(expectedValue),
true, args);
289 TestUdfImpl(HIVE_BUILTIN_JAR, c, expectedValue, getType(expectedValue),
false, args);
298 TestHiveUdfNoValidate(UDFRand.class, createDouble(0));
299 TestHiveUdfNoValidate(UDFRand.class, createDouble(0), createBigInt(10));
300 TestHiveUdf(UDFExp.class, createDouble(Math.exp(10)), createDouble(10));
301 TestHiveUdf(UDFLn.class, createDouble(Math.log(10)), createDouble(10));
302 TestHiveUdf(UDFLog10.class, createDouble(Math.log10(10)), createDouble(10));
303 TestHiveUdf(UDFLog2.class, createDouble(Math.log(10) / Math.log(2)),
305 TestHiveUdf(UDFLog.class, createDouble(Math.log(3) / Math.log(10)),
306 createDouble(10), createDouble(3));
307 TestHiveUdf(UDFSqrt.class, createDouble(Math.sqrt(3)), createDouble(3));
308 TestHiveUdf(UDFSin.class, createDouble(Math.sin(1)), createDouble(1));
309 TestHiveUdf(UDFAsin.class, createDouble(Math.asin(1)), createDouble(1));
310 TestHiveUdf(UDFCos.class, createDouble(Math.cos(1)), createDouble(1));
311 TestHiveUdf(UDFAcos.class, createDouble(Math.acos(1)), createDouble(1));
312 TestHiveUdf(UDFTan.class, createDouble(Math.tan(1)), createDouble(1));
313 TestHiveUdf(UDFAtan.class, createDouble(Math.atan(1)), createDouble(1));
314 TestHiveUdf(UDFDegrees.class, createDouble(0), createDouble(0));
315 TestHiveUdf(UDFRadians.class, createDouble(0), createDouble(0));
317 TestHiveUdf(UDFPI.class, createDouble(Math.PI));
318 TestHiveUdf(UDFE.class, createDouble(Math.E));
319 TestHiveUdf(UDFSign.class, createDouble(1), createDouble(3));
321 TestHiveUdf(UDFBin.class, createBytes(
"1100100"), createBigInt(100));
323 TestHiveUdf(UDFHex.class, createBytes(
"1F4"), createBigInt(500));
324 TestHiveUdf(UDFHex.class, createBytes(
"3E8"), createBigInt(1000));
326 TestHiveUdf(UDFHex.class, createText(
"31303030"),
"1000");
327 TestHiveUdf(UDFUnhex.class, createText(
"aAzZ"),
"61417A5A");
328 TestHiveUdf(UDFConv.class, createText(
"1111011"),
329 "123", createInt(10), createInt(2));
337 TestHiveUdf(UDFAscii.class, createInt(
'1'),
"123");
338 TestHiveUdf(UDFFindInSet.class, createInt(2),
"31",
"12,31,23");
339 TestHiveUdf(UDFLength.class, createInt(5), createText(
"Hello"));
340 TestHiveUdf(UDFRepeat.class, createText(
"abcabc"),
"abc", createInt(2));
341 TestHiveUdf(UDFReverse.class, createText(
"cba"),
"abc");
342 TestHiveUdf(UDFSpace.class, createText(
" "), createInt(4));
343 TestHiveUdf(UDFSubstr.class, createText(
"World"),
344 "HelloWorld", createInt(6), createInt(5));
351 TestUdf(null,
TestUdf.class, createBoolean(
true), createBoolean(
true));
353 TestUdf(null,
TestUdf.class, createSmallInt(1), createSmallInt(1));
356 TestUdf(null,
TestUdf.class, createFloat(1.1f), createFloat(1.1f));
357 TestUdf(null,
TestUdf.class, createDouble(1.1), createDouble(1.1));
361 createDouble(1), createDouble(2));
Writable createText(String v)
long createStringValue(String v)
Writable createBytes(String v)
Writable createDouble(double v)
static final ScalarType BIGINT
void TestUdfImpl(String jar, Class<?> c, Object expectedValue, Type expectedType, boolean validate, Object...args)
Writable createInt(int v)
Writable createObject(PrimitiveType t, Object o)
Writable createSmallInt(int v)
static final ScalarType STRING
static final Unsafe UNSAFE
PrimitiveType getPrimitiveType()
static final ScalarType BOOLEAN
void TestHiveUdfNoValidate(Class<?> c, Writable expectedValue, Object...args)
void TestUdf(String jar, Class<?> c, Writable expectedValue, Object...args)
static final ScalarType SMALLINT
static final ScalarType FLOAT
void TestUdf(String jar, Class<?> c, String expectedValue, Object...args)
Writable createTinyInt(int v)
static final ScalarType DOUBLE
static final ScalarType TINYINT
long allocate(int byteSize)
void TestHiveUdf(Class<?> c, Writable expectedValue, Object...args)
static final ScalarType INT
uint64_t Test(T *ht, const ProbeTuple *input, uint64_t num_tuples)
Writable createBoolean(boolean v)
static final ScalarType INVALID
Writable createBigInt(long v)
Writable createFloat(float v)