Search in sources :

Example 1 with Function

use of org.teiid.language.Function in project teiid by teiid.

the class SubstringFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    this.modify(function);
    Expression from = function.getParameters().get(1);
    Boolean isFromNegative = isNegative(from);
    Function length = new Function(SourceSystemFunctions.LENGTH, Arrays.asList(function.getParameters().get(0)), TypeFacility.RUNTIME_TYPES.INTEGER);
    if (function.getParameters().size() == 2 && (isFromNegative == null || isFromNegative)) {
        // couchbase does not handle default length with a negative from index
        function.getParameters().add(length);
    }
    if (function.getParameters().size() == 3) {
        // case when length > LENGTH(string) - start + 1 then LENGTH(string) - start + 1 case when length > 0 then length end
        Expression forLength = function.getParameters().get(2);
        List<SearchedWhenClause> clauses = new ArrayList<SearchedWhenClause>(2);
        Boolean isNegative = isNegative(forLength);
        Expression adjustedFrom = from;
        if (isFromNegative == null || isFromNegative) {
            adjustedFrom = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(from, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.LT), new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(new Function(SourceSystemFunctions.ADD_OP, Arrays.asList(length, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER), from), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
        }
        Expression maxLength = new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(length, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER);
        clauses.add(new SearchedWhenClause(new Comparison(forLength, maxLength, Operator.GT), maxLength));
        Expression defaultExpr = null;
        if (isNegative == null) {
            clauses.add(new SearchedWhenClause(new Comparison(forLength, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), forLength));
        } else if (isNegative) {
            // TODO: could be done in the rewriter
            return Arrays.asList(new Literal(null, TypeFacility.RUNTIME_TYPES.STRING));
        } else {
            defaultExpr = forLength;
        }
        SearchedCase sc = new SearchedCase(clauses, defaultExpr, TypeFacility.RUNTIME_TYPES.INTEGER);
        function.getParameters().set(2, sc);
    }
    Expression adjustedFrom = function.getParameters().get(1);
    if (isFromNegative == null) {
        // case when start > 0 then start - 1 else start end
        SearchedCase sc = new SearchedCase(Arrays.asList(new SearchedWhenClause(new Comparison(adjustedFrom, new Literal(0, TypeFacility.RUNTIME_TYPES.INTEGER), Operator.GT), new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER))), from, TypeFacility.RUNTIME_TYPES.INTEGER);
        function.getParameters().set(1, sc);
    } else if (!isFromNegative) {
        function.getParameters().set(1, new Function(SourceSystemFunctions.SUBTRACT_OP, Arrays.asList(adjustedFrom, new Literal(1, TypeFacility.RUNTIME_TYPES.INTEGER)), TypeFacility.RUNTIME_TYPES.INTEGER));
    }
    return null;
}
Also used : SearchedCase(org.teiid.language.SearchedCase) Function(org.teiid.language.Function) SearchedWhenClause(org.teiid.language.SearchedWhenClause) Expression(org.teiid.language.Expression) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList)

Example 2 with Function

use of org.teiid.language.Function in project teiid by teiid.

the class TestMongoDBQueryExecution method testGeoFunctionInWhereWithGeometry.

