use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.
the class FunctionConverter method getHolder.
public <T extends DrillFunc> DrillFuncHolder getHolder(AnnotatedClassDescriptor func, ClassLoader classLoader) {
FunctionTemplate template = func.getAnnotationProxy(FunctionTemplate.class);
if (template == null) {
return failure("Class does not declare FunctionTemplate annotation.", func);
}
String name = template.name();
List<String> names = Arrays.asList(template.names());
if (name.isEmpty() && names.isEmpty()) {
// none set
return failure("Must define 'name' or 'names'", func);
}
if (!name.isEmpty() && !names.isEmpty()) {
// both are set
return failure("Must use only one annotations 'name' or 'names', not both", func);
}
// start by getting field information.
List<ValueReference> params = Lists.newArrayList();
List<WorkspaceReference> workspaceFields = Lists.newArrayList();
ValueReference outputField = null;
for (FieldDescriptor field : func.getFields()) {
Param param = field.getAnnotationProxy(Param.class);
Output output = field.getAnnotationProxy(Output.class);
Workspace workspace = field.getAnnotationProxy(Workspace.class);
Inject inject = field.getAnnotationProxy(Inject.class);
Annotation[] annotations = { param, output, workspace, inject };
int annotationCount = 0;
for (Annotation annotationDescriptor : annotations) {
if (annotationDescriptor != null) {
annotationCount += 1;
}
}
if (annotationCount == 0) {
return failure("The field must be either a @Param, @Output, @Inject or @Workspace field.", func, field);
} else if (annotationCount > 1) {
return failure("The field must be only one of @Param, @Output, @Inject or @Workspace. It currently has more than one of these annotations.", func, field);
}
// TODO(Julien): verify there are a few of those and we can load them
Class<?> fieldClass = field.getFieldClass();
if (param != null || output != null) {
// Special processing for @Param FieldReader
if (param != null && FieldReader.class.isAssignableFrom(fieldClass)) {
params.add(ValueReference.createFieldReaderRef(field.getName()));
continue;
}
// Special processing for @Output ComplexWriter
if (output != null && ComplexWriter.class.isAssignableFrom(fieldClass)) {
if (outputField != null) {
return failure("You've declared more than one @Output field. You must declare one and only @Output field per Function class.", func, field);
} else {
outputField = ValueReference.createComplexWriterRef(field.getName());
}
continue;
}
// check that param and output are value holders.
if (!ValueHolder.class.isAssignableFrom(fieldClass)) {
return failure(String.format("The field doesn't holds value of type %s which does not implement the ValueHolder interface. All fields of type @Param or @Output must extend this interface..", fieldClass), func, field);
}
// get the type field from the value holder.
MajorType type = null;
try {
type = getStaticFieldValue("TYPE", fieldClass, MajorType.class);
} catch (Exception e) {
return failure("Failure while trying to access the ValueHolder's TYPE static variable. All ValueHolders must contain a static TYPE variable that defines their MajorType.", e, func, field);
}
ValueReference p = new ValueReference(type, field.getName());
if (param != null) {
p.setConstant(param.constant());
params.add(p);
} else {
if (outputField != null) {
return failure("You've declared more than one @Output field. You must declare one and only @Output field per Function class.", func, field);
} else {
outputField = p;
}
}
} else {
// workspace work.
boolean isInject = inject != null;
if (isInject && UdfUtilities.INJECTABLE_GETTER_METHODS.get(fieldClass) == null) {
return failure(String.format("A %s cannot be injected into a %s," + " available injectable classes are: %s.", fieldClass, DrillFunc.class.getSimpleName(), Joiner.on(",").join(UdfUtilities.INJECTABLE_GETTER_METHODS.keySet())), func, field);
}
WorkspaceReference wsReference = new WorkspaceReference(fieldClass, field.getName(), isInject);
if (!isInject && template.scope() == FunctionScope.POINT_AGGREGATE && !ValueHolder.class.isAssignableFrom(fieldClass)) {
return failure(String.format("Aggregate function '%s' workspace variable '%s' is of type '%s'. Please change it to Holder type.", func.getClassName(), field.getName(), fieldClass), func, field);
}
//If the workspace var is of Holder type, get its MajorType and assign to WorkspaceReference.
if (ValueHolder.class.isAssignableFrom(fieldClass)) {
MajorType majorType = null;
try {
majorType = getStaticFieldValue("TYPE", fieldClass, MajorType.class);
} catch (Exception e) {
return failure("Failure while trying to access the ValueHolder's TYPE static variable. All ValueHolders must contain a static TYPE variable that defines their MajorType.", e, func, field);
}
wsReference.setMajorType(majorType);
}
workspaceFields.add(wsReference);
}
}
if (outputField == null) {
return failure("This function declares zero output fields. A function must declare one output field.", func);
}
FunctionInitializer initializer = new FunctionInitializer(func.getClassName(), classLoader);
try {
// return holder
ValueReference[] ps = params.toArray(new ValueReference[params.size()]);
WorkspaceReference[] works = workspaceFields.toArray(new WorkspaceReference[workspaceFields.size()]);
FunctionAttributes functionAttributes = new FunctionAttributes(template, ps, outputField, works);
switch(template.scope()) {
case POINT_AGGREGATE:
return new DrillAggFuncHolder(functionAttributes, initializer);
case SIMPLE:
return outputField.isComplexWriter() ? new DrillComplexWriterFuncHolder(functionAttributes, initializer) : new DrillSimpleFuncHolder(functionAttributes, initializer);
case HOLISTIC_AGGREGATE:
case RANGE_AGGREGATE:
default:
return failure("Unsupported Function Type.", func);
}
} catch (Exception | NoSuchFieldError | AbstractMethodError ex) {
return failure("Failure while creating function holder.", ex, func);
}
}
use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.
the class RangeExprEvaluator method evalCastFunc.
private Statistics evalCastFunc(FunctionHolderExpression holderExpr, Statistics input) {
try {
DrillSimpleFuncHolder funcHolder = (DrillSimpleFuncHolder) holderExpr.getHolder();
DrillSimpleFunc interpreter = funcHolder.createInterpreter();
final ValueHolder minHolder, maxHolder;
TypeProtos.MinorType srcType = holderExpr.args.get(0).getMajorType().getMinorType();
TypeProtos.MinorType destType = holderExpr.getMajorType().getMinorType();
if (srcType.equals(destType)) {
// same type cast ==> NoOp.
return input;
} else if (!CAST_FUNC.containsKey(srcType) || !CAST_FUNC.get(srcType).contains(destType)) {
// cast func between srcType and destType is NOT allowed.
return null;
}
switch(srcType) {
case INT:
minHolder = ValueHolderHelper.getIntHolder(((IntStatistics) input).getMin());
maxHolder = ValueHolderHelper.getIntHolder(((IntStatistics) input).getMax());
break;
case BIGINT:
minHolder = ValueHolderHelper.getBigIntHolder(((LongStatistics) input).getMin());
maxHolder = ValueHolderHelper.getBigIntHolder(((LongStatistics) input).getMax());
break;
case FLOAT4:
minHolder = ValueHolderHelper.getFloat4Holder(((FloatStatistics) input).getMin());
maxHolder = ValueHolderHelper.getFloat4Holder(((FloatStatistics) input).getMax());
break;
case FLOAT8:
minHolder = ValueHolderHelper.getFloat8Holder(((DoubleStatistics) input).getMin());
maxHolder = ValueHolderHelper.getFloat8Holder(((DoubleStatistics) input).getMax());
break;
default:
return null;
}
final ValueHolder[] args1 = { minHolder };
final ValueHolder[] args2 = { maxHolder };
final ValueHolder minFuncHolder = InterpreterEvaluator.evaluateFunction(interpreter, args1, holderExpr.getName());
final ValueHolder maxFuncHolder = InterpreterEvaluator.evaluateFunction(interpreter, args2, holderExpr.getName());
switch(destType) {
//TODO : need handle # of nulls.
case INT:
return getStatistics(((IntHolder) minFuncHolder).value, ((IntHolder) maxFuncHolder).value);
case BIGINT:
return getStatistics(((BigIntHolder) minFuncHolder).value, ((BigIntHolder) maxFuncHolder).value);
case FLOAT4:
return getStatistics(((Float4Holder) minFuncHolder).value, ((Float4Holder) maxFuncHolder).value);
case FLOAT8:
return getStatistics(((Float8Holder) minFuncHolder).value, ((Float8Holder) maxFuncHolder).value);
default:
return null;
}
} catch (Exception e) {
throw new DrillRuntimeException("Error in evaluating function of " + holderExpr.getName());
}
}
use of org.apache.drill.exec.expr.holders.ValueHolder in project drill by apache.
the class FragmentContext method getConstantValueHolder.
@Override
public ValueHolder getConstantValueHolder(String value, MinorType type, Function<DrillBuf, ValueHolder> holderInitializer) {
if (!constantValueHolderCache.containsKey(value)) {
constantValueHolderCache.put(value, Maps.<MinorType, ValueHolder>newHashMap());
}
Map<MinorType, ValueHolder> holdersByType = constantValueHolderCache.get(value);
ValueHolder valueHolder = holdersByType.get(type);
if (valueHolder == null) {
valueHolder = holderInitializer.apply(getManagedBuffer());
holdersByType.put(type, valueHolder);
}
return valueHolder;
}
Aggregations