Search in sources :

Example 6 with FunctionModifier

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

the class PostgreSQLExecutionFactory method start.

public void start() throws TranslatorException {
    // TODO: all of the functions (except for convert) can be handled through just the escape syntax
    super.start();
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOG, new AliasModifier("ln"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOG10, new AliasModifier("log"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITAND, new AliasModifier("&"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITNOT, new AliasModifier("~"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITOR, new AliasModifier("|"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.BITXOR, new AliasModifier("#"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CHAR, new AliasModifier("chr"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CONCAT, new AliasModifier("||"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("lower"));
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Object> parts = new ArrayList<Object>();
            // $NON-NLS-1$
            parts.add("substring(");
            parts.add(function.getParameters().get(0));
            // $NON-NLS-1$
            parts.add(" from ");
            Expression index = function.getParameters().get(1);
            if (!(index instanceof Literal)) {
                // $NON-NLS-1$
                parts.add("case sign(");
                parts.add(index);
                // $NON-NLS-1$
                parts.add(") when -1 then length(");
                parts.add(function.getParameters().get(0));
                // $NON-NLS-1$
                parts.add(") + 1 + ");
                parts.add(index);
                // $NON-NLS-1$
                parts.add(" when 0 then 1 else ");
                parts.add(index);
                // $NON-NLS-1$
                parts.add(" end");
            } else {
                parts.add(index);
            }
            if (function.getParameters().size() > 2) {
                // $NON-NLS-1$
                parts.add(" for ");
                parts.add(function.getParameters().get(2));
            }
            // $NON-NLS-1$
            parts.add(")");
            return parts;
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("upper"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYNAME, new MonthOrDayNameFunctionModifier(getLanguageFactory(), "Day"));
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.HOUR, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.MINUTE, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.MONTH, new ExtractFunctionModifier(INTEGER_TYPE));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MONTHNAME, new MonthOrDayNameFunctionModifier(getLanguageFactory(), "Month"));
    registerFunctionModifier(SourceSystemFunctions.QUARTER, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.SECOND, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.WEEK, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.YEAR, new ExtractFunctionModifier(INTEGER_TYPE));
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new LocateFunctionModifier(getLanguageFactory()));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("coalesce"));
    registerFunctionModifier(SourceSystemFunctions.PARSETIMESTAMP, parseModifier);
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.FORMATTIMESTAMP, new PostgreSQLFormatFunctionModifier("TO_CHAR(", false));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MOD, new ModFunctionModifier("%", getLanguageFactory(), Arrays.asList(TypeFacility.RUNTIME_TYPES.BIG_INTEGER, TypeFacility.RUNTIME_TYPES.BIG_DECIMAL)));
    // specific to 8.2 client or later
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new EscapeSyntaxModifier());
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.RAND, new AliasModifier("random"));
    registerFunctionModifier(SourceSystemFunctions.ARRAY_GET, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return Arrays.asList(function.getParameters().get(0), '[', function.getParameters().get(1), ']');
        }
    });
    registerFunctionModifier(SourceSystemFunctions.ARRAY_LENGTH, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            if (function.getParameters().size() == 1) {
                function.getParameters().add(new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER));
            }
            return null;
        }
    });
    registerFunctionModifier(SourceSystemFunctions.ROUND, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            if (function.getParameters().size() > 1) {
                Expression ex = function.getParameters().get(0);
                if (ex.getType() == TypeFacility.RUNTIME_TYPES.DOUBLE || ex.getType() == TypeFacility.RUNTIME_TYPES.FLOAT) {
                    if (function.getParameters().get(1) instanceof Literal && Integer.valueOf(0).equals(((Literal) function.getParameters().get(1)).getValue())) {
                        function.getParameters().remove(1);
                    } else {
                        // $NON-NLS-1$
                        function.getParameters().set(0, new Function(SourceSystemFunctions.CONVERT, Arrays.asList(ex, new Literal("bigdecimal", TypeFacility.RUNTIME_TYPES.STRING)), TypeFacility.RUNTIME_TYPES.BIG_DECIMAL));
                    }
                }
            }
            return null;
        }
    });
    // add in type conversion
    ConvertModifier convertModifier = new ConvertModifier();
    // $NON-NLS-1$
    convertModifier.addTypeMapping("boolean", FunctionModifier.BOOLEAN);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("smallint", FunctionModifier.BYTE, FunctionModifier.SHORT);
    convertModifier.addTypeMapping(INTEGER_TYPE, FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("bigint", FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("real", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("float8", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(38)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("decimal", 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("date", FunctionModifier.DATE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("time", FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP);
    convertModifier.addConvert(FunctionModifier.BIGDECIMAL, FunctionModifier.BOOLEAN, new NonIntegralNumberToBoolean());
    convertModifier.addConvert(FunctionModifier.FLOAT, FunctionModifier.BOOLEAN, new NonIntegralNumberToBoolean());
    convertModifier.addConvert(FunctionModifier.BIGDECIMAL, FunctionModifier.BOOLEAN, new NonIntegralNumberToBoolean());
    convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.TIMESTAMP, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$
            return Arrays.asList(function.getParameters().get(0), " + TIMESTAMP '1970-01-01'");
        }
    });
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(date_trunc('second', ", function.getParameters().get(0), ") AS time)");
        }
    });
    // $NON-NLS-1$ //$NON-NLS-2$
    convertModifier.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new ConvertModifier.FormatModifier("to_char", "YYYY-MM-DD"));
    // $NON-NLS-1$ //$NON-NLS-2$
    convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new ConvertModifier.FormatModifier("to_char", "HH24:MI:SS"));
    // $NON-NLS-1$ //$NON-NLS-2$
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new ConvertModifier.FormatModifier("to_char", "YYYY-MM-DD HH24:MI:SS.US"));
    convertModifier.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            Expression stringValue = function.getParameters().get(0);
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("CASE WHEN ", stringValue, " THEN 'true' WHEN not(", stringValue, ") THEN 'false' END");
        }
    });
    convertModifier.addSourceConversion(new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            ((Literal) function.getParameters().get(1)).setValue(INTEGER_TYPE);
            return null;
        }
    }, FunctionModifier.BOOLEAN);
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
    // standard function form of several predicates
    // $NON-NLS-1$ //$NON-NLS-2$
    addPushDownFunction(POSTGRESQL, "ilike", BOOLEAN, STRING, STRING).setProperty(SQLStringVisitor.TEIID_NATIVE_QUERY, "($1 ilike $2)");
    // $NON-NLS-1$ //$NON-NLS-2$
    addPushDownFunction(POSTGRESQL, "rlike", BOOLEAN, STRING, STRING).setProperty(SQLStringVisitor.TEIID_NATIVE_QUERY, "($1 ~ $2)");
    // $NON-NLS-1$ //$NON-NLS-2$
    addPushDownFunction(POSTGRESQL, "iregexp", BOOLEAN, STRING, STRING).setProperty(SQLStringVisitor.TEIID_NATIVE_QUERY, "($1 ~* $2)");
}
Also used : ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) ExtractFunctionModifier(org.teiid.translator.jdbc.ExtractFunctionModifier) EscapeSyntaxModifier(org.teiid.translator.jdbc.EscapeSyntaxModifier) ConvertModifier(org.teiid.translator.jdbc.ConvertModifier) MonthOrDayNameFunctionModifier(org.teiid.translator.jdbc.oracle.MonthOrDayNameFunctionModifier) MonthOrDayNameFunctionModifier(org.teiid.translator.jdbc.oracle.MonthOrDayNameFunctionModifier) LeftOrRightFunctionModifier(org.teiid.translator.jdbc.oracle.LeftOrRightFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) OracleFormatFunctionModifier(org.teiid.translator.jdbc.oracle.OracleFormatFunctionModifier) ExtractFunctionModifier(org.teiid.translator.jdbc.ExtractFunctionModifier) ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 7 with FunctionModifier

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

