Search in sources :

Example 51 with Function

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

the class TestFunction method testFunction2.

public void testFunction2() {
    // $NON-NLS-1$ //$NON-NLS-2$
    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") });
    UnitTestUtil.helpTestEquivalence(0, f1, f2);
}
Also used : Function(org.teiid.query.sql.symbol.Function) Constant(org.teiid.query.sql.symbol.Constant)

Example 52 with Function

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

the class TestFunction method testFunction4.

public void testFunction4() {
    // $NON-NLS-1$
    Function f1 = new Function("f1", new Expression[] { null });
    // $NON-NLS-1$
    Function f2 = new Function("f1", new Expression[] { null });
    UnitTestUtil.helpTestEquivalence(0, f1, f2);
}
Also used : Function(org.teiid.query.sql.symbol.Function)

Example 53 with Function

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

the class ODataExpressionToSQLVisitor method addOne.

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;
}
Also used : Function(org.teiid.query.sql.symbol.Function) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression) Constant(org.teiid.query.sql.symbol.Constant) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery)

Example 54 with Function

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

the class ODataExpressionToSQLVisitor method visit.

@Override
public void visit(Method expr) {
    List<org.teiid.query.sql.symbol.Expression> teiidExprs = new ArrayList<org.teiid.query.sql.symbol.Expression>();
    for (Expression exp : expr.getParameters()) {
        accept(exp);
        teiidExprs.add(this.stack.pop());
    }
    switch(expr.getMethod()) {
        case CONTAINS:
            CompareCriteria criteria = new CompareCriteria(new Function("LOCATE", new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(1), teiidExprs.get(0), new Constant(1) }), CompareCriteria.GE, // $NON-NLS-1$
            new Constant(1));
            this.stack.push(criteria);
            break;
        case STARTSWITH:
            criteria = new CompareCriteria(new Function("LOCATE", new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(1), teiidExprs.get(0), new Constant(1) }), CompareCriteria.EQ, // $NON-NLS-1$
            new Constant(1));
            this.stack.push(criteria);
            break;
        case ENDSWITH:
            criteria = new CompareCriteria(new Function("ENDSWITH", new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(1), teiidExprs.get(0) }), CompareCriteria.EQ, // $NON-NLS-1$
            new Constant(Boolean.TRUE));
            this.stack.push(criteria);
            break;
        case LENGTH:
            this.stack.push(new Function("LENGTH", // $NON-NLS-1$
            new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) }));
            break;
        case INDEXOF:
            stack.push(minusOne(new Function("LOCATE", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(1), teiidExprs.get(0) })));
            break;
        case SUBSTRING:
            org.teiid.query.sql.symbol.Expression[] exprs = teiidExprs.toArray(new org.teiid.query.sql.symbol.Expression[teiidExprs.size()]);
            exprs[1] = addOne(exprs[1]);
            // $NON-NLS-1$
            this.stack.push(new Function("SUBSTRING", exprs));
            break;
        case TOLOWER:
            this.stack.push(new Function("LCASE", // $NON-NLS-1$
            new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) }));
            break;
        case TOUPPER:
            this.stack.push(new Function("UCASE", // $NON-NLS-1$
            new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0) }));
            break;
        case TRIM:
            this.stack.push(new Function("TRIM", new org.teiid.query.sql.symbol.Expression[] { new Constant("BOTH"), new Constant(' '), // $NON-NLS-1$ //$NON-NLS-2$
            teiidExprs.get(0) }));
            break;
        case CONCAT:
            this.stack.push(new Function("CONCAT", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0), teiidExprs.get(1) }));
            break;
        case YEAR:
            this.stack.push(new Function("YEAR", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case MONTH:
            this.stack.push(new Function("MONTH", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case DAY:
            this.stack.push(new Function("DAYOFMONTH", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case HOUR:
            this.stack.push(new Function("HOUR", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case MINUTE:
            this.stack.push(new Function("MINUTE", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case SECOND:
            this.stack.push(new Function("SECOND", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case NOW:
            // $NON-NLS-1$
            this.stack.push(new Function("NOW", new org.teiid.query.sql.symbol.Expression[] {}));
            break;
        case ROUND:
            stack.push(new Function("ROUND", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0), new Constant(0) }));
            break;
        case FLOOR:
            this.stack.push(new Function("FLOOR", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case CEILING:
            this.stack.push(new Function("CEILING", new org.teiid.query.sql.symbol.Expression[] // $NON-NLS-1$
            { teiidExprs.get(0) }));
            break;
        case CAST:
            this.stack.push(new Function(CONVERT, new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0), teiidExprs.get(1) }));
            break;
        case DATE:
            this.stack.push(new Function(CONVERT, new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0), new Constant(DataTypeManager.DefaultDataTypes.DATE) }));
            break;
        case TIME:
            this.stack.push(new Function(CONVERT, new org.teiid.query.sql.symbol.Expression[] { teiidExprs.get(0), new Constant(DataTypeManager.DefaultDataTypes.TIME) }));
            break;
        case FRACTIONALSECONDS:
        case TOTALSECONDS:
        case TOTALOFFSETMINUTES:
        case MINDATETIME:
        case MAXDATETIME:
        case GEODISTANCE:
        case GEOLENGTH:
        case GEOINTERSECTS:
        case ISOF:
        default:
            throw new TeiidRuntimeException(new TeiidNotImplementedException(ODataPlugin.Event.TEIID16027, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16027, expr.getMethod())));
    }
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) ArrayList(java.util.ArrayList) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) Function(org.teiid.query.sql.symbol.Function) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression)

Example 55 with Function

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

the class ODataExpressionToSQLVisitor method visit.

@Override
public void visit(Binary expr) {
    accept(expr.getLeftOperand());
    org.teiid.query.sql.symbol.Expression lhs = this.stack.pop();
    accept(expr.getRightOperand());
    org.teiid.query.sql.symbol.Expression rhs = this.stack.pop();
    org.teiid.query.sql.symbol.Expression binaryExpr = null;
    switch(expr.getOperator()) {
        case HAS:
            // TODO: not supported. What would be SQL equivalent?
            throw new TeiidRuntimeException(new TeiidNotImplementedException(ODataPlugin.Event.TEIID16036, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16036)));
        case MUL:
            // $NON-NLS-1$
            binaryExpr = new Function("*", new org.teiid.query.sql.symbol.Expression[] { lhs, rhs });
            break;
        case DIV:
            // $NON-NLS-1$
            binaryExpr = new Function("/", new org.teiid.query.sql.symbol.Expression[] { lhs, rhs });
            break;
        case MOD:
            // $NON-NLS-1$
            binaryExpr = new Function("MOD", new org.teiid.query.sql.symbol.Expression[] { lhs, rhs });
            break;
        case ADD:
            // $NON-NLS-1$
            binaryExpr = new Function("+", new org.teiid.query.sql.symbol.Expression[] { lhs, rhs });
            break;
        case SUB:
            // $NON-NLS-1$
            binaryExpr = new Function("-", new org.teiid.query.sql.symbol.Expression[] { lhs, rhs });
            break;
        case GT:
            binaryExpr = new CompareCriteria(lhs, CompareCriteria.GT, rhs);
            break;
        case GE:
            binaryExpr = new CompareCriteria(lhs, CompareCriteria.GE, rhs);
            break;
        case LT:
            binaryExpr = new CompareCriteria(lhs, CompareCriteria.LT, rhs);
            break;
        case LE:
            binaryExpr = new CompareCriteria(lhs, CompareCriteria.LE, rhs);
            break;
        case EQ:
            if (rhs instanceof Constant && ((Constant) rhs).getType() == DataTypeManager.DefaultDataClasses.NULL) {
                binaryExpr = new IsNullCriteria(lhs);
            } else {
                binaryExpr = new CompareCriteria(lhs, CompareCriteria.EQ, rhs);
            }
            break;
        case NE:
            if (rhs instanceof Constant && ((Constant) rhs).getType() == DataTypeManager.DefaultDataClasses.NULL) {
                IsNullCriteria crit = new IsNullCriteria(lhs);
                crit.setNegated(true);
                binaryExpr = crit;
            } else {
                binaryExpr = new CompareCriteria(lhs, CompareCriteria.NE, rhs);
            }
            break;
        case AND:
            binaryExpr = new CompoundCriteria(CompoundCriteria.AND, (Criteria) lhs, (Criteria) rhs);
            break;
        case OR:
            binaryExpr = new CompoundCriteria(CompoundCriteria.OR, (Criteria) lhs, (Criteria) rhs);
            break;
    }
    this.stack.push(binaryExpr);
}
Also used : Constant(org.teiid.query.sql.symbol.Constant) ScalarSubquery(org.teiid.query.sql.symbol.ScalarSubquery) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) Function(org.teiid.query.sql.symbol.Function) SearchedCaseExpression(org.teiid.query.sql.symbol.SearchedCaseExpression)

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