Search in sources :

Example 16 with Literal

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

the class AddDiffModifier method translate.

@Override
public List<?> translate(Function function) {
    if (add) {
        // $NON-NLS-1$
        function.setName("dateadd");
    } else {
        // $NON-NLS-1$
        function.setName("datediff");
    }
    Literal intervalType = (Literal) function.getParameters().get(0);
    String interval = ((String) intervalType.getValue()).toUpperCase();
    String newInterval = INTERVAL_MAP.get(interval);
    if (newInterval != null) {
        intervalType.setValue(newInterval);
        return null;
    }
    if (supportsQuarter && interval.equals(NonReserved.SQL_TSI_QUARTER)) {
        // $NON-NLS-1$
        intervalType.setValue("QUARTER");
        return null;
    }
    if (add) {
        if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
            // $NON-NLS-1$
            intervalType.setValue("MILLISECOND");
            Expression[] args = new Expression[] { function.getParameters().get(1), factory.createLiteral(1000000, TypeFacility.RUNTIME_TYPES.INTEGER) };
            // $NON-NLS-1$
            function.getParameters().set(1, factory.createFunction("/", args, TypeFacility.RUNTIME_TYPES.INTEGER));
        } else if (interval.equals(NonReserved.SQL_TSI_QUARTER)) {
            intervalType.setValue(ExtractFunctionModifier.DAY);
            Expression[] args = new Expression[] { function.getParameters().get(1), factory.createLiteral(91, TypeFacility.RUNTIME_TYPES.INTEGER) };
            // $NON-NLS-1$
            function.getParameters().set(1, factory.createFunction("*", args, TypeFacility.RUNTIME_TYPES.INTEGER));
        } else {
            intervalType.setValue(ExtractFunctionModifier.DAY);
            Expression[] args = new Expression[] { function.getParameters().get(1), factory.createLiteral(7, TypeFacility.RUNTIME_TYPES.INTEGER) };
            // $NON-NLS-1$
            function.getParameters().set(1, factory.createFunction("*", args, TypeFacility.RUNTIME_TYPES.INTEGER));
        }
        return null;
    }
    if (interval.equals(NonReserved.SQL_TSI_FRAC_SECOND)) {
        // $NON-NLS-1$
        intervalType.setValue("MILLISECOND");
        // $NON-NLS-1$
        return Arrays.asList(function, " * 1000000");
    } else if (interval.equals(NonReserved.SQL_TSI_QUARTER)) {
        intervalType.setValue(ExtractFunctionModifier.DAY);
        // $NON-NLS-1$
        return Arrays.asList(function, " / 91");
    }
    intervalType.setValue(ExtractFunctionModifier.DAY);
    // $NON-NLS-1$
    return Arrays.asList(function, " / 7");
}
Also used : Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal)

Example 17 with Literal

use of org.teiid.language.Literal 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)

Example 18 with Literal

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

the class ConcatFunctionModifier method translate.

@Override
public List<?> translate(Function function) {
    Expression a = function.getParameters().get(0);
    Expression b = function.getParameters().get(1);
    List<Condition> crits = new ArrayList<Condition>();
    Literal nullValue = langFactory.createLiteral(null, TypeFacility.RUNTIME_TYPES.STRING);
    if (isNull(a)) {
        return Arrays.asList(nullValue);
    } else if (!isNotNull(a)) {
        crits.add(langFactory.createIsNullCriteria(a, false));
    }
    if (isNull(b)) {
        return Arrays.asList(nullValue);
    } else if (!isNotNull(b)) {
        crits.add(langFactory.createIsNullCriteria(b, false));
    }
    Condition crit = null;
    if (crits.isEmpty()) {
        return null;
    } else if (crits.size() == 1) {
        crit = crits.get(0);
    } else {
        crit = langFactory.createAndOr(Operator.OR, crits.get(0), crits.get(1));
    }
    List<SearchedWhenClause> cases = Arrays.asList(langFactory.createSearchedWhenCondition(crit, nullValue));
    return Arrays.asList(langFactory.createSearchedCaseExpression(cases, function, TypeFacility.RUNTIME_TYPES.STRING));
}
Also used : Condition(org.teiid.language.Condition) SearchedWhenClause(org.teiid.language.SearchedWhenClause) Expression(org.teiid.language.Expression) Literal(org.teiid.language.Literal) ArrayList(java.util.ArrayList)

Example 19 with Literal

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

the class DirectSpreadsheetQueryExecution method execute.

