TypeResolverUtil { public static IAType resolve(List<IAType> inputTypes) { IAType currentType = null; for (IAType type : inputTypes) { currentType = currentType == null ? type : generalizeTypes(currentType, type); } return currentType; } private  TypeResolverUtil(); static IAType resolve(List<IAType> inputTypes); static IAType resolve(IAType... inputTypes); static boolean needsCast(IAType reqType, IAType inputType);  }
@Test public void testRecordType() { ARecordType leftRecordType = new ARecordType(null, new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, false, null); ARecordType rightRecordType = new ARecordType(null, new String[] { "b", "c" }, new IAType[] { BuiltinType.AINT32, BuiltinType.ABINARY }, false, null); List<IAType> inputTypes = new ArrayList<>(); inputTypes.add(leftRecordType); inputTypes.add(rightRecordType); ARecordType resolvedType = (ARecordType) TypeResolverUtil.resolve(inputTypes); Set<String> possibleAdditionalFields = new HashSet<>(); possibleAdditionalFields.add("a"); possibleAdditionalFields.add("c"); ARecordType expectedType = new ARecordType(null, new String[] { "b" }, new IAType[] { BuiltinType.AINT32 }, true, possibleAdditionalFields); Assert.assertEquals(resolvedType, expectedType); Assert.assertEquals(resolvedType.getAllPossibleAdditonalFieldNames(), expectedType.getAllPossibleAdditonalFieldNames()); }
@Test public void testIsmophicRecordType() { ARecordType leftRecordType = new ARecordType(null, new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, false, null); ARecordType rightRecordType = new ARecordType(null, new String[] { "b", "a" }, new IAType[] { BuiltinType.AINT32, BuiltinType.ASTRING }, false, null); List<IAType> inputTypes = new ArrayList<>(); inputTypes.add(leftRecordType); inputTypes.add(rightRecordType); ARecordType resolvedType = (ARecordType) TypeResolverUtil.resolve(inputTypes); Assert.assertEquals(resolvedType, leftRecordType); }
@Test public void testNestedRecordType() { ARecordType leftRecordType = new ARecordType("null", new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, new ARecordType(null, new String[] { "c", "d" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, false, null) }, false, null); ARecordType rightRecordType = new ARecordType("null", new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, new ARecordType(null, new String[] { "d", "e" }, new IAType[] { BuiltinType.AINT32, BuiltinType.AINT32 }, false, null) }, false, null); List<IAType> inputTypes = new ArrayList<>(); inputTypes.add(leftRecordType); inputTypes.add(rightRecordType); ARecordType resolvedType = (ARecordType) TypeResolverUtil.resolve(inputTypes); ARecordType nestedRecordType = (ARecordType) resolvedType.getFieldType("b"); Set<String> nestedPossibleAdditionalFields = new HashSet<>(); nestedPossibleAdditionalFields.add("c"); nestedPossibleAdditionalFields.add("e"); ARecordType expectedType = new ARecordType(null, new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, new ARecordType(null, new String[] { "d" }, new IAType[] { BuiltinType.AINT32 }, true, nestedPossibleAdditionalFields) }, false, null); Assert.assertEquals(expectedType, resolvedType); Assert.assertEquals(nestedRecordType.getAllPossibleAdditonalFieldNames(), nestedPossibleAdditionalFields); }
@Test public void testOrderedListType() { ARecordType leftRecordType = new ARecordType("null", new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, true, Collections.singleton("d")); AOrderedListType leftListType = new AOrderedListType(leftRecordType, "null"); ARecordType rightRecordType = new ARecordType("null", new String[] { "b", "c" }, new IAType[] { BuiltinType.AINT32, BuiltinType.ABINARY }, true, Collections.singleton("e")); AOrderedListType rightListType = new AOrderedListType(rightRecordType, "null"); List<IAType> inputTypes = new ArrayList<>(); inputTypes.add(leftListType); inputTypes.add(rightListType); AbstractCollectionType resolvedType = (AbstractCollectionType) TypeResolverUtil.resolve(inputTypes); ARecordType resolvedRecordType = (ARecordType) resolvedType.getItemType(); Set<String> possibleAdditionalFields = new HashSet<>(); possibleAdditionalFields.add("a"); possibleAdditionalFields.add("c"); possibleAdditionalFields.add("d"); possibleAdditionalFields.add("e"); ARecordType expectedRecordType = new ARecordType(null, new String[] { "b" }, new IAType[] { BuiltinType.AINT32 }, true, possibleAdditionalFields); AOrderedListType expectedListType = new AOrderedListType(expectedRecordType, null); Assert.assertEquals(resolvedType, expectedListType); Assert.assertEquals(resolvedRecordType.getAllPossibleAdditonalFieldNames(), expectedRecordType.getAllPossibleAdditonalFieldNames()); }
@Test public void testUnorderedListType() { ARecordType leftRecordType = new ARecordType(null, new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, true, Collections.singleton("d")); AUnorderedListType leftListType = new AUnorderedListType(leftRecordType, null); AUnorderedListType rightListType = new AUnorderedListType(BuiltinType.ASTRING, null); List<IAType> inputTypes = new ArrayList<>(); inputTypes.add(leftListType); inputTypes.add(rightListType); AbstractCollectionType resolvedType = (AbstractCollectionType) TypeResolverUtil.resolve(inputTypes); Assert.assertEquals(resolvedType, new AUnorderedListType(BuiltinType.ANY, null)); }
@Test public void testNullType() { ARecordType leftRecordType = new ARecordType(null, new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, false, null); List<IAType> inputTypes = new ArrayList<>(); inputTypes.add(leftRecordType); inputTypes.add(BuiltinType.ANULL); IAType resolvedType = TypeResolverUtil.resolve(inputTypes); Assert.assertEquals(resolvedType, AUnionType.createUnknownableType(leftRecordType)); }
@Test public void testMissingType() { ARecordType leftRecordType = new ARecordType(null, new String[] { "a", "b" }, new IAType[] { BuiltinType.ASTRING, BuiltinType.AINT32 }, false, null); List<IAType> inputTypes = new ArrayList<>(); inputTypes.add(leftRecordType); inputTypes.add(BuiltinType.AMISSING); IAType resolvedType = TypeResolverUtil.resolve(inputTypes); Assert.assertEquals(resolvedType, AUnionType.createUnknownableType(leftRecordType)); }