use of org.apache.drill.exec.resolver.FunctionResolver in project drill by apache.
the class TypeInferenceUtils method resolveDrillFuncHolder.
private static DrillFuncHolder resolveDrillFuncHolder(final SqlOperatorBinding opBinding, final List<DrillFuncHolder> functions) {
final FunctionCall functionCall = convertSqlOperatorBindingToFunctionCall(opBinding);
final FunctionResolver functionResolver = FunctionResolverFactory.getResolver(functionCall);
final DrillFuncHolder func = functionResolver.getBestMatch(functions, functionCall);
// if no DrillFuncHolder matched for the given list of operand types
if (func == null) {
String operandTypes = "";
for (int i = 0; i < opBinding.getOperandCount(); ++i) {
operandTypes += opBinding.getOperandType(i).getSqlTypeName();
if (i < opBinding.getOperandCount() - 1) {
operandTypes += ",";
}
}
throw UserException.functionError().message(String.format("%s does not support operand types (%s)", opBinding.getOperator().getName(), operandTypes)).build(logger);
}
return func;
}
use of org.apache.drill.exec.resolver.FunctionResolver in project drill by apache.
the class ExpressionTreeMaterializer method addCastExpression.
public static LogicalExpression addCastExpression(LogicalExpression fromExpr, MajorType toType, FunctionLookupContext functionLookupContext, ErrorCollector errorCollector, boolean exactResolver) {
String castFuncName = CastFunctions.getCastFunc(toType.getMinorType());
List<LogicalExpression> castArgs = Lists.newArrayList();
//input_expr
castArgs.add(fromExpr);
if (fromExpr.getMajorType().getMinorType() == MinorType.UNION && toType.getMinorType() == MinorType.UNION) {
return fromExpr;
}
if (!Types.isFixedWidthType(toType) && !Types.isUnion(toType)) {
/* We are implicitly casting to VARCHAR so we don't have a max length,
* using an arbitrary value. We trim down the size of the stored bytes
* to the actual size so this size doesn't really matter.
*/
castArgs.add(new ValueExpressions.LongExpression(Types.MAX_VARCHAR_LENGTH, null));
} else if (CoreDecimalUtility.isDecimalType(toType)) {
// Add the scale and precision to the arguments of the implicit cast
castArgs.add(new ValueExpressions.LongExpression(toType.getPrecision(), null));
castArgs.add(new ValueExpressions.LongExpression(toType.getScale(), null));
}
FunctionCall castCall = new FunctionCall(castFuncName, castArgs, ExpressionPosition.UNKNOWN);
FunctionResolver resolver;
if (exactResolver) {
resolver = FunctionResolverFactory.getExactResolver(castCall);
} else {
resolver = FunctionResolverFactory.getResolver(castCall);
}
DrillFuncHolder matchedCastFuncHolder = functionLookupContext.findDrillFunction(resolver, castCall);
if (matchedCastFuncHolder == null) {
logFunctionResolutionError(errorCollector, castCall);
return NullExpression.INSTANCE;
}
return matchedCastFuncHolder.getExpr(castFuncName, castArgs, ExpressionPosition.UNKNOWN);
}
use of org.apache.drill.exec.resolver.FunctionResolver in project drill by apache.
the class FunctionImplementationRegistry method findDrillFunction.
/**
* First attempts to find the Drill function implementation that matches the name, arg types and return type.
* If exact function implementation was not found,
* syncs local function registry with remote function registry if needed
* and tries to find function implementation one more time
* but this time using given <code>functionResolver</code>.
*
* @param functionResolver function resolver
* @param functionCall function call
* @return best matching function holder
*/
@Override
public DrillFuncHolder findDrillFunction(FunctionResolver functionResolver, FunctionCall functionCall) {
AtomicLong version = new AtomicLong();
String newFunctionName = functionReplacement(functionCall);
if (useDynamicUdfs) {
List<DrillFuncHolder> functions = localFunctionRegistry.getMethods(newFunctionName, version);
FunctionResolver exactResolver = FunctionResolverFactory.getExactResolver(functionCall);
DrillFuncHolder holder = exactResolver.getBestMatch(functions, functionCall);
if (holder != null) {
return holder;
}
syncWithRemoteRegistry(version.get());
}
// Whether Dynamic UDFs or not: look in the registry for
// an inexact match.
List<DrillFuncHolder> functions = localFunctionRegistry.getMethods(newFunctionName, version);
return functionResolver.getBestMatch(functions, functionCall);
}
use of org.apache.drill.exec.resolver.FunctionResolver in project drill by apache.
the class ExpressionTreeMaterializer method convertToNullableType.
public static LogicalExpression convertToNullableType(LogicalExpression fromExpr, MinorType toType, FunctionLookupContext functionLookupContext, ErrorCollector errorCollector) {
String funcName = "convertToNullable" + toType.toString();
List<LogicalExpression> args = Lists.newArrayList();
args.add(fromExpr);
FunctionCall funcCall = new FunctionCall(funcName, args, ExpressionPosition.UNKNOWN);
FunctionResolver resolver = FunctionResolverFactory.getResolver(funcCall);
DrillFuncHolder matchedConvertToNullableFuncHolder = functionLookupContext.findDrillFunction(resolver, funcCall);
if (matchedConvertToNullableFuncHolder == null) {
logFunctionResolutionError(errorCollector, funcCall);
return NullExpression.INSTANCE;
}
return matchedConvertToNullableFuncHolder.getExpr(funcName, args, ExpressionPosition.UNKNOWN);
}
use of org.apache.drill.exec.resolver.FunctionResolver in project drill by apache.
the class TestSimpleFunctions method resolveHash.
public void resolveHash(DrillConfig config, LogicalExpression arg, TypeProtos.MajorType expectedArg, TypeProtos.MajorType expectedOut, TypeProtos.DataMode expectedBestInputMode, FunctionImplementationRegistry registry) throws JClassAlreadyExistsException, IOException {
final List<LogicalExpression> args = new ArrayList<>();
args.add(arg);
FunctionCall call = new FunctionCall("hash", args, ExpressionPosition.UNKNOWN);
final FunctionResolver resolver = FunctionResolverFactory.getResolver(call);
final DrillFuncHolder matchedFuncHolder = registry.findDrillFunction(resolver, call);
assertEquals(expectedBestInputMode, matchedFuncHolder.getParmMajorType(0).getMode());
}
Aggregations