.append(name) .append("` (") .append(type.getMinorType().name()); if (type.hasPrecision()) { builder.append("("); builder.append(type.getPrecision()); if (type.hasScale()) { builder.append(", "); builder.append(type.getScale()); .append(type.getMode().name()) .append(")"); if (type.getSubTypeCount() > 0) { builder .append(", subtypes=(") .append(type.getSubTypeList().toString()) .append(")");
@Override public MajorType getMajorType() { if (outputType != null) { return outputType; } MajorType elseType = elseExpression.getMajorType(); MajorType ifType = ifCondition.expression.getMajorType(); if (elseType.getMinorType() == MinorType.UNION) { Set<MinorType> subtypes = Sets.newHashSet(); for (MinorType subtype : elseType.getSubTypeList()) { subtypes.add(subtype); } for (MinorType subtype : ifType.getSubTypeList()) { subtypes.add(subtype); } MajorType.Builder builder = MajorType.newBuilder().setMinorType(MinorType.UNION).setMode(DataMode.OPTIONAL); for (MinorType subtype : subtypes) { builder.addSubType(subtype); } return builder.build(); } MajorType.Builder builder = MajorType.newBuilder().setMinorType(ifType.getMinorType()); builder.setMode(elseType.getMode() == DataMode.OPTIONAL || ifType.getMode() == DataMode.OPTIONAL ? DataMode.OPTIONAL : elseType.getMode()); builder = Types.calculateTypePrecisionAndScale(ifType, elseType, builder); return builder.build(); }
org.apache.drill.common.types.TypeProtos.MajorType.Builder subBuilder = null; if (((bitField0_ & 0x00000001) == 0x00000001)) { subBuilder = majorType_.toBuilder();
@Override public MajorType getMajorType() { if (outputType != null) { return outputType; } MajorType elseType = elseExpression.getMajorType(); MajorType ifType = ifCondition.expression.getMajorType(); if (elseType.getMinorType() == MinorType.UNION) { Set<MinorType> subtypes = Sets.newHashSet(); for (MinorType subtype : elseType.getSubTypeList()) { subtypes.add(subtype); } for (MinorType subtype : ifType.getSubTypeList()) { subtypes.add(subtype); } MajorType.Builder builder = MajorType.newBuilder().setMinorType(MinorType.UNION).setMode(DataMode.OPTIONAL); for (MinorType subtype : subtypes) { builder.addSubType(subtype); } return builder.build(); } MajorType.Builder builder = MajorType.newBuilder().setMinorType(ifType.getMinorType()); builder.setMode(elseType.getMode() == DataMode.OPTIONAL || ifType.getMode() == DataMode.OPTIONAL ? DataMode.OPTIONAL : elseType.getMode()); builder = Types.calculateTypePrecisionAndScale(ifType, elseType, builder); return builder.build(); }
/** * Defines function return type and sets precision if it can be calculated. * Return type precision is sum of input types precisions. * If at least one input type does not have precision, return type will be without precision. * If calculated precision is greater than {@link Types#MAX_VARCHAR_LENGTH}, * it is replaced with {@link Types#MAX_VARCHAR_LENGTH}. * * @param logicalExpressions logical expressions * @param attributes function attributes * @return return type */ @Override public TypeProtos.MajorType getType(List<LogicalExpression> logicalExpressions, FunctionAttributes attributes) { TypeProtos.MajorType.Builder builder = TypeProtos.MajorType.newBuilder() .setMinorType(attributes.getReturnValue().getType().getMinorType()) .setMode(FunctionUtils.getReturnTypeDataMode(logicalExpressions, attributes)); int totalPrecision = 0; for (LogicalExpression expression : logicalExpressions) { if (expression.getMajorType().hasPrecision()) { totalPrecision += expression.getMajorType().getPrecision(); } else { // if at least one expression has unknown precision, return type without precision return builder.build(); } } return builder.setPrecision(totalPrecision > Types.MAX_VARCHAR_LENGTH ? Types.MAX_VARCHAR_LENGTH : totalPrecision).build(); } }
private AbstractObjectWriter buildUnion(UnionVector vector, VectorDescrip descrip) { // Dummy writers are used when the schema is known up front, but the // query chooses not to project a column. Variants are used in the case when // the schema is not known, and we discover it on the fly. In this case, // (which currently occurs only in JSON) dummy vectors are not used. if (vector == null) { throw new UnsupportedOperationException("Dummy variant writer not yet supported"); } final AbstractObjectWriter variants[] = new AbstractObjectWriter[MinorType.values().length]; final MetadataProvider mdProvider = descrip.childProvider(); int i = 0; for (final MinorType type : vector.getField().getType().getSubTypeList()) { // This call will create the vector if it does not yet exist. // Will throw an exception for unsupported types. // so call this only if the MajorType reports that the type // already exists. final ValueVector memberVector = vector.getMember(type); final VectorDescrip memberDescrip = new VectorDescrip(mdProvider, i++, memberVector.getField()); variants[type.ordinal()] = buildVectorWriter(memberVector, memberDescrip); } return new VariantObjectWriter( new UnionWriterImpl(descrip.metadata, vector, variants)); }
/** * We treat fields with same set of Subtypes as equal, even if they are in a different order * @param t1 * @param t2 * @return */ private boolean majorTypeEqual(MajorType t1, MajorType t2) { if (t1.equals(t2)) { return true; } if (!t1.getMinorType().equals(t2.getMinorType())) { return false; } if (!t1.getMode().equals(t2.getMode())) { return false; } if (!Sets.newHashSet(t1.getSubTypeList()).equals(Sets.newHashSet(t2.getSubTypeList()))) { return false; } return true; }
/** * Sets max precision from both types if these types are string scalar types. * Sets max precision and scale from both types if these types are decimal types. * Both types should be of the same minor type. * * @param leftType type from left side * @param rightType type from right side * @param typeBuilder type builder * @return type builder */ public static MajorType.Builder calculateTypePrecisionAndScale(MajorType leftType, MajorType rightType, MajorType.Builder typeBuilder) { if (leftType.getMinorType().equals(rightType.getMinorType())) { boolean isScalarString = Types.isScalarStringType(leftType) && Types.isScalarStringType(rightType); boolean isDecimal = isDecimalType(leftType); if ((isScalarString || isDecimal) && leftType.hasPrecision() && rightType.hasPrecision()) { typeBuilder.setPrecision(Math.max(leftType.getPrecision(), rightType.getPrecision())); } if (isDecimal && leftType.hasScale() && rightType.hasScale()) { typeBuilder.setScale(Math.max(leftType.getScale(), rightType.getScale())); } } return typeBuilder; }
@Override public void setChildVector(ValueVector childVector) { // Unlike the repeated list vector, the (plain) list vector // adds the dummy vector as a child type. assert field.getChildren().size() == 1; assert field.getChildren().iterator().next().getType().getMinorType() == MinorType.LATE; field.removeChild(vector.getField()); super.setChildVector(childVector); // Initial LATE type vector not added as a subtype initially. // So, no need to remove it, just add the new subtype. Since the // MajorType is immutable, must build a new one and replace the type // in the materialized field. (We replace the type, rather than creating // a new materialized field, to preserve the link to this field from // a parent map, list or union.) assert field.getType().getSubTypeCount() == 0; field.replaceType( field.getType().toBuilder() .addSubType(childVector.getField().getType().getMinorType()) .build()); }
private AbstractObjectReader buildUnion(UnionVector vector, VectorAccessor unionAccessor, VectorDescrip descrip) { final MetadataProvider provider = descrip.childProvider(); final AbstractObjectReader variants[] = new AbstractObjectReader[MinorType.values().length]; int i = 0; for (final MinorType type : vector.getField().getType().getSubTypeList()) { // This call will create the vector if it does not yet exist. // Will throw an exception for unsupported types. // so call this only if the MajorType reports that the type // already exists. final ValueVector memberVector = vector.getMember(type); final VectorDescrip memberDescrip = new VectorDescrip(provider, i++, memberVector.getField()); variants[type.ordinal()] = buildVectorReader(memberVector, memberDescrip); } return UnionReaderImpl.build( descrip.metadata, unionAccessor, variants); }
public void writeTo(com.dyuproject.protostuff.Output output, org.apache.drill.common.types.TypeProtos.MajorType message) throws java.io.IOException { if(message.hasMinorType()) output.writeEnum(1, message.getMinorType().getNumber(), false); if(message.hasMode()) output.writeEnum(2, message.getMode().getNumber(), false); if(message.hasWidth()) output.writeInt32(3, message.getWidth(), false); if(message.hasPrecision()) output.writeInt32(4, message.getPrecision(), false); if(message.hasScale()) output.writeInt32(5, message.getScale(), false); if(message.hasTimeZone()) output.writeInt32(6, message.getTimeZone(), false); for(org.apache.drill.common.types.TypeProtos.MinorType subType : message.getSubTypeList()) output.writeEnum(7, subType.getNumber(), true); } public boolean isInitialized(org.apache.drill.common.types.TypeProtos.MajorType message)
public MaterializedField getOtherNullableVersion() { MajorType mt = type; DataMode newDataMode; switch (mt.getMode()){ case OPTIONAL: newDataMode = DataMode.REQUIRED; break; case REQUIRED: newDataMode = DataMode.OPTIONAL; break; default: throw new UnsupportedOperationException(); } return new MaterializedField(name, mt.toBuilder().setMode(newDataMode).build(), children); }
public static MajorTypeHolder get(MajorType mt) { MajorTypeHolder h = new MajorTypeHolder(); h.minorType = mt.getMinorType(); h.mode = mt.getMode(); if (mt.hasPrecision()) { h.precision = mt.getPrecision(); } if (mt.hasScale()) { h.scale = mt.getScale(); } if (mt.hasWidth()) { h.width = mt.getWidth(); } return h; } }
public List<MinorType> getSubTypes() { return field.getType().getSubTypeList(); }
MajorType getLastPathType() { if ((this.getField().getType().getMinorType() == MinorType.LIST && this.getField().getType().getMode() == DataMode.REPEATED)) { // Use Repeated scalar type instead of Required List. VectorWithOrdinal vord = getChildVectorWithOrdinal(null); ValueVector v = vord.vector; if (!(v instanceof AbstractContainerVector)) { return v.getField().getType(); } } else if (this.getField().getType().getMinorType() == MinorType.MAP && this.getField().getType().getMode() == DataMode.REPEATED) { // Use Required Map return this.getField().getType().toBuilder().setMode(DataMode.REQUIRED).build(); } return this.getField().getType(); }
@Override public OutputWidthExpression visitVarDecimalConstant(VarDecimalExpression varDecimalExpression, OutputWidthVisitorState state) throws RuntimeException { Preconditions.checkArgument(varDecimalExpression.getMajorType().hasPrecision()); return new FixedLenExpr(varDecimalExpression.getMajorType().getPrecision()); }
@Override public OutputWidthExpression visitNullConstant(TypedNullConstant nullConstant, OutputWidthVisitorState state) throws RuntimeException { int width; if (nullConstant.getMajorType().hasPrecision()) { width = nullConstant.getMajorType().getPrecision(); } else { width = 0; } return new FixedLenExpr(width); }
/** * Get the <code>precision</code> of given type. * * @param majorType major type * @return precision value */ public static int getPrecision(MajorType majorType) { if (majorType.hasPrecision()) { return majorType.getPrecision(); } return isScalarStringType(majorType) ? MAX_VARCHAR_LENGTH : UNDEFINED; }
public MaterializedField cloneEmpty() { return create(name, type.toBuilder() .clearSubType() .build()); }
private VariantSchema inferUnionSchema(UnionVector vector) { final VariantSchema schema = new VariantSchema(); for (final MinorType type : vector.getField().getType().getSubTypeList()) { schema.addType(inferVector(vector.getMember(type))); } return schema; } }