public static org.teiid.query.sql.lang.GroupBy helpExampleWithFunctions() { List<Expression> symbols = new ArrayList<Expression>(); ElementSymbol e1 = TestElementImpl.helpExample("vm1.g1", "e1");//$NON-NLS-1$ //$NON-NLS-2$ Function f = new Function("length", new Expression[] { e1 } );//$NON-NLS-1$ symbols.add(e1); symbols.add(f); return new org.teiid.query.sql.lang.GroupBy(symbols); }
private org.teiid.query.sql.symbol.Expression addOne( org.teiid.query.sql.symbol.Expression expr) { org.teiid.query.sql.symbol.Expression when = new CompareCriteria(expr, CompareCriteria.LT, new Constant(0)); SearchedCaseExpression caseExpr = new SearchedCaseExpression(Arrays.asList(when), Arrays.asList(expr)); caseExpr.setElseExpression(new Function("+", new org.teiid.query.sql.symbol.Expression[] { expr, new Constant(1) })); return caseExpr; }
public static org.teiid.query.sql.symbol.Function helpExample(String name) { Constant c1 = new Constant(new Integer(100)); Constant c2 = new Constant(new Integer(200)); org.teiid.query.sql.symbol.Function f = new org.teiid.query.sql.symbol.Function(name, new org.teiid.query.sql.symbol.Expression[] {c1, c2}); f.setType(Integer.class); return f; }
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); }
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 testCompareCriteria1() { ElementSymbol e1 = new ElementSymbol("e1"); //$NON-NLS-1$ Function f = new Function("+", new Expression[] { new Constant(new Integer(2)), new Constant(new Integer(5)) }); //$NON-NLS-1$ Map<Expression, Expression> map = new HashMap<Expression, Expression>(); map.put(e1, f); CompareCriteria before = new CompareCriteria(e1, CompareCriteria.EQ, new Constant("xyz")); //$NON-NLS-1$ CompareCriteria after = new CompareCriteria(f, CompareCriteria.EQ, new Constant("xyz")); //$NON-NLS-1$ helpTest(before, map, after); }
public void testFunction6() { Function f1 = new Function("f1", new Expression[] {new Constant("xyz")}); //$NON-NLS-1$ //$NON-NLS-2$ Function f2 = new Function("f1", new Expression[] {new Constant("xyz"), new Constant("xyz")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ UnitTestUtil.helpTestEquivalence(1, f1, f2); }
@Test public void testCastFunction2() { Function func = new Function("cast", new Expression[] { //$NON-NLS-1$ null, new Constant("integer") //$NON-NLS-1$ }); helpTest(func, "cast(<undefined> AS integer)"); //$NON-NLS-1$ }
@Test public void testConvertFunction2() { Function func = new Function("convert", new Expression[] { //$NON-NLS-1$ null, new Constant("integer") //$NON-NLS-1$ }); helpTest(func, "convert(<undefined>, integer)"); //$NON-NLS-1$ }
@Test public void testSupportsScalar4() throws Exception { BasicSourceCapabilities caps = new BasicSourceCapabilities(); caps.setFunctionSupport("now", true); //$NON-NLS-1$ Function func = new Function("NOW", new Expression[] { }); //$NON-NLS-1$ helpTestSupportsScalar(caps, func, true); }
@Test public void testResolveBadConvert() throws Exception { Function function = new Function("convert", new Expression[] {new Constant(new Character('a')), new Constant(DataTypeManager.DefaultDataTypes.DATE)}); //$NON-NLS-1$ try { ResolverVisitor.resolveLanguageObject(function, RealMetadataFactory.example1Cached()); fail("excpetion expected"); //$NON-NLS-1$ } catch (QueryResolverException err) { assertEquals("TEIID30071 The conversion from char to date is not allowed.", err.getMessage()); //$NON-NLS-1$ } }
@Test public void testResolvesClosestType() throws Exception { ElementSymbol e1 = new ElementSymbol("pm1.g1.e1"); //$NON-NLS-1$ //dummy resolve to a byte e1.setType(DataTypeManager.DefaultDataClasses.BYTE); e1.setMetadataID(new Object()); Function function = new Function("abs", new Expression[] {e1}); //$NON-NLS-1$ ResolverVisitor.resolveLanguageObject(function, RealMetadataFactory.example1Cached()); assertEquals(DataTypeManager.DefaultDataClasses.INTEGER, function.getType()); }
@Test public void testCastFunction1() { Function func = new Function("cast", new Expression[] { //$NON-NLS-1$ new Constant("5"), //$NON-NLS-1$ new Constant("integer") //$NON-NLS-1$ }); helpTest(func, "cast('5' AS integer)"); //$NON-NLS-1$ }
public void testVisitSubquerySetCriteria() { SubquerySetCriteria ssc = new SubquerySetCriteria(); ssc.setExpression(new Function("length", new Expression[] {exampleElement(true, 0)})); //$NON-NLS-1$ ssc.setCommand(new Query()); helpTest(ssc,getSymbolMap()); }
@Test public void testFunctionWithUnderscore() { String sql = "SELECT yowza_yowza() FROM a.thing"; //$NON-NLS-1$ Query query = new Query(); Select select = new Select(); Function func1 = new Function("yowza_yowza", new Expression[] { }); //$NON-NLS-1$ select.addSymbol(func1); query.setSelect(select); From from = new From(); from.addGroup(new GroupSymbol("a.thing")); //$NON-NLS-1$ query.setFrom(from); helpTest(sql, "SELECT yowza_yowza() FROM a.thing", query); //$NON-NLS-1$ }