Search in sources :

Example 16 with Condition

use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.

the class DataServiceExecutorTest method testNullNotNullKeywords.

@Test
public void testNullNotNullKeywords() throws Exception {
    String sql = "SELECT * FROM " + DATA_SERVICE_NAME + " WHERE column1 IS NOT NULL AND column2 IS NULL";
    DataServiceExecutor executor = new DataServiceExecutor.Builder(new SQL(sql), dataService, context).serviceTrans(serviceTrans).sqlTransGenerator(sqlTransGenerator).genTrans(genTrans).build();
    Condition condition = executor.getSql().getWhereCondition().getCondition();
    Condition condition1 = condition.getCondition(0);
    Condition condition2 = condition.getCondition(1);
    assertEquals("column1", condition1.getLeftValuename());
    assertNull(condition1.getRightExact());
    assertEquals(Condition.FUNC_NOT_NULL, condition1.getFunction());
    assertEquals("column2", condition2.getLeftValuename());
    assertNull(condition2.getRightExact());
    assertEquals(Condition.FUNC_NULL, condition2.getFunction());
}
Also used : Condition(org.pentaho.di.core.Condition) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Example 17 with Condition

use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.

the class ParameterGenerationTest method testActivation.

@Test
public void testActivation() throws Exception {
    // ( A & ( B | C ) )
    Condition condition = newCondition("A_src", "A_value");
    condition.addCondition(newCondition(AND, "B_src", "B_value"));
    condition.getCondition(1).addCondition(newCondition(OR, "C_src", "C_value"));
    SQL query = mockSql(condition);
    when(executor.getSql()).thenReturn(query);
    assertTrue(paramGen.activate(executor, stepInterface));
    ArgumentCaptor<Condition> pushDownCaptor = ArgumentCaptor.forClass(Condition.class);
    verify(service).pushDown(pushDownCaptor.capture(), same(paramGen), same(stepInterface));
    Condition verify = pushDownCaptor.getValue();
    assertEquals("A_tgt", verify.getCondition(0).getLeftValuename());
    assertEquals("A_value", verify.getCondition(0).getRightExactString());
    assertEquals(AND, verify.getCondition(1).getOperator());
    assertEquals("B_tgt", verify.getCondition(1).getCondition(0).getLeftValuename());
    assertEquals("B_value", verify.getCondition(1).getCondition(0).getRightExactString());
    assertEquals(OR, verify.getCondition(1).getCondition(1).getOperator());
    assertEquals("C_tgt", verify.getCondition(1).getCondition(1).getLeftValuename());
    assertEquals("C_value", verify.getCondition(1).getCondition(1).getRightExactString());
}
Also used : SQLCondition(org.pentaho.di.core.sql.SQLCondition) Condition(org.pentaho.di.core.Condition) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Example 18 with Condition

use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.

the class ParameterGenerationTest method newCondition.

public static Condition newCondition(int op, String lhs) throws KettleValueException {
    Condition condition = newCondition(lhs);
    condition.setOperator(op);
    return condition;
}
Also used : SQLCondition(org.pentaho.di.core.sql.SQLCondition) Condition(org.pentaho.di.core.Condition)

Example 19 with Condition

use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.

the class TableInputParameterGenerationTest method testInListCondition.

@SuppressWarnings("unchecked")
protected void testInListCondition(String valueData, String[] inListExpectedValues, String expectedSql) throws KettleValueException, PushDownOptimizationException {
    ValueMetaAndData rightExactInList = new ValueMetaAndData("mock_value", valueData);
    Condition inListCondition = new Condition("field_name", Condition.FUNC_IN_LIST, null, rightExactInList);
    RowMeta inListParamsMeta = mock(RowMeta.class);
    List<Object> inListParams = mock(List.class);
    assertThat(service.convertAtomicCondition(inListCondition, inListParamsMeta, inListParams), equalTo(expectedSql));
    for (String inListValue : inListExpectedValues) {
        verify(inListParams).add(inListValue);
    }
    verify(inListParamsMeta, times(inListExpectedValues.length)).addValueMeta(resolvedValueMeta);
    verifyNoMoreInteractions(inListParams, inListParamsMeta);
}
Also used : Condition(org.pentaho.di.core.Condition) ParameterGenerationTest.newCondition(org.pentaho.di.trans.dataservice.optimization.paramgen.ParameterGenerationTest.newCondition) RowMeta(org.pentaho.di.core.row.RowMeta) ValueMetaAndData(org.pentaho.di.core.row.ValueMetaAndData) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString)

Example 20 with Condition

use of org.pentaho.di.core.Condition in project pdi-dataservice-server-plugin by pentaho.

the class TableInputParameterGenerationTest method testPushDown.

@Test
public void testPushDown() throws Exception {
    // Add filters to both WHERE and GROUP by converting original query to a prepared statement...
    String originalQuery = "SELECT DepartmentName, COUNT(*) as EmployeeCount " + "FROM Department, Employee " + "WHERE Employee.DepartmentId = Department.DepartmentId AND ${EMPLOYEE_FILTER} ";
    ParameterGeneration employeeFilterParamGen = factory.createPushDown();
    employeeFilterParamGen.setParameterName("EMPLOYEE_FILTER");
    // Employee.Grade = "G7"
    Condition employeeFilter = newCondition("Employee.Grade", "G7");
    // Push Down condition
    service.pushDown(employeeFilter, employeeFilterParamGen, stepInterface);
    // Verify that the database for this step is now 'wrapped'
    assertThat(data.db, is(instanceOf(DatabaseWrapper.class)));
    final DatabaseWrapper databaseWrapper = (DatabaseWrapper) data.db;
    // The employee filter variable should now be set
    ArgumentCaptor<String> varCaptor = ArgumentCaptor.forClass(String.class);
    verify(stepInterface).setVariable(eq(employeeFilterParamGen.getParameterName()), varCaptor.capture());
    // Verify stored data for runtime injection
    final List<String> fragmentIds = varCaptor.getAllValues();
    assertThat(fragmentIds.size(), is(1));
    assertTrue(databaseWrapper.pushDownMap.keySet().containsAll(fragmentIds));
    // Update original query with variable values
    Variables variables = new Variables();
    variables.setVariable(employeeFilterParamGen.getParameterName(), fragmentIds.get(0));
    String runtimeQuery = variables.environmentSubstitute(originalQuery);
    for (String fragment : fragmentIds) {
        assertThat(runtimeQuery, containsString(fragment));
    }
    // During trans runtime, values and sql fragments will be injected
    RowMeta rowMeta = new RowMeta();
    List<Object> values = new LinkedList<Object>();
    String resultQuery = databaseWrapper.injectRuntime(databaseWrapper.pushDownMap, runtimeQuery, rowMeta, values);
    String expectedQuery = "SELECT DepartmentName, COUNT(*) as EmployeeCount " + "FROM Department, Employee " + "WHERE Employee.DepartmentId = Department.DepartmentId AND Employee.Grade = ? ";
    assertThat(resultQuery, equalTo(expectedQuery));
    assertThat(rowMeta.getValueMetaList(), equalTo(Arrays.asList(resolvedValueMeta)));
    assertThat(values, equalTo(Arrays.<Object>asList("G7")));
}
Also used : Condition(org.pentaho.di.core.Condition) ParameterGenerationTest.newCondition(org.pentaho.di.trans.dataservice.optimization.paramgen.ParameterGenerationTest.newCondition) Variables(org.pentaho.di.core.variables.Variables) RowMeta(org.pentaho.di.core.row.RowMeta) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) LinkedList(java.util.LinkedList) 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