Search in sources :

Example 1 with TypeMismatchException

use of org.apache.asterix.om.exceptions.TypeMismatchException in project asterixdb by apache.

the class RecordRemoveFieldsTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expression;
    String funcName = funcExpr.getFunctionIdentifier().getName();
    IAType type0 = (IAType) env.getType(funcExpr.getArguments().get(0).getValue());
    List<List<String>> pathList = new ArrayList<>();
    Set<String> fieldNameSet = new HashSet<>();
    Deque<String> fieldPathStack = new ArrayDeque<>();
    ARecordType inputRecordType = getRecordTypeFromType(funcName, type0);
    if (inputRecordType == null) {
        return BuiltinType.ANY;
    }
    AbstractLogicalExpression arg1 = (AbstractLogicalExpression) funcExpr.getArguments().get(1).getValue();
    IAType inputListType = (IAType) env.getType(arg1);
    AOrderedListType inputOrderedListType = TypeComputeUtils.extractOrderedListType(inputListType);
    if (inputOrderedListType == null) {
        throw new TypeMismatchException(funcName, 1, inputListType.getTypeTag(), ATypeTag.ARRAY);
    }
    ATypeTag tt = inputOrderedListType.getItemType().getTypeTag();
    if (tt == ATypeTag.STRING) {
        // If top-fieldlist
        if (setFieldNameSet(arg1, fieldNameSet)) {
            return buildOutputType(fieldPathStack, inputRecordType, fieldNameSet, pathList);
        } else {
            return DefaultOpenFieldType.NESTED_OPEN_RECORD_TYPE;
        }
    } else {
        // tt == ATypeTag.ANY, meaning the list is nested
        computeTypeFromNonConstantExpression(funcName, arg1, fieldNameSet, pathList);
        IAType resultType = buildOutputType(fieldPathStack, inputRecordType, fieldNameSet, pathList);
        return resultType;
    }
}
Also used : AbstractLogicalExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractLogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) TypeMismatchException(org.apache.asterix.om.exceptions.TypeMismatchException) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) ArrayDeque(java.util.ArrayDeque) ATypeTag(org.apache.asterix.om.types.ATypeTag) AOrderedList(org.apache.asterix.om.base.AOrderedList) ArrayList(java.util.ArrayList) List(java.util.List) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType) HashSet(java.util.HashSet)

Example 2 with TypeMismatchException

use of org.apache.asterix.om.exceptions.TypeMismatchException in project asterixdb by apache.

