Search in sources :

Example 76 with ARecordType

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

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

the class FieldAccessNestedResultType method getResultType.

@Override
protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
    IAType firstArgType = strippedInputTypes[0];
    if (firstArgType.getTypeTag() != ATypeTag.OBJECT) {
        return BuiltinType.ANY;
    }
    AbstractFunctionCallExpression funcExpr = (AbstractFunctionCallExpression) expr;
    ILogicalExpression arg1 = funcExpr.getArguments().get(1).getValue();
    if (arg1.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
        return BuiltinType.ANY;
    }
    ConstantExpression ce = (ConstantExpression) arg1;
    IAObject v = ((AsterixConstantValue) ce.getValue()).getObject();
    List<String> fieldPath = new ArrayList<>();
    if (v.getType().getTypeTag() == ATypeTag.ARRAY) {
        for (int i = 0; i < ((AOrderedList) v).size(); i++) {
            fieldPath.add(((AString) ((AOrderedList) v).getItem(i)).getStringValue());
        }
    } else {
        fieldPath.add(((AString) v).getStringValue());
    }
    ARecordType recType = (ARecordType) firstArgType;
    IAType fieldType = recType.getSubFieldType(fieldPath);
    return fieldType == null ? BuiltinType.ANY : fieldType;
}
Also used : ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AOrderedList(org.apache.asterix.om.base.AOrderedList) AsterixConstantValue(org.apache.asterix.om.constants.AsterixConstantValue) 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) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 78 with ARecordType

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

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

the class RecordRemoveFieldsTypeComputer method addField.

private void addField(ARecordType inputRecordType, String fieldName, List<String> resultFieldNames, List<IAType> resultFieldTypes) throws AlgebricksException {
    resultFieldNames.add(fieldName);
    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));
    }
}
Also used : ARecordType(org.apache.asterix.om.types.ARecordType)

Example 80 with ARecordType

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

the class AOrderedListSerializerDeserializerTest method test.

@Test
public void test() {
    // Generates types.
    ARecordType addrRecordType = SerializerDeserializerTestUtils.generateAddressRecordType();
    ARecordType employeeType = SerializerDeserializerTestUtils.generateEmployeeRecordType(addrRecordType);
    AOrderedListType employeeListType = new AOrderedListType(employeeType, "employee_list");
    //Generates records.
    ARecord[] records = SerializerDeserializerTestUtils.generateRecords(addrRecordType, employeeType);
    // Generates lists
    AOrderedList[] lists = new AOrderedList[4];
    for (int index = 0; index < lists.length; ++index) {
        lists[index] = new AOrderedList(employeeListType, Arrays.asList(records));
    }
    AOrderedListSerializerDeserializer serde = new AOrderedListSerializerDeserializer(employeeListType);
    // Run four test threads to serialize/deserialize lists concurrently.
    SerializerDeserializerTestUtils.concurrentSerDeTestRun(serde, lists);
}
Also used : ARecord(org.apache.asterix.om.base.ARecord) AOrderedList(org.apache.asterix.om.base.AOrderedList) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ARecordType(org.apache.asterix.om.types.ARecordType) Test(org.junit.Test)

Aggregations

ARecordType (org.apache.asterix.om.types.ARecordType)105 IAType (org.apache.asterix.om.types.IAType)73 ArrayList (java.util.ArrayList)48 List (java.util.List)24 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)22 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)20 Dataset (org.apache.asterix.metadata.entities.Dataset)19 AString (org.apache.asterix.om.base.AString)19 AbstractFunctionCallExpression (org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression)19 Test (org.junit.Test)16 AsterixException (org.apache.asterix.common.exceptions.AsterixException)15 Index (org.apache.asterix.metadata.entities.Index)15 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)15 CompilationException (org.apache.asterix.common.exceptions.CompilationException)13 AOrderedListType (org.apache.asterix.om.types.AOrderedListType)13 Mutable (org.apache.commons.lang3.mutable.Mutable)13 IOException (java.io.IOException)12 MetadataException (org.apache.asterix.metadata.MetadataException)12 AUnionType (org.apache.asterix.om.types.AUnionType)11 Pair (org.apache.hyracks.algebricks.common.utils.Pair)10