use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class ParameterPushdown method captureParameterValues.
@VisibleForTesting
protected Map<String, String> captureParameterValues(SQL sql) {
Optional<SQLCondition> whereCondition = Optional.fromNullable(sql.getWhereCondition());
Multimap<String, Condition> conditionMap = FluentIterable.from(whereCondition.asSet()).transformAndConcat(new Function<SQLCondition, Iterable<Condition>>() {
@Override
public Iterable<Condition> apply(SQLCondition sqlCondition) {
Condition condition = sqlCondition.getCondition();
condition.simplify();
// Flatten first level of conditions
if (!condition.isComposite()) {
return Collections.singleton(condition);
}
// All child conditions must have allowable operators
for (Condition child : condition.getChildren()) {
if (!ALLOWED_OPERATORS.contains(child.getOperator())) {
return Collections.emptySet();
}
}
return condition.getChildren();
}
}).filter(new Predicate<Condition>() {
@Override
public boolean apply(Condition condition) {
// in the form of: WHERE A = 1
return !condition.isComposite() && !condition.isNegated() && condition.getRightExact() != null && ALLOWED_FUNCTIONS.contains(condition.getFunction());
}
}).index(new Function<Condition, String>() {
@Override
public String apply(Condition condition) {
// Group by field for easy look up
return condition.getLeftValuename();
}
});
Map<String, String> builder = Maps.newLinkedHashMap();
for (Definition definition : definitions) {
// There should be either 0 or 1 conditions for each field.
for (Condition condition : conditionMap.get(definition.getFieldName())) {
builder.put(definition.getParameter(), definition.format(condition));
}
}
return ImmutableMap.copyOf(builder);
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class MongodbPredicate method conditionListToDBObjectArray.
private DBObject[] conditionListToDBObjectArray(List<Condition> conditions) throws PushDownOptimizationException {
BasicDBList basicDbList = new BasicDBList();
for (Condition condition : conditions) {
QueryBuilder childContainer = QueryBuilder.start();
buildMongoCondition(condition, childContainer);
basicDbList.add(childContainer.get());
}
return basicDbList.toArray(new DBObject[basicDbList.size()]);
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class ParameterGeneration method applyMapping.
private boolean applyMapping(Condition condition, Map<String, SourceTargetFields> sourceTargetFieldsMap) {
// Atomic: check for simple mapping
if (condition.isAtomic()) {
String key = condition.getLeftValuename();
SourceTargetFields mapping = sourceTargetFieldsMap.get(key);
if (mapping != null) {
condition.setLeftValuename(mapping.getTargetFieldName());
return true;
} else {
return false;
}
} else {
// Composite: decide if all child conditions are required
List<Condition> children = condition.getChildren();
int requireAllOp = condition.isNegated() ? Condition.OPERATOR_AND : Condition.OPERATOR_OR;
boolean requireAll = false;
for (Condition child : children) {
if (child.getOperator() == requireAllOp) {
requireAll = true;
break;
}
}
// Map each child
for (Iterator<Condition> i = children.iterator(); i.hasNext(); ) {
Condition child = i.next();
if (!applyMapping(child, sourceTargetFieldsMap)) {
if (requireAll) {
// If all were required, give up now
return false;
} else {
i.remove();
}
}
}
// Successful if any children were mapped
return !children.isEmpty();
}
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class DataServiceExecutor method extractConditionParameters.
private void extractConditionParameters(Condition condition, Map<String, String> parameters) {
if (condition.isAtomic()) {
if (condition.getFunction() == Condition.FUNC_TRUE) {
parameters.put(condition.getLeftValuename(), condition.getRightExactString());
stripFieldNamesFromTrueFunction(condition);
}
} else {
for (Condition sub : condition.getChildren()) {
extractConditionParameters(sub, parameters);
}
}
}
use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.
the class DataServiceExecutorTest method testConditionResolution.
@Test
public void testConditionResolution() throws Exception {
RowMeta rowMeta = new RowMeta();
rowMeta.addValueMeta(new ValueMetaString("aString"));
rowMeta.addValueMeta(new ValueMetaInteger("anInt"));
rowMeta.addValueMeta(new ValueMetaDate("aDate"));
String query = "SELECT COUNT(aString), aString FROM " + DATA_SERVICE_NAME + " WHERE anInt = 2 AND aDate IN ('2014-12-05','2008-01-01')" + " GROUP BY aString HAVING COUNT(aString) > 2";
when(transMeta.getStepFields(DATA_SERVICE_STEP)).thenReturn(rowMeta);
DataServiceExecutor executor = new DataServiceExecutor.Builder(new SQL(query), dataService, context).serviceTrans(transMeta).prepareExecution(false).build();
Condition condition = executor.getSql().getWhereCondition().getCondition();
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(2014, Calendar.DECEMBER, 5);
assertThat(condition.evaluate(rowMeta, new Object[] { "value", 2L, calendar.getTime() }), is(true));
assertThat(condition.evaluate(rowMeta, new Object[] { "value", 2L, new Date() }), is(false));
}
Aggregations