the class SybaseExecutionFactory method start.

public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MOD, new ModFunctionModifier("%", getLanguageFactory()));
    if (nullPlusNonNullIsNull()) {
        // $NON-NLS-1$
        registerFunctionModifier(SourceSystemFunctions.CONCAT, new AliasModifier("+"));
    } else {
        registerFunctionModifier(SourceSystemFunctions.CONCAT, new ConcatFunctionModifier(getLanguageFactory()) {

            @Override
            public List<?> translate(Function function) {
                // $NON-NLS-1$
                function.setName("+");
                return super.translate(function);
            }
        });
    }
    registerFunctionModifier(SourceSystemFunctions.LPAD, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Expression> params = function.getParameters();
            return // $NON-NLS-1$
            Arrays.asList(// $NON-NLS-1$
            "RIGHT(REPLICATE(", // $NON-NLS-1$ //$NON-NLS-2$
            params.size() > 2 ? params.get(2) : new Literal(" ", TypeFacility.RUNTIME_TYPES.STRING), // $NON-NLS-1$ //$NON-NLS-2$
            ", ", params.get(1), ") + ", params.get(0), ", ", params.get(1), // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            ")");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.RPAD, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Expression> params = function.getParameters();
            return // $NON-NLS-1$ //$NON-NLS-2$
            Arrays.asList(// $NON-NLS-1$ //$NON-NLS-2$
            "LEFT(", // $NON-NLS-1$ //$NON-NLS-2$
            params.get(0), // $NON-NLS-1$ //$NON-NLS-2$
            " + REPLICATE(", // $NON-NLS-1$ //$NON-NLS-2$
            params.size() > 2 ? params.get(2) : new Literal(" ", TypeFacility.RUNTIME_TYPES.STRING), // $NON-NLS-1$ //$NON-NLS-2$
            ", ", params.get(1), "), ", params.get(1), // $NON-NLS-1$ //$NON-NLS-2$
            ")");
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("lower"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("isnull"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("upper"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.REPEAT, new AliasModifier("replicate"));
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new SubstringFunctionModifier(getLanguageFactory()));
    registerFunctionModifier(SourceSystemFunctions.DAYNAME, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MONTHNAME, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.HOUR, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MINUTE, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.QUARTER, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.SECOND, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.WEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.LENGTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.ATAN2, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new EscapeSyntaxModifier() {

        @Override
        public List<?> translate(Function function) {
            if (!isFracSeconds(function)) {
                return super.translate(function);
            }
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("dateadd(millisecond, ", function.getParameters().get(1), "/1000000, ", function.getParameters().get(2), ")");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new EscapeSyntaxModifier() {

        @Override
        public List<?> translate(Function function) {
            if (!isFracSeconds(function)) {
                return super.translate(function);
            }
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Arrays.asList("datediff(millisecond, ", function.getParameters().get(1), ",", function.getParameters().get(2), ")*1000000");
        }
    });
    // add in type conversion
    convertModifier.setBooleanNullable(booleanNullable());
    // 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("int", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(19,0)", FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("real", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("double precision", FunctionModifier.DOUBLE);
    // $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);
    convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.DATE, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            List<Object> result = new ArrayList<Object>();
            // $NON-NLS-1$
            result.add("cast(");
            result.addAll(convertDateToString(function));
            // $NON-NLS-1$
            result.add(" AS datetime)");
            return result;
        }
    });
    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);
        }
    });
    convertModifier.addNumericBooleanConversions();
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.PARSETIMESTAMP, new SybaseFormatFunctionModifier("CONVERT(DATETIME, ", formatMap));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.FORMATTIMESTAMP, new SybaseFormatFunctionModifier("CONVERT(VARCHAR, ", formatMap));
}
Also used : ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) EscapeSyntaxModifier(org.teiid.translator.jdbc.EscapeSyntaxModifier) Function(org.teiid.language.Function) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) ParseFormatFunctionModifier(org.teiid.translator.jdbc.ParseFormatFunctionModifier) ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) Literal(org.teiid.language.Literal) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier)

