Skip to content

Commit

Permalink
DRILL-4456: Fix Hive translate UDF
Browse files Browse the repository at this point in the history
  • Loading branch information
vvysotskyi committed Nov 7, 2018
1 parent ad61c6b commit 0109c8b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;

import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.sql.SqlOperatorBinding;
import org.apache.calcite.sql.fun.OracleSqlOperatorTable;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.type.SqlReturnTypeInference;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.drill.common.config.DrillConfig;
Expand All @@ -38,6 +41,7 @@
import org.apache.drill.exec.planner.sql.HiveUDFOperator;
import org.apache.drill.exec.planner.sql.HiveUDFOperatorWithoutInference;
import org.apache.drill.exec.planner.sql.TypeInferenceUtils;
import org.apache.drill.shaded.guava.com.google.common.collect.ImmutableMap;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.udf.UDFType;
Expand All @@ -48,8 +52,17 @@
import org.apache.drill.shaded.guava.com.google.common.collect.ArrayListMultimap;
import org.apache.drill.shaded.guava.com.google.common.collect.Sets;

public class HiveFunctionRegistry implements PluggableFunctionRegistry{
static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HiveFunctionRegistry.class);
public class HiveFunctionRegistry implements PluggableFunctionRegistry {
private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(HiveFunctionRegistry.class);

/**
* Map for renaming Hive UDFs whose names satisfy the predicate in the key by names from the map value.
*/
private static final Map<Predicate<String[]>, String[]> FUNCTION_REPLACE_MAP = ImmutableMap.<Predicate<String[]>, String[]> builder()
.put(names -> names.length == 1
&& SqlStdOperatorTable.TRANSLATE.getName().equalsIgnoreCase(names[0]),
new String[]{OracleSqlOperatorTable.TRANSLATE3.getName().toLowerCase()}) // renames Hive's TRANSLATE UDF to TRANSLATE3 due to CALCITE-1115
.build();

private ArrayListMultimap<String, Class<? extends GenericUDF>> methodsGenericUDF = ArrayListMultimap.create();
private ArrayListMultimap<String, Class<? extends UDF>> methodsUDF = ArrayListMultimap.create();
Expand Down Expand Up @@ -102,15 +115,15 @@ public void register(DrillOperatorTable operatorTable) {
}
}

private <C,I> void register(Class<? extends I> clazz, ArrayListMultimap<String,Class<? extends I>> methods) {
private <I> void register(Class<? extends I> clazz, ArrayListMultimap<String, Class<? extends I>> methods) {
Description desc = clazz.getAnnotation(Description.class);
String[] names;
if (desc != null) {
names = desc.name().split(",");
for (int i=0; i<names.length; i++) {
for (int i = 0; i < names.length; i++) {
names[i] = names[i].trim();
}
}else{
} else {
names = new String[]{clazz.getName().replace('.', '_')};
}

Expand All @@ -119,10 +132,26 @@ private <C,I> void register(Class<? extends I> clazz, ArrayListMultimap<String,C
nonDeterministicUDFs.add(clazz);
}

names = renameUDF(names);

for (String name : names) {
methods.put(name.toLowerCase(), clazz);
}
}

for(int i=0; i<names.length;i++) {
methods.put(names[i].toLowerCase(), clazz);
/**
* Checks using predicate from {@link #FUNCTION_REPLACE_MAP}
* whether specified function names should be replaced
* with the names from the map.
*/
private String[] renameUDF(String[] namesToCheck) {
for (Map.Entry<Predicate<String[]>, String[]> predicateEntry : FUNCTION_REPLACE_MAP.entrySet()) {
if (predicateEntry.getKey().test(namesToCheck)) {
return predicateEntry.getValue();
}
}

return namesToCheck;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,17 @@ public void testToUTCTimestamp() throws Exception {
.go();
}

@Test // DRILL-4456
public void testTranslate3() throws Exception {
testBuilder()
.sqlQuery("SELECT translate(string_field, 's', 'S') as ts," +
"translate(varchar_field, 'v', 'V') as tv,\n" +
"translate('literal', 'l', 'L') as tl from hive.readtest")
.unOrdered()
.baselineColumns("ts", "tv", "tl")
.baselineValues("Stringfield", "Varcharfield", "LiteraL")
.baselineValues(null, null, "LiteraL")
.go();
}

}

0 comments on commit 0109c8b

Please sign in to comment.