use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer in project asterixdb by apache.
the class ExpressionTypeComputer method getTypeForFunction.
private IAType getTypeForFunction(AbstractFunctionCallExpression expr, IVariableTypeEnvironment env, IMetadataProvider<?, ?> mp) throws AlgebricksException {
FunctionIdentifier fi = expr.getFunctionIdentifier();
// Note: built-in functions + udfs
IResultTypeComputer rtc;
FunctionSignature signature = new FunctionSignature(fi.getNamespace(), fi.getName(), fi.getArity());
if (BuiltinFunctions.isBuiltinCompilerFunction(signature, true)) {
rtc = BuiltinFunctions.getResultTypeComputer(fi);
} else {
rtc = ((ExternalFunctionInfo) expr.getFunctionInfo()).getResultTypeComputer();
}
if (rtc == null) {
throw new AlgebricksException("Type computer missing for " + fi);
}
return rtc.computeType(expr, env, mp);
}
use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer 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.typecomputer.base.IResultTypeComputer in project asterixdb by apache.
the class ExceptionTest method test.
@Test
public void test() throws Exception {
// Tests all usual type computers.
Reflections reflections = new Reflections("org.apache.asterix.om.typecomputer", new SubTypesScanner(false));
Set<Class<? extends IResultTypeComputer>> classes = reflections.getSubTypesOf(IResultTypeComputer.class);
int numTypeComputers = 0;
for (Class<? extends IResultTypeComputer> c : classes) {
if (Modifier.isAbstract(c.getModifiers())) {
continue;
}
testTypeComputer(c);
++numTypeComputers;
}
// Currently, there are 83 type computers.
Assert.assertTrue(numTypeComputers >= 83);
}
use of org.apache.asterix.om.typecomputer.base.IResultTypeComputer 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.typecomputer.base.IResultTypeComputer in project asterixdb by apache.
the class TypeComputerTest method test.
@Test
public void test() throws Exception {
// Several exceptional type computers.
Set<String> exceptionalTypeComputers = new HashSet<>();
exceptionalTypeComputers.add("InjectFailureTypeComputer");
exceptionalTypeComputers.add("RecordAddFieldsTypeComputer");
exceptionalTypeComputers.add("OpenRecordConstructorResultType");
exceptionalTypeComputers.add("RecordRemoveFieldsTypeComputer");
exceptionalTypeComputers.add("ClosedRecordConstructorResultType");
exceptionalTypeComputers.add("LocalAvgTypeComputer");
exceptionalTypeComputers.add("BooleanOnlyTypeComputer");
exceptionalTypeComputers.add("AMissingTypeComputer");
exceptionalTypeComputers.add("NullableDoubleTypeComputer");
exceptionalTypeComputers.add("RecordMergeTypeComputer");
exceptionalTypeComputers.add("BooleanOrMissingTypeComputer");
// Tests all usual type computers.
Reflections reflections = new Reflections("org.apache.asterix.om.typecomputer", new SubTypesScanner(false));
Set<Class<? extends IResultTypeComputer>> classes = reflections.getSubTypesOf(IResultTypeComputer.class);
for (Class<? extends IResultTypeComputer> c : classes) {
if (exceptionalTypeComputers.contains(c.getSimpleName()) || Modifier.isAbstract(c.getModifiers())) {
continue;
}
System.out.println("Test type computer: " + c.getName());
Assert.assertTrue(testTypeComputer(c));
}
}
Aggregations