Package org.teiid.query.sql.symbol

Examples of org.teiid.query.sql.symbol.Function


      //TODO: this can be relaxed for order preserving operations
        if(!(crit.getOperator() == CompareCriteria.EQ || crit.getOperator() == CompareCriteria.NE)) {
          return crit;
        }
      boolean isFormat = false;
      Function leftFunction = (Function) crit.getLeftExpression();
        String funcName = leftFunction.getName().toLowerCase();
        String inverseFunction = null;
        if(funcName.startsWith("parse")) { //$NON-NLS-1$
            String type = funcName.substring(5);
            if (!PARSE_FORMAT_TYPES.contains(type)) {
                return crit;
            }
            inverseFunction = "format" + type; //$NON-NLS-1$
        } else if(funcName.startsWith("format")) { //$NON-NLS-1$
            String type = funcName.substring(6);
            if (!PARSE_FORMAT_TYPES.contains(type)) {
                return crit;
            }
            inverseFunction = "parse" + type; //$NON-NLS-1$
            isFormat = true;
        } else {
            return crit;
        }
        Expression rightExpr = crit.getRightExpression();
        if (!(rightExpr instanceof Constant)) {
          return crit;
        }
        Expression leftExpr = leftFunction.getArgs()[0];
        Expression formatExpr = leftFunction.getArgs()[1];
        if(!(formatExpr instanceof Constant)) {
            return crit;
        }
        String format = (String)((Constant)formatExpr).getValue();
        FunctionLibrary funcLib = this.metadata.getFunctionLibrary();
        FunctionDescriptor descriptor = funcLib.findFunction(inverseFunction, new Class[] { rightExpr.getType(), formatExpr.getType() });
        if(descriptor == null){
            return crit;
        }
      Object value = ((Constant)rightExpr).getValue();
      try {
        Object result = descriptor.invokeFunction(new Object[] {((Constant)rightExpr).getValue(), format});
        result = leftFunction.getFunctionDescriptor().invokeFunction(new Object[] { result, format } );
        if (((Comparable)value).compareTo(result) != 0) {
          return getSimpliedCriteria(crit, leftExpr, crit.getOperator() != CompareCriteria.EQ, true);
        }
      } catch(FunctionExecutionException e) {
            //Not all numeric formats are invertable, so just return the criteria as it may still be valid
View Full Code Here


                if (!(obj.getArg(0) instanceof ElementSymbol)){
                    handleValidationError(QueryPlugin.Util.getString("ERR.015.004.0036"), obj)//$NON-NLS-1$
                }
               
                for (Iterator<Function> functions = FunctionCollectorVisitor.getFunctions(obj.getArg(1), false).iterator(); functions.hasNext();) {
                    Function function = functions.next();
                   
                    if (function.getFunctionDescriptor().getName().equalsIgnoreCase(FunctionLibrary.CONTEXT)) {
                        handleValidationError(QueryPlugin.Util.getString("ValidationVisitor.Context_function_nested"), obj); //$NON-NLS-1$
                    }
                }
            }
      } else if (obj.getFunctionDescriptor().getName().equalsIgnoreCase(FunctionLibrary.ROWLIMIT) ||
View Full Code Here

        PreOrderNavigator.doVisit(obj, visitor);  
        visitor = new FunctionCollectorVisitor(rowLimitFunctions, FunctionLibrary.ROWLIMITEXCEPTION);
        PreOrderNavigator.doVisit(obj, visitor);           
        final int functionCount = rowLimitFunctions.size();
        if (functionCount > 0) {
            Function function = null;
            Expression expr = null;
            if (obj.getLeftExpression() instanceof Function) {
                Function leftExpr = (Function)obj.getLeftExpression();
                if (leftExpr.getFunctionDescriptor().getName().equalsIgnoreCase(FunctionLibrary.ROWLIMIT) ||
                    leftExpr.getFunctionDescriptor().getName().equalsIgnoreCase(FunctionLibrary.ROWLIMITEXCEPTION)) {
                    function = leftExpr;
                    expr = obj.getRightExpression();
                }
            }
            if (function == null && obj.getRightExpression() instanceof Function) {
                Function rightExpr = (Function)obj.getRightExpression();
                if (rightExpr.getFunctionDescriptor().getName().equalsIgnoreCase(FunctionLibrary.ROWLIMIT) ||
                    rightExpr.getFunctionDescriptor().getName().equalsIgnoreCase(FunctionLibrary.ROWLIMITEXCEPTION)) {
                    function = rightExpr;
                    expr = obj.getLeftExpression();
                }
            }
            if (function == null) {
View Full Code Here

        assertEquals("Did not get expected mapped expression", expected, original);     //$NON-NLS-1$
    }
   
    @Test public void testCompareCriteria1() {
        ElementSymbol e1 = new ElementSymbol("e1"); //$NON-NLS-1$
        Function f = new Function("+", new Expression[] { new Constant(new Integer(2)), new Constant(new Integer(5)) }); //$NON-NLS-1$
        Map map = new HashMap();
        map.put(e1, f);
        CompareCriteria before = new CompareCriteria(e1, CompareCriteria.EQ, new Constant("xyz")); //$NON-NLS-1$
        CompareCriteria after = new CompareCriteria(f, CompareCriteria.EQ, new Constant("xyz")); //$NON-NLS-1$
        helpTest(before, map, after);
View Full Code Here

        helpTest(before, map, after);
    }
   
    @Test public void testCompareCriteria2() {
        ElementSymbol e1 = new ElementSymbol("e1"); //$NON-NLS-1$
        Function f = new Function("+", new Expression[] { new Constant(new Integer(2)), new Constant(new Integer(5)) }); //$NON-NLS-1$
        Map map = new HashMap();
        map.put(e1, f);
        CompareCriteria before = new CompareCriteria(new Constant("xyz"), CompareCriteria.EQ, e1); //$NON-NLS-1$
        CompareCriteria after = new CompareCriteria(new Constant("xyz"), CompareCriteria.EQ, f); //$NON-NLS-1$
        helpTest(before, map, after);
View Full Code Here

    @Test public void testFunction1() {
        ElementSymbol e1 = new ElementSymbol("e1"); //$NON-NLS-1$
        ElementSymbol e2 = new ElementSymbol("e2"); //$NON-NLS-1$
        ElementSymbol e3 = new ElementSymbol("e3"); //$NON-NLS-1$
        ElementSymbol e4 = new ElementSymbol("e4"); //$NON-NLS-1$
        Function f1 = new Function("+", new Expression[] { e1, e2 }); //$NON-NLS-1$
        Function f2 = new Function("+", new Expression[] { f1, e3 }); //$NON-NLS-1$
        Function f3 = new Function("+", new Expression[] { f2, e4 }); //$NON-NLS-1$
       
        ElementSymbol e5 = new ElementSymbol("e5");                 //$NON-NLS-1$
        ElementSymbol e6 = new ElementSymbol("e6"); //$NON-NLS-1$
        ElementSymbol e7 = new ElementSymbol("e7"); //$NON-NLS-1$
        Function f4 = new Function("*", new Expression[] { e5, e6 }); //$NON-NLS-1$
                       
        Map map = new HashMap();
        map.put(e3, f4);
        map.put(e2, e7);
       
        Function f5 = new Function("+", new Expression[] { e1, e7 }); //$NON-NLS-1$
        Function f6 = new Function("+", new Expression[] { f5, f4 }); //$NON-NLS-1$
        Function f7 = new Function("+", new Expression[] { f6, e4 }); //$NON-NLS-1$
        helpTest(f3, map, f7);
    }
View Full Code Here

     * a function.
     */
    @Test public void testRecursionDetection() {
      ElementSymbol e1 = new ElementSymbol("e1"); //$NON-NLS-1$
      AggregateSymbol a1 = new AggregateSymbol("x", NonReserved.SUM, false, e1); //$NON-NLS-1$
      Function f = new Function(SourceSystemFunctions.ADD_OP, new Expression[] {a1, a1});
      HashMap<AggregateSymbol, AggregateSymbol> map = new HashMap<AggregateSymbol, AggregateSymbol>();
      map.put(a1, new AggregateSymbol("x", NonReserved.SUM, false, a1)); //$NON-NLS-1$
      ExpressionMappingVisitor.mapExpressions(f, map);
        assertEquals("(SUM(SUM(e1)) + SUM(SUM(e1)))", f.toString()); //$NON-NLS-1$
    }
View Full Code Here

  @Test public void testConversionFunction() {
    GroupSymbol g = new GroupSymbol("g"); //$NON-NLS-1$
    From from = new From();
    from.addGroup(g);

    Function f = new Function("CONVERT", new Expression[] {new ElementSymbol("a", false), new Constant("string")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    ExpressionSymbol es = new ExpressionSymbol("expr", f); //$NON-NLS-1$
    Select select = new Select();
    select.addSymbol(es);

    Query query = new Query();
View Full Code Here

  @Test public void testConversionFunction2() {
    GroupSymbol g = new GroupSymbol("g"); //$NON-NLS-1$
    From from = new From();
    from.addGroup(g);

    Function f = new Function("CONVERT", new Expression[] {new ElementSymbol("a", false), new Constant("timestamp")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Function f2 = new Function("CONVERT", new Expression[] {f, new Constant("string")}); //$NON-NLS-1$ //$NON-NLS-2$
    ExpressionSymbol es = new ExpressionSymbol("expr", f2); //$NON-NLS-1$
    Select select = new Select();
    select.addSymbol(es);

    Query query = new Query();
View Full Code Here

  @Test public void testMultiFunction() {
    GroupSymbol g = new GroupSymbol("g"); //$NON-NLS-1$
    From from = new From();
    from.addGroup(g);

    Function f = new Function("concat", new Expression[] {new ElementSymbol("a", false), new Constant("x")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    Function f2 = new Function("length", new Expression[] {f}); //$NON-NLS-1$
    Function f3 = new Function("+", new Expression[] {new Constant(new Integer(5)), f2}); //$NON-NLS-1$
    ExpressionSymbol es = new ExpressionSymbol("expr", f3); //$NON-NLS-1$
    Select select = new Select();
    select.addSymbol(es);

    Query query = new Query();
View Full Code Here

TOP

Related Classes of org.teiid.query.sql.symbol.Function

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.