Package org.teiid.query.function

Examples of org.teiid.query.function.FunctionDescriptor


      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


        e1.setType(String.class);       
        ElementSymbol e2 = new ElementSymbol("e2"); //$NON-NLS-1$
        e2.setType(String.class);
       
        Function func = new Function("concat", new Expression[] { e1, e2 }); //$NON-NLS-1$
        FunctionDescriptor desc = FakeMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("concat", new Class[] { String.class, String.class } ); //$NON-NLS-1$
        func.setFunctionDescriptor(desc);

        SingleElementSymbol[] elements = new SingleElementSymbol[] {
            e1, e2
        };
View Full Code Here

        e1.setType(String.class);       
        ElementSymbol e2 = new ElementSymbol("e2"); //$NON-NLS-1$
        e2.setType(String.class);
       
        Function func = new Function("concat", new Expression[] { e2, e1 }); //$NON-NLS-1$
        FunctionDescriptor desc = FakeMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("concat", new Class[] { String.class, String.class } ); //$NON-NLS-1$
        func.setFunctionDescriptor(desc);

        SingleElementSymbol[] elements = new SingleElementSymbol[] {
            e1, e2
        };
View Full Code Here

        e1.setType(String.class);       
        ElementSymbol e2 = new ElementSymbol("e2"); //$NON-NLS-1$
        e1.setType(Integer.class);       
       
        Function func = new Function("lookup", new Expression[] { new Constant("pm1.g1"), new Constant("e2"), new Constant("e1"), e1 }); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        FunctionDescriptor desc = FakeMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("lookup", new Class[] { String.class, String.class, String.class, String.class } ); //$NON-NLS-1$
        func.setFunctionDescriptor(desc);

        SingleElementSymbol[] elements = new SingleElementSymbol[] {
            e1, e2
        };
View Full Code Here

        }
    }

    @Test public void testUser() throws Exception {
        Function func = new Function("user", new Expression[] {}); //$NON-NLS-1$
        FunctionDescriptor desc = FakeMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("user", new Class[] {} );         //$NON-NLS-1$
        func.setFunctionDescriptor(desc);

        FakeDataManager dataMgr = new FakeDataManager();
        CommandContext context = new CommandContext(new Long(1), null, null, null, 0);
        context.setUserName("logon")//$NON-NLS-1$
View Full Code Here

     * these files.
     * @throws Exception
     */
    @Test public void testEnv() throws Exception {
        Function func = new Function("env", new Expression[] {}); //$NON-NLS-1$
        FunctionDescriptor desc = FakeMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("env", new Class[] {String.class} );         //$NON-NLS-1$
        func.setFunctionDescriptor(desc);
       
        FakeDataManager dataMgr = new FakeDataManager();
       
        Properties props = new Properties();
View Full Code Here

        if(property == null) {
            parameterSignature = new Class[] {};
        } else {
            parameterSignature = new Class[] { String.class };
        }       
        FunctionDescriptor desc = FakeMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("commandpayload", parameterSignature );         //$NON-NLS-1$
        func.setFunctionDescriptor(desc);
       
        FakeDataManager dataMgr = new FakeDataManager();      
        CommandContext context = new CommandContext(new Long(-1), null, "user", payload, "vdb", 1, null, false); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
View Full Code Here

        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();
   
    // Expected criteria
View Full Code Here

    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();
   
    // Expected criteria
View Full Code Here

    @Test public void testUser() {
        //String sql = "select intkey from SmallA where user() = 'bqt2'";

        // Expected left expression
        FunctionLibrary library = FakeMetadataFactory.SFM.getSystemFunctionLibrary();                         
        FunctionDescriptor fd = library.findFunction(FunctionLibrary.USER, new Class[] { });
        Function user = new Function(fd.getName(), new Expression[] {});
        user.setFunctionDescriptor(fd);

        // Expected criteria
        CompareCriteria expected = new CompareCriteria();
        // Expected right expression
View Full Code Here

TOP

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

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.