@Test
public void testGeoFunctionInWhereWithGeometry() throws Exception {
    Table table = this.utility.createRuntimeMetadata().getTable("northwind.Categories");
    NamedTable namedTable = new NamedTable("Categories", "g0", table);
    ColumnReference colRef = new ColumnReference(namedTable, "CategoryName", table.getColumnByName("CategoryName"), String.class);
    DerivedColumn col = new DerivedColumn("CategoryName", colRef);
    Select select = new Select();
    select.setDerivedColumns(Arrays.asList(col));
    List<TableReference> tables = new ArrayList<TableReference>();
    tables.add(namedTable);
    select.setFrom(tables);
    final GeometryType geo = GeometryUtils.geometryFromClob(new ClobType(new ClobImpl("POLYGON ((1.0 2.0,3.0 4.0,5.0 6.0,1.0 2.0))")));
    Function function = new // $NON-NLS-1$
    Function(// $NON-NLS-1$
    "mongo.geoWithin", // $NON-NLS-1$
    Arrays.asList(colRef, new Literal(geo, GeometryType.class)), // $NON-NLS-2$
    Boolean.class);
    function.setMetadataObject(getFunctionMethod("mongo.geoWithin"));
    Comparison c = new Comparison(function, new Literal(true, Boolean.class), Comparison.Operator.EQ);
    select.setWhere(c);
    DBCollection dbCollection = helpExecute(select, new String[] { "Categories" }, 2);
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("CategoryName");
    // $NON-NLS-1$
    builder.push("$geoWithin");
    // $NON-NLS-1$
    builder.add("$geometry", "{\"type\":\"Polygon\",\"coordinates\":[[[1.0,2.0],[3.0,4.0],[5.0,6.0],[1.0,2.0]]]}");
    BasicDBObject result = new BasicDBObject();
    result.append("CategoryName", "$CategoryName");
    List<DBObject> pipeline = buildArray(new BasicDBObject("$match", builder.get()), new BasicDBObject("$project", result));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : NamedTable(org.teiid.language.NamedTable) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) ArrayList(java.util.ArrayList) ClobType(org.teiid.core.types.ClobType) GeometryType(org.teiid.core.types.GeometryType) Function(org.teiid.language.Function) TableReference(org.teiid.language.TableReference) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) Select(org.teiid.language.Select) DerivedColumn(org.teiid.language.DerivedColumn) ClobImpl(org.teiid.core.types.ClobImpl) ColumnReference(org.teiid.language.ColumnReference) Test(org.junit.Test)

Example 3 with Function

use of org.teiid.language.Function in project teiid by teiid.

the class DB2ExecutionFactory method start.

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

        @Override
        public List<?> translate(Function function) {
            List<Expression> p = function.getParameters();
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
            return Arrays.asList("STRIP(", p.get(2), ", ", ((Literal) p.get(0)).getValue(), ", ", p.get(1), ")");
        }
    });
    registerFunctionModifier(SourceSystemFunctions.WEEK, new AliasModifier(WEEK_ISO));
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    addPushDownFunction("db2", "substr", "string", TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.INTEGER);
}
Also used : Function(org.teiid.language.Function) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) Literal(org.teiid.language.Literal) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Function

use of org.teiid.language.Function in project teiid by teiid.

the class InterSystemsCacheExecutionFactory method start.

