use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestGroupByImpl method helpExampleWithFunctions.
public static org.teiid.query.sql.lang.GroupBy helpExampleWithFunctions() {
List<Expression> symbols = new ArrayList<Expression>();
// $NON-NLS-1$ //$NON-NLS-2$
ElementSymbol e1 = TestElementImpl.helpExample("vm1.g1", "e1");
// $NON-NLS-1$
Function f = new Function("length", new Expression[] { e1 });
symbols.add(e1);
symbols.add(f);
return new org.teiid.query.sql.lang.GroupBy(symbols);
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestCapabilitiesUtil method testSupportsScalar1.
// Test where capabilities don't support scalar functions
@Test
public void testSupportsScalar1() throws Exception {
BasicSourceCapabilities caps = new BasicSourceCapabilities();
// $NON-NLS-1$
Function func = new Function("+", new Expression[] { new Constant(1), new Constant(2) });
helpTestSupportsScalar(caps, func, false);
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestResolver method testDateToTimestampConversion_defect9747.
@Test
public void testDateToTimestampConversion_defect9747() {
// Expected left expression
// $NON-NLS-1$
ElementSymbol e1 = new ElementSymbol("pm3.g1.e4");
e1.setType(DataTypeManager.DefaultDataClasses.TIMESTAMP);
// Expected right expression
Constant e2 = new Constant(TimestampUtil.createDate(96, 0, 31), DataTypeManager.DefaultDataClasses.DATE);
// $NON-NLS-1$
Function f1 = new Function("convert", new Expression[] { e2, new Constant(DataTypeManager.DefaultDataTypes.TIMESTAMP) });
f1.makeImplicit();
// Expected criteria
CompareCriteria expected = new CompareCriteria();
expected.setLeftExpression(e1);
expected.setOperator(CompareCriteria.GT);
expected.setRightExpression(f1);
// Resolve the query and check against expected objects
// $NON-NLS-1$
CompareCriteria actual = (CompareCriteria) helpResolveCriteria("pm3.g1.e4 > {d '1996-01-31'}");
// $NON-NLS-1$
assertEquals("Did not match expected criteria", expected, actual);
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestResolver method testSubQueryINClauseImplicitConversion.
/**
* An implicit type conversion needs to be inserted because the
* project symbol of the subquery is not the same type as the expression in
* the SubquerySetCriteria object
*/
@Test
public void testSubQueryINClauseImplicitConversion() {
// select e1 from pm1.g1 where e2 in (select e1 from pm4.g1)
// sub command
Select innerSelect = new Select();
// $NON-NLS-1$
ElementSymbol e1inner = new ElementSymbol("e1");
innerSelect.addSymbol(e1inner);
From innerFrom = new From();
// $NON-NLS-1$
GroupSymbol pm4g1 = new GroupSymbol("pm4.g1");
innerFrom.addGroup(pm4g1);
Query innerQuery = new Query();
innerQuery.setSelect(innerSelect);
innerQuery.setFrom(innerFrom);
// outer command
Select outerSelect = new Select();
// $NON-NLS-1$
ElementSymbol e1 = new ElementSymbol("e1");
outerSelect.addSymbol(e1);
From outerFrom = new From();
// $NON-NLS-1$
GroupSymbol pm1g1 = new GroupSymbol("pm1.g1");
outerFrom.addGroup(pm1g1);
// $NON-NLS-1$
ElementSymbol e2 = new ElementSymbol("e2");
SubquerySetCriteria crit = new SubquerySetCriteria(e2, innerQuery);
Query outerQuery = new Query();
outerQuery.setSelect(outerSelect);
outerQuery.setFrom(outerFrom);
outerQuery.setCriteria(crit);
// test
helpResolve(outerQuery);
// $NON-NLS-1$
helpCheckFrom(outerQuery, new String[] { "pm1.g1" });
// $NON-NLS-1$
helpCheckFrom(innerQuery, new String[] { "pm4.g1" });
// $NON-NLS-1$
helpCheckSelect(outerQuery, new String[] { "pm1.g1.e1" });
// $NON-NLS-1$
helpCheckSelect(innerQuery, new String[] { "pm4.g1.e1" });
helpCheckElements(outerQuery.getSelect(), // $NON-NLS-1$
new String[] { "pm1.g1.e1" }, // $NON-NLS-1$
new String[] { "pm1.g1.e1" });
helpCheckElements(innerQuery.getSelect(), // $NON-NLS-1$
new String[] { "pm4.g1.e1" }, // $NON-NLS-1$
new String[] { "pm4.g1.e1" });
// $NON-NLS-1$
String sql = "SELECT e1 FROM pm1.g1 WHERE e2 IN (SELECT e1 FROM pm4.g1)";
// $NON-NLS-1$
assertEquals("Resolved string form was incorrect ", sql, outerQuery.toString());
// make sure there is a convert function wrapping the criteria left expression
Collection functions = FunctionCollectorVisitor.getFunctions(outerQuery, true);
assertTrue(functions.size() == 1);
Function function = (Function) functions.iterator().next();
assertTrue(function.getName().equals(FunctionLibrary.CONVERT));
Expression[] args = function.getArgs();
assertSame(e2, args[0]);
assertTrue(args[1] instanceof Constant);
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestGroupingNode method helpTestLookupFunctionInAggregate.
private void helpTestLookupFunctionInAggregate(int batchSize) throws Exception {
BufferManagerImpl mgr = BufferManagerFactory.createBufferManager();
mgr.setProcessorBatchSize(batchSize);
// Set up
GroupingNode node = new GroupingNode(1);
List outputElements = new ArrayList();
// $NON-NLS-1$
ElementSymbol col1 = new ElementSymbol("col1");
col1.setType(Integer.class);
// $NON-NLS-1$
ElementSymbol col2 = new ElementSymbol("col2");
col2.setType(Integer.class);
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
Function func = new Function("lookup", new Expression[] { new Constant("pm1.g1"), new Constant("e2"), new Constant("e1"), col2 });
// $NON-NLS-1$
FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("lookup", new Class[] { String.class, String.class, String.class, Integer.class });
func.setFunctionDescriptor(desc);
func.setType(DataTypeManager.DefaultDataClasses.INTEGER);
outputElements.add(col1);
// $NON-NLS-1$ //$NON-NLS-2$
outputElements.add(new AggregateSymbol("COUNT", false, func));
// $NON-NLS-1$ //$NON-NLS-2$
outputElements.add(new AggregateSymbol("SUM", false, func));
// $NON-NLS-1$ //$NON-NLS-2$
outputElements.add(new AggregateSymbol("SUM", true, func));
node.setElements(outputElements);
List groupingElements = new ArrayList();
groupingElements.add(col1);
node.setOrderBy(new OrderBy(groupingElements).getOrderByItems());
// $NON-NLS-1$ //$NON-NLS-2$
CommandContext context = new CommandContext("pid", "test", null, null, 1);
FakeDataManager dataMgr = new FakeDataManager();
dataMgr.setThrowBlocked(true);
Map valueMap = new HashMap();
valueMap.put(new Integer(0), new Integer(1));
valueMap.put(new Integer(1), new Integer(2));
valueMap.put(new Integer(2), new Integer(3));
valueMap.put(new Integer(3), new Integer(4));
valueMap.put(new Integer(4), new Integer(5));
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
dataMgr.defineCodeTable("pm1.g1", "e1", "e2", valueMap);
List[] expected = new List[] { Arrays.asList(new Object[] { null, new Integer(1), new Long(4), new Long(4) }), Arrays.asList(new Object[] { new Integer(0), new Integer(1), new Long(5), new Long(5) }), Arrays.asList(new Object[] { new Integer(1), new Integer(1), new Long(3), new Long(3) }), Arrays.asList(new Object[] { new Integer(2), new Integer(4), new Long(9), new Long(5) }), Arrays.asList(new Object[] { new Integer(3), new Integer(1), new Long(1), new Long(1) }), Arrays.asList(new Object[] { new Integer(4), new Integer(2), new Long(7), new Long(7) }), Arrays.asList(new Object[] { new Integer(5), new Integer(1), new Long(4), new Long(4) }), Arrays.asList(new Object[] { new Integer(6), new Integer(2), new Long(9), new Long(9) }) };
helpProcess(mgr, node, context, expected, dataMgr);
}
Aggregations