Search in sources :

Example 36 with SQL

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

the class DataServiceExecutorTest method testExecuteConcurrentModification.

@Test
public void testExecuteConcurrentModification() throws Exception {
    String sql = "SELECT * FROM " + DATA_SERVICE_NAME;
    DataServiceExecutor executor = new DataServiceExecutor.Builder(new SQL(sql), dataService, context).prepareExecution(false).sqlTransGenerator(sqlTransGenerator).serviceTrans(serviceTrans).genTrans(genTrans).build();
    final DataServiceExecutor.ExecutionPoint stage = DataServiceExecutor.ExecutionPoint.OPTIMIZE;
    final ListMultimap<DataServiceExecutor.ExecutionPoint, Runnable> listenerMap = executor.getListenerMap();
    final Runnable task = new Runnable() {

        @Override
        public void run() {
            // Remove itself on run
            assertTrue(listenerMap.remove(stage, this));
        }
    };
    listenerMap.put(stage, task);
    executor.executeQuery();
    // Note the error reported to logs
    verify(genTrans.getLogChannel()).logError(anyString(), eq(stage), eq(ImmutableList.of(task)), eq(ImmutableList.of()));
}
Also used : 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 37 with SQL

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

the class DataServiceExecutorTest method testQueryWithParams.

@Test
public void testQueryWithParams() throws Exception {
    String sql = "SELECT * FROM " + DATA_SERVICE_NAME + " WHERE PARAMETER('foo') = 'bar' AND PARAMETER('baz') = 'bop'";
    final SQL theSql = new SQL(sql);
    DataServiceExecutor executor = new DataServiceExecutor.Builder(theSql, dataService, context).serviceTrans(serviceTrans).sqlTransGenerator(sqlTransGenerator).genTrans(genTrans).parameters(ImmutableMap.of("BUILD_PARAM", "TRUE")).build();
    List<Condition> conditions = theSql.getWhereCondition().getCondition().getChildren();
    assertEquals(2, conditions.size());
    for (Condition condition : conditions) {
        // verifies that each of the parameter conditions have their left and right valuename
        // set to null after executor initialization.  This prevents failure due to non-existent
        // fieldnames being present.
        assertNull(condition.getLeftValuename());
        assertNull(condition.getRightValuename());
    }
    assertThat(executor.getParameters(), equalTo((Map<String, String>) ImmutableMap.of("baz", "bop", "foo", "bar", "BUILD_PARAM", "TRUE")));
    // Late parameter modification is okay
    executor.getParameters().put("AFTER_BUILD", "TRUE");
    // Parameters should not be set on the trans until execute
    verify(serviceTrans, never()).setParameterValue(anyString(), anyString());
    executor.executeQuery();
    // verify that the parameter values were correctly extracted from the WHERE and applied
    verify(serviceTrans).setParameterValue("foo", "bar");
    verify(serviceTrans).setParameterValue("baz", "bop");
    verify(serviceTrans).setParameterValue("BUILD_PARAM", "TRUE");
    verify(serviceTrans).setParameterValue("AFTER_BUILD", "TRUE");
}
Also used : Condition(org.pentaho.di.core.Condition) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Example 38 with SQL

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

the class DataServiceExecutorTest method testGetRowLimit.

@Test
public void testGetRowLimit() throws KettleException {
    String sql = "SELECT * FROM " + DATA_SERVICE_NAME;
    when(serviceTrans.isRunning()).thenReturn(true);
    when(genTrans.isRunning()).thenReturn(true);
    when(sqlTransGenerator.getRowLimit()).thenReturn(999);
    DataServiceExecutor executor = new DataServiceExecutor.Builder(new SQL(sql), dataService, context).serviceTrans(serviceTrans).sqlTransGenerator(sqlTransGenerator).genTrans(genTrans).build();
    assertEquals(999, executor.getRowLimit());
}
Also used : 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 39 with SQL

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

the class DataServiceExecutorTest method testExecuteStreamQuery.

