Search in sources :

Example 16 with Function

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);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Function(org.teiid.query.sql.symbol.Function) Expression(org.teiid.query.sql.symbol.Expression) Constant(org.teiid.query.sql.symbol.Constant) FunctionLibrary(org.teiid.query.function.FunctionLibrary) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) Test(org.junit.Test)

Example 17 with Function

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;
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Constant(org.teiid.query.sql.symbol.Constant) ArrayList(java.util.ArrayList) TupleBuffer(org.teiid.common.buffer.TupleBuffer) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) BlockedException(org.teiid.common.buffer.BlockedException) CompareCriteria(org.teiid.query.sql.lang.CompareCriteria) TeiidProcessingException(org.teiid.core.TeiidProcessingException) Function(org.teiid.query.sql.symbol.Function) ArrayList(java.util.ArrayList) List(java.util.List) TeiidComponentException(org.teiid.core.TeiidComponentException)

Example 18 with Function

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());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Function(org.teiid.query.sql.symbol.Function) Test(org.junit.Test)

Example 19 with Function

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));
}
Also used : Function(org.teiid.query.sql.symbol.Function) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Test(org.junit.Test)

Example 20 with Function

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);
}
Also used : Function(org.teiid.query.sql.symbol.Function) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) 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