Search in sources :

Example 51 with MinorType

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);
            }
    }
}
Also used : MajorType(org.apache.drill.common.types.TypeProtos.MajorType) DataMode(org.apache.drill.common.types.TypeProtos.DataMode) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) MaterializedField(org.apache.drill.exec.record.MaterializedField)

Example 52 with MinorType

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);
    }
}
Also used : PrimitiveType(org.apache.parquet.schema.PrimitiveType) GroupType(org.apache.parquet.schema.GroupType) MessageType(org.apache.parquet.schema.MessageType) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) Type(org.apache.parquet.schema.Type) OriginalType(org.apache.parquet.schema.OriginalType) GroupType(org.apache.parquet.schema.GroupType) DataMode(org.apache.drill.common.types.TypeProtos.DataMode) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) MaterializedField(org.apache.drill.exec.record.MaterializedField)

Example 53 with MinorType

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);
}
Also used : OriginalType(org.apache.parquet.schema.OriginalType) MinorType(org.apache.drill.common.types.TypeProtos.MinorType) PrimitiveType(org.apache.parquet.schema.PrimitiveType) DecimalMetadata(org.apache.parquet.schema.DecimalMetadata) Repetition(org.apache.parquet.schema.Type.Repetition) PrimitiveTypeName(org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName)

Example 54 with MinorType

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;
}
Also used : MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) MinorType(org.apache.drill.common.types.TypeProtos.MinorType)

Example 55 with MinorType

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;
}
Also used : MinorType(org.apache.drill.common.types.TypeProtos.MinorType)

Aggregations

MinorType (org.apache.drill.common.types.TypeProtos.MinorType)86 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)32 MaterializedField (org.apache.drill.exec.record.MaterializedField)17 ValueVector (org.apache.drill.exec.vector.ValueVector)11 DataMode (org.apache.drill.common.types.TypeProtos.DataMode)10 SchemaBuilder (org.apache.drill.exec.record.metadata.SchemaBuilder)8 TupleMetadata (org.apache.drill.exec.record.metadata.TupleMetadata)7 SubOperatorTest (org.apache.drill.test.SubOperatorTest)6 Test (org.junit.Test)6 ImmutableList (com.google.common.collect.ImmutableList)5 SchemaPath (org.apache.drill.common.expression.SchemaPath)5 ValueHolder (org.apache.drill.exec.expr.holders.ValueHolder)5 IOException (java.io.IOException)4 UserException (org.apache.drill.common.exceptions.UserException)4 OriginalType (org.apache.parquet.schema.OriginalType)4 PrimitiveType (org.apache.parquet.schema.PrimitiveType)4 SQLException (java.sql.SQLException)3 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)3 SchemaChangeException (org.apache.drill.exec.exception.SchemaChangeException)3 ExtendableRowSet (org.apache.drill.exec.physical.rowSet.RowSet.ExtendableRowSet)3