/** * Find conversion function and set return type to proper type. * @param sourceType The source type class * @param targetType The target type class * @return A CONVERT function descriptor or null if not possible */ public FunctionDescriptor findTypedConversionFunction(Class<?> sourceType, Class<?> targetType) { //TODO: should array to string be prohibited? FunctionDescriptor fd = findFunction(CONVERT, new Class[] {sourceType, DataTypeManager.DefaultDataClasses.STRING}); if (fd != null) { return copyFunctionChangeReturnType(fd, targetType); } return null; }
/** * Find conversion function and set return type to proper type. * @param sourceType The source type class * @param targetType The target type class * @return A CONVERT function descriptor or null if not possible */ public FunctionDescriptor findTypedConversionFunction(Class<?> sourceType, Class<?> targetType) { //TODO: should array to string be prohibited? FunctionDescriptor fd = findFunction(CONVERT, new Class[] {sourceType, DataTypeManager.DefaultDataClasses.STRING}); if (fd != null) { return copyFunctionChangeReturnType(fd, targetType); } return null; }
/** * Find conversion function and set return type to proper type. * @param sourceType The source type class * @param targetType The target type class * @return A CONVERT function descriptor or null if not possible */ public FunctionDescriptor findTypedConversionFunction(Class<?> sourceType, Class<?> targetType) { //TODO: should array to string be prohibited? FunctionDescriptor fd = findFunction(CONVERT, new Class[] {sourceType, DataTypeManager.DefaultDataClasses.STRING}); if (fd != null) { return copyFunctionChangeReturnType(fd, targetType); } return null; }
private void helpFindFunctionFail(String fname, Class<?>[] types) { FunctionDescriptor actual = library.findFunction(fname, types); assertNull("Function was found but should not have been: " + actual, actual); //$NON-NLS-1$ }
static Expression op(String op, Expression expr1, Expression expr2, FunctionLibrary functionLibrary) { if (expr1 == null) { return expr2; } if (expr2 == null) { return expr1; } Function newExpr = new Function(op, new Expression[] {expr1, expr2}); newExpr.setFunctionDescriptor(functionLibrary.findFunction(op, new Class[] {DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.INTEGER})); newExpr.setType(newExpr.getFunctionDescriptor().getReturnType()); return evaluateIfPossible(newExpr); }
@Test public void testNullDependent() { FunctionDescriptor actual = library.findFunction("concat2", new Class<?>[] {String.class, String.class}); //$NON-NLS-1$ assertTrue(actual.isNullDependent()); actual = library.findFunction("concat", new Class<?>[] {String.class, String.class}); //$NON-NLS-1$ assertFalse(actual.isNullDependent()); }
static Expression op(String op, Expression expr1, Expression expr2, FunctionLibrary functionLibrary) { if (expr1 == null) { return expr2; } if (expr2 == null) { return expr1; } Function newExpr = new Function(op, new Expression[] {expr1, expr2}); newExpr.setFunctionDescriptor(functionLibrary.findFunction(op, new Class[] {DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.INTEGER})); newExpr.setType(newExpr.getFunctionDescriptor().getReturnType()); return evaluateIfPossible(newExpr); }
static Expression op(String op, Expression expr1, Expression expr2, FunctionLibrary functionLibrary) { if (expr1 == null) { return expr2; } if (expr2 == null) { return expr1; } Function newExpr = new Function(op, new Expression[] {expr1, expr2}); newExpr.setFunctionDescriptor(functionLibrary.findFunction(op, new Class[] {DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.INTEGER})); newExpr.setType(newExpr.getFunctionDescriptor().getReturnType()); return evaluateIfPossible(newExpr); }
private void helpFindFunction(String fname, Class<?>[] types, FunctionDescriptor expected) { FunctionDescriptor actual = library.findFunction(fname, types); assertEquals("Function names do not match: ", expected.getName().toLowerCase(), actual.getName().toLowerCase()); //$NON-NLS-1$ assertEquals("Arg lengths do not match: ", expected.getTypes().length, actual.getTypes().length); //$NON-NLS-1$ }
@Test public void testProjectExpression() throws Exception { ElementSymbol es1 = new ElementSymbol("e1"); //$NON-NLS-1$ es1.setType(DataTypeManager.DefaultDataClasses.STRING); List elements = new ArrayList(); elements.add(es1); Function func = new Function("concat", new Expression[] { es1, new Constant("abc")}); //$NON-NLS-1$ //$NON-NLS-2$ FunctionDescriptor fd = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("concat", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ func.setFunctionDescriptor(fd); func.setType(DataTypeManager.DefaultDataClasses.STRING); ExpressionSymbol expr = new ExpressionSymbol("expr", func); //$NON-NLS-1$ List projectElements = new ArrayList(); projectElements.add(expr); List[] data = new List[] { Arrays.asList(new Object[] { "1" }), //$NON-NLS-1$ Arrays.asList(new Object[] { "2" }) }; //$NON-NLS-1$ List[] expected = new List[] { Arrays.asList(new Object[] { "1abc" }), //$NON-NLS-1$ Arrays.asList(new Object[] { "2abc" }) }; //$NON-NLS-1$ helpTestProject(projectElements, data, elements, expected, null); }
@Test public void testUser() throws Exception { Function func = new Function("user", new Expression[] {}); //$NON-NLS-1$ FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("user", new Class[] {} ); //$NON-NLS-1$ func.setFunctionDescriptor(desc); FakeDataManager dataMgr = new FakeDataManager(); CommandContext context = new CommandContext(new Long(1), null, null, null, 0); context.setUserName("logon"); //$NON-NLS-1$ assertEquals(context.getUserName(), new Evaluator(Collections.emptyMap(), dataMgr, context).evaluate(func, Collections.emptyList()) ); }
@Test public void testProjectExpressionFunctionFails() throws Exception { ElementSymbol es1 = new ElementSymbol("e1"); //$NON-NLS-1$ es1.setType(DataTypeManager.DefaultDataClasses.STRING); List elements = new ArrayList(); elements.add(es1); Function func = new Function("convert", new Expression[] { es1, new Constant("integer")}); //$NON-NLS-1$ //$NON-NLS-2$ FunctionDescriptor fd = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("convert", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ func.setFunctionDescriptor(fd); func.setType(DataTypeManager.DefaultDataClasses.INTEGER); ExpressionSymbol expr = new ExpressionSymbol("expr", func); //$NON-NLS-1$ List projectElements = new ArrayList(); projectElements.add(expr); List[] data = new List[] { Arrays.asList(new Object[] { "1" }), //$NON-NLS-1$ Arrays.asList(new Object[] { "2x" }) }; //$NON-NLS-1$ String expectedMessage = "TEIID30328 UNABLE TO EVALUATE CONVERT(E1, INTEGER): TEIID30384 ERROR WHILE EVALUATING FUNCTION CONVERT"; //$NON-NLS-1$ helpTestProjectFails(projectElements, data, elements, expectedMessage); }
@Test public void testMultiPartName() throws Exception { FunctionMethod method = new FunctionMethod( "x.y.dummy", null, null, PushDown.CANNOT_PUSHDOWN, TestFunctionTree.class.getName(), "toString", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ Arrays.asList(new FunctionParameter("in", DataTypeManager.DefaultDataTypes.VARBINARY)), //$NON-NLS-1$ new FunctionParameter("output", DataTypeManager.DefaultDataTypes.STRING), //$NON-NLS-1$ true, Determinism.DETERMINISTIC); FunctionTree sys = RealMetadataFactory.SFM.getSystemFunctions(); FunctionLibrary fl = new FunctionLibrary(sys, new FunctionTree("foo", new UDFSource(Arrays.asList(method)), true)); assertNotNull(fl.findFunction("dummy", new Class<?>[] {DataTypeManager.DefaultDataClasses.VARBINARY})); assertNotNull(fl.findFunction("y.dummy", new Class<?>[] {DataTypeManager.DefaultDataClasses.VARBINARY})); }
@Test public void testVarbinary() throws Exception { FunctionMethod method = new FunctionMethod( "dummy", null, null, PushDown.CANNOT_PUSHDOWN, TestFunctionTree.class.getName(), "toString", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ Arrays.asList(new FunctionParameter("in", DataTypeManager.DefaultDataTypes.VARBINARY)), //$NON-NLS-1$ new FunctionParameter("output", DataTypeManager.DefaultDataTypes.STRING), //$NON-NLS-1$ true, Determinism.DETERMINISTIC); FunctionTree sys = RealMetadataFactory.SFM.getSystemFunctions(); FunctionLibrary fl = new FunctionLibrary(sys, new FunctionTree("foo", new UDFSource(Arrays.asList(method)), true)); FunctionDescriptor fd = fl.findFunction("dummy", new Class<?>[] {DataTypeManager.DefaultDataClasses.VARBINARY}); String hello = "hello"; assertEquals(hello, fd.invokeFunction(new Object[] {new BinaryType(hello.getBytes())}, null, null)); }
public void helpTestCommandPayload(Serializable payload, String property, String expectedValue) throws Exception { Function func = new Function("commandpayload", new Expression[] {}); //$NON-NLS-1$ Class[] parameterSignature = null; if(property == null) { parameterSignature = new Class[] {}; } else { parameterSignature = new Class[] { String.class }; } FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("commandpayload", parameterSignature ); //$NON-NLS-1$ func.setFunctionDescriptor(desc); FakeDataManager dataMgr = new FakeDataManager(); CommandContext context = new CommandContext(null, "user", payload, "vdb", 1, false); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ if(property != null) { func.setArgs(new Expression[] {new Constant(property)}); } String actual = (String) new Evaluator(Collections.emptyMap(), dataMgr, context).evaluate(func, Collections.emptyList()); assertEquals(expectedValue, actual); }
private void modifyToCheckMatViewStatus(QueryMetadataInterface metadata, QueryCommand queryCommand, Set<Object> ids) throws QueryMetadataException, TeiidComponentException { for (Object viewMatadataId : ids) { String schemaName = metadata.getName(metadata.getModelID(viewMatadataId)); String viewName = metadata.getName(viewMatadataId); Expression expr1 = new Constant(schemaName); Expression expr2 = new Constant(viewName); Function status = new Function("mvstatus", new Expression[] {expr1, expr2}); //$NON-NLS-1$ status.setType(DataTypeManager.DefaultDataClasses.INTEGER); FunctionDescriptor descriptor = metadata.getFunctionLibrary().findFunction("mvstatus", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ status.setFunctionDescriptor(descriptor); Query query = queryCommand.getProjectedQuery(); //insert first so that it gets evaluated ahead of any false predicate query.setCriteria(Criteria.combineCriteria(new CompareCriteria(status, CompareCriteria.EQ, new Constant(1)), query.getCriteria())); } }
private void modifyToCheckMatViewStatus(QueryMetadataInterface metadata, QueryCommand queryCommand, Set<Object> ids) throws QueryMetadataException, TeiidComponentException { for (Object viewMatadataId : ids) { String schemaName = metadata.getName(metadata.getModelID(viewMatadataId)); String viewName = metadata.getName(viewMatadataId); Expression expr1 = new Constant(schemaName); Expression expr2 = new Constant(viewName); Function status = new Function("mvstatus", new Expression[] {expr1, expr2}); //$NON-NLS-1$ status.setType(DataTypeManager.DefaultDataClasses.INTEGER); FunctionDescriptor descriptor = metadata.getFunctionLibrary().findFunction("mvstatus", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ status.setFunctionDescriptor(descriptor); Query query = queryCommand.getProjectedQuery(); //insert first so that it gets evaluated ahead of any false predicate query.setCriteria(Criteria.combineCriteria(new CompareCriteria(status, CompareCriteria.EQ, new Constant(1)), query.getCriteria())); } }
private void modifyToCheckMatViewStatus(QueryMetadataInterface metadata, QueryCommand queryCommand, Set<Object> ids) throws QueryMetadataException, TeiidComponentException { for (Object viewMatadataId : ids) { String schemaName = metadata.getName(metadata.getModelID(viewMatadataId)); String viewName = metadata.getName(viewMatadataId); Expression expr1 = new Constant(schemaName); Expression expr2 = new Constant(viewName); Function status = new Function("mvstatus", new Expression[] {expr1, expr2}); //$NON-NLS-1$ status.setType(DataTypeManager.DefaultDataClasses.INTEGER); FunctionDescriptor descriptor = metadata.getFunctionLibrary().findFunction("mvstatus", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING }); //$NON-NLS-1$ status.setFunctionDescriptor(descriptor); Query query = queryCommand.getProjectedQuery(); //insert first so that it gets evaluated ahead of any false predicate query.setCriteria(Criteria.combineCriteria(new CompareCriteria(status, CompareCriteria.EQ, new Constant(1)), query.getCriteria())); } }
@Test public void testFunction2() { ElementSymbol e1 = new ElementSymbol("e1"); //$NON-NLS-1$ e1.setType(String.class); ElementSymbol e2 = new ElementSymbol("e2"); //$NON-NLS-1$ e2.setType(String.class); Function func = new Function("concat", new Expression[] { e2, e1 }); //$NON-NLS-1$ FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("concat", new Class[] { String.class, String.class } ); //$NON-NLS-1$ func.setFunctionDescriptor(desc); Expression[] elements = new Expression[] { e1, e2 }; Object[] values = new Object[] { "xyz", "abc" //$NON-NLS-1$ //$NON-NLS-2$ }; helpTestEval(func, elements, values, null, null, "abcxyz"); //$NON-NLS-1$ }
@Test public void testFunction1() { ElementSymbol e1 = new ElementSymbol("e1"); //$NON-NLS-1$ e1.setType(String.class); ElementSymbol e2 = new ElementSymbol("e2"); //$NON-NLS-1$ e2.setType(String.class); Function func = new Function("concat", new Expression[] { e1, e2 }); //$NON-NLS-1$ FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("concat", new Class[] { String.class, String.class } ); //$NON-NLS-1$ func.setFunctionDescriptor(desc); Expression[] elements = new Expression[] { e1, e2 }; Object[] values = new Object[] { "xyz", "abc" //$NON-NLS-1$ //$NON-NLS-2$ }; helpTestEval(func, elements, values, null, null, "xyzabc"); //$NON-NLS-1$ }