the class RecordAddFieldsTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expression;
    String funcName = funcExpr.getFunctionIdentifier().getName();
    IAType type0 = (IAType) env.getType(funcExpr.getArguments().get(0).getValue());
    ARecordType inputRecordType = TypeComputeUtils.extractRecordType(type0);
    if (inputRecordType == null) {
        throw new TypeMismatchException(funcName, 0, type0.getTypeTag(), ATypeTag.OBJECT);
    }
    ILogicalExpression arg1 = funcExpr.getArguments().get(1).getValue();
    IAType type1 = (IAType) env.getType(arg1);
    AOrderedListType inputOrderedListType = TypeComputeUtils.extractOrderedListType(type1);
    if (inputOrderedListType == null) {
        return inputRecordType;
    }
    boolean unknownable = TypeHelper.canBeUnknown(type0) || TypeHelper.canBeUnknown(type1);
    Map<String, IAType> additionalFields = new HashMap<>();
    List<String> resultFieldNames = new ArrayList<>();
    List<IAType> resultFieldTypes = new ArrayList<>();
    resultFieldNames.addAll(Arrays.asList(inputRecordType.getFieldNames()));
    Collections.sort(resultFieldNames);
    for (String fieldName : resultFieldNames) {
        if (inputRecordType.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
            ARecordType nestedType = (ARecordType) inputRecordType.getFieldType(fieldName);
            //Deep Copy prevents altering of input types
            resultFieldTypes.add(nestedType.deepCopy(nestedType));
        } else {
            resultFieldTypes.add(inputRecordType.getFieldType(fieldName));
        }
    }
    if (!containsVariable(arg1)) {
        AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) arg1;
        List<Mutable<ILogicalExpression>> args = f.getArguments();
        String fieldName = null;
        IAType fieldType = null;
        // Iterating through the orderlist input
        for (Mutable<ILogicalExpression> arg : args) {
            AbstractFunctionCallExpression recConsExpr = (AbstractFunctionCallExpression) arg.getValue();
            ARecordType rtype = TypeComputeUtils.extractRecordType((IAType) env.getType(recConsExpr));
            if (rtype != null) {
                String[] fn = rtype.getFieldNames();
                IAType[] ft = rtype.getFieldTypes();
                for (int j = 0; j < fn.length; j++) {
                    if (fn[j].equals(FIELD_NAME_NAME)) {
                        ILogicalExpression fieldNameExpr = recConsExpr.getArguments().get(j).getValue();
                        if (ConstantExpressionUtil.getStringConstant(fieldNameExpr) == null) {
                            throw new InvalidExpressionException(funcName, 1, fieldNameExpr, LogicalExpressionTag.CONSTANT);
                        }
                        // Get the actual "field-name" string
                        fieldName = ConstantExpressionUtil.getStringArgument(recConsExpr, j + 1);
                    } else if (fn[j].equals(FIELD_VALUE_VALUE)) {
                        fieldType = ft[j];
                    }
                }
                if (fieldName != null) {
                    additionalFields.put(fieldName, fieldType);
                }
            }
        }
        if (!additionalFields.isEmpty()) {
            Iterator<Map.Entry<String, IAType>> it = additionalFields.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, IAType> entry = it.next();
                resultFieldNames.add(entry.getKey());
                resultFieldTypes.add(entry.getValue());
            }
        }
    }
    // If variable ignore, deal with the addition at runtime
    String resultTypeName = "appended(" + inputRecordType.getTypeName() + ")";
    int n = resultFieldNames.size();
    IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[n]), resultFieldTypes.toArray(new IAType[n]), true);
    if (unknownable) {
        resultType = AUnionType.createUnknownableType(resultType);
    }
    return resultType;
}
Also used : HashMap(java.util.HashMap) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) TypeMismatchException(org.apache.asterix.om.exceptions.TypeMismatchException) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) ARecordType(org.apache.asterix.om.types.ARecordType) HashMap(java.util.HashMap) Map(java.util.Map) IAType(org.apache.asterix.om.types.IAType)

Example 3 with TypeMismatchException

use of org.apache.asterix.om.exceptions.TypeMismatchException in project asterixdb by apache.

