Package org.teiid.query.function

Examples of org.teiid.query.function.FunctionLibrary


        caps.setCapabilitySupport(Capability.CRITERIA_COMPARE_ORDERED, true);
        caps.setCapabilitySupport(Capability.CRITERIA_NOT, true);
        caps.setFunctionSupport("xyz", true);         //$NON-NLS-1$
        capFinder.addCapabilities("pm1", caps); //$NON-NLS-1$

        FunctionLibrary funcLibrary = new FunctionLibrary(FakeMetadataFactory.SFM.getSystemFunctions(), new FunctionTree("foo", new FakeFunctionMetadataSource()));
        FakeMetadataFacade metadata = new FakeMetadataFacade(FakeMetadataFactory.example1Cached().getStore(), funcLibrary);
       
        
        ProcessorPlan plan = helpPlan(
            "SELECT e1 FROM pm1.g1 WHERE xyz() > 0"//$NON-NLS-1$
View Full Code Here


      //explicitly denied
        helpTest(exampleAuthSvc2(), "create local temporary table x (y string)", FakeMetadataFactory.example1Cached(), new String[] {"x"}, FakeMetadataFactory.example1VDB()); //$NON-NLS-1$
    }
   
    @Test public void testFunction() throws Exception {
      FunctionLibrary funcLibrary = new FunctionLibrary(FakeMetadataFactory.SFM.getSystemFunctions(), new FunctionTree("foo", new FakeFunctionMetadataSource()));
        FakeMetadataFacade metadata = new FakeMetadataFacade(FakeMetadataFactory.example1Cached().getStore(), funcLibrary);
      helpTest(exampleAuthSvc1(), "SELECT e1 FROM pm1.g1 where xyz() > 0", metadata, new String[] {}, FakeMetadataFactory.example1VDB()); //$NON-NLS-1$
        helpTest(exampleAuthSvc2(), "SELECT e1, curdate() FROM pm1.g2 where xyz() > 0", metadata, new String[] {"xyz()"}, FakeMetadataFactory.example1VDB()); //$NON-NLS-1$
    }
View Full Code Here

  public void setUDF(final Collection<FunctionMethod> methods) {
    this.metadata = new BasicQueryMetadataWrapper(this.metadata) {
      @Override
      public FunctionLibrary getFunctionLibrary() {
        SystemFunctionManager sfm = new SystemFunctionManager();
        return new FunctionLibrary(sfm.getSystemFunctions(), new FunctionTree("foo", new UDFSource(methods)))//$NON-NLS-1$
      }
    };
  }
View Full Code Here

    @Test public void testRewriteCritSubqueryFalse2() {
        helpTestRewriteCriteria("pm1.g1.e1 < ALL (select 'a' from pm1.g1 where 1=0)", "pm1.g1.e1 IS NOT NULL"); //$NON-NLS-1$ //$NON-NLS-2$
    }
   
  @Test public void testUDFParse() throws Exception {    
        FunctionLibrary funcLibrary = new FunctionLibrary(FakeMetadataFactory.SFM.getSystemFunctions(), new FunctionTree("foo", new FakeFunctionMetadataSource()));
        FakeMetadataFacade metadata = new FakeMetadataFacade(FakeMetadataFactory.example1Cached().getStore(), funcLibrary);
    String sql = "parsedate_(pm1.g1.e1) = {d'2001-01-01'}";
        helpTestRewriteCriteria(sql, parseCriteria(sql, metadata), metadata);     
  }
View Full Code Here

            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;
        }

