Search in sources :

Example 41 with Function

use of org.teiid.query.sql.symbol.Function in project teiid by teiid.

the class TestProjectNode method testProjectWithLookupFunction.

@Test
public void testProjectWithLookupFunction() throws Exception {
    // $NON-NLS-1$
    ElementSymbol es1 = new ElementSymbol("e1");
    es1.setType(DataTypeManager.DefaultDataClasses.STRING);
    List elements = new ArrayList();
    elements.add(es1);
    // $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, String.class });
    func.setFunctionDescriptor(desc);
    func.setType(DataTypeManager.DefaultDataClasses.STRING);
    // $NON-NLS-1$
    ExpressionSymbol expr = new ExpressionSymbol("expr", func);
    List projectElements = new ArrayList();
    projectElements.add(expr);
    List[] data = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { "1" }), // $NON-NLS-1$
    Arrays.asList(new Object[] { "2" }) };
    List[] expected = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { "a" }), // $NON-NLS-1$
    Arrays.asList(new Object[] { "b" }) };
    FakeDataManager dataMgr = new FakeDataManager();
    dataMgr.setThrowBlocked(true);
    Map valueMap = new HashMap();
    // $NON-NLS-1$ //$NON-NLS-2$
    valueMap.put("1", "a");
    // $NON-NLS-1$ //$NON-NLS-2$
    valueMap.put("2", "b");
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    dataMgr.defineCodeTable("pm1.g1", "e1", "e2", valueMap);
    helpTestProject(projectElements, data, elements, expected, dataMgr);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Function(org.teiid.query.sql.symbol.Function) FakeDataManager(org.teiid.query.processor.FakeDataManager) HashMap(java.util.HashMap) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 42 with Function

use of org.teiid.query.sql.symbol.Function in project teiid by teiid.

the class TestProjectNode method testProjectExpression.

@Test
public void testProjectExpression() throws Exception {
    // $NON-NLS-1$
    ElementSymbol es1 = new ElementSymbol("e1");
    es1.setType(DataTypeManager.DefaultDataClasses.STRING);
    List elements = new ArrayList();
    elements.add(es1);
    // $NON-NLS-1$ //$NON-NLS-2$
    Function func = new Function("concat", new Expression[] { es1, new Constant("abc") });
    // $NON-NLS-1$
    FunctionDescriptor fd = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("concat", new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
    func.setFunctionDescriptor(fd);
    func.setType(DataTypeManager.DefaultDataClasses.STRING);
    // $NON-NLS-1$
    ExpressionSymbol expr = new ExpressionSymbol("expr", func);
    List projectElements = new ArrayList();
    projectElements.add(expr);
    List[] data = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { "1" }), // $NON-NLS-1$
    Arrays.asList(new Object[] { "2" }) };
    List[] expected = new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { "1abc" }), // $NON-NLS-1$
    Arrays.asList(new Object[] { "2abc" }) };
    helpTestProject(projectElements, data, elements, expected, null);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Function(org.teiid.query.sql.symbol.Function) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) ExpressionSymbol(org.teiid.query.sql.symbol.ExpressionSymbol) Test(org.junit.Test)

Example 43 with Function

use of org.teiid.query.sql.symbol.Function in project teiid by teiid.

the class TestSelectNode method testSelectWithLookup.

@Test
public void testSelectWithLookup() throws TeiidComponentException, TeiidProcessingException {
    // $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.STRING);
    List elements = new ArrayList();
    elements.add(es1);
    // $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 crit = new CompareCriteria(func, CompareCriteria.EQ, new Constant(new Integer(1)));
    List[] data = new List[20];
    for (int i = 0; i < 20; i++) {
        data[i] = new ArrayList();
        data[i].add(new Integer((i * 51) % 11));
        // $NON-NLS-1$
        String str = "" + (i * 3);
        str = str.substring(0, 1);
        data[i].add(str);
    }
    List childElements = new ArrayList();
    childElements.add(es1);
    childElements.add(es2);
    List[] expected = new List[] { Arrays.asList(new Object[] { new Integer(0) }), Arrays.asList(new Object[] { new Integer(0) }) };
    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));
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    dataMgr.defineCodeTable("pm1.g1", "e1", "e2", valueMap);
    helpTestSelect(elements, crit, data, childElements, dataMgr, expected);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) FakeDataManager(org.teiid.query.processor.FakeDataManager) HashMap(java.util.HashMap) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) Function(org.teiid.query.sql.symbol.Function) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 44 with Function

