Search in sources :

Example 26 with QueryCompletedEvent

use of io.trino.spi.eventlistener.QueryCompletedEvent in project trino by trinodb.

the class TestEventListenerBasic method testPrepareAndExecute.

@Test
public void testPrepareAndExecute() throws Exception {
    String selectQuery = "SELECT count(*) FROM lineitem WHERE shipmode = ?";
    String prepareQuery = "PREPARE stmt FROM " + selectQuery;
    // QueryCreated: 1, QueryCompleted: 1, Splits: 0
    runQueryAndWaitForEvents(prepareQuery, 2);
    QueryCreatedEvent queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    assertEquals(queryCreatedEvent.getContext().getServerVersion(), "testversion");
    assertEquals(queryCreatedEvent.getContext().getServerAddress(), "127.0.0.1");
    assertEquals(queryCreatedEvent.getContext().getEnvironment(), "testing");
    assertEquals(queryCreatedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQuery(), prepareQuery);
    assertFalse(queryCreatedEvent.getMetadata().getPreparedQuery().isPresent());
    QueryCompletedEvent queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    assertTrue(queryCompletedEvent.getContext().getResourceGroupId().isPresent());
    assertEquals(queryCompletedEvent.getContext().getResourceGroupId().get(), createResourceGroupId("global", "user-user"));
    assertEquals(queryCompletedEvent.getIoMetadata().getOutput(), Optional.empty());
    // Prepare has no inputs
    assertEquals(queryCompletedEvent.getIoMetadata().getInputs().size(), 0);
    assertEquals(queryCompletedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQueryId(), queryCompletedEvent.getMetadata().getQueryId());
    assertFalse(queryCompletedEvent.getMetadata().getPreparedQuery().isPresent());
    // Prepare has no splits
    assertEquals(queryCompletedEvent.getStatistics().getCompletedSplits(), 0);
    // Add prepared statement to a new session to eliminate any impact on other tests in this suite.
    Session sessionWithPrepare = Session.builder(getSession()).addPreparedStatement("stmt", selectQuery).build();
    queries.runQueryAndWaitForEvents("EXECUTE stmt USING 'SHIP'", 2, sessionWithPrepare);
    queryCreatedEvent = getOnlyElement(generatedEvents.getQueryCreatedEvents());
    assertEquals(queryCreatedEvent.getContext().getServerVersion(), "testversion");
    assertEquals(queryCreatedEvent.getContext().getServerAddress(), "127.0.0.1");
    assertEquals(queryCreatedEvent.getContext().getEnvironment(), "testing");
    assertEquals(queryCreatedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(queryCreatedEvent.getMetadata().getQuery(), "EXECUTE stmt USING 'SHIP'");
    assertTrue(queryCreatedEvent.getMetadata().getPreparedQuery().isPresent());
    assertEquals(queryCreatedEvent.getMetadata().getPreparedQuery().get(), selectQuery);
    queryCompletedEvent = getOnlyElement(generatedEvents.getQueryCompletedEvents());
    assertTrue(queryCompletedEvent.getContext().getResourceGroupId().isPresent());
    assertEquals(queryCompletedEvent.getContext().getResourceGroupId().get(), createResourceGroupId("global", "user-user"));
    assertEquals(queryCompletedEvent.getIoMetadata().getOutput(), Optional.empty());
    assertEquals(queryCompletedEvent.getIoMetadata().getInputs().size(), 1);
    assertEquals(queryCompletedEvent.getContext().getClientInfo().get(), "{\"clientVersion\":\"testVersion\"}");
    assertEquals(getOnlyElement(queryCompletedEvent.getIoMetadata().getInputs()).getCatalogName(), "tpch");
    assertEquals(queryCreatedEvent.getMetadata().getQueryId(), queryCompletedEvent.getMetadata().getQueryId());
    assertTrue(queryCompletedEvent.getMetadata().getPreparedQuery().isPresent());
    assertEquals(queryCompletedEvent.getMetadata().getPreparedQuery().get(), selectQuery);
}
Also used : QueryCreatedEvent(io.trino.spi.eventlistener.QueryCreatedEvent) QueryCompletedEvent(io.trino.spi.eventlistener.QueryCompletedEvent) Session(io.trino.Session) Test(org.testng.annotations.Test)

Aggregations

QueryCompletedEvent (io.trino.spi.eventlistener.QueryCompletedEvent)26 Test (org.testng.annotations.Test)22 OutputColumnMetadata (io.trino.spi.eventlistener.OutputColumnMetadata)13 ColumnDetail (io.trino.spi.eventlistener.ColumnDetail)9 TableInfo (io.trino.spi.eventlistener.TableInfo)8 ColumnInfo (io.trino.spi.eventlistener.ColumnInfo)6 QueryCreatedEvent (io.trino.spi.eventlistener.QueryCreatedEvent)4 QueryStatistics (io.trino.spi.eventlistener.QueryStatistics)3 Session (io.trino.Session)2 SplitCompletedEvent (io.trino.spi.eventlistener.SplitCompletedEvent)2 MaterializedResult (io.trino.testing.MaterializedResult)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables.getOnlyElement (com.google.common.collect.Iterables.getOnlyElement)1 MockConnectorFactory (io.trino.connector.MockConnectorFactory)1 QueryStats (io.trino.execution.QueryStats)1 TestingEventListenerPlugin (io.trino.execution.TestEventListenerPlugin.TestingEventListenerPlugin)1 TestQueues.createResourceGroupId (io.trino.execution.TestQueues.createResourceGroupId)1 ResourceGroupManagerPlugin (io.trino.plugin.resourcegroups.ResourceGroupManagerPlugin)1