Search in sources :

Example 56 with IAType

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

the class MetadataNode method confirmDatatypeIsUnusedByDatatypes.

private void confirmDatatypeIsUnusedByDatatypes(JobId jobId, String dataverseName, String datatypeName) throws MetadataException, RemoteException {
    //If any datatype uses this type, throw an error
    //TODO: Currently this loads all types into memory. This will need to be fixed for large numbers of types
    Datatype dataTypeToBeDropped = getDatatype(jobId, dataverseName, datatypeName);
    assert dataTypeToBeDropped != null;
    IAType typeToBeDropped = dataTypeToBeDropped.getDatatype();
    List<Datatype> datatypes = getAllDatatypes(jobId);
    for (Datatype dataType : datatypes) {
        //skip types in different dataverses as well as the type to be dropped itself
        if (!dataType.getDataverseName().equals(dataverseName) || dataType.getDatatype().getTypeName().equals(datatypeName)) {
            continue;
        }
        AbstractComplexType recType = (AbstractComplexType) dataType.getDatatype();
        if (recType.containsType(typeToBeDropped)) {
            throw new MetadataException("Cannot drop type " + dataverseName + "." + datatypeName + " being used by type " + dataverseName + "." + recType.getTypeName());
        }
    }
}
Also used : AbstractComplexType(org.apache.asterix.om.types.AbstractComplexType) Datatype(org.apache.asterix.metadata.entities.Datatype) IAType(org.apache.asterix.om.types.IAType)

Example 57 with IAType

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

the class MetadataNode method getNestedComplexDatatypeNamesForThisDatatype.

private List<String> getNestedComplexDatatypeNamesForThisDatatype(JobId jobId, String dataverseName, String datatypeName) throws MetadataException, RemoteException {
    //Return all field types that aren't builtin types
    Datatype parentType = getDatatype(jobId, dataverseName, datatypeName);
    List<IAType> subTypes = null;
    if (parentType.getDatatype().getTypeTag() == ATypeTag.OBJECT) {
        ARecordType recType = (ARecordType) parentType.getDatatype();
        subTypes = Arrays.asList(recType.getFieldTypes());
    } else if (parentType.getDatatype().getTypeTag() == ATypeTag.UNION) {
        AUnionType recType = (AUnionType) parentType.getDatatype();
        subTypes = recType.getUnionList();
    }
    List<String> nestedTypes = new ArrayList<>();
    if (subTypes != null) {
        for (IAType subType : subTypes) {
            if (!(subType instanceof BuiltinType)) {
                nestedTypes.add(subType.getTypeName());
            }
        }
    }
    return nestedTypes;
}
Also used : BuiltinType(org.apache.asterix.om.types.BuiltinType) AUnionType(org.apache.asterix.om.types.AUnionType) ArrayList(java.util.ArrayList) AString(org.apache.asterix.om.base.AString) AMutableString(org.apache.asterix.om.base.AMutableString) ARecordType(org.apache.asterix.om.types.ARecordType) Datatype(org.apache.asterix.metadata.entities.Datatype) IAType(org.apache.asterix.om.types.IAType)

Example 58 with IAType

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

the class SerializerDeserializerTestUtils method generateEmployeeRecordType.

public static ARecordType generateEmployeeRecordType(ARecordType addrRecordType) {
    AOrderedListType addrListType = new AOrderedListType(addrRecordType, "address_list");
    String[] fieldNames = new String[] { "id", "name", "addresses_history" };
    IAType[] fieldTypes = new IAType[] { BuiltinType.AINT64, BuiltinType.ASTRING, addrListType };
    return new ARecordType("employee", fieldNames, fieldTypes, true);
}
Also used : AOrderedListType(org.apache.asterix.om.types.AOrderedListType) AString(org.apache.asterix.om.base.AString) ARecordType(org.apache.asterix.om.types.ARecordType) IAType(org.apache.asterix.om.types.IAType)

Example 59 with IAType

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

the class ExceptionTest method testTypeComputer.