@Test
public void testExecuteStreamQuery() throws Exception {
    when(genTrans.isFinishedOrStopped()).thenReturn(true);
    SQL sql = new SQL("SELECT * FROM " + DATA_SERVICE_NAME);
    when(serviceTrans.getTransMeta().listParameters()).thenReturn(new String[0]);
    when(sqlTransGenerator.getSql()).thenReturn(sql);
    PushDownOptimizationMeta optimization = mock(PushDownOptimizationMeta.class);
    when(optimization.isEnabled()).thenReturn(true);
    dataService.getPushDownOptimizationMeta().add(optimization);
    dataService.setStreaming(true);
    IMetaStore metastore = mock(IMetaStore.class);
    DataServiceExecutor executor = new DataServiceExecutor.Builder(sql, dataService, context).serviceTrans(serviceTrans).sqlTransGenerator(sqlTransGenerator).genTrans(genTrans).metastore(metastore).windowMode(IDataServiceClientService.StreamingMode.ROW_BASED).windowSize(1).windowEvery(0).windowLimit(0).build();
    ArgumentCaptor<String> objectIds = ArgumentCaptor.forClass(String.class);
    verify(serviceTrans).setContainerObjectId(objectIds.capture());
    when(serviceTrans.getContainerObjectId()).thenReturn(objectIds.getValue());
    verify(genTrans).setContainerObjectId(objectIds.capture());
    when(genTrans.getContainerObjectId()).thenReturn(objectIds.getValue());
    verify(serviceTrans).setMetaStore(metastore);
    verify(genTrans).setMetaStore(metastore);
    RowProducer sqlTransRowProducer = mock(RowProducer.class);
    when(genTrans.addRowProducer(INJECTOR_STEP_NAME, 0)).thenReturn(sqlTransRowProducer);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Start Execution
    executor.executeQuery(new DataOutputStream(outputStream));
    // Check header was written
    assertThat(outputStream.size(), greaterThan(0));
    outputStream.reset();
    executor.waitUntilFinished();
    verify(serviceTrans, times(0)).waitUntilFinished();
    verify(genTrans).waitUntilFinished();
}
Also used : RowProducer(org.pentaho.di.trans.RowProducer) DataOutputStream(java.io.DataOutputStream) PushDownOptimizationMeta(org.pentaho.di.trans.dataservice.optimization.PushDownOptimizationMeta) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) Matchers.anyString(org.mockito.Matchers.anyString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IMetaStore(org.pentaho.metastore.api.IMetaStore) SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Example 40 with SQL

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

the class DataServiceExecutorTest method testDynamicLimitDefault.

@Test
public void testDynamicLimitDefault() throws Exception {
    final int defaultLimit = 50000;
    boolean userDef = dataService.isUserDefined();
    try {
        System.getProperties().remove(DataServiceConstants.ROW_LIMIT_PROPERTY);
        System.getProperties().remove(DataServiceConstants.LEGACY_LIMIT_PROPERTY);
        dataService.setUserDefined(false);
        DataServiceExecutor executor = new DataServiceExecutor.Builder(new SQL("SELECT * FROM " + DATA_SERVICE_NAME), dataService, context).serviceTrans(serviceTrans).genTrans(genTrans).build();
        assertEquals(defaultLimit, executor.getServiceRowLimit());
        System.setProperty(DataServiceConstants.ROW_LIMIT_PROPERTY, "baah");
        executor = new DataServiceExecutor.Builder(new SQL("SELECT * FROM " + DATA_SERVICE_NAME), dataService, context).serviceTrans(serviceTrans).genTrans(genTrans).build();
        assertEquals(defaultLimit, executor.getServiceRowLimit());
        verify(logChannel).logError(anyString());
    } finally {
        dataService.setUserDefined(userDef);
        System.getProperties().remove(DataServiceConstants.ROW_LIMIT_PROPERTY);
    }
}
Also used : SQL(org.pentaho.di.core.sql.SQL) Test(org.junit.Test)

Aggregations

SQL (org.pentaho.di.core.sql.SQL)58 Test (org.junit.Test)49 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)31 RowMeta (org.pentaho.di.core.row.RowMeta)18 Matchers.anyString (org.mockito.Matchers.anyString)17 IMetaStore (org.pentaho.metastore.api.IMetaStore)17 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)16 SelectValuesMeta (org.pentaho.di.trans.steps.selectvalues.SelectValuesMeta)12 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)11 DataServiceExecutor (org.pentaho.di.trans.dataservice.DataServiceExecutor)7 PushDownOptimizationMeta (org.pentaho.di.trans.dataservice.optimization.PushDownOptimizationMeta)7 Condition (org.pentaho.di.core.Condition)6 KettleException (org.pentaho.di.core.exception.KettleException)4 SQLCondition (org.pentaho.di.core.sql.SQLCondition)4 TransMeta (org.pentaho.di.trans.TransMeta)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 RowProducer (org.pentaho.di.trans.RowProducer)3 Trans (org.pentaho.di.trans.Trans)3 ImmutableMap (com.google.common.collect.ImmutableMap)2