View Full Code Here

        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 {
View Full Code Here

   
  private Expression rewriteFunction(Function function) throws TeiidComponentException, TeiidProcessingException{
    //rewrite alias functions
    String functionLowerName = function.getName().toLowerCase();
    String actualName =ALIASED_FUNCTIONS.get(functionLowerName);
    FunctionLibrary funcLibrary = this.metadata.getFunctionLibrary();

    if (actualName != null) {
      function.setName(actualName);
      Expression[] args = function.getArgs();
        Class<?>[] types = new Class[args.length];
        for(int i=0; i<args.length; i++) {
            types[i] = args[i].getType();
        }
      FunctionDescriptor descriptor = funcLibrary.findFunction(actualName, types);
      function.setFunctionDescriptor(descriptor);
    }
   
    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);
        caseExpr.setElseExpression(concat);
        caseExpr.setType(DataTypeManager.DefaultDataClasses.STRING);
        return rewriteExpressionDirect(caseExpr);
      }
      case 5: {
        if (function.getType() != DataTypeManager.DefaultDataClasses.TIMESTAMP) {
          FunctionDescriptor descriptor =
            funcLibrary.findFunction(SourceSystemFunctions.TIMESTAMPADD, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.INTEGER, DataTypeManager.DefaultDataClasses.TIMESTAMP });
          function.setFunctionDescriptor(descriptor);
          Class<?> type = function.getType();
          function.setType(DataTypeManager.DefaultDataClasses.TIMESTAMP);
          function.getArgs()[2] = ResolverUtil.getConversion(function.getArg(2), DataTypeManager.getDataTypeName(type), DataTypeManager.DefaultDataTypes.TIMESTAMP, false, funcLibrary);
          function = ResolverUtil.getConversion(function, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.getDataTypeName(type), false, funcLibrary);
        }
        break;
      }
      case 6:
      case 7: {
        FunctionDescriptor descriptor =
          funcLibrary.findFunction(SourceSystemFunctions.PARSETIMESTAMP, new Class[] { DataTypeManager.DefaultDataClasses.STRING, DataTypeManager.DefaultDataClasses.STRING });
        function.setName(SourceSystemFunctions.PARSETIMESTAMP);
        function.setFunctionDescriptor(descriptor);
        Class<?> type = function.getType();
        function.setType(DataTypeManager.DefaultDataClasses.TIMESTAMP);
        function = ResolverUtil.getConversion(function, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.getDataTypeName(type), false, funcLibrary);
        break;
      }
      case 8:
      case 9: {
        FunctionDescriptor descriptor =
          funcLibrary.findFunction(SourceSystemFunctions.FORMATTIMESTAMP, new Class[] { DataTypeManager.DefaultDataClasses.TIMESTAMP, DataTypeManager.DefaultDataClasses.STRING });
        function.setName(SourceSystemFunctions.FORMATTIMESTAMP);
        function.setFunctionDescriptor(descriptor);
        function.getArgs()[0] = ResolverUtil.getConversion(function.getArg(0), DataTypeManager.getDataTypeName(function.getArg(0).getType()), DataTypeManager.DefaultDataTypes.TIMESTAMP, false, funcLibrary);
        break;
      }
View Full Code Here

        // Expected right expression
        Class srcType = DataTypeManager.DefaultDataClasses.STRING;
        String tgtTypeName = DataTypeManager.DefaultDataTypes.DATE;
        Expression expression = new Constant("2003-02-27"); //$NON-NLS-1$
       
    FunctionLibrary library = FakeMetadataFactory.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();
View Full Code Here

    // Expected right expression
    Class srcType = DataTypeManager.DefaultDataClasses.STRING;
    String tgtTypeName = DataTypeManager.DefaultDataTypes.DATE;
    Expression expression = new Constant("2003-02-27"); //$NON-NLS-1$
       
    FunctionLibrary library = FakeMetadataFactory.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();
View Full Code Here

    }
   
  @Test public void testNamespacedFunction() throws Exception {    
    String sql = "SELECT namespace.func('e1')  FROM vm1.g1 "; //$NON-NLS-1$

        FunctionLibrary funcLibrary = new FunctionLibrary(FakeMetadataFactory.SFM.getSystemFunctions(), new FunctionTree("foo", new FakeFunctionMetadataSource()));
        FakeMetadataFacade metadata = new FakeMetadataFacade(FakeMetadataFactory.example1Cached().getStore(), funcLibrary);
   
    Query command = (Query) helpParse(sql);
    QueryResolver.resolveCommand(command, metadata);
   
View Full Code Here

TOP

Related Classes of org.teiid.query.function.FunctionLibrary

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.