use of org.teiid.query.sql.symbol.Function in project teiid by teiid.

the class TestFunctionResolving method testResolveAmbiguousFunction.

@Test
public void testResolveAmbiguousFunction() throws Exception {
    // $NON-NLS-1$
    Function function = new Function("LCASE", new Expression[] { new Reference(0) });
    try {
        ResolverVisitor.resolveLanguageObject(function, RealMetadataFactory.example1Cached());
        // $NON-NLS-1$
        fail("excpetion expected");
    } catch (QueryResolverException err) {
        // $NON-NLS-1$
        assertEquals("TEIID30069 The function 'LCASE(?)' has more than one possible signature.", err.getMessage());
    }
}
Also used : Function(org.teiid.query.sql.symbol.Function) Reference(org.teiid.query.sql.symbol.Reference) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) Test(org.junit.Test)

Example 45 with Function

use of org.teiid.query.sql.symbol.Function in project teiid by teiid.

the class TestFunctionResolving method testImportedPushdown.

@Test
public void testImportedPushdown() throws Exception {
    RealMetadataFactory.example1Cached();
    QueryMetadataInterface tm = RealMetadataFactory.fromDDL("x", new DDLHolder("y", "create foreign function func(x object) returns object;"), new DDLHolder("z", "create foreign function func(x object) returns object;"));
    String sql = "func('a')";
    Function func = (Function) QueryParser.getQueryParser().parseExpression(sql);
    try {
        ResolverVisitor.resolveLanguageObject(func, tm);
        fail("should be ambiguous");
    } catch (QueryResolverException e) {
    }
    tm = RealMetadataFactory.fromDDL("x", new DDLHolder("y", "create foreign function func(x object) returns object options (\"teiid_rel:system-name\" 'f');"), new DDLHolder("z", "create foreign function func(x object) returns object options (\"teiid_rel:system-name\" 'f');"));
    func = (Function) QueryParser.getQueryParser().parseExpression(sql);
    ResolverVisitor.resolveLanguageObject(func, tm);
    tm = RealMetadataFactory.fromDDL("x", new DDLHolder("y", "create foreign function func() returns object options (\"teiid_rel:system-name\" 'f');"), new DDLHolder("z", "create foreign function func() returns object options (\"teiid_rel:system-name\" 'f');"));
    func = (Function) QueryParser.getQueryParser().parseExpression("func()");
    ResolverVisitor.resolveLanguageObject(func, tm);
}
Also used : Function(org.teiid.query.sql.symbol.Function) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) Test(org.junit.Test)

Aggregations

Function (org.teiid.query.sql.symbol.Function)55 Constant (org.teiid.query.sql.symbol.Constant)31 Test (org.junit.Test)25 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)16 Expression (org.teiid.query.sql.symbol.Expression)12 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)11 ArrayList (java.util.ArrayList)10 List (java.util.List)7 ScalarSubquery (org.teiid.query.sql.symbol.ScalarSubquery)7 SearchedCaseExpression (org.teiid.query.sql.symbol.SearchedCaseExpression)6 HashMap (java.util.HashMap)5 ExpressionSymbol (org.teiid.query.sql.symbol.ExpressionSymbol)5 Map (java.util.Map)4 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)4 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)4 Reference (org.teiid.query.sql.symbol.Reference)4 FunctionLibrary (org.teiid.query.function.FunctionLibrary)3 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)3 FakeDataManager (org.teiid.query.processor.FakeDataManager)3 Query (org.teiid.query.sql.lang.Query)3