Search in sources :

Example 1 with TemplateFunctionModifier

use of org.teiid.translator.jdbc.TemplateFunctionModifier in project teiid by teiid.

the class SAPIQExecutionFactory method start.

public void start() throws TranslatorException {
    super.start();
    registerFunctionModifier(SourceSystemFunctions.CONCAT, new ConcatFunctionModifier(getLanguageFactory()) {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$
            function.setName("||");
            return super.translate(function);
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CONCAT2, new AliasModifier("STRING"));
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new EscapeSyntaxModifier());
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new TemplateFunctionModifier("DATEPART(dy,", 0, ")"));
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.WEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new AddDiffModifier(true, this.getLanguageFactory()).supportsQuarter(true));
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new AddDiffModifier(false, this.getLanguageFactory()).supportsQuarter(true));
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier(SourceSystemFunctions.COALESCE));
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Expression> params = function.getParameters();
            Expression param1 = params.get(0);
            Expression param2 = params.set(1, param1);
            params.set(0, param2);
            return null;
        }
    });
    // add in type conversion
    ConvertModifier convertModifier = new ConvertModifier();
    convertModifier.setBooleanNullable(booleanNullable());
    convertModifier.addNumericBooleanConversions();
    // boolean isn't treated as bit, since it doesn't support null
    // byte is treated as smallint, since tinyint is unsigned
    // $NON-NLS-1$
    convertModifier.addTypeMapping("smallint", FunctionModifier.BYTE, FunctionModifier.SHORT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("bigint", FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("int", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("double", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("real", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38, 0)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38, 19)", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("varchar(4000)", FunctionModifier.STRING);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("varbinary", FunctionModifier.VARBINARY);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("date", FunctionModifier.DATE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("time", FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP);
    convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertTimeToString(function);
        }
    });
    convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertDateToString(function);
        }
    });
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return convertTimestampToString(function);
        }
    });
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
}
Also used : Function(org.teiid.language.Function) TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) Expression(org.teiid.language.Expression) TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) EscapeSyntaxModifier(org.teiid.translator.jdbc.EscapeSyntaxModifier) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) AddDiffModifier(org.teiid.translator.jdbc.hsql.AddDiffModifier) ConvertModifier(org.teiid.translator.jdbc.ConvertModifier)

Example 2 with TemplateFunctionModifier

use of org.teiid.translator.jdbc.TemplateFunctionModifier in project teiid by teiid.

