Search in sources :

Example 1 with ConvertModifier

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

the class ActianVectorInHadoopExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    ConvertModifier convertModifier = new ConvertModifier();
    // $NON-NLS-1$
    convertModifier.addTypeMapping("CHAR(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("VARCHAR", FunctionModifier.STRING);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("NVARCHAR", FunctionModifier.STRING);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("INTEGER1", FunctionModifier.BYTE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("INTEGER2", FunctionModifier.SHORT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("INTEGER4", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("INTEGER8", FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("DECIMAL", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("MONEY", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("FLOAT", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("FLOAT4", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("ANSIDATE", FunctionModifier.DATE);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("TIME WITHOUT TIME ZONE", FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("TIME WITH TIME ZONE", FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("TIME WITH LOCAL TIME ZONE", FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("TIMESTAMP WITHOUT TIME ZONE", FunctionModifier.TIMESTAMP);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("TIMESTAMP WITH TIME ZONE", FunctionModifier.TIMESTAMP);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("TIMESTAMP WITH LOCAL TIME ZONE", FunctionModifier.TIMESTAMP);
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
    registerFunctionModifier(SourceSystemFunctions.CHAR, new AliasModifier("CHR"));
    registerFunctionModifier(SourceSystemFunctions.LCASE, new AliasModifier("LOWERCASE"));
    registerFunctionModifier(SourceSystemFunctions.UCASE, new AliasModifier("UPPERCASE"));
    registerFunctionModifier(SourceSystemFunctions.CEILING, new AliasModifier("CEIL"));
    registerFunctionModifier(SourceSystemFunctions.NULLIF, new AliasModifier("NVL"));
    registerFunctionModifier(SourceSystemFunctions.CURDATE, new Constant("CURRENT_DATE"));
    registerFunctionModifier(SourceSystemFunctions.CURTIME, new Constant("CURRENT_TIME"));
    registerFunctionModifier(SourceSystemFunctions.RAND, new AliasModifier("RANDOM"));
    registerFunctionModifier("CURRENT_TIMESTAMP", new Constant());
    registerFunctionModifier("CURRENT_USER", new Constant());
    registerFunctionModifier("INITIAL_USER", new Constant());
    registerFunctionModifier("LOCAL_TIME", new Constant());
    registerFunctionModifier("LOCAL_TIMESTAMP", new Constant());
    registerFunctionModifier("SESSION_USER", new Constant());
    registerFunctionModifier("SYSTEM_USER", new Constant());
    registerFunctionModifier("USER", new Constant());
    // pushdown
    addPushDownFunction(ACTIAN, "CHAREXTRACT", CHAR, STRING, INTEGER);
    addPushDownFunction(ACTIAN, "SHIFT", STRING, STRING, INTEGER);
    addPushDownFunction(ACTIAN, "SIZE", INTEGER, STRING);
    addPushDownFunction(ACTIAN, "SOUNDEX", STRING, STRING);
    addPushDownFunction(ACTIAN, "SQUEEZE", STRING, STRING);
    addPushDownFunction(ACTIAN, "TRUNC", BIG_DECIMAL, BIG_DECIMAL, INTEGER);
    addPushDownFunction(ACTIAN, "DAY", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "DAY", INTEGER, DATE);
    addPushDownFunction(ACTIAN, "CURRENT_TIMESTAMP", TIMESTAMP);
    addPushDownFunction(ACTIAN, "CURRENT_USER", STRING);
    addPushDownFunction(ACTIAN, "INITIAL_USER", STRING);
    addPushDownFunction(ACTIAN, "LOCAL_TIME", TIME);
    addPushDownFunction(ACTIAN, "LOCAL_TIMESTAMP", TIMESTAMP);
    addPushDownFunction(ACTIAN, "SESSION_USER", STRING);
    addPushDownFunction(ACTIAN, "SYSTEM_USER", STRING);
    addPushDownFunction(ACTIAN, "USER", STRING);
    addPushDownFunction(ACTIAN, "ADD_MONTHS", TIMESTAMP, TIMESTAMP, INTEGER);
    addPushDownFunction(ACTIAN, "DATE_FORMAT", STRING, TIMESTAMP, STRING);
    addPushDownFunction(ACTIAN, "DATE_PART", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "DATE_TRUNC", TIMESTAMP, TIMESTAMP);
    addPushDownFunction(ACTIAN, "DAY", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "DAYOFMONTH", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "DAYOFWEEK", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "DAYOFYEAR", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "FROM_UNIXTIME", TIMESTAMP, INTEGER);
    addPushDownFunction(ACTIAN, "HOUR", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "LASTDAY", TIMESTAMP, TIMESTAMP);
    addPushDownFunction(ACTIAN, "MICROSECOND", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "MILLISECOND", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "MINUTE", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "MONTH", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "MONTHS_BETWEEN", FLOAT, TIMESTAMP, DATE, DATE);
    addPushDownFunction(ACTIAN, "MONTHS_BETWEEN", FLOAT, TIMESTAMP, TIMESTAMP, TIMESTAMP);
    addPushDownFunction(ACTIAN, "NANOSECOND", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "QUATER", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "SECOND", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "WEEK", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "YEAR", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "YEAR_WEEK", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "UNIX_TIMESTAMP", INTEGER, TIMESTAMP);
    addPushDownFunction(ACTIAN, "WEEK_ISO", INTEGER, TIMESTAMP);
}
Also used : AliasModifier(org.teiid.translator.jdbc.AliasModifier) ConvertModifier(org.teiid.translator.jdbc.ConvertModifier)