@Override
public void start() throws TranslatorException {
    super.start();
    // $NON-NLS-1$
    convert.addTypeMapping("tinyint", FunctionModifier.BYTE);
    // $NON-NLS-1$
    convert.addTypeMapping("smallint", FunctionModifier.SHORT);
    // $NON-NLS-1$
    convert.addTypeMapping("integer", FunctionModifier.INTEGER);
    // $NON-NLS-1$
    convert.addTypeMapping("bigint", FunctionModifier.LONG);
    // $NON-NLS-1$
    convert.addTypeMapping("decimal(38,19)", FunctionModifier.BIGDECIMAL);
    // $NON-NLS-1$
    convert.addTypeMapping("decimal(19,0)", FunctionModifier.BIGINTEGER);
    // $NON-NLS-1$
    convert.addTypeMapping("character", FunctionModifier.CHAR);
    // $NON-NLS-1$
    convert.addTypeMapping("varchar(4000)", FunctionModifier.STRING);
    // $NON-NLS-1$
    convert.addTypeMapping("date", FunctionModifier.DATE);
    // $NON-NLS-1$
    convert.addTypeMapping("time", FunctionModifier.TIME);
    // $NON-NLS-1$
    convert.addTypeMapping("timestamp", FunctionModifier.TIMESTAMP);
    convert.addNumericBooleanConversions();
    registerFunctionModifier(SourceSystemFunctions.CONVERT, convert);
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.IFNULL, new AliasModifier("nvl"));
    registerFunctionModifier(SourceSystemFunctions.CONCAT, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.ACOS, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.ASIN, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.ATAN, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.COS, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.COT, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.CURDATE, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.CURTIME, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYNAME, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFMONTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFWEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DAYOFYEAR, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.EXP, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.HOUR, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.LOG, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.LOG10, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.LEFT, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MINUTE, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MONTH, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MONTHNAME, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.MOD, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.NOW, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.PI, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.QUARTER, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.RIGHT, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.SIN, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.SECOND, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.SQRT, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TAN, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPADD, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.TRUNCATE, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.WEEK, new EscapeSyntaxModifier());
    registerFunctionModifier(SourceSystemFunctions.DIVIDE_OP, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            if (function.getType() == TypeFacility.RUNTIME_TYPES.INTEGER || function.getType() == TypeFacility.RUNTIME_TYPES.LONG) {
                Function result = ConvertModifier.createConvertFunction(getLanguageFactory(), function, TypeFacility.getDataTypeName(function.getType()));
                function.setType(TypeFacility.RUNTIME_TYPES.BIG_DECIMAL);
                return Arrays.asList(result);
            }
            return null;
        }
    });
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "CHARACTER_LENGTH", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "CHAR_LENGTH", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "CHARINDEX", INTEGER, STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "CHARINDEX", INTEGER, STRING, STRING, INTEGER);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "INSTR", INTEGER, STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "INSTR", INTEGER, STRING, STRING, INTEGER);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "IS_NUMERIC", INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "REPLICATE", STRING, STRING, INTEGER);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "REVERSE", STRING, STRING);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "STUFF", STRING, STRING, STRING, INTEGER, STRING);
    // $NON-NLS-1$
    addPushDownFunction(INTER_CACHE, "TRIM", STRING, STRING);
}
Also used : Function(org.teiid.language.Function) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) AliasModifier(org.teiid.translator.jdbc.AliasModifier) EscapeSyntaxModifier(org.teiid.translator.jdbc.EscapeSyntaxModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Function

use of org.teiid.language.Function in project teiid by teiid.

the class MySQL5ExecutionFactory method start.

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

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

        @Override
        public List<?> translate(Function function) {
            Literal intervalType = (Literal) function.getParameters().get(0);
            String interval = ((String) intervalType.getValue()).toUpperCase();
            if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
                // $NON-NLS-1$
                intervalType.setValue("MICROSECOND");
                Expression[] args = new Expression[] { function.getParameters().get(1), getLanguageFactory().createLiteral(1000, TypeFacility.RUNTIME_TYPES.INTEGER) };
                // $NON-NLS-1$
                function.getParameters().set(1, getLanguageFactory().createFunction("/", args, TypeFacility.RUNTIME_TYPES.INTEGER));
            }
            return null;
        }
    });
    // $NON-NLS-1$ //$NON-NLS-2$
    addPushDownFunction("mysql", "timestampdiff", TypeFacility.RUNTIME_NAMES.INTEGER, TypeFacility.RUNTIME_NAMES.STRING, TypeFacility.RUNTIME_NAMES.TIMESTAMP, TypeFacility.RUNTIME_NAMES.TIMESTAMP);
    registerFunctionModifier(SourceSystemFunctions.TIMESTAMPDIFF, new FunctionModifier() {

        @Override
        public List<?> translate(Function function) {
            Literal intervalType = (Literal) function.getParameters().get(0);
            String interval = ((String) intervalType.getValue()).toUpperCase();
            if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
                // $NON-NLS-1$
                intervalType.setValue("MICROSECOND");
                // $NON-NLS-1$
                return Arrays.asList(function, " * 1000");
            }
            return null;
        }
    });
    // $NON-NLS-1$
    registerFunctionModifier(SourceSystemFunctions.ST_SRID, new AliasModifier("SRID"));
}
Also used : Function(org.teiid.language.Function) FunctionModifier(org.teiid.translator.jdbc.FunctionModifier) Literal(org.teiid.language.Literal) AliasModifier(org.teiid.translator.jdbc.AliasModifier) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

Function (org.teiid.language.Function)173 Test (org.junit.Test)127 BigInteger (java.math.BigInteger)36 ArrayList (java.util.ArrayList)16 Literal (org.teiid.language.Literal)15 BigDecimal (java.math.BigDecimal)14 List (java.util.List)13 Expression (org.teiid.language.Expression)13 AliasModifier (org.teiid.translator.jdbc.AliasModifier)12 FunctionModifier (org.teiid.translator.jdbc.FunctionModifier)12 SQLConversionVisitor (org.teiid.translator.jdbc.SQLConversionVisitor)9 LinkedList (java.util.LinkedList)3 ColumnReference (org.teiid.language.ColumnReference)3 Comparison (org.teiid.language.Comparison)3 SearchedCase (org.teiid.language.SearchedCase)3 EscapeSyntaxModifier (org.teiid.translator.jdbc.EscapeSyntaxModifier)3 ModFunctionModifier (org.teiid.translator.jdbc.ModFunctionModifier)3 ConcatFunctionModifier (org.teiid.translator.jdbc.oracle.ConcatFunctionModifier)3 Timestamp (java.sql.Timestamp)2 AggregateFunction (org.teiid.language.AggregateFunction)2