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

HIVE-685 GenericUDFQuote #532

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -284,6 +284,7 @@ public final class FunctionRegistry {
system.registerUDF("replace", UDFReplace.class, false);
system.registerUDF("regexp_extract", UDFRegExpExtract.class, false);
system.registerUDF("parse_url", UDFParseUrl.class, false);
system.registerGenericUDF("quote", GenericUDFQuote.class);
system.registerGenericUDF("nvl", GenericUDFCoalesce.class); //HIVE-20961
system.registerGenericUDF("split", GenericUDFSplit.class);
system.registerGenericUDF("str_to_map", GenericUDFStringToMap.class);
Expand Down
@@ -0,0 +1,72 @@
/*
* 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 org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TextConverter;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.io.Text;

/**
* Source for GenericUDFQuote.
*/
@Description(name = "quote", value = "_FUNC_() - Returns the Quoted string" + "Example:\n "
+ " > SELECT _FUNC_('Don't') FROM src LIMIT 1;\n" + " 'Don\'t'\n Including Single quotes\n")
public class GenericUDFQuote extends GenericUDF {
private transient TextConverter converter;

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 1) {
throw new UDFArgumentException("QUOTE() requires one value argument. Found :" + arguments.length);
}
PrimitiveObjectInspector argumentOI;
if (arguments[0] instanceof PrimitiveObjectInspector) {
argumentOI = (PrimitiveObjectInspector) arguments[0];
} else {
throw new UDFArgumentException("QUOTE() takes only primitive types. Found " + arguments[0].getTypeName());
}
converter = new TextConverter(argumentOI);
return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}

@Override
public Text evaluate(DeferredObject[] arguments) throws HiveException {
final String qtChar = "'";
final String qtCharRep = "\\\\'";

Object valObject = arguments[0].get();
if (valObject == null) {
return null;
}
String val = ((Text) converter.convert(valObject)).toString();
String sp = (String) val.replaceAll(qtChar, qtCharRep);
sp = qtChar + sp + qtChar;
return new Text(sp);
}

@Override
public String getDisplayString(String[] children) {
return getStandardDisplayString("quote", children);
}
}
@@ -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 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.Text;

import junit.framework.TestCase;
/**
* Source for TestGenericUDFQuote.
*/
public class TestGenericUDFQuote extends TestCase {
public TestGenericUDFQuote() {}
public void testQuote() throws HiveException {
GenericUDFQuote udf = new GenericUDFQuote();
ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
ObjectInspector[] arguments = {valueOI};

udf.initialize(arguments);
runAndVerify("", "''", udf);
runAndVerify(" ", "' '", udf);
runAndVerify("'", "'\\''", udf);
runAndVerify("DONT", "'DONT'", udf);
runAndVerify(" DON'T", "' DON\\'T'", udf);
runAndVerify("DON\\'T", "'DON\\\\'T'", udf);
}

private void runAndVerify(String str, String expResult, GenericUDF udf) throws HiveException {
DeferredObject valueObj = new DeferredJavaObject(new Text(str));
DeferredObject[] args = {valueObj};
Text output = (Text) udf.evaluate(args);
if (expResult != null) {
assertEquals("quote() test ", expResult, output.toString());
} else {
assertNull("quote() test ", output.toString());
}
}
}
2 changes: 2 additions & 0 deletions ql/src/test/results/clientpositive/show_functions.q.out
Expand Up @@ -200,6 +200,7 @@ pow
power
printf
quarter
quote
radians
rand
rank
Expand Down Expand Up @@ -370,6 +371,7 @@ parse_url_tuple
percentile
posexplode
positive
quote
regexp_replace
regr_slope
replace
Expand Down