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 898b6a5..fbde66d 100644 --- ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java +++ ql/src/java/org/apache/hadoop/hive/ql/exec/FunctionRegistry.java @@ -44,8 +44,6 @@ import org.apache.hadoop.hive.ql.parse.SemanticException; import org.apache.hadoop.hive.ql.plan.ExprNodeDesc; import org.apache.hadoop.hive.ql.plan.ExprNodeGenericFuncDesc; -import org.apache.hadoop.hive.ql.udf.GenericUDFDecode; -import org.apache.hadoop.hive.ql.udf.GenericUDFEncode; import org.apache.hadoop.hive.ql.udf.SettableUDF; import org.apache.hadoop.hive.ql.udf.UDAFPercentile; import org.apache.hadoop.hive.ql.udf.UDFAcos; diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/GenericUDFDecode.java ql/src/java/org/apache/hadoop/hive/ql/udf/GenericUDFDecode.java deleted file mode 100644 index d99579b..0000000 --- ql/src/java/org/apache/hadoop/hive/ql/udf/GenericUDFDecode.java +++ /dev/null @@ -1,114 +0,0 @@ -/** - * 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; - -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; -import java.nio.charset.CharsetDecoder; -import java.nio.charset.CodingErrorAction; - -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.exec.UDFArgumentTypeException; -import org.apache.hadoop.hive.ql.metadata.HiveException; -import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; -import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category; -import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector; -import org.apache.hadoop.io.Text; - -@Description(name = "decode", - value = "_FUNC_(bin, str) - Decode the first argument using the second argument character set", - extended = "Possible options for the character set are 'US_ASCII', 'ISO-8859-1',\n" + - "'UTF-8', 'UTF-16BE', 'UTF-16LE', and 'UTF-16'. If either argument\n" + - "is null, the result will also be null") -public class GenericUDFDecode extends GenericUDF { - private transient CharsetDecoder decoder = null; - private transient BinaryObjectInspector bytesOI = null; - private transient StringObjectInspector charsetOI = null; - - @Override - public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { - if (arguments.length != 2) { - throw new UDFArgumentLengthException("Encode() requires exactly two arguments"); - } - - if (arguments[0].getCategory() != Category.PRIMITIVE || - ((PrimitiveObjectInspector)arguments[0]).getPrimitiveCategory() != PrimitiveCategory.BINARY){ - throw new UDFArgumentTypeException(0, "The first argument to Encode() must be a binary"); - } - - bytesOI = (BinaryObjectInspector) arguments[0]; - - if (arguments[1].getCategory() != Category.PRIMITIVE || - ((PrimitiveObjectInspector)arguments[1]).getPrimitiveCategory() != PrimitiveCategory.STRING){ - throw new UDFArgumentTypeException(1, "The second argument to Encode() must be a string"); - } - - charsetOI = (StringObjectInspector) arguments[1]; - - // If the character set for encoding is constant, we can optimize that - StringObjectInspector charSetOI = (StringObjectInspector) arguments[1]; - if (charSetOI instanceof ConstantObjectInspector){ - String charSetName = ((Text) ((ConstantObjectInspector) charSetOI).getWritableConstantValue()).toString(); - decoder = Charset.forName(charSetName).newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT); - } - - return (ObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector; - } - - @Override - public Object evaluate(DeferredObject[] arguments) throws HiveException { - byte[] value = bytesOI.getPrimitiveJavaObject(arguments[0].get()); - if (value == null) { - return null; - } - - ByteBuffer wrappedBytes = ByteBuffer.wrap(value); - CharBuffer decoded; - if (decoder != null){ - try { - decoded = decoder.decode(wrappedBytes); - } catch (CharacterCodingException e) { - throw new HiveException(e); - } - } else { - decoded = Charset.forName(charsetOI.getPrimitiveJavaObject(arguments[1].get())).decode(wrappedBytes); - } - return decoded.toString(); - } - - @Override - public String getDisplayString(String[] children) { - assert (children.length == 2); - StringBuilder sb = new StringBuilder(); - sb.append("encode("); - sb.append(children[0]).append(","); - sb.append(children[1]).append(")"); - return sb.toString(); - } -} diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/GenericUDFEncode.java ql/src/java/org/apache/hadoop/hive/ql/udf/GenericUDFEncode.java deleted file mode 100644 index f0c6b9d..0000000 --- ql/src/java/org/apache/hadoop/hive/ql/udf/GenericUDFEncode.java +++ /dev/null @@ -1,125 +0,0 @@ -/** - * 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; - -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; -import java.nio.charset.CodingErrorAction; - -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.exec.UDFArgumentTypeException; -import org.apache.hadoop.hive.ql.metadata.HiveException; -import org.apache.hadoop.hive.ql.udf.generic.GenericUDF; -import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category; -import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector; -import org.apache.hadoop.io.BytesWritable; -import org.apache.hadoop.io.Text; - -@Description(name = "encode", -value = "_FUNC_(str, str) - Encode the first argument using the second argument character set", -extended = "Possible options for the character set are 'US_ASCII', 'ISO-8859-1',\n" + - "'UTF-8', 'UTF-16BE', 'UTF-16LE', and 'UTF-16'. If either argument\n" + - "is null, the result will also be null") -public class GenericUDFEncode extends GenericUDF { - private transient CharsetEncoder encoder = null; - private transient PrimitiveObjectInspector stringOI = null; - private transient PrimitiveObjectInspector charsetOI = null; - private transient BytesWritable result = new BytesWritable(); - - @Override - public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { - if (arguments.length != 2) { - throw new UDFArgumentLengthException("Encode() requires exactly two arguments"); - } - - if (arguments[0].getCategory() != Category.PRIMITIVE || - PrimitiveGrouping.STRING_GROUP != PrimitiveObjectInspectorUtils.getPrimitiveGrouping( - ((PrimitiveObjectInspector)arguments[0]).getPrimitiveCategory())){ - throw new UDFArgumentTypeException( - 0, "The first argument to Encode() must be a string/varchar"); - } - - stringOI = (PrimitiveObjectInspector) arguments[0]; - - if (arguments[1].getCategory() != Category.PRIMITIVE || - PrimitiveGrouping.STRING_GROUP != PrimitiveObjectInspectorUtils.getPrimitiveGrouping( - ((PrimitiveObjectInspector)arguments[1]).getPrimitiveCategory())){ - throw new UDFArgumentTypeException( - 1, "The second argument to Encode() must be a string/varchar"); - } - - charsetOI = (PrimitiveObjectInspector) arguments[1]; - - // If the character set for encoding is constant, we can optimize that - if (charsetOI instanceof ConstantObjectInspector){ - String charSetName = - ((ConstantObjectInspector) arguments[1]).getWritableConstantValue().toString(); - encoder = Charset.forName(charSetName).newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT); - } - - result = new BytesWritable(); - - return (ObjectInspector) PrimitiveObjectInspectorFactory.writableBinaryObjectInspector; - } - - @Override - public Object evaluate(DeferredObject[] arguments) throws HiveException { - String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI); - if (value == null) { - return null; - } - - ByteBuffer encoded; - if (encoder != null){ - try { - encoded = encoder.encode(CharBuffer.wrap(value)); - } catch (CharacterCodingException e) { - throw new HiveException(e); - } - } else { - encoded = Charset.forName( - PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value); - } - result.setSize(encoded.limit()); - encoded.get(result.getBytes(), 0, encoded.limit()); - return result; - } - - @Override - public String getDisplayString(String[] children) { - assert (children.length == 2); - StringBuilder sb = new StringBuilder(); - sb.append("encode("); - sb.append(children[0]).append(","); - sb.append(children[1]).append(")"); - return sb.toString(); - } -} diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDecode.java ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDecode.java new file mode 100644 index 0000000..ebf52d6 --- /dev/null +++ ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDecode.java @@ -0,0 +1,113 @@ +/** + * 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 java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; +import java.nio.charset.CodingErrorAction; + +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.exec.UDFArgumentTypeException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.BinaryObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector; +import org.apache.hadoop.io.Text; + +@Description(name = "decode", + value = "_FUNC_(bin, str) - Decode the first argument using the second argument character set", + extended = "Possible options for the character set are 'US_ASCII', 'ISO-8859-1',\n" + + "'UTF-8', 'UTF-16BE', 'UTF-16LE', and 'UTF-16'. If either argument\n" + + "is null, the result will also be null") +public class GenericUDFDecode extends GenericUDF { + private transient CharsetDecoder decoder = null; + private transient BinaryObjectInspector bytesOI = null; + private transient StringObjectInspector charsetOI = null; + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + if (arguments.length != 2) { + throw new UDFArgumentLengthException("Encode() requires exactly two arguments"); + } + + if (arguments[0].getCategory() != Category.PRIMITIVE || + ((PrimitiveObjectInspector)arguments[0]).getPrimitiveCategory() != PrimitiveCategory.BINARY){ + throw new UDFArgumentTypeException(0, "The first argument to Encode() must be a binary"); + } + + bytesOI = (BinaryObjectInspector) arguments[0]; + + if (arguments[1].getCategory() != Category.PRIMITIVE || + ((PrimitiveObjectInspector)arguments[1]).getPrimitiveCategory() != PrimitiveCategory.STRING){ + throw new UDFArgumentTypeException(1, "The second argument to Encode() must be a string"); + } + + charsetOI = (StringObjectInspector) arguments[1]; + + // If the character set for encoding is constant, we can optimize that + StringObjectInspector charSetOI = (StringObjectInspector) arguments[1]; + if (charSetOI instanceof ConstantObjectInspector){ + String charSetName = ((Text) ((ConstantObjectInspector) charSetOI).getWritableConstantValue()).toString(); + decoder = Charset.forName(charSetName).newDecoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT); + } + + return (ObjectInspector) PrimitiveObjectInspectorFactory.javaStringObjectInspector; + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + byte[] value = bytesOI.getPrimitiveJavaObject(arguments[0].get()); + if (value == null) { + return null; + } + + ByteBuffer wrappedBytes = ByteBuffer.wrap(value); + CharBuffer decoded; + if (decoder != null){ + try { + decoded = decoder.decode(wrappedBytes); + } catch (CharacterCodingException e) { + throw new HiveException(e); + } + } else { + decoded = Charset.forName(charsetOI.getPrimitiveJavaObject(arguments[1].get())).decode(wrappedBytes); + } + return decoded.toString(); + } + + @Override + public String getDisplayString(String[] children) { + assert (children.length == 2); + StringBuilder sb = new StringBuilder(); + sb.append("encode("); + sb.append(children[0]).append(","); + sb.append(children[1]).append(")"); + return sb.toString(); + } +} diff --git ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEncode.java ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEncode.java new file mode 100644 index 0000000..9bb0f45 --- /dev/null +++ ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFEncode.java @@ -0,0 +1,121 @@ +/** + * 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 java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CodingErrorAction; + +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.exec.UDFArgumentTypeException; +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category; +import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping; +import org.apache.hadoop.io.BytesWritable; + +@Description(name = "encode", +value = "_FUNC_(str, str) - Encode the first argument using the second argument character set", +extended = "Possible options for the character set are 'US_ASCII', 'ISO-8859-1',\n" + + "'UTF-8', 'UTF-16BE', 'UTF-16LE', and 'UTF-16'. If either argument\n" + + "is null, the result will also be null") +public class GenericUDFEncode extends GenericUDF { + private transient CharsetEncoder encoder = null; + private transient PrimitiveObjectInspector stringOI = null; + private transient PrimitiveObjectInspector charsetOI = null; + private transient BytesWritable result = new BytesWritable(); + + @Override + public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException { + if (arguments.length != 2) { + throw new UDFArgumentLengthException("Encode() requires exactly two arguments"); + } + + if (arguments[0].getCategory() != Category.PRIMITIVE || + PrimitiveGrouping.STRING_GROUP != PrimitiveObjectInspectorUtils.getPrimitiveGrouping( + ((PrimitiveObjectInspector)arguments[0]).getPrimitiveCategory())){ + throw new UDFArgumentTypeException( + 0, "The first argument to Encode() must be a string/varchar"); + } + + stringOI = (PrimitiveObjectInspector) arguments[0]; + + if (arguments[1].getCategory() != Category.PRIMITIVE || + PrimitiveGrouping.STRING_GROUP != PrimitiveObjectInspectorUtils.getPrimitiveGrouping( + ((PrimitiveObjectInspector)arguments[1]).getPrimitiveCategory())){ + throw new UDFArgumentTypeException( + 1, "The second argument to Encode() must be a string/varchar"); + } + + charsetOI = (PrimitiveObjectInspector) arguments[1]; + + // If the character set for encoding is constant, we can optimize that + if (charsetOI instanceof ConstantObjectInspector){ + String charSetName = + ((ConstantObjectInspector) arguments[1]).getWritableConstantValue().toString(); + encoder = Charset.forName(charSetName).newEncoder().onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT); + } + + result = new BytesWritable(); + + return (ObjectInspector) PrimitiveObjectInspectorFactory.writableBinaryObjectInspector; + } + + @Override + public Object evaluate(DeferredObject[] arguments) throws HiveException { + String value = PrimitiveObjectInspectorUtils.getString(arguments[0].get(), stringOI); + if (value == null) { + return null; + } + + ByteBuffer encoded; + if (encoder != null){ + try { + encoded = encoder.encode(CharBuffer.wrap(value)); + } catch (CharacterCodingException e) { + throw new HiveException(e); + } + } else { + encoded = Charset.forName( + PrimitiveObjectInspectorUtils.getString(arguments[1].get(), charsetOI)).encode(value); + } + result.setSize(encoded.limit()); + encoded.get(result.getBytes(), 0, encoded.limit()); + return result; + } + + @Override + public String getDisplayString(String[] children) { + assert (children.length == 2); + StringBuilder sb = new StringBuilder(); + sb.append("encode("); + sb.append(children[0]).append(","); + sb.append(children[1]).append(")"); + return sb.toString(); + } +} diff --git ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFAbs.java ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFAbs.java deleted file mode 100644 index 0762082..0000000 --- ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFAbs.java +++ /dev/null @@ -1,157 +0,0 @@ -/** - * 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; - -import junit.framework.TestCase; - -import org.apache.hadoop.hive.common.type.HiveDecimal; -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.ql.udf.generic.GenericUDFAbs; -import org.apache.hadoop.hive.serde2.io.DoubleWritable; -import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable; -import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; -import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; -import org.apache.hadoop.io.FloatWritable; -import org.apache.hadoop.io.IntWritable; -import org.apache.hadoop.io.LongWritable; -import org.apache.hadoop.io.Text; - -public class TestGenericUDFAbs extends TestCase { - - public void testInt() throws HiveException { - GenericUDFAbs udf = new GenericUDFAbs(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector; - ObjectInspector[] arguments = {valueOI}; - - udf.initialize(arguments); - DeferredObject valueObj = new DeferredJavaObject(new IntWritable(107)); - DeferredObject[] args = {valueObj}; - IntWritable output = (IntWritable) udf.evaluate(args); - - assertEquals("abs() test for INT failed ", 107, output.get()); - - valueObj = new DeferredJavaObject(new IntWritable(-107)); - args[0] = valueObj; - output = (IntWritable) udf.evaluate(args); - - assertEquals("abs() test for INT failed ", 107, output.get()); - } - - public void testLong() throws HiveException { - GenericUDFAbs udf = new GenericUDFAbs(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableLongObjectInspector; - ObjectInspector[] arguments = {valueOI}; - - udf.initialize(arguments); - DeferredObject valueObj = new DeferredJavaObject(new LongWritable(107L)); - DeferredObject[] args = {valueObj}; - LongWritable output = (LongWritable) udf.evaluate(args); - - assertEquals("abs() test for LONG failed ", 107, output.get()); - - valueObj = new DeferredJavaObject(new LongWritable(-107L)); - args[0] = valueObj; - output = (LongWritable) udf.evaluate(args); - - assertEquals("abs() test for LONG failed ", 107, output.get()); - } - - public void testDouble() throws HiveException { - GenericUDFAbs udf = new GenericUDFAbs(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableDoubleObjectInspector; - ObjectInspector[] arguments = {valueOI}; - - udf.initialize(arguments); - DeferredObject valueObj = new DeferredJavaObject(new DoubleWritable(107.78)); - DeferredObject[] args = {valueObj}; - DoubleWritable output = (DoubleWritable) udf.evaluate(args); - - assertEquals("abs() test for Double failed ", 107.78, output.get()); - - valueObj = new DeferredJavaObject(new DoubleWritable(-107.78)); - args[0] = valueObj; - output = (DoubleWritable) udf.evaluate(args); - - assertEquals("abs() test for Double failed ", 107.78, output.get()); - } - - public void testFloat() throws HiveException { - GenericUDFAbs udf = new GenericUDFAbs(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableFloatObjectInspector; - ObjectInspector[] arguments = {valueOI}; - - udf.initialize(arguments); - DeferredObject valueObj = new DeferredJavaObject(new FloatWritable(107.78f)); - DeferredObject[] args = {valueObj}; - DoubleWritable output = (DoubleWritable) udf.evaluate(args); - - // Make sure flow and double equality compare works - assertTrue("abs() test for Float failed ", Math.abs(107.78 - output.get()) < 0.0001); - - valueObj = new DeferredJavaObject(new FloatWritable(-107.78f)); - args[0] = valueObj; - output = (DoubleWritable) udf.evaluate(args); - - assertTrue("abs() test for Float failed ", Math.abs(107.78 - output.get()) < 0.0001); - } - - - public void testText() throws HiveException { - GenericUDFAbs udf = new GenericUDFAbs(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector; - ObjectInspector[] arguments = {valueOI}; - - udf.initialize(arguments); - DeferredObject valueObj = new DeferredJavaObject(new Text("123.45")); - DeferredObject[] args = {valueObj}; - DoubleWritable output = (DoubleWritable) udf.evaluate(args); - - assertEquals("abs() test for String failed ", "123.45", output.toString()); - - valueObj = new DeferredJavaObject(new Text("-123.45")); - args[0] = valueObj; - output = (DoubleWritable) udf.evaluate(args); - - assertEquals("abs() test for String failed ", "123.45", output.toString()); - } - - public void testHiveDecimal() throws HiveException { - GenericUDFAbs udf = new GenericUDFAbs(); - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector; - ObjectInspector[] arguments = {valueOI}; - - udf.initialize(arguments); - DeferredObject valueObj = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create( - "107.123456789"))); - DeferredObject[] args = {valueObj}; - HiveDecimalWritable output = (HiveDecimalWritable) udf.evaluate(args); - - assertEquals("abs() test for HiveDecimal failed ", 107.123456789, output.getHiveDecimal() - .doubleValue()); - - valueObj = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create("-107.123456789"))); - args[0] = valueObj; - output = (HiveDecimalWritable) udf.evaluate(args); - - assertEquals("abs() test for HiveDecimal failed ", 107.123456789, output.getHiveDecimal() - .doubleValue()); - } -} diff --git ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDecode.java ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDecode.java deleted file mode 100644 index 05f5bf6..0000000 --- ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFDecode.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * 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; - -import java.io.UnsupportedEncodingException; - -import junit.framework.TestCase; - -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.primitive.PrimitiveObjectInspectorFactory; - -public class TestGenericUDFDecode extends TestCase { - public void testDecode() throws UnsupportedEncodingException, HiveException { - String[] charsetNames = {"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16"}; - for (String charsetName : charsetNames){ - verifyDecode("A sample string", charsetName); - } - } - - public void verifyDecode(String string, String charsetName) throws UnsupportedEncodingException, HiveException{ - GenericUDFDecode udf = new GenericUDFDecode(); - byte[] bytes = string.getBytes(charsetName); - - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector; - ObjectInspector charsetOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector; - ObjectInspector[] initArguments = {valueOI, charsetOI}; - udf.initialize(initArguments); - - DeferredObject valueObj = new DeferredJavaObject(bytes); - DeferredObject charsetObj = new DeferredJavaObject(charsetName); - DeferredObject[] arguments = {valueObj, charsetObj}; - String output = (String) udf.evaluate(arguments); - - assertEquals("Decoding failed for CharSet: " + charsetName, string, output); - } -} - diff --git ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFEncode.java ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFEncode.java deleted file mode 100644 index b3ab88f..0000000 --- ql/src/test/org/apache/hadoop/hive/ql/udf/TestGenericUDFEncode.java +++ /dev/null @@ -1,60 +0,0 @@ -/** - * 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; - -import java.io.UnsupportedEncodingException; - -import junit.framework.TestCase; - -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.primitive.PrimitiveObjectInspectorFactory; -import org.apache.hadoop.io.BytesWritable; - -public class TestGenericUDFEncode extends TestCase { - public void testEncode() throws UnsupportedEncodingException, HiveException{ - String[] charsetNames = {"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16"}; - for (String charsetName : charsetNames){ - verifyEncode("A sample string", charsetName); - } - } - - public void verifyEncode(String string, String charsetName) throws UnsupportedEncodingException, HiveException{ - GenericUDFEncode udf = new GenericUDFEncode(); - byte[] expected = string.getBytes(charsetName); - - ObjectInspector valueOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector; - ObjectInspector charsetOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector; - ObjectInspector[] initArguments = {valueOI, charsetOI}; - udf.initialize(initArguments); - - DeferredObject valueObj = new DeferredJavaObject(string); - DeferredObject charsetObj = new DeferredJavaObject(charsetName); - DeferredObject[] arguments = {valueObj, charsetObj}; - BytesWritable outputWritable = (BytesWritable) udf.evaluate(arguments); - - byte[] output = outputWritable.getBytes(); - assertTrue("Encoding failed for CharSet: " + charsetName, expected.length == outputWritable.getLength()); - for (int i = 0; i < expected.length; i++){ - assertEquals("Encoding failed for CharSet: " + charsetName, expected[i], output[i]); - } - } -} diff --git ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java new file mode 100644 index 0000000..1fe5361 --- /dev/null +++ ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFAbs.java @@ -0,0 +1,157 @@ +/** + * 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 junit.framework.TestCase; + +import org.apache.hadoop.hive.common.type.HiveDecimal; +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.ql.udf.generic.GenericUDFAbs; +import org.apache.hadoop.hive.serde2.io.DoubleWritable; +import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable; +import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; +import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.io.FloatWritable; +import org.apache.hadoop.io.IntWritable; +import org.apache.hadoop.io.LongWritable; +import org.apache.hadoop.io.Text; + +public class TestGenericUDFAbs extends TestCase { + + public void testInt() throws HiveException { + GenericUDFAbs udf = new GenericUDFAbs(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableIntObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj = new DeferredJavaObject(new IntWritable(107)); + DeferredObject[] args = {valueObj}; + IntWritable output = (IntWritable) udf.evaluate(args); + + assertEquals("abs() test for INT failed ", 107, output.get()); + + valueObj = new DeferredJavaObject(new IntWritable(-107)); + args[0] = valueObj; + output = (IntWritable) udf.evaluate(args); + + assertEquals("abs() test for INT failed ", 107, output.get()); + } + + public void testLong() throws HiveException { + GenericUDFAbs udf = new GenericUDFAbs(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableLongObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj = new DeferredJavaObject(new LongWritable(107L)); + DeferredObject[] args = {valueObj}; + LongWritable output = (LongWritable) udf.evaluate(args); + + assertEquals("abs() test for LONG failed ", 107, output.get()); + + valueObj = new DeferredJavaObject(new LongWritable(-107L)); + args[0] = valueObj; + output = (LongWritable) udf.evaluate(args); + + assertEquals("abs() test for LONG failed ", 107, output.get()); + } + + public void testDouble() throws HiveException { + GenericUDFAbs udf = new GenericUDFAbs(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableDoubleObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj = new DeferredJavaObject(new DoubleWritable(107.78)); + DeferredObject[] args = {valueObj}; + DoubleWritable output = (DoubleWritable) udf.evaluate(args); + + assertEquals("abs() test for Double failed ", 107.78, output.get()); + + valueObj = new DeferredJavaObject(new DoubleWritable(-107.78)); + args[0] = valueObj; + output = (DoubleWritable) udf.evaluate(args); + + assertEquals("abs() test for Double failed ", 107.78, output.get()); + } + + public void testFloat() throws HiveException { + GenericUDFAbs udf = new GenericUDFAbs(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableFloatObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj = new DeferredJavaObject(new FloatWritable(107.78f)); + DeferredObject[] args = {valueObj}; + DoubleWritable output = (DoubleWritable) udf.evaluate(args); + + // Make sure flow and double equality compare works + assertTrue("abs() test for Float failed ", Math.abs(107.78 - output.get()) < 0.0001); + + valueObj = new DeferredJavaObject(new FloatWritable(-107.78f)); + args[0] = valueObj; + output = (DoubleWritable) udf.evaluate(args); + + assertTrue("abs() test for Float failed ", Math.abs(107.78 - output.get()) < 0.0001); + } + + + public void testText() throws HiveException { + GenericUDFAbs udf = new GenericUDFAbs(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj = new DeferredJavaObject(new Text("123.45")); + DeferredObject[] args = {valueObj}; + DoubleWritable output = (DoubleWritable) udf.evaluate(args); + + assertEquals("abs() test for String failed ", "123.45", output.toString()); + + valueObj = new DeferredJavaObject(new Text("-123.45")); + args[0] = valueObj; + output = (DoubleWritable) udf.evaluate(args); + + assertEquals("abs() test for String failed ", "123.45", output.toString()); + } + + public void testHiveDecimal() throws HiveException { + GenericUDFAbs udf = new GenericUDFAbs(); + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableHiveDecimalObjectInspector; + ObjectInspector[] arguments = {valueOI}; + + udf.initialize(arguments); + DeferredObject valueObj = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create( + "107.123456789"))); + DeferredObject[] args = {valueObj}; + HiveDecimalWritable output = (HiveDecimalWritable) udf.evaluate(args); + + assertEquals("abs() test for HiveDecimal failed ", 107.123456789, output.getHiveDecimal() + .doubleValue()); + + valueObj = new DeferredJavaObject(new HiveDecimalWritable(HiveDecimal.create("-107.123456789"))); + args[0] = valueObj; + output = (HiveDecimalWritable) udf.evaluate(args); + + assertEquals("abs() test for HiveDecimal failed ", 107.123456789, output.getHiveDecimal() + .doubleValue()); + } +} diff --git ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDecode.java ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDecode.java new file mode 100644 index 0000000..fff93dd --- /dev/null +++ ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDecode.java @@ -0,0 +1,57 @@ +/** + * 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 java.io.UnsupportedEncodingException; + +import junit.framework.TestCase; + +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDFDecode; +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.primitive.PrimitiveObjectInspectorFactory; + +public class TestGenericUDFDecode extends TestCase { + public void testDecode() throws UnsupportedEncodingException, HiveException { + String[] charsetNames = {"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16"}; + for (String charsetName : charsetNames){ + verifyDecode("A sample string", charsetName); + } + } + + public void verifyDecode(String string, String charsetName) throws UnsupportedEncodingException, HiveException{ + GenericUDFDecode udf = new GenericUDFDecode(); + byte[] bytes = string.getBytes(charsetName); + + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector; + ObjectInspector charsetOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector; + ObjectInspector[] initArguments = {valueOI, charsetOI}; + udf.initialize(initArguments); + + DeferredObject valueObj = new DeferredJavaObject(bytes); + DeferredObject charsetObj = new DeferredJavaObject(charsetName); + DeferredObject[] arguments = {valueObj, charsetObj}; + String output = (String) udf.evaluate(arguments); + + assertEquals("Decoding failed for CharSet: " + charsetName, string, output); + } +} + diff --git ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEncode.java ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEncode.java new file mode 100644 index 0000000..3563947 --- /dev/null +++ ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFEncode.java @@ -0,0 +1,61 @@ +/** + * 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 java.io.UnsupportedEncodingException; + +import junit.framework.TestCase; + +import org.apache.hadoop.hive.ql.metadata.HiveException; +import org.apache.hadoop.hive.ql.udf.generic.GenericUDFEncode; +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.primitive.PrimitiveObjectInspectorFactory; +import org.apache.hadoop.io.BytesWritable; + +public class TestGenericUDFEncode extends TestCase { + public void testEncode() throws UnsupportedEncodingException, HiveException{ + String[] charsetNames = {"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE", "UTF-16LE", "UTF-16"}; + for (String charsetName : charsetNames){ + verifyEncode("A sample string", charsetName); + } + } + + public void verifyEncode(String string, String charsetName) throws UnsupportedEncodingException, HiveException{ + GenericUDFEncode udf = new GenericUDFEncode(); + byte[] expected = string.getBytes(charsetName); + + ObjectInspector valueOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector; + ObjectInspector charsetOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector; + ObjectInspector[] initArguments = {valueOI, charsetOI}; + udf.initialize(initArguments); + + DeferredObject valueObj = new DeferredJavaObject(string); + DeferredObject charsetObj = new DeferredJavaObject(charsetName); + DeferredObject[] arguments = {valueObj, charsetObj}; + BytesWritable outputWritable = (BytesWritable) udf.evaluate(arguments); + + byte[] output = outputWritable.getBytes(); + assertTrue("Encoding failed for CharSet: " + charsetName, expected.length == outputWritable.getLength()); + for (int i = 0; i < expected.length; i++){ + assertEquals("Encoding failed for CharSet: " + charsetName, expected[i], output[i]); + } + } +}