Search in sources :

Example 11 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class ExternalDataUtils method getValueParserFactories.

public static IValueParserFactory[] getValueParserFactories(ARecordType recordType) {
    int n = recordType.getFieldTypes().length;
    IValueParserFactory[] fieldParserFactories = new IValueParserFactory[n];
    for (int i = 0; i < n; i++) {
        ATypeTag tag = null;
        if (recordType.getFieldTypes()[i].getTypeTag() == ATypeTag.UNION) {
            AUnionType unionType = (AUnionType) recordType.getFieldTypes()[i];
            if (!unionType.isUnknownableType()) {
                throw new NotImplementedException("Non-optional UNION type is not supported.");
            }
            tag = unionType.getActualType().getTypeTag();
        } else {
            tag = recordType.getFieldTypes()[i].getTypeTag();
        }
        if (tag == null) {
            throw new NotImplementedException("Failed to get the type information for field " + i + ".");
        }
        fieldParserFactories[i] = getParserFactory(tag);
    }
    return fieldParserFactories;
}
Also used : IValueParserFactory(org.apache.hyracks.dataflow.common.data.parsers.IValueParserFactory) ATypeTag(org.apache.asterix.om.types.ATypeTag) AUnionType(org.apache.asterix.om.types.AUnionType) NotImplementedException(org.apache.hyracks.algebricks.common.exceptions.NotImplementedException)

Example 12 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class JSONDeserializerForTypes method convertFromJSON.

/**
     * Deserialize an arbitrary JSON representation of a type.
     *
     * @param typeInJSON
     *            the JSON representation of the type.
     * @return an valid AsterixDB type.
     * @throws Exception
     */
public static IAType convertFromJSON(JsonNode typeInJSON) throws Exception {
    String typeName = typeInJSON.get("type").asText();
    // Deals with ordered list.
    if (typeName.equals(AOrderedListType.class.getName())) {
        IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
        return new AOrderedListType(itemType, "ordered-list");
    }
    // Deals with unordered list.
    if (typeName.equals(AUnorderedListType.class.getName())) {
        IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
        return new AUnorderedListType(itemType, "unordered-list");
    }
    // Deals with Union Type.
    if (typeName.equals(AUnionType.class.getName())) {
        List<IAType> unionTypes = new ArrayList<IAType>();
        JsonNode fields = typeInJSON.get("fields");
        for (int i = 0; i < fields.size(); i++) {
            JsonNode fieldType = fields.get(i);
            unionTypes.add(convertFromJSON(fieldType));
        }
        return new AUnionType(unionTypes, "union");
    }
    // Deals with record types.
    if (typeName.equals(ARecordType.class.getName())) {
        String name = typeInJSON.get("name").asText();
        boolean openType = typeInJSON.get("open").asBoolean();
        JsonNode fields = typeInJSON.get("fields");
        String[] fieldNames = new String[fields.size()];
        IAType[] fieldTypes = new IAType[fields.size()];
        for (int i = 0; i < fields.size(); ++i) {
            JsonNode field = fields.get(i);
            List<String> names = Lists.newArrayList(field.fieldNames());
            String fieldName = names.get(0);
            fieldNames[i] = fieldName;
            fieldTypes[i] = convertFromJSON(field.get(fieldName));
        }
        return new ARecordType(name, fieldNames, fieldTypes, openType);
    }
    // Deals with primitive types.
    Class<?> cl = BuiltinType.class;
    Field typeField = cl.getDeclaredField(typeName.toUpperCase());
    return (IAType) typeField.get(null);
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) BuiltinType(org.apache.asterix.om.types.BuiltinType) Field(java.lang.reflect.Field) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 13 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class OpenRecordConstructorResultType method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    /**
         * if type has been top-down propagated, use the enforced type
         */
    ARecordType type = (ARecordType) TypeCastUtils.getRequiredType(f);
    if (type != null) {
        return type;
    }
    Iterator<Mutable<ILogicalExpression>> argIter = f.getArguments().iterator();
    List<String> namesList = new ArrayList<>();
    List<IAType> typesList = new ArrayList<>();
    // The following set of names do not belong to the closed part,
    // but are additional possible field names. For example, a field "foo" with type
    // ANY cannot be in the closed part, but "foo" is a possible field name.
    Set<String> allPossibleAdditionalFieldNames = new HashSet<>();
    boolean canProvideAdditionFieldInfo = true;
    boolean isOpen = false;
    while (argIter.hasNext()) {
        ILogicalExpression e1 = argIter.next().getValue();
        ILogicalExpression e2 = argIter.next().getValue();
        IAType t2 = (IAType) env.getType(e2);
        String fieldName = ConstantExpressionUtil.getStringConstant(e1);
        if (fieldName != null && t2 != null && TypeHelper.isClosed(t2)) {
            namesList.add(fieldName);
            if (t2.getTypeTag() == ATypeTag.UNION) {
                AUnionType unionType = (AUnionType) t2;
                t2 = AUnionType.createUnknownableType(unionType.getActualType());
            }
            typesList.add(t2);
        } else {
            if (canProvideAdditionFieldInfo && fieldName != null) {
                allPossibleAdditionalFieldNames.add(fieldName);
            } else {
                canProvideAdditionFieldInfo = false;
            }
            isOpen = true;
        }
    }
    String[] fieldNames = namesList.toArray(new String[0]);
    IAType[] fieldTypes = typesList.toArray(new IAType[0]);
    return canProvideAdditionFieldInfo ? new ARecordType(null, fieldNames, fieldTypes, isOpen, allPossibleAdditionalFieldNames) : new ARecordType(null, fieldNames, fieldTypes, isOpen);
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType) HashSet(java.util.HashSet)

