Search in sources :

Example 11 with Condition

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);
}
Also used : SQLCondition(org.pentaho.di.core.sql.SQLCondition) Condition(org.pentaho.di.core.Condition) SQLCondition(org.pentaho.di.core.sql.SQLCondition) FluentIterable(com.google.common.collect.FluentIterable) Predicate(com.google.common.base.Predicate) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 12 with Condition

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()]);
}
Also used : Condition(org.pentaho.di.core.Condition) BasicDBList(com.mongodb.BasicDBList) QueryBuilder(com.mongodb.QueryBuilder)

Example 13 with Condition

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();
    }
}
Also used : Condition(org.pentaho.di.core.Condition) SourceTargetFields(org.pentaho.di.trans.dataservice.optimization.SourceTargetFields)

Example 14 with Condition

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);
        }
    }
}
Also used : Condition(org.pentaho.di.core.Condition)

Example 15 with Condition

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));
}
Also used : Condition(org.pentaho.di.core.Condition) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RowMeta(org.pentaho.di.core.row.RowMeta) Calendar(java.util.Calendar) ValueMetaInteger(org.pentaho.di.core.row.value.ValueMetaInteger) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) Date(java.util.Date) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Aggregations

Condition (org.pentaho.di.core.Condition)42 Test (org.junit.Test)14 Matchers.anyString (org.mockito.Matchers.anyString)6 RowMeta (org.pentaho.di.core.row.RowMeta)6 SQLCondition (org.pentaho.di.core.sql.SQLCondition)6 StepMeta (org.pentaho.di.trans.step.StepMeta)6 KettleException (org.pentaho.di.core.exception.KettleException)5 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)5 SQL (org.pentaho.di.core.sql.SQL)5 ParameterGenerationTest.newCondition (org.pentaho.di.trans.dataservice.optimization.paramgen.ParameterGenerationTest.newCondition)5 KettleStepException (org.pentaho.di.core.exception.KettleStepException)4 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)4 ValueMetaAndData (org.pentaho.di.core.row.ValueMetaAndData)4 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)3 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)2 SelectionEvent (org.eclipse.swt.events.SelectionEvent)2