Search in sources :

Example 96 with ConnectorSession

use of io.trino.spi.connector.ConnectorSession in project trino by trinodb.

the class TestCachingJdbcClient method testDifferentIdentityKeys.

@Test
public void testDifferentIdentityKeys() {
    CachingJdbcClient cachingJdbcClient = new CachingJdbcClient(database.getJdbcClient(), SESSION_PROPERTIES_PROVIDERS, new ExtraCredentialsBasedIdentityCacheMapping(new ExtraCredentialConfig().setUserCredentialName("user").setPasswordCredentialName("password")), FOREVER, true, 10000);
    ConnectorSession alice = createUserSession("alice");
    ConnectorSession bob = createUserSession("bob");
    JdbcTableHandle table = createTable(new SchemaTableName(schema, "table"));
    assertTableNamesCache(cachingJdbcClient).loads(2).misses(2).afterRunning(() -> {
        assertThat(cachingJdbcClient.getTableNames(alice, Optional.empty())).contains(table.getRequiredNamedRelation().getSchemaTableName());
        assertThat(cachingJdbcClient.getTableNames(bob, Optional.empty())).contains(table.getRequiredNamedRelation().getSchemaTableName());
    });
    assertTableNamesCache(cachingJdbcClient).hits(2).afterRunning(() -> {
        assertThat(cachingJdbcClient.getTableNames(alice, Optional.empty())).contains(table.getRequiredNamedRelation().getSchemaTableName());
        assertThat(cachingJdbcClient.getTableNames(bob, Optional.empty())).contains(table.getRequiredNamedRelation().getSchemaTableName());
    });
    // Drop tables by not using caching jdbc client
    jdbcClient.dropTable(SESSION, table);
}
Also used : ExtraCredentialConfig(io.trino.plugin.jdbc.credential.ExtraCredentialConfig) ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) SchemaTableName(io.trino.spi.connector.SchemaTableName) Test(org.testng.annotations.Test)

Example 97 with ConnectorSession

use of io.trino.spi.connector.ConnectorSession in project trino by trinodb.

the class TestCachingJdbcClient method testColumnsCacheInvalidationOnTableDrop.

@Test
public void testColumnsCacheInvalidationOnTableDrop() {
    ConnectorSession firstSession = createSession("first");
    ConnectorSession secondSession = createSession("second");
    JdbcTableHandle firstTable = createTable(new SchemaTableName(schema, "first_table"));
    JdbcTableHandle secondTable = createTable(new SchemaTableName(schema, "second_table"));
    JdbcColumnHandle firstColumn = addColumn(firstTable, "first_column");
    JdbcColumnHandle secondColumn = addColumn(secondTable, "second_column");
    // Load columns for both tables into cache and assert cache loads (sessions x tables)
    assertColumnCacheStats(cachingJdbcClient).loads(4).misses(4).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, firstTable)).contains(firstColumn);
        assertThat(cachingJdbcClient.getColumns(firstSession, secondTable)).contains(secondColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, firstTable)).contains(firstColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, secondTable)).contains(secondColumn);
    });
    // Load columns from cache
    assertColumnCacheStats(cachingJdbcClient).hits(2).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, firstTable)).contains(firstColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, secondTable)).contains(secondColumn);
    });
    // Rename column
    cachingJdbcClient.renameColumn(firstSession, firstTable, firstColumn, "another_column");
    assertColumnCacheStats(cachingJdbcClient).loads(1).misses(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(secondSession, firstTable)).doesNotContain(firstColumn).containsAll(jdbcClient.getColumns(SESSION, firstTable));
    });
    // Drop columns and caches for first table
    cachingJdbcClient.dropTable(secondSession, firstTable);
    assertColumnCacheStats(cachingJdbcClient).loads(2).misses(2).afterRunning(() -> {
        assertThatThrownBy(() -> cachingJdbcClient.getColumns(firstSession, firstTable)).isInstanceOf(TableNotFoundException.class);
        assertThatThrownBy(() -> cachingJdbcClient.getColumns(secondSession, firstTable)).isInstanceOf(TableNotFoundException.class);
    });
    // Check if second table is still cached
    assertColumnCacheStats(cachingJdbcClient).hits(2).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, secondTable)).contains(secondColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, secondTable)).contains(secondColumn);
    });
    cachingJdbcClient.dropTable(secondSession, secondTable);
}
Also used : ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) SchemaTableName(io.trino.spi.connector.SchemaTableName) Test(org.testng.annotations.Test)

Example 98 with ConnectorSession

use of io.trino.spi.connector.ConnectorSession in project trino by trinodb.

the class TestCachingJdbcClient method testTruncateTable.

@Test
public void testTruncateTable() {
    CachingJdbcClient cachingJdbcClient = cachingStatisticsAwareJdbcClient(FOREVER, true, 10000);
    ConnectorSession session = createSession("table");
    JdbcTableHandle table = createTable(new SchemaTableName(schema, "table"));
    // load
    assertStatisticsCacheStats(cachingJdbcClient).loads(1).misses(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getTableStatistics(session, table, TupleDomain.all())).isEqualTo(NON_EMPTY_STATS);
    });
    // read from cache
    assertStatisticsCacheStats(cachingJdbcClient).hits(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getTableStatistics(session, table, TupleDomain.all())).isEqualTo(NON_EMPTY_STATS);
    });
    // invalidate
    cachingJdbcClient.truncateTable(SESSION, table);
    // load again
    assertStatisticsCacheStats(cachingJdbcClient).loads(1).misses(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getTableStatistics(session, table, TupleDomain.all())).isEqualTo(NON_EMPTY_STATS);
    });
    // read from cache
    assertStatisticsCacheStats(cachingJdbcClient).hits(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getTableStatistics(session, table, TupleDomain.all())).isEqualTo(NON_EMPTY_STATS);
    });
    // cleanup
    this.jdbcClient.dropTable(SESSION, table);
}
Also used : ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) SchemaTableName(io.trino.spi.connector.SchemaTableName) Test(org.testng.annotations.Test)

