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;
}
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());
}
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);
}
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));
}
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;
}
Aggregations