Search in sources :

Example 1 with Comparison

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

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

the class TestCompareCriteriaImpl method testGetLeftExpression.

public void testGetLeftExpression() throws Exception {
    Comparison impl = example(AbstractCompareCriteria.GE, 200, 100);
    assertNotNull(impl.getLeftExpression());
    assertTrue(impl.getLeftExpression() instanceof Literal);
    assertEquals(new Integer(200), ((Literal) impl.getLeftExpression()).getValue());
}
Also used : Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal)

Example 3 with Comparison

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

the class TestCompoundCriteriaImpl method testGetCriteria.

public void testGetCriteria() throws Exception {
    AndOr cc = example(org.teiid.query.sql.lang.CompoundCriteria.AND);
    assertTrue(cc.getLeftCondition() instanceof Comparison);
    assertTrue(cc.getRightCondition() instanceof Comparison);
}
Also used : Comparison(org.teiid.language.Comparison) AndOr(org.teiid.language.AndOr)

Example 4 with Comparison

use of org.teiid.language.Comparison 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 5 with Comparison

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

the class LocateFunctionModifier method ensurePositiveStartIndex.

private Expression ensurePositiveStartIndex(Expression startIndex) {
    if (startIndex instanceof Literal) {
        Literal literal = (Literal) startIndex;
        if (literal.getValue() instanceof Integer && ((Integer) literal.getValue() < 1)) {
            literal.setValue(1);
        }
    } else {
        Comparison whenExpr = langFactory.createCompareCriteria(Operator.LT, startIndex, langFactory.createLiteral(1, Integer.class));
        Literal thenExpr = langFactory.createLiteral(1, Integer.class);
        startIndex = langFactory.createSearchedCaseExpression(Arrays.asList(langFactory.createSearchedWhenCondition(whenExpr, thenExpr)), startIndex, TypeFacility.RUNTIME_TYPES.INTEGER);
    }
    return startIndex;
}
Also used : Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal)

Aggregations

Comparison (org.teiid.language.Comparison)15 Literal (org.teiid.language.Literal)8 Test (org.junit.Test)5 Select (org.teiid.language.Select)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Expression (org.teiid.language.Expression)3 Function (org.teiid.language.Function)3 CommandBuilder (org.teiid.cdk.CommandBuilder)2 Array (org.teiid.language.Array)2 ColumnReference (org.teiid.language.ColumnReference)2 NamedTable (org.teiid.language.NamedTable)2 Parameter (org.teiid.language.Parameter)2 SearchedCase (org.teiid.language.SearchedCase)2 SearchedWhenClause (org.teiid.language.SearchedWhenClause)2 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)2 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)2 CallableStatement (java.sql.CallableStatement)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1