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());
}
}
}
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;
}
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);
}
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;
}
}
}
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()));
}
Aggregations