private void testTypeComputer(Class<? extends IResultTypeComputer> c) throws Exception {
    // Mocks the type environment.
    IVariableTypeEnvironment mockTypeEnv = mock(IVariableTypeEnvironment.class);
    // Mocks the metadata provider.
    IMetadataProvider<?, ?> mockMetadataProvider = mock(IMetadataProvider.class);
    // Mocks function expression.
    AbstractFunctionCallExpression mockExpr = mock(AbstractFunctionCallExpression.class);
    FunctionIdentifier fid = mock(FunctionIdentifier.class);
    when(mockExpr.getFunctionIdentifier()).thenReturn(fid);
    when(fid.getName()).thenReturn("testFunction");
    int numCombination = (int) Math.pow(ATypeTag.values().length, 2);
    // Sets two arguments for the mocked function expression.
    for (int index = 0; index < numCombination; ++index) {
        try {
            List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
            for (int argIndex = 0; argIndex < 2; ++argIndex) {
                int base = (int) Math.pow(ATypeTag.values().length, argIndex);
                ILogicalExpression mockArg = mock(ILogicalExpression.class);
                argRefs.add(new MutableObject<>(mockArg));
                IAType mockType = mock(IAType.class);
                when(mockTypeEnv.getType(mockArg)).thenReturn(mockType);
                int serializedTypeTag = (index / base) % ATypeTag.values().length + 1;
                ATypeTag typeTag = ATypeTag.VALUE_TYPE_MAPPING[serializedTypeTag];
                if (typeTag == null) {
                    // For some reason, type tag 39 does not exist.
                    typeTag = ATypeTag.ANY;
                }
                when(mockType.getTypeTag()).thenReturn(typeTag);
            }
            // Sets up arguments for the mocked expression.
            when(mockExpr.getArguments()).thenReturn(argRefs);
            // Sets up required/actual types of the mocked expression.
            Object[] opaqueParameters = new Object[2];
            opaqueParameters[0] = BuiltinType.ANY;
            opaqueParameters[1] = BuiltinType.ANY;
            when(mockExpr.getOpaqueParameters()).thenReturn(opaqueParameters);
            // Invokes a type computer.
            IResultTypeComputer instance = (IResultTypeComputer) c.getField("INSTANCE").get(null);
            instance.computeType(mockExpr, mockTypeEnv, mockMetadataProvider);
        } catch (AlgebricksException ae) {
            String msg = ae.getMessage();
            if (msg.startsWith("ASX")) {
                // Verifies the error code.
                int errorCode = Integer.parseInt(msg.substring(3, 7));
                Assert.assertTrue(errorCode >= 1000 && errorCode < 2000);
                continue;
            } else {
                // Any root-level compilation exceptions thrown from type computers should have an error code.
                Assert.assertTrue(!(ae instanceof AlgebricksException) || (ae.getCause() != null));
            }
        } catch (ClassCastException e) {
            continue;
        }
    }
}
Also used : IResultTypeComputer(org.apache.asterix.om.typecomputer.base.IResultTypeComputer) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) ArrayList(java.util.ArrayList) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) ATypeTag(org.apache.asterix.om.types.ATypeTag) MutableObject(org.apache.commons.lang3.mutable.MutableObject) IVariableTypeEnvironment(org.apache.hyracks.algebricks.core.algebra.expressions.IVariableTypeEnvironment) IAType(org.apache.asterix.om.types.IAType)

Example 60 with IAType

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

the class JSONDeserializerForTypesTest method test.

@Test
public void test() throws Exception {
    // Tests a record type with primitive types.
    String[] fieldNames = { "a1", "a2", "a3" };
    IAType[] fieldTypes = { BuiltinType.ASTRING, BuiltinType.AINT16, BuiltinType.ABITARRAY };
    ARecordType recordType = new ARecordType("ARecord", fieldNames, fieldTypes, true);
    Assert.assertEquals(recordType, JSONDeserializerForTypes.convertFromJSON(recordType.toJSON()));
    // Tests a record type with a nested record type.
    String[] fieldNames2 = { "a1", "a2" };
    IAType[] fieldTypes2 = { BuiltinType.ABOOLEAN, recordType };
    ARecordType recordType2 = new ARecordType("ARecord2", fieldNames2, fieldTypes2, true);
    Assert.assertEquals(recordType2, JSONDeserializerForTypes.convertFromJSON(recordType2.toJSON()));
    // Tests a record type with a union type.
    String[] fieldNames3 = { "a1", "a2" };
    List<IAType> unionTypes = new ArrayList<IAType>();
    unionTypes.add(BuiltinType.ADURATION);
    unionTypes.add(recordType2);
    AUnionType unionType = new AUnionType(unionTypes, "union");
    IAType[] fieldTypes3 = { BuiltinType.ABOOLEAN, unionType };
    ARecordType recordType3 = new ARecordType("ARecord3", fieldNames3, fieldTypes3, true);
    Assert.assertEquals(recordType3, JSONDeserializerForTypes.convertFromJSON(recordType3.toJSON()));
    // Tests a record type with an ordered list.
    String[] fieldNames4 = { "a1", "a2" };
    IAType[] fieldTypes4 = { BuiltinType.ABOOLEAN, new AOrderedListType(BuiltinType.ADATETIME, "list") };
    ARecordType recordType4 = new ARecordType("ARecord4", fieldNames4, fieldTypes4, false);
    Assert.assertEquals(recordType4, JSONDeserializerForTypes.convertFromJSON(recordType4.toJSON()));
    // Tests a record type with an unordered list.
    String[] fieldNames5 = { "a1", "a2" };
    IAType[] fieldTypes5 = { BuiltinType.ABOOLEAN, new AUnorderedListType(recordType2, "list") };
    ARecordType recordType5 = new ARecordType("ARecord5", fieldNames5, fieldTypes5, false);
    Assert.assertEquals(recordType5, JSONDeserializerForTypes.convertFromJSON(recordType5.toJSON()));
}
Also used : AUnionType(org.apache.asterix.om.types.AUnionType) AOrderedListType(org.apache.asterix.om.types.AOrderedListType) ArrayList(java.util.ArrayList) ARecordType(org.apache.asterix.om.types.ARecordType) AUnorderedListType(org.apache.asterix.om.types.AUnorderedListType) IAType(org.apache.asterix.om.types.IAType) Test(org.junit.Test)

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