Example 8 with FunctionModifier

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

the class SybaseExecutionFactory method handleTimeConversions.

private void handleTimeConversions() {
    if (!hasTimeType()) {
        // $NON-NLS-1$
        convertModifier.addTypeMapping("datetime", FunctionModifier.DATE, FunctionModifier.TIME, FunctionModifier.TIMESTAMP);
        convertModifier.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new FunctionModifier() {

            @Override
            public List<?> translate(Function function) {
                List<Object> result = new ArrayList<Object>();
                // $NON-NLS-1$
                result.add("cast(");
                boolean needsEnd = false;
                if (!nullPlusNonNullIsNull() && !ConcatFunctionModifier.isNotNull(function.getParameters().get(0))) {
                    // $NON-NLS-1$
                    result.add("CASE WHEN ");
                    result.add(function.getParameters().get(0));
                    // $NON-NLS-1$
                    result.add(" IS NOT NULL THEN ");
                    needsEnd = true;
                }
                // $NON-NLS-1$
                result.add("'1970-01-01 ' + ");
                result.addAll(convertTimeToString(function));
                if (needsEnd) {
                    // $NON-NLS-1$
                    result.add(" END");
                }
                // $NON-NLS-1$
                result.add(" AS datetime)");
                return result;
            }
        });
    } else {
        // $NON-NLS-1$
        convertModifier.addTypeMapping("datetime", FunctionModifier.DATE, FunctionModifier.TIMESTAMP);
        // $NON-NLS-1$
        convertModifier.addTypeMapping("time", FunctionModifier.TIME);
    }
}
Also used : Function(org.teiid.language.Function) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) ParseFormatFunctionModifier(org.teiid.translator.jdbc.ParseFormatFunctionModifier) ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Example 9 with FunctionModifier

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

