use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class ExternalDataUtils method getValueParserFactories.
public static IValueParserFactory[] getValueParserFactories(ARecordType recordType) {
int n = recordType.getFieldTypes().length;
IValueParserFactory[] fieldParserFactories = new IValueParserFactory[n];
for (int i = 0; i < n; i++) {
ATypeTag tag = null;
if (recordType.getFieldTypes()[i].getTypeTag() == ATypeTag.UNION) {
AUnionType unionType = (AUnionType) recordType.getFieldTypes()[i];
if (!unionType.isUnknownableType()) {
throw new NotImplementedException("Non-optional UNION type is not supported.");
}
tag = unionType.getActualType().getTypeTag();
} else {
tag = recordType.getFieldTypes()[i].getTypeTag();
}
if (tag == null) {
throw new NotImplementedException("Failed to get the type information for field " + i + ".");
}
fieldParserFactories[i] = getParserFactory(tag);
}
return fieldParserFactories;
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class JSONDeserializerForTypes method convertFromJSON.
/**
* Deserialize an arbitrary JSON representation of a type.
*
* @param typeInJSON
* the JSON representation of the type.
* @return an valid AsterixDB type.
* @throws Exception
*/
public static IAType convertFromJSON(JsonNode typeInJSON) throws Exception {
String typeName = typeInJSON.get("type").asText();
// Deals with ordered list.
if (typeName.equals(AOrderedListType.class.getName())) {
IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
return new AOrderedListType(itemType, "ordered-list");
}
// Deals with unordered list.
if (typeName.equals(AUnorderedListType.class.getName())) {
IAType itemType = convertFromJSON(typeInJSON.get("item-type"));
return new AUnorderedListType(itemType, "unordered-list");
}
// Deals with Union Type.
if (typeName.equals(AUnionType.class.getName())) {
List<IAType> unionTypes = new ArrayList<IAType>();
JsonNode fields = typeInJSON.get("fields");
for (int i = 0; i < fields.size(); i++) {
JsonNode fieldType = fields.get(i);
unionTypes.add(convertFromJSON(fieldType));
}
return new AUnionType(unionTypes, "union");
}
// Deals with record types.
if (typeName.equals(ARecordType.class.getName())) {
String name = typeInJSON.get("name").asText();
boolean openType = typeInJSON.get("open").asBoolean();
JsonNode fields = typeInJSON.get("fields");
String[] fieldNames = new String[fields.size()];
IAType[] fieldTypes = new IAType[fields.size()];
for (int i = 0; i < fields.size(); ++i) {
JsonNode field = fields.get(i);
List<String> names = Lists.newArrayList(field.fieldNames());
String fieldName = names.get(0);
fieldNames[i] = fieldName;
fieldTypes[i] = convertFromJSON(field.get(fieldName));
}
return new ARecordType(name, fieldNames, fieldTypes, openType);
}
// Deals with primitive types.
Class<?> cl = BuiltinType.class;
Field typeField = cl.getDeclaredField(typeName.toUpperCase());
return (IAType) typeField.get(null);
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class OpenRecordConstructorResultType method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
/**
* if type has been top-down propagated, use the enforced type
*/
ARecordType type = (ARecordType) TypeCastUtils.getRequiredType(f);
if (type != null) {
return type;
}
Iterator<Mutable<ILogicalExpression>> argIter = f.getArguments().iterator();
List<String> namesList = new ArrayList<>();
List<IAType> typesList = new ArrayList<>();
// The following set of names do not belong to the closed part,
// but are additional possible field names. For example, a field "foo" with type
// ANY cannot be in the closed part, but "foo" is a possible field name.
Set<String> allPossibleAdditionalFieldNames = new HashSet<>();
boolean canProvideAdditionFieldInfo = true;
boolean isOpen = false;
while (argIter.hasNext()) {
ILogicalExpression e1 = argIter.next().getValue();
ILogicalExpression e2 = argIter.next().getValue();
IAType t2 = (IAType) env.getType(e2);
String fieldName = ConstantExpressionUtil.getStringConstant(e1);
if (fieldName != null && t2 != null && TypeHelper.isClosed(t2)) {
namesList.add(fieldName);
if (t2.getTypeTag() == ATypeTag.UNION) {
AUnionType unionType = (AUnionType) t2;
t2 = AUnionType.createUnknownableType(unionType.getActualType());
}
typesList.add(t2);
} else {
if (canProvideAdditionFieldInfo && fieldName != null) {
allPossibleAdditionalFieldNames.add(fieldName);
} else {
canProvideAdditionFieldInfo = false;
}
isOpen = true;
}
}
String[] fieldNames = namesList.toArray(new String[0]);
IAType[] fieldTypes = typesList.toArray(new IAType[0]);
return canProvideAdditionFieldInfo ? new ARecordType(null, fieldNames, fieldTypes, isOpen, allPossibleAdditionalFieldNames) : new ARecordType(null, fieldNames, fieldTypes, isOpen);
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class NotUnknownTypeComputer method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> metadataProvider) throws AlgebricksException {
AbstractFunctionCallExpression f = (AbstractFunctionCallExpression) expression;
IAType type = (IAType) env.getType(f.getArguments().get(0).getValue());
if (type.getTypeTag() != ATypeTag.UNION) {
// directly return the input type if it is not a union
return type;
}
AUnionType unionType = (AUnionType) type;
return unionType.getActualType();
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class TypeComputeUtils method resolveCateogry.
private static byte resolveCateogry(IAType... inputTypes) {
byte category = CERTAIN;
boolean meetNull = false;
for (IAType inputType : inputTypes) {
switch(inputType.getTypeTag()) {
case UNION:
AUnionType unionType = (AUnionType) inputType;
if (unionType.isNullableType()) {
category |= NULLABLE;
}
if (unionType.isMissableType()) {
category |= MISSABLE;
}
break;
case MISSING:
return MISSING;
case NULL:
meetNull = true;
break;
case ANY:
category |= NULLABLE;
category |= MISSABLE;
break;
default:
break;
}
}
if (meetNull) {
return NULL;
}
return category;
}
Aggregations