the class SQLServerExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    registerFunctionModifier(SourceSystemFunctions.WEEK, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("DATEPART(ISO_WEEK, ", function.getParameters().get(0), ")");
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new AliasModifier("CHARINDEX"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.MD5, new TemplateFunctionModifier("HASHBYTES('MD5', ", 0, ")"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.SHA1, new TemplateFunctionModifier("HASHBYTES('SHA1', ", 0, ")"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.SHA2_256, new TemplateFunctionModifier("HASHBYTES('SHA2_256', ", 0, ")"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.SHA2_512, new TemplateFunctionModifier("HASHBYTES('SHA2_512', ", 0, ")"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new UpperLowerFunctionModifier("upper"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new UpperLowerFunctionModifier("lower"));
}
Also used : TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with TemplateFunctionModifier

use of org.teiid.translator.jdbc.TemplateFunctionModifier in project teiid by teiid.

the class HanaExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("lower"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CEILING, new AliasModifier("ceil"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("lower"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CHAR, new AliasModifier("to_nvarchar"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("upper"));
    registerFunctionModifier(SourceSystemFunctions.LOG, new Log10FunctionModifier(getLanguageFactory()));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CEILING, new AliasModifier("ceil"));
    registerFunctionModifier(SourceSystemFunctions.LOG10, new Log10FunctionModifier(getLanguageFactory()));
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new // $NON-NLS-1$
    LocateFunctionModifier(// $NON-NLS-1$
    getLanguageFactory(), // $NON-NLS-1$
    "locate", // $NON-NLS-1$
    true) {

        @Override
        public void modify(Function function) {
            super.modify(function);
            // If a start index was passed in, we convert to a substring on the search string since
            // HANA does not support the start index parameter in LOCATE().
            List<Expression> args = function.getParameters();
            if (args.size() > 2) {
                List<Expression> substringArgs = new ArrayList<Expression>();
                substringArgs.add(args.get(0));
                substringArgs.add(args.get(2));
                args.set(0, getLanguageFactory().createFunction(SourceSystemFunctions.SUBSTRING, substringArgs, null));
                args.remove(2);
            }
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CURDATE, new AliasModifier("current_date"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CURTIME, new AliasModifier("current_time"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.WEEK, new TemplateFunctionModifier("cast(substring(isoweek(", 0, "), 7, 2) as integer)"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new TemplateFunctionModifier("(MOD((WEEKDAY(", 0, ")+1),7)+1)"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.DAYNAME, new TemplateFunctionModifier("initcap(lower(dayname(", 0, ")))"));
    // $NON-NLS-1$ //$NON-NLS-2$
    registerFunctionModifier(SourceSystemFunctions.QUARTER, new TemplateFunctionModifier("((month(", 0, ")+2)/3)"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.NOW, new AliasModifier("current_timestamp"));
    // spatial functions
    registerFunctionModifier(SourceSystemFunctions.ST_ASEWKT, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_ASBINARY, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_ASGEOJSON, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_ASTEXT, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_CONTAINS, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_CROSSES, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_DISTANCE, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_DISJOINT, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_EQUALS, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_INTERSECTS, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_SRID, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_GEOMFROMTEXT, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_OVERLAPS, new HanaSpatialFunctionModifier());
    registerFunctionModifier(SourceSystemFunctions.ST_TOUCHES, new HanaSpatialFunctionModifier());
    // ////////////////////////////////////////////////////////
    // TYPE CONVERION MODIFIERS////////////////////////////////
    // ////////////////////////////////////////////////////////
    ConvertModifier convertModifier = new ConvertModifier();
    // $NON-NLS-1$
    convertModifier.addTypeMapping("boolean", FunctionModifier.BOOLEAN);
    convertModifier.addTypeMapping(TINYINT_TYPE, FunctionModifier.BYTE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("smallint", FunctionModifier.SHORT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("integer", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("bigint", FunctionModifier.LONG, FunctionModifier.BIGINTEGER);
    // convertModifier.addTypeMapping("smalldecimal", FunctionModifier.FLOAT); //$NON-NLS-1$
    // $NON-NLS-1$
    convertModifier.addTypeMapping("decimal", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("float", FunctionModifier.FLOAT);
    // convertModifier.addTypeMapping("real", FunctionModifier.FLOAT); //$NON-NLS-1$
    // $NON-NLS-1$
    convertModifier.addTypeMapping("date", FunctionModifier.DATE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("double", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("time", FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP);
    // convertModifier.addTypeMapping("seconddate", FunctionModifier.TIMESTAMP); //$NON-NLS-1$
    // $NON-NLS-1$
    convertModifier.addTypeMapping("nvarchar", FunctionModifier.STRING);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("nvarchar(1)", FunctionModifier.CHAR);
    // convertModifier.addTypeMapping("alphanum", FunctionModifier.STRING); //$NON-NLS-1$
    // $NON-NLS-1$
    convertModifier.addTypeMapping("varbinary", FunctionModifier.VARBINARY);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("blob", FunctionModifier.BLOB);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("nclob", FunctionModifier.CLOB);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("text", FunctionModifier.XML);
    // convertModifier.addTypeMapping("shorttext", FunctionModifier.STRING); //$NON-NLS-1$
    // $NON-NLS-1$
    convertModifier.addTypeMapping("st_geometry", FunctionModifier.GEOMETRY);
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.DATE, new ConvertModifier.FormatModifier("to_date", DATE_FORMAT));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIME, new ConvertModifier.FormatModifier("to_time", TIME_FORMAT));
    // $NON-NLS-1$
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.TIMESTAMP, new ConvertModifier.FormatModifier("to_timestamp", DATETIME_FORMAT));
    convertModifier.setWideningNumericImplicit(true);
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            Expression trueValue = function.getParameters().get(0);
            Expression falseValue = trueValue;
            falseValue = new IsNull(falseValue, true);
            if (!(trueValue instanceof Predicate)) {
                trueValue = new Comparison(trueValue, new Literal(Boolean.TRUE, TypeFacility.RUNTIME_TYPES.BOOLEAN), Comparison.Operator.EQ);
            }
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("CASE WHEN ", trueValue, " THEN 'true' WHEN ", falseValue, " THEN 'false' END");
        }
    });
    convertModifier.addSourceConversion(new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            ((Literal) function.getParameters().get(1)).setValue(TINYINT_TYPE);
            return null;
        }
    }, FunctionModifier.BOOLEAN);
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
    /*
    	 * Date/Time Pushdown functions
    	 */
    addPushDownFunction(HANA, ADD_DAYS, DATE, DATE, INTEGER);
    addPushDownFunction(HANA, ADD_MONTHS, DATE, DATE, INTEGER);
    addPushDownFunction(HANA, ADD_SECONDS, TIMESTAMP, TIMESTAMP, INTEGER);
    addPushDownFunction(HANA, ADD_WORKDAYS, DATE, DATE, INTEGER);
    addPushDownFunction(HANA, ADD_YEARS, DATE, DATE, INTEGER);
    addPushDownFunction(HANA, CURRENT_UTCDATE, DATE);
    addPushDownFunction(HANA, CURRENT_UTCTIME, TIME);
    addPushDownFunction(HANA, CURRENT_UTCTIMESTAMP, TIMESTAMP);
    addPushDownFunction(HANA, DAYS_BETWEEN, INTEGER, DATE, DATE);
    addPushDownFunction(HANA, ISOWEEK, STRING, DATE);
    addPushDownFunction(HANA, LAST_DAY, DATE, DATE);
    addPushDownFunction(HANA, LOCALTOUTC, TIMESTAMP, TIMESTAMP, STRING);
    addPushDownFunction(HANA, NANO100_BETWEEN, INTEGER, DATE, DATE);
    addPushDownFunction(HANA, NEXT_DAY, DATE, DATE);
    addPushDownFunction(HANA, SECONDS_BETWEEN, INTEGER, DATE, DATE);
    addPushDownFunction(HANA, WEEKDAY, INTEGER, DATE);
    addPushDownFunction(HANA, WORKDAYS_BETWEEN, DATE, INTEGER);
    /*
    	 * Numeric Pushdown functions
    	 */
    addPushDownFunction(HANA, COSH, INTEGER, FLOAT);
    addPushDownFunction(HANA, BITSET, STRING, VARBINARY, INTEGER, INTEGER);
    addPushDownFunction(HANA, BITUNSET, STRING, VARBINARY, INTEGER, INTEGER);
    addPushDownFunction(HANA, COSH, FLOAT, FLOAT);
    addPushDownFunction(HANA, HEXTOBIN, BLOB, STRING);
    addPushDownFunction(HANA, RAND, BLOB);
    addPushDownFunction(HANA, SINH, FLOAT, FLOAT);
    addPushDownFunction(HANA, TANH, FLOAT, FLOAT);
    addPushDownFunction(HANA, UMINUS, INTEGER, INTEGER);
}
Also used : ConvertModifier(org.teiid.translator.jdbc.ConvertModifier) TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) TemplateFunctionModifier(org.teiid.translator.jdbc.TemplateFunctionModifier) LocateFunctionModifier(org.teiid.translator.jdbc.LocateFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 AliasModifier (org.teiid.translator.jdbc.AliasModifier)3 FunctionModifier (org.teiid.translator.jdbc.FunctionModifier)3 TemplateFunctionModifier (org.teiid.translator.jdbc.TemplateFunctionModifier)3 ConvertModifier (org.teiid.translator.jdbc.ConvertModifier)2 LinkedList (java.util.LinkedList)1 Expression (org.teiid.language.Expression)1 Function (org.teiid.language.Function)1 EscapeSyntaxModifier (org.teiid.translator.jdbc.EscapeSyntaxModifier)1 LocateFunctionModifier (org.teiid.translator.jdbc.LocateFunctionModifier)1 AddDiffModifier (org.teiid.translator.jdbc.hsql.AddDiffModifier)1 ConcatFunctionModifier (org.teiid.translator.jdbc.oracle.ConcatFunctionModifier)1