the class TeradataExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    convert.addTypeMapping("byteint", FunctionModifier.BYTE, FunctionModifier.SHORT, FunctionModifier.BOOLEAN);
    // $NON-NLS-1$
    convert.addTypeMapping("double precision", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convert.addTypeMapping("numeric(18,0)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convert.addTypeMapping("char(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new CastModifier("TIME"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.DATE, new CastModifier("DATE"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.TIME, FunctionModifier.TIMESTAMP, new CastModifier("TIMESTAMP"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.DATE, FunctionModifier.TIMESTAMP, new CastModifier("TIMESTAMP"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.INTEGER, new CastModifier("integer"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.BIGDECIMAL, new CastModifier("decimal(37,5)"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.BIGINTEGER, new CastModifier("numeric(18,0)"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.FLOAT, new CastModifier("float"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.BOOLEAN, new CastModifier("byteint"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.LONG, new CastModifier("numeric(18,0)"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.SHORT, new CastModifier("smallint"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.DOUBLE, new CastModifier("double precision"));
    // $NON-NLS-1$
    convert.addConvert(FunctionModifier.STRING, FunctionModifier.BYTE, new CastModifier("byteint"));
    convert.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(cast(", function.getParameters().get(0), " AS FORMAT 'Y4-MM-DDBHH:MI:SSDS(6)') AS VARCHAR(26))");
        }
    });
    convert.addConvert(FunctionModifier.TIME, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(cast(", function.getParameters().get(0), " AS FORMAT 'HH:MI:SS') AS VARCHAR(9))");
        }
    });
    convert.addConvert(FunctionModifier.DATE, FunctionModifier.STRING, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(cast(", function.getParameters().get(0), " AS FORMAT 'YYYY-MM-DD') AS VARCHAR(11))");
        }
    });
    // $NON-NLS-1$
    convert.addTypeMapping("varchar(4000)", FunctionModifier.STRING);
    convert.addNumericBooleanConversions();
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convert);
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new AliasModifier("substr"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LOG, new AliasModifier("LN"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("LOWER"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("UPPER"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LENGTH, new AliasModifier("character_length"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CURDATE, new AliasModifier("CURRENT_DATE"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CURTIME, new AliasModifier("CURRENT_TIME"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.YEAR, new ExtractModifier("YEAR"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MONTH, new ExtractModifier("MONTH"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new ExtractModifier("DAY"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.HOUR, new ExtractModifier("HOUR"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MINUTE, new ExtractModifier("MINUTE"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.SECOND, new ExtractModifier("SECOND"));
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new LocateModifier(this.convert));
    registerFunctionModifier(SourceSystemFunctions.LEFT, new LeftOrRightFunctionModifier(getLanguageFactory(), this.convert));
    registerFunctionModifier(SourceSystemFunctions.RIGHT, new LeftOrRightFunctionModifier(getLanguageFactory(), this.convert));
    registerFunctionModifier(SourceSystemFunctions.COT, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            function.setName(SourceSystemFunctions.TAN);
            return Arrays.asList(getLanguageFactory().createFunction(SourceSystemFunctions.DIVIDE_OP, new Expression[] { new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER), function }, TypeFacility.RUNTIME_TYPES.DOUBLE));
        }
    });
    registerFunctionModifier(SourceSystemFunctions.LTRIM, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            ArrayList<Object> target = new ArrayList<Object>();
            // $NON-NLS-1$
            target.add("TRIM(LEADING FROM ");
            target.add(function.getParameters().get(0));
            // $NON-NLS-1$
            target.add(")");
            return target;
        }
    });
    registerFunctionModifier(SourceSystemFunctions.RTRIM, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            ArrayList<Object> target = new ArrayList<Object>();
            // $NON-NLS-1$
            target.add("TRIM(TRAILING FROM ");
            target.add(function.getParameters().get(0));
            // $NON-NLS-1$
            target.add(")");
            return target;
        }
    });
    registerFunctionModifier(SourceSystemFunctions.MOD, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$
            return Arrays.asList(function.getParameters().get(0), " MOD ", function.getParameters().get(1));
        }
    });
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "COSH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "TANH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "ACOSH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "ASINH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "ATANH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "CHAR2HEXINT", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "INDEX", INTEGER, STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "BYTES", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "OCTET_LENGTH", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "HASHAMP", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "HASHBAKAMP", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "HASHBUCKET", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "HASHROW", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "NULLIFZERO", BIG_DECIMAL, BIG_DECIMAL);
    // $NON-NLS-1$
    addPushDownFunction(TERADATA, "ZEROIFNULL", BIG_DECIMAL, BIG_DECIMAL);
    registerFunctionModifier(SourceSystemFunctions.COT, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            function.setName(SourceSystemFunctions.TAN);
            return Arrays.asList(getLanguageFactory().createFunction(SourceSystemFunctions.DIVIDE_OP, new Expression[] { new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER), function }, TypeFacility.RUNTIME_TYPES.DOUBLE));
        }
    });
    registerFunctionModifier(SourceSystemFunctions.LTRIM, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("TRIM(LEADING FROM ", function.getParameters().get(0), ")");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.RTRIM, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("TRIM(TRAILING FROM ", function.getParameters().get(0), ")");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.MOD, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$
            return Arrays.asList(function.getParameters().get(0), " MOD ", function.getParameters().get(1));
        }
    });
}
Also used : ArrayList(java.util.ArrayList) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with FunctionModifier

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

