Package org.teiid.query.sql.symbol

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


          }
          return getSimpliedCriteria(criteria, criteria.getExpression(), !criteria.isNegated(), true);
        }
       
        if(criteria.getExpression() instanceof Function ) {
            Function leftFunction = (Function)criteria.getExpression();
            if(FunctionLibrary.isConvert(leftFunction)) {
                return simplifyConvertFunction(criteria);       
            }
        }
View Full Code Here


        return true;
      }
      if (!(ex instanceof Function)) {
        return false;
      }
      Function f = (Function)ex;
    if (!FunctionLibrary.isConvert(f)) {
      return false;
    }
    return isConstantConvert(f.getArg(0));
    }
View Full Code Here

   
    Integer code = FUNCTION_MAP.get(functionLowerName);
    if (code != null) {
      switch (code) {
      case 0: { //space(x) => repeat(' ', x)
        Function result = new Function(SourceSystemFunctions.REPEAT,
            new Expression[] {new Constant(" "), function.getArg(0)}); //$NON-NLS-1$
        //resolve the function
        FunctionDescriptor descriptor =
          funcLibrary.findFunction(SourceSystemFunctions.REPEAT, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.INTEGER});
        result.setFunctionDescriptor(descriptor);
        result.setType(DataTypeManager.DefaultDataClasses.STRING);
        function = result;
        break;
      }
      case 1: {//from_unixtime(a) => timestampadd(SQL_TSI_SECOND, a, new Timestamp(0))
        Function result = new Function(FunctionLibrary.TIMESTAMPADD,
            new Expression[] {new Constant(NonReserved.SQL_TSI_SECOND), function.getArg(0), new Constant(new Timestamp(0)) });
        //resolve the function
        FunctionDescriptor descriptor =
          funcLibrary.findFunction(FunctionLibrary.TIMESTAMPADD, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.TIMESTAMP });
        result.setFunctionDescriptor(descriptor);
        result.setType(DataTypeManager.DefaultDataClasses.TIMESTAMP);
        function = result;
        break;
      }
      case 2: //rewrite nullif(a, b) => case when (a = b) then null else a
        List when = Arrays.asList(new Criteria[] {new CompareCriteria(function.getArg(0), CompareCriteria.EQ, function.getArg(1))});
        Constant nullConstant = new Constant(null, function.getType());
        List then = Arrays.asList(new Expression[] {nullConstant});
        SearchedCaseExpression caseExpr = new SearchedCaseExpression(when, then);
        caseExpr.setElseExpression(function.getArg(0));
        caseExpr.setType(function.getType());
        return rewriteExpressionDirect(caseExpr);
      }
      case 3: {
        Expression[] args = function.getArgs();
        if (args.length == 2) {
          Function result = new Function(SourceSystemFunctions.IFNULL,
              new Expression[] {function.getArg(0), function.getArg(1) });
          //resolve the function
          FunctionDescriptor descriptor =
            funcLibrary.findFunction(SourceSystemFunctions.IFNULL, new Class[] { function.getType(), function.getType()  });
          result.setFunctionDescriptor(descriptor);
          result.setType(function.getType());
          function = result;
        }
        break;
      }
      case 4: { //rewrite concat2 - CONCAT2(a, b) ==> CASE WHEN (a is NULL AND b is NULL) THEN NULL ELSE CONCAT( NVL(a, ''), NVL(b, '') )
        Expression[] args = function.getArgs();
        Function[] newArgs = new Function[args.length];

        for(int i=0; i<args.length; i++) {
          newArgs[i] = new Function(SourceSystemFunctions.IFNULL, new Expression[] {args[i], new Constant("")}); //$NON-NLS-1$
          newArgs[i].setType(args[i].getType());
          Assertion.assertTrue(args[i].getType() == DataTypeManager.DefaultDataClasses.STRING);
              FunctionDescriptor descriptor =
                funcLibrary.findFunction(SourceSystemFunctions.IFNULL, new Class[] { args[i].getType(), DataTypeManager.DefaultDataClasses.STRING });
              newArgs[i].setFunctionDescriptor(descriptor);
        }
       
        Function concat = new Function(SourceSystemFunctions.CONCAT, newArgs);
        concat.setType(DataTypeManager.DefaultDataClasses.STRING);
        FunctionDescriptor descriptor =
          funcLibrary.findFunction(SourceSystemFunctions.CONCAT, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
        concat.setFunctionDescriptor(descriptor);
       
        List when = Arrays.asList(new Criteria[] {new CompoundCriteria(CompoundCriteria.AND, new IsNullCriteria(args[0]), new IsNullCriteria(args[1]))});
        Constant nullConstant = new Constant(null, DataTypeManager.DefaultDataClasses.STRING);
        List then = Arrays.asList(new Expression[] {nullConstant});
        SearchedCaseExpression caseExpr = new SearchedCaseExpression(when, then);
View Full Code Here

    LoopStatement ls = new LoopStatement(b, query, "X"); //$NON-NLS-1$
    parent.addStatement(ls);
    AssignmentStatement as = new AssignmentStatement();
    ElementSymbol rowsUpdate = new ElementSymbol(ProcedureReservedWords.VARIABLES+ElementSymbol.SEPARATOR+ProcedureReservedWords.ROWS_UPDATED);
    as.setVariable(rowsUpdate);
    as.setExpression(new Function("+", new Expression[] {rowsUpdate, new Constant(1)})); //$NON-NLS-1$
    b.addStatement(as);
    cupc.setBlock(parent);
    cupc.setVirtualGroup(group);
    QueryResolver.resolveCommand(cupc, metadata);
    return rewrite(cupc, metadata, context);
View Full Code Here

  public static final Constant sampleConstant() {
    return new Constant(new Integer(5));
  }
 
  public static final Function sampleFunction() {
    return new Function("+", new Expression[] { sampleElement(), sampleConstant() }); //$NON-NLS-1$
  }
View Full Code Here

                case CompareCriteria.GE:    criteria.setOperator(CompareCriteria.LE);   break;
            }
            rightConstant = true;
    }
       
      Function f = null;
      while (rightConstant && f != criteria.getLeftExpression() && criteria.getLeftExpression() instanceof Function) {
            f = (Function)criteria.getLeftExpression();
          Criteria result = simplifyWithInverse(criteria);
          if (!(result instanceof CompareCriteria)) {
            return result;
View Full Code Here

    }
   
    private Criteria simplifyWithInverse(CompareCriteria criteria) throws TeiidComponentException, TeiidProcessingException{
        Expression leftExpr = criteria.getLeftExpression();
       
        Function leftFunction = (Function) leftExpr;
        if(isSimpleMathematicalFunction(leftFunction)) {
            return simplifyMathematicalCriteria(criteria);
        }  
        if (FunctionLibrary.isConvert(leftFunction)) {
          return simplifyConvertFunction(criteria);
View Full Code Here

        Expression leftExpr = criteria.getLeftExpression();
        Expression rightExpr = criteria.getRightExpression();

        // Identify all the pieces of this criteria
        Function function = (Function) leftExpr;
        String funcName = function.getName();
        Expression[] args = function.getArgs();
        Constant const1 = null;
        Expression expr = null;
        if(args[1] instanceof Constant) {
            const1 = (Constant) args[1];
            expr = args[0];
        } else {
            if(funcName.equals("+") || funcName.equals("*")) { //$NON-NLS-1$ //$NON-NLS-2$
                const1 = (Constant) args[0];
                expr = args[1];
            } else {
                // If we have "5 - x = 10" or "5 / x = 10", abort!
                return criteria;
            }
        }
        int operator = criteria.getOperator();

        // Determine opposite function
        String oppFunc = null;
        switch(funcName.charAt(0)) {
            case '+':   oppFunc = "-"break; //$NON-NLS-1$
            case '-':   oppFunc = "+"break; //$NON-NLS-1$
            case '*':   oppFunc = "/"break; //$NON-NLS-1$
            case '/':   oppFunc = "*"break; //$NON-NLS-1$
        }

        // Create a function of the two constants and evaluate it
        Expression combinedConst = null;
        FunctionLibrary funcLib = this.metadata.getFunctionLibrary();
        FunctionDescriptor descriptor = funcLib.findFunction(oppFunc, new Class[] { rightExpr.getType(), const1.getType() });
        if (descriptor == null){
            //See defect 9380 - this can be caused by const2 being a null Constant, for example (? + 1) < null
            return criteria;
        }

       
        if (rightExpr instanceof Constant) {
            Constant const2 = (Constant)rightExpr;
            try {
                Object result = descriptor.invokeFunction(new Object[] { const2.getValue(), const1.getValue() } );
                combinedConst = new Constant(result, descriptor.getReturnType());
            } catch(FunctionExecutionException e) {
              throw new QueryValidatorException(e, "ERR.015.009.0003", QueryPlugin.Util.getString("ERR.015.009.0003", e.getMessage())); //$NON-NLS-1$ //$NON-NLS-2$
          }
        } else {
            Function conversion = new Function(descriptor.getName(), new Expression[] { rightExpr, const1 });
            conversion.setType(leftExpr.getType());
            conversion.setFunctionDescriptor(descriptor);
            combinedConst = conversion;
           
        }

        // Flip operator if necessary
View Full Code Here

     * @return same Criteria instance (possibly optimized)
     * @throws QueryValidatorException
     * @since 4.2
     */
    private Criteria simplifyConvertFunction(CompareCriteria crit) {
        Function leftFunction = (Function) crit.getLeftExpression();
        Expression leftExpr = leftFunction.getArgs()[0];
       
        if(!(crit.getRightExpression() instanceof Constant)
            //TODO: this can be relaxed for order preserving operations
            || !(crit.getOperator() == CompareCriteria.EQ || crit.getOperator() == CompareCriteria.NE)) {
          return crit;
View Full Code Here

     * @return same Criteria instance (possibly optimized)
     * @throws QueryValidatorException
     * @since 4.2
     */
  private Criteria simplifyConvertFunction(SetCriteria crit) throws TeiidComponentException, TeiidProcessingException{
        Function leftFunction = (Function) crit.getExpression();
        Expression leftExpr = leftFunction.getArgs()[0];
        String leftExprTypeName = DataTypeManager.getDataTypeName(leftExpr.getType());
       
        Iterator i = crit.getValues().iterator();
        Collection newValues = new ArrayList(crit.getNumberOfValues());
       
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.