use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class TypeComputerTest method testTypeComputer.
private boolean 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");
// A function at most has six argument.
List<Mutable<ILogicalExpression>> argRefs = new ArrayList<>();
for (int argIndex = 0; argIndex < 6; ++argIndex) {
ILogicalExpression mockArg = mock(ILogicalExpression.class);
argRefs.add(new MutableObject<>(mockArg));
when(mockTypeEnv.getType(mockArg)).thenReturn(BuiltinType.ANY);
}
// 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);
// Tests the return type. It should be either ANY or NULLABLE/MISSABLE.
IResultTypeComputer instance = (IResultTypeComputer) c.getField("INSTANCE").get(null);
IAType resultType = instance.computeType(mockExpr, mockTypeEnv, mockMetadataProvider);
ATypeTag typeTag = resultType.getTypeTag();
if (typeTag == ATypeTag.ANY) {
return true;
}
if (typeTag == ATypeTag.UNION) {
AUnionType unionType = (AUnionType) resultType;
return unionType.isMissableType() && unionType.isNullableType();
}
return false;
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class SubsetCollectionTypeComputer method computeType.
@Override
public IAType computeType(ILogicalExpression expression, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
AbstractFunctionCallExpression fun = (AbstractFunctionCallExpression) expression;
String funcName = fun.getFunctionIdentifier().getName();
IAType t = (IAType) env.getType(fun.getArguments().get(0).getValue());
ATypeTag actualTypeTag = t.getTypeTag();
switch(actualTypeTag) {
case MULTISET:
case ARRAY:
{
AbstractCollectionType act = (AbstractCollectionType) t;
return act.getItemType();
}
case UNION:
{
AUnionType ut = (AUnionType) t;
if (!ut.isUnknownableType()) {
throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
}
IAType t2 = ut.getActualType();
ATypeTag tag2 = t2.getTypeTag();
if (tag2 == ATypeTag.MULTISET || tag2 == ATypeTag.ARRAY) {
AbstractCollectionType act = (AbstractCollectionType) t2;
return act.getItemType();
}
throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
}
case ANY:
return BuiltinType.ANY;
default:
throw new TypeMismatchException(funcName, 0, actualTypeTag, ATypeTag.MULTISET, ATypeTag.ARRAY);
}
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class TypeComputeUtils method isUnknownableType.
private static boolean[] isUnknownableType(IAType... inputTypes) {
boolean[] unknownable = new boolean[inputTypes.length];
for (int index = 0; index < unknownable.length; ++index) {
IAType type = inputTypes[index];
unknownable[index] = false;
if (type.getTypeTag() != ATypeTag.UNION) {
continue;
} else {
AUnionType unionType = (AUnionType) type;
unknownable[index] = unionType.isUnknownableType();
}
}
return unknownable;
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class TypeComputeUtils method extractUnorderedListType.
public static AUnorderedListType extractUnorderedListType(IAType t) {
if (t.getTypeTag() == ATypeTag.MULTISET) {
return (AUnorderedListType) t;
}
if (t.getTypeTag() == ATypeTag.UNION) {
AUnionType unionType = (AUnionType) t;
IAType innerType = unionType.getActualType();
if (innerType.getTypeTag() == ATypeTag.MULTISET) {
return (AUnorderedListType) innerType;
}
}
return null;
}
use of org.apache.asterix.om.types.AUnionType in project asterixdb by apache.
the class NonTaggedFormatUtil method hasOptionalField.
public static final boolean hasOptionalField(ARecordType recType) {
for (int i = 0; i < recType.getFieldTypes().length; i++) {
IAType type = recType.getFieldTypes()[i];
if (type != null) {
ATypeTag tag = type.getTypeTag();
if (tag == ATypeTag.NULL || tag == ATypeTag.MISSING) {
return true;
}
if (tag != ATypeTag.UNION) {
continue;
}
// union
AUnionType unionType = (AUnionType) type;
if (unionType.isUnknownableType()) {
return true;
}
}
}
return false;
}
Aggregations