Example 2 with ConvertModifier

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

the class BaseDB2ExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.CHAR, new AliasModifier("chr"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new AliasModifier("day"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("coalesce"));
    registerFunctionModifier(SourceSystemFunctions.LOCATE, new LocateFunctionModifier(getLanguageFactory()));
    registerFunctionModifier(SourceSystemFunctions.SUBSTRING, new SubstringFunctionModifier());
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.MOD, new ModFunctionModifier("MOD", getLanguageFactory()));
    // add in type conversion
    ConvertModifier convertModifier = new ConvertModifier();
    // $NON-NLS-1$
    convertModifier.addTypeMapping("real", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(31,0)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("numeric(31,12)", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("char(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("blob", FunctionModifier.BLOB, FunctionModifier.OBJECT);
    // $NON-NLS-1$
    convertModifier.addTypeMapping("clob", FunctionModifier.CLOB, FunctionModifier.XML);
    convertModifier.addConvert(FunctionModifier.TIME, FunctionModifier.TIMESTAMP, new FunctionModifier() {

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

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("timestamp(", function.getParameters().get(0), ", '00:00:00')");
        }
    });
    // the next convert is not strictly necessary for db2, but it also works for derby
    convertModifier.addConvert(FunctionModifier.STRING, FunctionModifier.FLOAT, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Arrays.asList("cast(double(", function.getParameters().get(0), ") as real)");
        }
    });
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("varchar"), FunctionModifier.STRING);
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("smallint"), FunctionModifier.BYTE, FunctionModifier.SHORT);
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("integer"), FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("bigint"), FunctionModifier.LONG);
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("double"), FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("date"), FunctionModifier.DATE);
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("time"), FunctionModifier.TIME);
    // $NON-NLS-1$
    convertModifier.addTypeConversion(new NullHandlingFormatModifier("timestamp"), FunctionModifier.TIMESTAMP);
    convertModifier.addNumericBooleanConversions();
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convertModifier);
}
Also used : ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) LocateFunctionModifier(org.teiid.translator.jdbc.LocateFunctionModifier) LocateFunctionModifier(org.teiid.translator.jdbc.LocateFunctionModifier) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List) ConvertModifier(org.teiid.translator.jdbc.ConvertModifier)

Example 3 with ConvertModifier

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

