use of org.apache.asterix.om.types.IAType 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.IAType in project asterixdb by apache.
the class RecordMergeTypeComputer method mergedNestedType.
private IAType mergedNestedType(String fieldName, IAType fieldType1, IAType fieldType0) throws AlgebricksException {
if (fieldType1.getTypeTag() != ATypeTag.OBJECT || fieldType0.getTypeTag() != ATypeTag.OBJECT) {
throw new CompilationException(ErrorCode.COMPILATION_DUPLICATE_FIELD_NAME, fieldName);
}
ARecordType resultType = (ARecordType) fieldType0;
ARecordType fieldType1Copy = (ARecordType) fieldType1;
for (int i = 0; i < fieldType1Copy.getFieldTypes().length; i++) {
String fname = fieldType1Copy.getFieldNames()[i];
int pos = resultType.getFieldIndex(fname);
if (pos >= 0) {
// If a sub-record do merge, else ignore and let the values decide what to do
if (fieldType1Copy.getFieldTypes()[i].getTypeTag() == ATypeTag.OBJECT) {
IAType[] oldTypes = resultType.getFieldTypes();
oldTypes[pos] = mergedNestedType(fname, fieldType1Copy.getFieldTypes()[i], resultType.getFieldTypes()[pos]);
resultType = new ARecordType(resultType.getTypeName(), resultType.getFieldNames(), oldTypes, resultType.isOpen());
}
} else {
IAType[] combinedFieldTypes = ArrayUtils.addAll(resultType.getFieldTypes().clone(), fieldType1Copy.getFieldTypes()[i]);
resultType = new ARecordType(resultType.getTypeName(), ArrayUtils.addAll(resultType.getFieldNames(), fieldType1Copy.getFieldNames()[i]), combinedFieldTypes, resultType.isOpen());
}
}
return resultType;
}
use of org.apache.asterix.om.types.IAType in project asterixdb by apache.
the class ListConstructorTypeComputer method computeTypeFromItems.
private IAType computeTypeFromItems(IVariableTypeEnvironment env, AbstractFunctionCallExpression f) throws AlgebricksException {
IAType currentType = null;
boolean any = false;
for (int k = 0; k < f.getArguments().size(); k++) {
IAType type = (IAType) env.getType(f.getArguments().get(k).getValue());
if (type.getTypeTag() == ATypeTag.UNION || (currentType != null && !currentType.equals(type))) {
any = true;
break;
}
currentType = type;
}
if (any || currentType == null) {
return getListType(BuiltinType.ANY);
} else {
return getListType(currentType);
}
}
use of org.apache.asterix.om.types.IAType in project asterixdb by apache.
the class NonTaggedGetItemResultType method getResultType.
@Override
protected IAType getResultType(ILogicalExpression expr, IAType... strippedInputTypes) throws AlgebricksException {
IAType type = strippedInputTypes[0];
if (type.getTypeTag() == ATypeTag.ANY) {
return BuiltinType.ANY;
}
IAType itemType = ((AbstractCollectionType) type).getItemType();
if (itemType.getTypeTag() == ATypeTag.ANY) {
return itemType;
}
// Could have out-of-bound access or null elements.
return AUnionType.createUnknownableType(itemType);
}
use of org.apache.asterix.om.types.IAType 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();
}
Aggregations