use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by axbaretto.
the class ContainerVisitor method apply.
protected R apply(ValueVector vector, A arg) {
MaterializedField schema = vector.getField();
MajorType majorType = schema.getType();
MinorType type = majorType.getMinorType();
DataMode mode = majorType.getMode();
switch(type) {
case MAP:
if (mode == DataMode.REPEATED) {
return visitRepeatedMap((RepeatedMapVector) vector, arg);
} else {
return visitMap((AbstractMapVector) vector, arg);
}
case LIST:
if (mode == DataMode.REPEATED) {
return visitRepeatedList((RepeatedListVector) vector, arg);
} else {
return visitList((ListVector) vector, arg);
}
default:
if (mode == DataMode.REPEATED) {
return visitRepeatedPrimitive((BaseRepeatedValueVector) vector, arg);
} else {
return visitPrimitive(vector, arg);
}
}
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by axbaretto.
the class ParquetRecordWriter method getType.
private Type getType(MaterializedField field) {
MinorType minorType = field.getType().getMinorType();
DataMode dataMode = field.getType().getMode();
switch(minorType) {
case MAP:
List<Type> types = Lists.newArrayList();
for (MaterializedField childField : field.getChildren()) {
types.add(getType(childField));
}
return new GroupType(dataMode == DataMode.REPEATED ? Repetition.REPEATED : Repetition.OPTIONAL, field.getName(), types);
case LIST:
throw new UnsupportedOperationException("Unsupported type " + minorType);
default:
return getPrimitiveType(field);
}
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by axbaretto.
the class ParquetRecordWriter method getPrimitiveType.
private PrimitiveType getPrimitiveType(MaterializedField field) {
MinorType minorType = field.getType().getMinorType();
String name = field.getName();
PrimitiveTypeName primitiveTypeName = ParquetTypeHelper.getPrimitiveTypeNameForMinorType(minorType);
Repetition repetition = ParquetTypeHelper.getRepetitionForDataMode(field.getDataMode());
OriginalType originalType = ParquetTypeHelper.getOriginalTypeForMinorType(minorType);
DecimalMetadata decimalMetadata = ParquetTypeHelper.getDecimalMetadataForField(field);
int length = ParquetTypeHelper.getLengthForMinorType(minorType);
return new PrimitiveType(repetition, primitiveTypeName, length, name, originalType, decimalMetadata, null);
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by axbaretto.
the class TypeCastRules method getCost.
/*
* code decide whether it's legal to do implicit cast. -1 : not allowed for
* implicit cast > 0: cost associated with implicit cast. ==0: parms are
* exactly same type of arg. No need of implicit.
*/
public static int getCost(List<MajorType> argumentTypes, DrillFuncHolder holder) {
int cost = 0;
if (argumentTypes.size() != holder.getParamCount()) {
return -1;
}
// Indicates whether we used secondary cast rules
boolean secondaryCast = false;
// number of arguments that could implicitly casts using precedence map or didn't require casting at all
int nCasts = 0;
/*
* If we are determining function holder for decimal data type, we need to make sure the output type of
* the function can fit the precision that we need based on the input types.
*/
if (holder.checkPrecisionRange() == true) {
List<LogicalExpression> logicalExpressions = Lists.newArrayList();
for (MajorType majorType : argumentTypes) {
logicalExpressions.add(new MajorTypeInLogicalExpression(majorType));
}
if (DecimalUtility.getMaxPrecision(holder.getReturnType().getMinorType()) < holder.getReturnType(logicalExpressions).getPrecision()) {
return -1;
}
}
final int numOfArgs = holder.getParamCount();
for (int i = 0; i < numOfArgs; i++) {
final MajorType argType = argumentTypes.get(i);
final MajorType parmType = holder.getParmMajorType(i);
// @Param FieldReader will match any type
if (holder.isFieldReader(i)) {
// if (Types.isComplex(call.args.get(i).getMajorType()) ||Types.isRepeated(call.args.get(i).getMajorType()) )
// add the max cost when encountered with a field reader considering that it is the most expensive factor
// contributing to the cost.
cost += ResolverTypePrecedence.MAX_IMPLICIT_CAST_COST;
continue;
// else
// return -1;
}
if (!TypeCastRules.isCastableWithNullHandling(argType, parmType, holder.getNullHandling())) {
return -1;
}
Integer parmVal = ResolverTypePrecedence.precedenceMap.get(parmType.getMinorType());
Integer argVal = ResolverTypePrecedence.precedenceMap.get(argType.getMinorType());
if (parmVal == null) {
throw new RuntimeException(String.format("Precedence for type %s is not defined", parmType.getMinorType().name()));
}
if (argVal == null) {
throw new RuntimeException(String.format("Precedence for type %s is not defined", argType.getMinorType().name()));
}
if (parmVal - argVal < 0) {
/* Precedence rules does not allow to implicitly cast, however check
* if the seconday rules allow us to cast
*/
Set<MinorType> rules;
if ((rules = (ResolverTypePrecedence.secondaryImplicitCastRules.get(parmType.getMinorType()))) != null && rules.contains(argType.getMinorType()) != false) {
secondaryCast = true;
} else {
return -1;
}
}
// Otherwise, the function implementation is not a match.
if (argType.getMode() != parmType.getMode()) {
// this allows for a non-nullable implementation to be preferred
if (holder.getNullHandling() == NullHandling.INTERNAL) {
// a function that expects required output, but nullable was provided
if (parmType.getMode() == DataMode.REQUIRED && argType.getMode() == DataMode.OPTIONAL) {
return -1;
} else if (parmType.getMode() == DataMode.OPTIONAL && argType.getMode() == DataMode.REQUIRED) {
cost += DATAMODE_CAST_COST;
}
}
}
int castCost;
if ((castCost = (parmVal - argVal)) >= 0) {
nCasts++;
cost += castCost;
}
}
if (secondaryCast) {
// We have a secondary cast for one or more of the arguments, determine the cost associated
int secondaryCastCost = Integer.MAX_VALUE - 1;
// Subtract maximum possible implicit costs from the secondary cast cost
secondaryCastCost -= (nCasts * (ResolverTypePrecedence.MAX_IMPLICIT_CAST_COST + DATAMODE_CAST_COST));
// Add cost of implicitly casting the rest of the arguments that didn't use secondary casting
secondaryCastCost += cost;
return secondaryCastCost;
}
return cost;
}
use of org.apache.drill.common.types.TypeProtos.MinorType in project drill by axbaretto.
the class TypeCastRules method getLeastRestrictiveType.
/*
* Function checks if casting is allowed from the 'from' -> 'to' minor type. If its allowed
* we also check if the precedence map allows such a cast and return true if both cases are satisfied
*/
public static MinorType getLeastRestrictiveType(List<MinorType> types) {
assert types.size() >= 2;
MinorType result = types.get(0);
if (result == MinorType.UNION) {
return result;
}
int resultPrec = ResolverTypePrecedence.precedenceMap.get(result);
for (int i = 1; i < types.size(); i++) {
MinorType next = types.get(i);
if (next == MinorType.UNION) {
return next;
}
if (next == result) {
// both args are of the same type; continue
continue;
}
int nextPrec = ResolverTypePrecedence.precedenceMap.get(next);
if (isCastable(next, result) && resultPrec >= nextPrec) {
// result is the least restrictive between the two args; nothing to do continue
continue;
} else if (isCastable(result, next) && nextPrec >= resultPrec) {
result = next;
resultPrec = nextPrec;
} else {
return null;
}
}
return result;
}
Aggregations