the class H2ExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.PARSETIMESTAMP, new AliasModifier("parsedatetime"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.FORMATTIMESTAMP, new AliasModifier("formatdatetime"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new AliasModifier("day_of_month"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new AliasModifier("day_of_week"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.WEEK, new AliasModifier("iso_week"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new AliasModifier("day_of_year"));
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.UNESCAPE, new AliasModifier("stringdecode"));
    registerFunctionModifier(SourceSystemFunctions.MOD, new ModFunctionModifier(SourceSystemFunctions.MOD, getLanguageFactory()));
    // TODO: this behavior is configurable in h2 starting with 1.1.119
    registerFunctionModifier(SourceSystemFunctions.CONCAT, new ConcatFunctionModifier(getLanguageFactory()));
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new AddDiffModifier(true, getLanguageFactory()));
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new AddDiffModifier(false, getLanguageFactory()));
    ConvertModifier convert = new ConvertModifier();
    // $NON-NLS-1$
    convert.addTypeMapping("boolean", FunctionModifier.BOOLEAN);
    // $NON-NLS-1$
    convert.addTypeMapping("tinyint", FunctionModifier.BYTE);
    // $NON-NLS-1$
    convert.addTypeMapping("smallint", FunctionModifier.SHORT);
    // $NON-NLS-1$
    convert.addTypeMapping("int", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convert.addTypeMapping("bigint", FunctionModifier.LONG);
    // $NON-NLS-1$
    convert.addTypeMapping("real", FunctionModifier.FLOAT);
    // $NON-NLS-1$
    convert.addTypeMapping("double", FunctionModifier.DOUBLE);
    // $NON-NLS-1$
    convert.addTypeMapping("decimal", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convert.addTypeMapping("decimal(38,0)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convert.addTypeMapping("date", FunctionModifier.DATE);
    // $NON-NLS-1$
    convert.addTypeMapping("time", FunctionModifier.TIME);
    // $NON-NLS-1$
    convert.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP);
    // $NON-NLS-1$
    convert.addTypeMapping("char(1)", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convert.addTypeMapping("varchar", FunctionModifier.STRING);
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convert);
    // $NON-NLS-1$ //$NON-NLS-2$
    addPushDownFunction("h2", "timestampdiff", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.TIMESTAMP, TypeFacility.RUNTIME_NAMES.TIMESTAMP);
}
Also used : ModFunctionModifier(org.teiid.translator.jdbc.ModFunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ConcatFunctionModifier(org.teiid.translator.jdbc.oracle.ConcatFunctionModifier) AddDiffModifier(org.teiid.translator.jdbc.hsql.AddDiffModifier) ConvertModifier(org.teiid.translator.jdbc.ConvertModifier)

Example 4 with ConvertModifier

use of org.teiid.translator.jdbc.ConvertModifier 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 5 with ConvertModifier

use of org.teiid.translator.jdbc.ConvertModifier 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)

Aggregations

ConvertModifier (org.teiid.translator.jdbc.ConvertModifier)10 AliasModifier (org.teiid.translator.jdbc.AliasModifier)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 FunctionModifier (org.teiid.translator.jdbc.FunctionModifier)6 LocateFunctionModifier (org.teiid.translator.jdbc.LocateFunctionModifier)4 ModFunctionModifier (org.teiid.translator.jdbc.ModFunctionModifier)3 Expression (org.teiid.language.Expression)2 Function (org.teiid.language.Function)2 EscapeSyntaxModifier (org.teiid.translator.jdbc.EscapeSyntaxModifier)2 ExtractFunctionModifier (org.teiid.translator.jdbc.ExtractFunctionModifier)2 TemplateFunctionModifier (org.teiid.translator.jdbc.TemplateFunctionModifier)2 AddDiffModifier (org.teiid.translator.jdbc.hsql.AddDiffModifier)2 ConcatFunctionModifier (org.teiid.translator.jdbc.oracle.ConcatFunctionModifier)2 LinkedList (java.util.LinkedList)1 LeftOrRightFunctionModifier (org.teiid.translator.jdbc.oracle.LeftOrRightFunctionModifier)1 MonthOrDayNameFunctionModifier (org.teiid.translator.jdbc.oracle.MonthOrDayNameFunctionModifier)1 OracleFormatFunctionModifier (org.teiid.translator.jdbc.oracle.OracleFormatFunctionModifier)1