Search in sources :

Example 31 with IAType

use of org.apache.asterix.om.types.IAType 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 32 with IAType

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

the class RecordMergeTypeComputer method mergedNestedType.

private IAType mergedNestedType(String fieldName, IAType fieldType1, IAType fieldType0) throws AlgebricksException {
    if (fieldType1.getTypeTag() != ATypeTag.OBJECT || fieldType0.getTypeTag() != ATypeTag.OBJECT) {
        throw new CompilationException(ErrorCode.COMPILATION_DUPLICATE_FIELD_NAME, fieldName);
    }
    ARecordType resultType = (ARecordType) fieldType0;
    ARecordType fieldType1Copy = (ARecordType) fieldType1;
    for (int i = 0; i < fieldType1Copy.getFieldTypes().length; i++) {
        String fname = fieldType1Copy.getFieldNames()[i];
        int pos = resultType.getFieldIndex(fname);
        if (pos >= 0) {
            // If a sub-record do merge, else ignore and let the values decide what to do
            if (fieldType1Copy.getFieldTypes()[i].getTypeTag() == ATypeTag.OBJECT) {
                IAType[] oldTypes = resultType.getFieldTypes();
                oldTypes[pos] = mergedNestedType(fname, fieldType1Copy.getFieldTypes()[i], resultType.getFieldTypes()[pos]);
                resultType = new ARecordType(resultType.getTypeName(), resultType.getFieldNames(), oldTypes, resultType.isOpen());
            }
        } else {
            IAType[] combinedFieldTypes = ArrayUtils.addAll(resultType.getFieldTypes().clone(), fieldType1Copy.getFieldTypes()[i]);
            resultType = new ARecordType(resultType.getTypeName(), ArrayUtils.addAll(resultType.getFieldNames(), fieldType1Copy.getFieldNames()[i]), combinedFieldTypes, resultType.isOpen());
        }
    }
    return resultType;
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 33 with IAType

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

the class ListConstructorTypeComputer method computeTypeFromItems.

private IAType computeTypeFromItems(IVariableTypeEnvironment env, AbstractFunctionCallExpression f) throws AlgebricksException {
    IAType currentType = null;
    boolean any = false;
    for (int k = 0; k < f.getArguments().size(); k++) {
        IAType type = (IAType) env.getType(f.getArguments().get(k).getValue());
        if (type.getTypeTag() == ATypeTag.UNION || (currentType != null && !currentType.equals(type))) {
            any = true;
            break;
        }
        currentType = type;
    }
    if (any || currentType == null) {
        return getListType(BuiltinType.ANY);
    } else {
        return getListType(currentType);
    }
}
Also used : IAType(org.apache.asterix.om.types.IAType)

Example 34 with IAType

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

the class NonTaggedGetItemResultType method getResultType.

@Override
protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
    IAType type = strippedInputTypes[0];
    if (type.getTypeTag() == ATypeTag.ANY) {
        return BuiltinType.ANY;
    }
    IAType itemType = ((AbstractCollectionType) type).getItemType();
    if (itemType.getTypeTag() == ATypeTag.ANY) {
        return itemType;
    }
    // Could have out-of-bound access or null elements.
    return AUnionType.createUnknownableType(itemType);
}
Also used : AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) IAType(org.apache.asterix.om.types.IAType)

Example 35 with IAType

use of org.apache.asterix.om.types.IAType 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)

Aggregations

IAType (org.apache.asterix.om.types.IAType)190 ARecordType (org.apache.asterix.om.types.ARecordType)73 ArrayList (java.util.ArrayList)64 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)42 ATypeTag (org.apache.asterix.om.types.ATypeTag)40 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)37 List (java.util.List)32 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)32 AUnionType (org.apache.asterix.om.types.AUnionType)31 AString (org.apache.asterix.om.base.AString)28 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)27 Mutable (org.apache.commons.lang3.mutable.Mutable)25 Pair (org.apache.hyracks.algebricks.common.utils.Pair)24 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)20 Dataset (org.apache.asterix.metadata.entities.Dataset)18 AsterixException (org.apache.asterix.common.exceptions.AsterixException)17 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)16 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)16 IVisitablePointable (org.apache.asterix.om.pointables.base.IVisitablePointable)15 IVariableTypeEnvironment (org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment)15