Search in sources :

Example 1 with InvalidExpressionException

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

the class RecordRemoveFieldsTypeComputer method getPathFromConstantExpression.

private void getPathFromConstantExpression(String funcName, ILogicalExpression expression, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
    ConstantExpression ce = (ConstantExpression) expression;
    if (!(ce.getValue() instanceof AsterixConstantValue)) {
        throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
    }
    IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
    ATypeTag type = item.getType().getTypeTag();
    switch(type) {
        case STRING:
            String fn = ((AString) item).getStringValue();
            fieldNameSet.add(fn);
            break;
        case ARRAY:
            AOrderedList pathOrdereList = (AOrderedList) item;
            String fieldName = ((AString) pathOrdereList.getItem(0)).getStringValue();
            fieldNameSet.add(fieldName);
            List<String> path = new ArrayList<>();
            for (int i = 0; i < pathOrdereList.size(); i++) {
                path.add(((AString) pathOrdereList.getItem(i)).getStringValue());
            }
            pathList.add(path);
            break;
        default:
            throw new UnsupportedTypeException(funcName, type);
    }
}
Also used : AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) IAObject(org.apache.asterix.om.base.IAObject) ArrayList(java.util.ArrayList) UnsupportedTypeException(org.apache.asterix.om.exceptions.UnsupportedTypeException) AString(org.apache.asterix.om.base.AString) AString(org.apache.asterix.om.base.AString)

Example 2 with InvalidExpressionException

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

the class ClosedRecordConstructorResultType method computeType.

@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
    AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
    String funcName = f.getFunctionIdentifier().getName();
    /**
         * if type has been top-down propagated, use the enforced type
         */
    ARecordType type = (ARecordType) TypeCastUtils.getRequiredType(f);
    if (type != null) {
        return type;
    }
    int n = f.getArguments().size() / 2;
    String[] fieldNames = new String[n];
    IAType[] fieldTypes = new IAType[n];
    int i = 0;
    Iterator<Mutable<ILogicalExpression>> argIter = f.getArguments().iterator();
    while (argIter.hasNext()) {
        ILogicalExpression e1 = argIter.next().getValue();
        ILogicalExpression e2 = argIter.next().getValue();
        IAType e2Type = (IAType) env.getType(e2);
        if (e2Type.getTypeTag() == ATypeTag.UNION) {
            AUnionType unionType = (AUnionType) e2Type;
            e2Type = AUnionType.createUnknownableType(unionType.getActualType());
        }
        fieldTypes[i] = e2Type;
        fieldNames[i] = ConstantExpressionUtil.getStringConstant(e1);
        if (fieldNames[i] == null) {
            throw new InvalidExpressionException(funcName, 2 * i, e1, LogicalExpressionTag.CONSTANT);
        }
        i++;
    }
    return new ARecordType(null, fieldNames, fieldTypes, false);
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) AUnionType(org.apache.asterix.om.types.AUnionType) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 3 with InvalidExpressionException

use of org.apache.asterix.om.exceptions.InvalidExpressionException 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 4 with InvalidExpressionException

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

the class RecordRemoveFieldsTypeComputer method getListFromExpression.

private List<String> getListFromExpression(String funcName, ILogicalExpression expression) throws AlgebricksException {
    AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) expression;
    List<Mutable<ILogicalExpression>> args = funcExp.getArguments();
    List<String> list = new ArrayList<>();
    for (Mutable<ILogicalExpression> arg : args) {
        // At this point all elements has to be a constant
        // Input list has only one level of nesting (list of list or list of strings)
        ConstantExpression ce = (ConstantExpression) arg.getValue();
        if (!(ce.getValue() instanceof AsterixConstantValue)) {
            throw new InvalidExpressionException(funcName, 1, ce, LogicalExpressionTag.CONSTANT);
        }
        IAObject item = ((AsterixConstantValue) ce.getValue()).getObject();
        ATypeTag type = item.getType().getTypeTag();
        if (type == ATypeTag.STRING) {
            list.add(((AString) item).getStringValue());
        } else {
            throw new UnsupportedTypeException(funcName, type);
        }
    }
    return list;
}
Also used : AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) IAObject(org.apache.asterix.om.base.IAObject) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) ATypeTag(org.apache.asterix.om.types.ATypeTag) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) UnsupportedTypeException(org.apache.asterix.om.exceptions.UnsupportedTypeException)

Example 5 with InvalidExpressionException

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

the class RecordRemoveFieldsTypeComputer method computeTypeFromNonConstantExpression.

private void computeTypeFromNonConstantExpression(String funcName, ILogicalExpression expression, Set<String> fieldNameSet, List<List<String>> pathList) throws AlgebricksException {
    AbstractFunctionCallExpression funcExp = (AbstractFunctionCallExpression) expression;
    List<Mutable<ILogicalExpression>> args = funcExp.getArguments();
    for (Mutable<ILogicalExpression> arg : args) {
        ILogicalExpression le = arg.getValue();
        switch(le.getExpressionTag()) {
            case CONSTANT:
                getPathFromConstantExpression(funcName, le, fieldNameSet, pathList);
                break;
            case FUNCTION_CALL:
                getPathFromFunctionExpression(funcName, le, fieldNameSet, pathList);
                break;
            default:
                throw new InvalidExpressionException(funcName, 1, le, LogicalExpressionTag.CONSTANT, LogicalExpressionTag.FUNCTION_CALL);
        }
    }
}
Also used : Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) InvalidExpressionException(org.apache.asterix.om.exceptions.InvalidExpressionException) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)

Aggregations

InvalidExpressionException (org.apache.asterix.om.exceptions.InvalidExpressionException)5 Mutable (org.apache.commons.lang3.mutable.Mutable)4 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)4 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)4 ArrayList (java.util.ArrayList)3 AString (org.apache.asterix.om.base.AString)2 IAObject (org.apache.asterix.om.base.IAObject)2 AsterixConstantValue (org.apache.asterix.om.constants.AsterixConstantValue)2 UnsupportedTypeException (org.apache.asterix.om.exceptions.UnsupportedTypeException)2 ARecordType (org.apache.asterix.om.types.ARecordType)2 ATypeTag (org.apache.asterix.om.types.ATypeTag)2 IAType (org.apache.asterix.om.types.IAType)2 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AOrderedList (org.apache.asterix.om.base.AOrderedList)1 TypeMismatchException (org.apache.asterix.om.exceptions.TypeMismatchException)1 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)1 AUnionType (org.apache.asterix.om.types.AUnionType)1