the class PIExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    convert.addTypeMapping("Int8", FunctionModifier.BYTE);
    // $NON-NLS-1$
    convert.addTypeMapping("Int16", FunctionModifier.SHORT);
    // $NON-NLS-1$
    convert.addTypeMapping("Int32", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convert.addTypeMapping("Int64", FunctionModifier.LONG);
    // $NON-NLS-1$
    convert.addTypeMapping("Single", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convert.addTypeMapping("Double", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convert.addTypeMapping("Boolean", FunctionModifier.BOOLEAN);
    // $NON-NLS-1$
    convert.addTypeMapping("String", FunctionModifier.STRING);
    // $NON-NLS-1$
    convert.addTypeMapping("DateTime", FunctionModifier.TIMESTAMP);
    // $NON-NLS-1$
    convert.addTypeMapping("Time", FunctionModifier.TIME);
    // $NON-NLS-1$
    convert.addTypeMapping("Variant", FunctionModifier.OBJECT);
    convert.addConvert(FunctionModifier.TIMESTAMP, FunctionModifier.TIME, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(format(", function.getParameters().get(0), ", 'hh:mm:ss.fff') as Time)");
        }
    });
    convert.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.FLOAT, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(cast(", function.getParameters().get(0), " as int8) as single)");
        }
    });
    convert.addConvert(FunctionModifier.BOOLEAN, FunctionModifier.DOUBLE, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(cast(", function.getParameters().get(0), " as int8) as double)");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convert);
    registerFunctionModifier(SourceSystemFunctions.MOD, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            return // $NON-NLS-1$ //$NON-NLS-2$
            Arrays.asList(// $NON-NLS-1$ //$NON-NLS-2$
            "cast(", // $NON-NLS-1$ //$NON-NLS-2$
            function.getParameters().get(0), // $NON-NLS-1$ //$NON-NLS-2$
            " as int64)", // $NON-NLS-1$ //$NON-NLS-2$
            "%", "cast(", function.getParameters().get(1), " as int64)");
        }
    });
    // $NON-NLS-1$
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new AliasModifier("DAY"));
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            if (function.getParameters().size() <= 2) {
                return Arrays.asList("INSTR(", function.getParameters().get(1), ",", function.getParameters().get(0), ")");
            }
            return Arrays.asList("INSTR(", function.getParameters().get(1), ",", function.getParameters().get(0), ",", function.getParameters().get(2), ")");
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("LOWER"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("UPPER"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new AliasModifier("SUBSTR"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.LENGTH, new AliasModifier("LEN"));
    // $NON-NLS-1$
    addPushDownFunction(PI, "COSH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(PI, "TANH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(PI, "SINH", FLOAT, FLOAT);
    // $NON-NLS-1$
    addPushDownFunction(PI, "FORMAT", STRING, FLOAT, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "FORMAT", STRING, INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "ParentName", STRING, STRING, INTEGER);
    // $NON-NLS-1$
    addPushDownFunction(PI, "List", STRING, STRING).setVarArgs(true);
    // $NON-NLS-1$
    addPushDownFunction(PI, "DIGCODE", INTEGER, STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "DIGSTRING", STRING, INTEGER);
    // $NON-NLS-1$
    addPushDownFunction(PI, "PE", STRING, OBJECT);
    // $NON-NLS-1$
    addPushDownFunction(PI, "ParentName", STRING, STRING, INTEGER);
    // $NON-NLS-1$
    addPushDownFunction(PI, "VarType", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "UOMID", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "UOMName", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "UOMAbbreviation", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "UOMClassName", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "UOMCanonicallD", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(PI, "UOMConvert", DOUBLE, DOUBLE, STRING, STRING);
    // $NON-NLS-1$
    FunctionMethod f = addPushDownFunction(PI, "interval", TIMESTAMP, STRING);
    // $NON-NLS-1$
    f.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "$1");
}
Also used : Function(org.teiid.language.Function) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) FunctionMethod(org.teiid.metadata.FunctionMethod) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

FunctionModifier (org.teiid.translator.jdbc.FunctionModifier)20 ArrayList (java.util.ArrayList)19 List (java.util.List)19 AliasModifier (org.teiid.translator.jdbc.AliasModifier)17 Function (org.teiid.language.Function)12 ConvertModifier (org.teiid.translator.jdbc.ConvertModifier)6 ModFunctionModifier (org.teiid.translator.jdbc.ModFunctionModifier)5 Expression (org.teiid.language.Expression)4 EscapeSyntaxModifier (org.teiid.translator.jdbc.EscapeSyntaxModifier)4 LocateFunctionModifier (org.teiid.translator.jdbc.LocateFunctionModifier)4 TemplateFunctionModifier (org.teiid.translator.jdbc.TemplateFunctionModifier)4 LinkedList (java.util.LinkedList)3 Literal (org.teiid.language.Literal)3 ConcatFunctionModifier (org.teiid.translator.jdbc.oracle.ConcatFunctionModifier)3 QueryExpression (org.teiid.language.QueryExpression)2 FunctionMethod (org.teiid.metadata.FunctionMethod)2 ExtractFunctionModifier (org.teiid.translator.jdbc.ExtractFunctionModifier)2 ParseFormatFunctionModifier (org.teiid.translator.jdbc.ParseFormatFunctionModifier)2 BasicDBList (com.mongodb.BasicDBList)1 AggregateFunction (org.teiid.language.AggregateFunction)1