use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestResolver method testStringConversion1.
@Test
public void testStringConversion1() {
// Expected left expression
// $NON-NLS-1$
ElementSymbol e1 = new ElementSymbol("pm3.g1.e2");
e1.setType(DataTypeManager.DefaultDataClasses.DATE);
// Expected right expression
Class srcType = DataTypeManager.DefaultDataClasses.STRING;
String tgtTypeName = DataTypeManager.DefaultDataTypes.DATE;
// $NON-NLS-1$
Expression expression = new Constant("2003-02-27");
FunctionLibrary library = RealMetadataFactory.SFM.getSystemFunctionLibrary();
FunctionDescriptor fd = library.findFunction(FunctionLibrary.CONVERT, new Class[] { srcType, DataTypeManager.DefaultDataClasses.STRING });
Function conversion = new Function(fd.getName(), new Expression[] { expression, new Constant(tgtTypeName) });
conversion.setType(DataTypeManager.getDataTypeClass(tgtTypeName));
conversion.setFunctionDescriptor(fd);
conversion.makeImplicit();
// Expected criteria
CompareCriteria expected = new CompareCriteria();
expected.setLeftExpression(e1);
expected.setOperator(CompareCriteria.EQ);
expected.setRightExpression(conversion);
// Resolve the query and check against expected objects
// $NON-NLS-1$
CompareCriteria actual = (CompareCriteria) helpResolveCriteria("pm3.g1.e2='2003-02-27'");
// $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 TestJoinNode method helpCreateJoin.
protected void helpCreateJoin() {
// $NON-NLS-1$
ElementSymbol es1 = new ElementSymbol("e1");
es1.setType(DataTypeManager.DefaultDataClasses.INTEGER);
// $NON-NLS-1$
ElementSymbol es2 = new ElementSymbol("e2");
es2.setType(DataTypeManager.DefaultDataClasses.INTEGER);
List leftElements = new ArrayList();
leftElements.add(es1);
leftNode = new BlockingFakeRelationalNode(1, leftTuples);
leftNode.setElements(leftElements);
List rightElements = new ArrayList();
rightElements.add(es2);
rightNode = new BlockingFakeRelationalNode(2, rightTuples) {
@Override
public boolean hasBuffer() {
return false;
}
@Override
public TupleBuffer getBufferDirect(int maxRows) throws BlockedException, TeiidComponentException, TeiidProcessingException {
fail();
throw new AssertionError();
}
};
rightNode.setElements(rightElements);
List joinElements = new ArrayList();
joinElements.add(es1);
joinElements.add(es2);
join = new JoinNode(3);
joinStrategy = new NestedLoopJoinStrategy();
join.setJoinStrategy(joinStrategy);
join.setElements(joinElements);
join.setJoinType(joinType);
switch(criteriaType) {
case NO_CRITERIA:
break;
case EQUAL_CRITERIA:
join.setJoinExpressions(Arrays.asList(es1), Arrays.asList(es2));
joinStrategy = new MergeJoinStrategy(SortOption.SORT, SortOption.SORT, false);
join.setJoinStrategy(joinStrategy);
break;
case FUNCTION_CRITERIA:
// $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"), es1 });
// $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);
CompareCriteria joinCriteria = new CompareCriteria(es2, CompareCriteria.EQ, func);
join.setJoinCriteria(joinCriteria);
break;
}
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestFunctionResolving method testResolvesClosestType.
@Test
public void testResolvesClosestType() throws Exception {
// $NON-NLS-1$
ElementSymbol e1 = new ElementSymbol("pm1.g1.e1");
// dummy resolve to a byte
e1.setType(DataTypeManager.DefaultDataClasses.BYTE);
e1.setMetadataID(new Object());
// $NON-NLS-1$
Function function = new Function("abs", new Expression[] { e1 });
ResolverVisitor.resolveLanguageObject(function, RealMetadataFactory.example1Cached());
assertEquals(DataTypeManager.DefaultDataClasses.INTEGER, function.getType());
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestFunctionResolving method testVarArgsFunction.
@Test
public void testVarArgsFunction() throws Exception {
String ddl = "create foreign function func (VARIADIC z object) returns string options (JAVA_CLASS '" + this.getClass().getName() + "', JAVA_METHOD 'vararg');\n";
TransformationMetadata tm = RealMetadataFactory.fromDDL(ddl, "x", "y");
String sql = "func(('a', 'b'))";
Function func = (Function) QueryParser.getQueryParser().parseExpression(sql);
ResolverVisitor.resolveLanguageObject(func, tm);
assertEquals(1, func.getArgs().length);
assertEquals("2", Evaluator.evaluate(func));
}
use of org.teiid.query.sql.symbol.Function in project teiid by teiid.
the class TestFunctionResolving method testAmbiguousUDF.
@Test
public void testAmbiguousUDF() throws Exception {
TransformationMetadata tm = RealMetadataFactory.fromDDL("x", new DDLHolder("y", "create foreign function f () returns string"), new DDLHolder("z", "create foreign function f () returns string"));
String sql = "f()";
Function func = (Function) QueryParser.getQueryParser().parseExpression(sql);
try {
ResolverVisitor.resolveLanguageObject(func, tm);
fail();
} catch (QueryResolverException e) {
}
sql = "z.f()";
func = (Function) QueryParser.getQueryParser().parseExpression(sql);
ResolverVisitor.resolveLanguageObject(func, tm);
}
Aggregations