the class RecordMergeTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    String funcName = f.getFunctionIdentifier().getName();
    IAType t0 = (IAType) env.getType(f.getArguments().get(0).getValue());
    IAType t1 = (IAType) env.getType(f.getArguments().get(1).getValue());
    boolean unknownable = TypeHelper.canBeUnknown(t0) || TypeHelper.canBeUnknown(t1);
    ARecordType recType0 = TypeComputeUtils.extractRecordType(t0);
    if (recType0 == null) {
        throw new TypeMismatchException(funcName, 0, t0.getTypeTag(), ATypeTag.OBJECT);
    }
    ARecordType recType1 = TypeComputeUtils.extractRecordType(t1);
    if (recType1 == null) {
        throw new TypeMismatchException(funcName, 1, t1.getTypeTag(), ATypeTag.OBJECT);
    }
    List<String> resultFieldNames = new ArrayList<>();
    for (String fieldName : recType0.getFieldNames()) {
        resultFieldNames.add(fieldName);
    }
    Collections.sort(resultFieldNames);
    List<IAType> resultFieldTypes = new ArrayList<>();
    for (String fieldName : resultFieldNames) {
        if (recType0.getFieldType(fieldName).getTypeTag() == ATypeTag.OBJECT) {
            ARecordType nestedType = (ARecordType) recType0.getFieldType(fieldName);
            //Deep Copy prevents altering of input types
            resultFieldTypes.add(nestedType.deepCopy(nestedType));
        } else {
            resultFieldTypes.add(recType0.getFieldType(fieldName));
        }
    }
    List<String> additionalFieldNames = new ArrayList<>();
    List<IAType> additionalFieldTypes = new ArrayList<>();
    String[] fieldNames = recType1.getFieldNames();
    IAType[] fieldTypes = recType1.getFieldTypes();
    for (int i = 0; i < fieldNames.length; ++i) {
        int pos = Collections.binarySearch(resultFieldNames, fieldNames[i]);
        if (pos >= 0) {
            IAType resultFieldType = resultFieldTypes.get(pos);
            if (resultFieldType.getTypeTag() != fieldTypes[i].getTypeTag()) {
                throw new CompilationException(ErrorCode.COMPILATION_DUPLICATE_FIELD_NAME, fieldNames[i]);
            }
            // Assuming fieldTypes[i].getTypeTag() = resultFieldType.getTypeTag()
            if (fieldTypes[i].getTypeTag() == ATypeTag.OBJECT) {
                resultFieldTypes.set(pos, mergedNestedType(fieldNames[i], fieldTypes[i], resultFieldType));
            }
        } else {
            additionalFieldNames.add(fieldNames[i]);
            additionalFieldTypes.add(fieldTypes[i]);
        }
    }
    resultFieldNames.addAll(additionalFieldNames);
    resultFieldTypes.addAll(additionalFieldTypes);
    String resultTypeName = "merged(" + recType0.getTypeName() + ", " + recType1.getTypeName() + ")";
    boolean isOpen = recType0.isOpen() || recType1.isOpen();
    IAType resultType = new ARecordType(resultTypeName, resultFieldNames.toArray(new String[] {}), resultFieldTypes.toArray(new IAType[] {}), isOpen);
    if (unknownable) {
        resultType = AUnionType.createUnknownableType(resultType);
    }
    return resultType;
}
Also used : CompilationException(org.apache.asterix.common.exceptions.CompilationException) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) TypeMismatchException(org.apache.asterix.om.exceptions.TypeMismatchException) ArrayList(java.util.ArrayList) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 4 with TypeMismatchException

use of org.apache.asterix.om.exceptions.TypeMismatchException in project asterixdb by apache.

the class SubsetCollectionTypeComputer method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
    AbstractFunctionCallExpression fun = (AbstractFunctionCallExpression) expression;
    String funcName = fun.getFunctionIdentifier().getName();
    IAType t = (IAType) env.getType(fun.getArguments().get(0).getValue());
    ATypeTag actualTypeTag = t.getTypeTag();
    switch(actualTypeTag) {
        case MULTISET:
        case ARRAY:
            {
                AbstractCollectionType act = (AbstractCollectionType) t;
                return act.getItemType();
            }
        case UNION:
            {
                AUnionType ut = (AUnionType) t;
                if (!ut.isUnknownableType()) {
                    throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
                }
                IAType t2 = ut.getActualType();
                ATypeTag tag2 = t2.getTypeTag();
                if (tag2 == ATypeTag.MULTISET || tag2 == ATypeTag.ARRAY) {
                    AbstractCollectionType act = (AbstractCollectionType) t2;
                    return act.getItemType();
                }
                throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
            }
        case ANY:
            return BuiltinType.ANY;
        default:
            throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
    }
}
Also used : ATypeTag(org.apache.asterix.om.types.ATypeTag) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) TypeMismatchException(org.apache.asterix.om.exceptions.TypeMismatchException) AbstractCollectionType(org.apache.asterix.om.types.AbstractCollectionType) IAType(org.apache.asterix.om.types.IAType)

Aggregations

TypeMismatchException (org.apache.asterix.om.exceptions.TypeMismatchException)4 IAType (org.apache.asterix.om.types.IAType)4 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)4 ArrayList (java.util.ArrayList)3 ARecordType (org.apache.asterix.om.types.ARecordType)3 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)2 ATypeTag (org.apache.asterix.om.types.ATypeTag)2 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 CompilationException (org.apache.asterix.common.exceptions.CompilationException)1 AOrderedList (org.apache.asterix.om.base.AOrderedList)1 AString (org.apache.asterix.om.base.AString)1 InvalidExpressionException (org.apache.asterix.om.exceptions.InvalidExpressionException)1 AUnionType (org.apache.asterix.om.types.AUnionType)1 AbstractCollectionType (org.apache.asterix.om.types.AbstractCollectionType)1 Mutable (org.apache.commons.lang3.mutable.Mutable)1 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)1