Example 14 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class NotUnknownTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    IAType type = (IAType) env.getType(f.getArguments().get(0).getValue());
    if (type.getTypeTag() != ATypeTag.UNION) {
        // directly return the input type if it is not a union
        return type;
    }
    AUnionType unionType = (AUnionType) type;
    return unionType.getActualType();
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) IAType(org.apache.asterix.om.types.IAType)

Example 15 with AUnionType

use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.

the class TypeComputeUtils method resolveCateogry.

private static byte resolveCateogry(IAType... inputTypes) {
    byte category = CERTAIN;
    boolean meetNull = false;
    for (IAType inputType : inputTypes) {
        switch(inputType.getTypeTag()) {
            case UNION:
                AUnionType unionType = (AUnionType) inputType;
                if (unionType.isNullableType()) {
                    category |= NULLABLE;
                }
                if (unionType.isMissableType()) {
                    category |= MISSABLE;
                }
                break;
            case MISSING:
                return MISSING;
            case NULL:
                meetNull = true;
                break;
            case ANY:
                category |= NULLABLE;
                category |= MISSABLE;
                break;
            default:
                break;
        }
    }
    if (meetNull) {
        return NULL;
    }
    return category;
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

AUnionType (org.apache.asterix.om.types.AUnionType)32 IAType (org.apache.asterix.om.types.IAType)31 ATypeTag (org.apache.asterix.om.types.ATypeTag)13 ARecordType (org.apache.asterix.om.types.ARecordType)11 ArrayList (java.util.ArrayList)10 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)7 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)6 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)5 AUnorderedListType (org.apache.asterix.om.types.AUnorderedListType)5 Mutable (org.apache.commons.lang3.mutable.Mutable)5 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)5 DataOutput (java.io.DataOutput)4 AString (org.apache.asterix.om.base.AString)4 IOException (java.io.IOException)3 List (java.util.List)3 RuntimeDataException (org.apache.asterix.common.exceptions.RuntimeDataException)3 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)3 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)3 HashSet (java.util.HashSet)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2