@Override
public void execute() throws TranslatorException {
    String worksheet = null;
    Integer limit = null;
    Integer offset = null;
    String toQuery = query;
    List<String> parts = StringUtil.tokenize(query, ';');
    for (String var : parts) {
        int index = var.indexOf('=');
        if (index == -1) {
            continue;
        }
        String key = var.substring(0, index).trim();
        String value = var.substring(index + 1).trim();
        if (key.equalsIgnoreCase(WORKSHEET)) {
            worksheet = value;
        } else if (key.equalsIgnoreCase(QUERY)) {
            StringBuilder buffer = new StringBuilder();
            SQLStringVisitor.parseNativeQueryParts(value, arguments, buffer, new SQLStringVisitor.Substitutor() {

                @Override
                public void substitute(Argument arg, StringBuilder builder, int index) {
                    Literal argumentValue = arg.getArgumentValue();
                    SpreadsheetSQLVisitor visitor = new SpreadsheetSQLVisitor(connection.getSpreadsheetInfo());
                    visitor.visit(argumentValue);
                    builder.append(visitor.getTranslatedSQL());
                }
            });
            toQuery = buffer.toString();
        } else if (key.equalsIgnoreCase(LIMIT)) {
            limit = Integer.parseInt(value);
        } else if (key.equalsIgnoreCase(OFFEST)) {
            offset = Integer.parseInt(value);
        }
    }
    this.rowIterator = this.connection.executeQuery(worksheet, toQuery, offset, limit, executionContext.getBatchSize()).iterator();
}
Also used : Argument(org.teiid.language.Argument) SpreadsheetSQLVisitor(org.teiid.translator.google.visitor.SpreadsheetSQLVisitor) Literal(org.teiid.language.Literal)

Example 20 with Literal

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

the class TestMongoDBDirectQueryExecution method testDirect.

@Test
public void testDirect() throws Exception {
    Command cmd = this.utility.parseCommand("SELECT * FROM Customers");
    MongoDBConnection connection = Mockito.mock(MongoDBConnection.class);
    ExecutionContext context = Mockito.mock(ExecutionContext.class);
    DBCollection dbCollection = Mockito.mock(DBCollection.class);
    DB db = Mockito.mock(DB.class);
    Mockito.stub(db.getCollection("MyTable")).toReturn(dbCollection);
    Mockito.stub(db.collectionExists(Mockito.anyString())).toReturn(true);
    Mockito.stub(connection.getDatabase()).toReturn(db);
    AggregationOutput output = Mockito.mock(AggregationOutput.class);
    Mockito.stub(output.results()).toReturn(new ArrayList<DBObject>());
    Mockito.stub(dbCollection.aggregate(Mockito.any(DBObject.class), Mockito.any(DBObject.class))).toReturn(output);
    Argument arg = new Argument(Direction.IN, null, String.class, null);
    arg.setArgumentValue(new Literal("MyTable;{$match:{\"id\":\"$1\"}};{$project:{\"_m0\":\"$user\"}}", String.class));
    Argument arg2 = new Argument(Direction.IN, null, String.class, null);
    arg2.setArgumentValue(new Literal("foo", String.class));
    ResultSetExecution execution = this.translator.createDirectExecution(Arrays.asList(arg, arg2), cmd, context, this.utility.createRuntimeMetadata(), connection);
    execution.execute();
    List<DBObject> pipeline = TestMongoDBQueryExecution.buildArray(new BasicDBObject("$match", new BasicDBObject("id", "foo")), new BasicDBObject("$project", new BasicDBObject("_m0", "$user")));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : AggregationOptions(com.mongodb.AggregationOptions) Argument(org.teiid.language.Argument) AggregationOutput(com.mongodb.AggregationOutput) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) ResultSetExecution(org.teiid.translator.ResultSetExecution) BasicDBObject(com.mongodb.BasicDBObject) ExecutionContext(org.teiid.translator.ExecutionContext) Command(org.teiid.language.Command) MongoDBConnection(org.teiid.mongodb.MongoDBConnection) Literal(org.teiid.language.Literal) DB(com.mongodb.DB) Test(org.junit.Test)

Aggregations

Literal (org.teiid.language.Literal)82 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)18 Expression (org.teiid.language.Expression)17 TranslatorException (org.teiid.translator.TranslatorException)16 Function (org.teiid.language.Function)15 Argument (org.teiid.language.Argument)12 ColumnReference (org.teiid.language.ColumnReference)10 Column (org.teiid.metadata.Column)10 Comparison (org.teiid.language.Comparison)9 ExpressionValueSource (org.teiid.language.ExpressionValueSource)7 List (java.util.List)5 Call (org.teiid.language.Call)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 BinaryWSProcedureExecution (org.teiid.translator.ws.BinaryWSProcedureExecution)4 DBCollection (com.mongodb.DBCollection)3 Timestamp (java.sql.Timestamp)3 Array (org.teiid.language.Array)3 Command (org.teiid.language.Command)3 Insert (org.teiid.language.Insert)3