Example 99 with ConnectorSession

use of io.trino.spi.connector.ConnectorSession in project trino by trinodb.

the class TestCachingJdbcClient method testColumnsNotCachedWhenCacheDisabled.

@Test
public void testColumnsNotCachedWhenCacheDisabled() {
    CachingJdbcClient cachingJdbcClient = createCachingJdbcClient(ZERO, true, 10000);
    ConnectorSession firstSession = createSession("first");
    ConnectorSession secondSession = createSession("second");
    JdbcTableHandle firstTable = createTable(new SchemaTableName(schema, "first_table"));
    JdbcTableHandle secondTable = createTable(new SchemaTableName(schema, "second_table"));
    JdbcColumnHandle firstColumn = addColumn(firstTable, "first_column");
    JdbcColumnHandle secondColumn = addColumn(secondTable, "second_column");
    assertColumnCacheStats(cachingJdbcClient).loads(4).misses(4).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, firstTable)).containsExactly(firstColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, firstTable)).containsExactly(firstColumn);
        assertThat(cachingJdbcClient.getColumns(firstSession, secondTable)).containsExactly(secondColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, secondTable)).containsExactly(secondColumn);
    });
    assertColumnCacheStats(cachingJdbcClient).loads(4).misses(4).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, firstTable)).containsExactly(firstColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, firstTable)).containsExactly(firstColumn);
        assertThat(cachingJdbcClient.getColumns(firstSession, secondTable)).containsExactly(secondColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, secondTable)).containsExactly(secondColumn);
    });
    // Drop tables by not using caching jdbc client
    jdbcClient.dropTable(SESSION, firstTable);
    jdbcClient.dropTable(SESSION, secondTable);
    // Columns are loaded bypassing a cache
    assertColumnCacheStats(cachingJdbcClient).loads(2).misses(2).afterRunning(() -> {
        assertThatThrownBy(() -> cachingJdbcClient.getColumns(firstSession, firstTable)).isInstanceOf(TableNotFoundException.class);
        assertThatThrownBy(() -> cachingJdbcClient.getColumns(firstSession, secondTable)).isInstanceOf(TableNotFoundException.class);
    });
}
Also used : ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) SchemaTableName(io.trino.spi.connector.SchemaTableName) Test(org.testng.annotations.Test)

Example 100 with ConnectorSession

use of io.trino.spi.connector.ConnectorSession in project trino by trinodb.

the class TestCachingJdbcClient method testColumnsCachedPerSession.

@Test
public void testColumnsCachedPerSession() {
    ConnectorSession firstSession = createSession("first");
    ConnectorSession secondSession = createSession("second");
    JdbcTableHandle table = getAnyTable(schema);
    JdbcColumnHandle phantomColumn = addColumn(table);
    // Load columns in first session scope
    assertColumnCacheStats(cachingJdbcClient).loads(1).misses(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, table)).contains(phantomColumn);
    });
    // Load columns in second session scope
    assertColumnCacheStats(cachingJdbcClient).loads(1).misses(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(secondSession, table)).contains(phantomColumn);
    });
    // Check that columns are cached
    assertColumnCacheStats(cachingJdbcClient).hits(1).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(secondSession, table)).contains(phantomColumn);
    });
    // Drop first column and invalidate cache for all sessions
    cachingJdbcClient.dropColumn(firstSession, table, phantomColumn);
    assertThat(jdbcClient.getColumns(firstSession, table)).doesNotContain(phantomColumn);
    // Load columns into cache in both sessions scope
    assertColumnCacheStats(cachingJdbcClient).loads(2).misses(2).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, table)).doesNotContain(phantomColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, table)).doesNotContain(phantomColumn);
    });
    // Read columns from cache
    assertColumnCacheStats(cachingJdbcClient).hits(2).afterRunning(() -> {
        assertThat(cachingJdbcClient.getColumns(firstSession, table)).doesNotContain(phantomColumn);
        assertThat(cachingJdbcClient.getColumns(secondSession, table)).doesNotContain(phantomColumn);
    });
}
Also used : ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) Test(org.testng.annotations.Test)

Aggregations

ConnectorSession (io.trino.spi.connector.ConnectorSession)252 SchemaTableName (io.trino.spi.connector.SchemaTableName)131 ConnectorMetadata (io.trino.spi.connector.ConnectorMetadata)122 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)119 List (java.util.List)111 ColumnHandle (io.trino.spi.connector.ColumnHandle)108 Optional (java.util.Optional)107 ImmutableList (com.google.common.collect.ImmutableList)98 Objects.requireNonNull (java.util.Objects.requireNonNull)97 TrinoException (io.trino.spi.TrinoException)94 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)92 Map (java.util.Map)88 TestingConnectorSession (io.trino.testing.TestingConnectorSession)87 ImmutableMap (com.google.common.collect.ImmutableMap)85 TupleDomain (io.trino.spi.predicate.TupleDomain)85 ConnectorTableMetadata (io.trino.spi.connector.ConnectorTableMetadata)83 Test (org.testng.annotations.Test)82 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)80 Constraint (io.trino.spi.connector.Constraint)76 Type (io.trino.spi.type.Type)72