Search in sources :

Example 81 with ColumnHandle

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

the class TestDefaultJdbcMetadata method testCombineFiltersWithAggregationPushdown.

@Test
public void testCombineFiltersWithAggregationPushdown() {
    ConnectorSession session = TestingConnectorSession.builder().setPropertyMetadata(new JdbcMetadataSessionProperties(new JdbcMetadataConfig().setAggregationPushdownEnabled(true), Optional.empty()).getSessionProperties()).build();
    ColumnHandle groupByColumn = metadata.getColumnHandles(session, tableHandle).get("text");
    ConnectorTableHandle baseTableHandle = metadata.getTableHandle(session, new SchemaTableName("example", "numbers"));
    Domain firstDomain = Domain.multipleValues(VARCHAR, ImmutableList.of(utf8Slice("one"), utf8Slice("two")));
    JdbcTableHandle filterResult = applyFilter(session, baseTableHandle, new Constraint(TupleDomain.withColumnDomains(ImmutableMap.of(groupByColumn, firstDomain))));
    ConnectorTableHandle aggregatedTable = applyCountAggregation(session, filterResult, ImmutableList.of(ImmutableList.of(groupByColumn)));
    Domain secondDomain = Domain.multipleValues(VARCHAR, ImmutableList.of(utf8Slice("one"), utf8Slice("three")));
    JdbcTableHandle tableHandleWithFilter = applyFilter(session, aggregatedTable, new Constraint(TupleDomain.withColumnDomains(ImmutableMap.of(groupByColumn, secondDomain))));
    assertEquals(tableHandleWithFilter.getConstraint().getDomains(), // as firstDomain has been converted into a PreparedQuery
    Optional.of(ImmutableMap.of(groupByColumn, secondDomain)));
    assertEquals(((JdbcQueryRelationHandle) tableHandleWithFilter.getRelationHandle()).getPreparedQuery().getQuery(), "SELECT \"TEXT\", count(*) AS \"_pfgnrtd_0\" " + "FROM \"" + database.getDatabaseName() + "\".\"EXAMPLE\".\"NUMBERS\" " + "WHERE \"TEXT\" IN (?,?) " + "GROUP BY \"TEXT\"");
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Constraint(io.trino.spi.connector.Constraint) ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Test(org.testng.annotations.Test)

Example 82 with ColumnHandle

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

the class TestDefaultJdbcMetadata method testNonGroupKeyPredicatePushdown.

@Test
public void testNonGroupKeyPredicatePushdown() {
    ConnectorSession session = TestingConnectorSession.builder().setPropertyMetadata(new JdbcMetadataSessionProperties(new JdbcMetadataConfig().setAggregationPushdownEnabled(true), Optional.empty()).getSessionProperties()).build();
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
    ColumnHandle groupByColumn = columnHandles.get("text");
    ColumnHandle nonGroupByColumn = columnHandles.get("value");
    ConnectorTableHandle baseTableHandle = metadata.getTableHandle(session, new SchemaTableName("example", "numbers"));
    ConnectorTableHandle aggregatedTable = applyCountAggregation(session, baseTableHandle, ImmutableList.of(ImmutableList.of(groupByColumn)));
    Domain domain = Domain.singleValue(BIGINT, 123L);
    JdbcTableHandle tableHandleWithFilter = applyFilter(session, aggregatedTable, new Constraint(TupleDomain.withColumnDomains(ImmutableMap.of(nonGroupByColumn, domain))));
    assertEquals(tableHandleWithFilter.getConstraint().getDomains(), Optional.of(ImmutableMap.of(nonGroupByColumn, domain)));
    assertEquals(((JdbcQueryRelationHandle) tableHandleWithFilter.getRelationHandle()).getPreparedQuery().getQuery(), "SELECT \"TEXT\", count(*) AS \"_pfgnrtd_0\" " + "FROM \"" + database.getDatabaseName() + "\".\"EXAMPLE\".\"NUMBERS\" " + "GROUP BY \"TEXT\"");
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Constraint(io.trino.spi.connector.Constraint) ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Test(org.testng.annotations.Test)

Example 83 with ColumnHandle

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

the class TestDefaultJdbcMetadata method tesMultiGroupKeyPredicatePushdown.

@Test
public void tesMultiGroupKeyPredicatePushdown() {
    ConnectorSession session = TestingConnectorSession.builder().setPropertyMetadata(new JdbcMetadataSessionProperties(new JdbcMetadataConfig().setAggregationPushdownEnabled(true), Optional.empty()).getSessionProperties()).build();
    Map<String, ColumnHandle> columnHandles = metadata.getColumnHandles(session, tableHandle);
    ColumnHandle textColumn = columnHandles.get("text");
    ColumnHandle valueColumn = columnHandles.get("value");
    ConnectorTableHandle baseTableHandle = metadata.getTableHandle(session, new SchemaTableName("example", "numbers"));
    ConnectorTableHandle aggregatedTable = applyCountAggregation(session, baseTableHandle, ImmutableList.of(ImmutableList.of(textColumn, valueColumn), ImmutableList.of(textColumn)));
    Domain domain = Domain.singleValue(BIGINT, 123L);
    JdbcTableHandle tableHandleWithFilter = applyFilter(session, aggregatedTable, new Constraint(TupleDomain.withColumnDomains(ImmutableMap.of(valueColumn, domain))));
    assertEquals(tableHandleWithFilter.getConstraint().getDomains(), Optional.of(ImmutableMap.of(valueColumn, domain)));
    assertEquals(((JdbcQueryRelationHandle) tableHandleWithFilter.getRelationHandle()).getPreparedQuery().getQuery(), "SELECT \"TEXT\", \"VALUE\", count(*) AS \"_pfgnrtd_0\" " + "FROM \"" + database.getDatabaseName() + "\".\"EXAMPLE\".\"NUMBERS\" " + "GROUP BY GROUPING SETS ((\"TEXT\", \"VALUE\"), (\"TEXT\"))");
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) Constraint(io.trino.spi.connector.Constraint) ConnectorSession(io.trino.spi.connector.ConnectorSession) TestingConnectorSession(io.trino.testing.TestingConnectorSession) Domain(io.trino.spi.predicate.Domain) TupleDomain(io.trino.spi.predicate.TupleDomain) SchemaTableName(io.trino.spi.connector.SchemaTableName) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Test(org.testng.annotations.Test)

Example 84 with ColumnHandle

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

the class TestDefaultJdbcQueryBuilder method testBuildSqlWithTimestamp.

@Test
public void testBuildSqlWithTimestamp() throws SQLException {
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(columns.get(6), Domain.create(SortedRangeSet.copyOf(TIMESTAMP_MILLIS, ImmutableList.of(Range.equal(TIMESTAMP_MILLIS, toPrestoTimestamp(2016, 6, 3, 0, 23, 37)), Range.equal(TIMESTAMP_MILLIS, toPrestoTimestamp(2016, 10, 19, 16, 23, 37)), Range.range(TIMESTAMP_MILLIS, toPrestoTimestamp(2016, 6, 7, 8, 23, 37), false, toPrestoTimestamp(2016, 6, 9, 12, 23, 37), true))), false)));
    Connection connection = database.getConnection();
    PreparedQuery preparedQuery = queryBuilder.prepareSelectQuery(jdbcClient, SESSION, connection, TEST_TABLE, Optional.empty(), columns, Map.of(), tupleDomain, Optional.empty());
    try (PreparedStatement preparedStatement = queryBuilder.prepareStatement(jdbcClient, SESSION, connection, preparedQuery)) {
        assertThat(preparedQuery.getQuery()).isEqualTo("" + "SELECT \"col_0\", \"col_1\", \"col_2\", \"col_3\", \"col_4\", \"col_5\", " + "\"col_6\", \"col_7\", \"col_8\", \"col_9\", \"col_10\", \"col_11\" " + "FROM \"test_table\" " + "WHERE ((\"col_6\" > ? AND \"col_6\" <= ?) OR \"col_6\" IN (?,?))");
        ImmutableSet.Builder<Timestamp> builder = ImmutableSet.builder();
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                builder.add((Timestamp) resultSet.getObject("col_6"));
            }
        }
        assertEquals(builder.build(), ImmutableSet.of(toTimestamp(2016, 6, 3, 0, 23, 37), toTimestamp(2016, 6, 8, 10, 23, 37), toTimestamp(2016, 6, 9, 12, 23, 37), toTimestamp(2016, 10, 19, 16, 23, 37)));
        assertContains(preparedStatement.toString(), "\"col_6\" > ?");
        assertContains(preparedStatement.toString(), "\"col_6\" <= ?");
        assertContains(preparedStatement.toString(), "\"col_6\" IN (?,?)");
    }
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) ImmutableSet(com.google.common.collect.ImmutableSet) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) Test(org.testng.annotations.Test)

Example 85 with ColumnHandle

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

the class TestDefaultJdbcQueryBuilder method testBuildSqlWithFloat.

@Test
public void testBuildSqlWithFloat() throws SQLException {
    TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(columns.get(10), Domain.create(SortedRangeSet.copyOf(REAL, ImmutableList.of(Range.equal(REAL, (long) floatToRawIntBits(100.0f + 0)), Range.equal(REAL, (long) floatToRawIntBits(100.008f + 0)), Range.equal(REAL, (long) floatToRawIntBits(100.0f + 14)))), false)));
    Connection connection = database.getConnection();
    PreparedQuery preparedQuery = queryBuilder.prepareSelectQuery(jdbcClient, SESSION, connection, TEST_TABLE, Optional.empty(), columns, Map.of(), tupleDomain, Optional.empty());
    try (PreparedStatement preparedStatement = queryBuilder.prepareStatement(jdbcClient, SESSION, connection, preparedQuery)) {
        assertThat(preparedQuery.getQuery()).isEqualTo("" + "SELECT \"col_0\", \"col_1\", \"col_2\", \"col_3\", \"col_4\", \"col_5\", " + "\"col_6\", \"col_7\", \"col_8\", \"col_9\", \"col_10\", \"col_11\" " + "FROM \"test_table\" " + "WHERE \"col_10\" IN (?,?,?)");
        ImmutableSet.Builder<Long> longBuilder = ImmutableSet.builder();
        ImmutableSet.Builder<Float> floatBuilder = ImmutableSet.builder();
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                longBuilder.add((Long) resultSet.getObject("col_0"));
                floatBuilder.add((Float) resultSet.getObject("col_10"));
            }
        }
        assertEquals(longBuilder.build(), ImmutableSet.of(0L, 14L));
        assertEquals(floatBuilder.build(), ImmutableSet.of(100.0f, 114.0f));
    }
}
Also used : ColumnHandle(io.trino.spi.connector.ColumnHandle) ImmutableSet(com.google.common.collect.ImmutableSet) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Test(org.testng.annotations.Test)

Aggregations

ColumnHandle (io.trino.spi.connector.ColumnHandle)268 Test (org.testng.annotations.Test)121 ImmutableList (com.google.common.collect.ImmutableList)106 TupleDomain (io.trino.spi.predicate.TupleDomain)104 ImmutableMap (com.google.common.collect.ImmutableMap)93 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)86 Domain (io.trino.spi.predicate.Domain)86 Map (java.util.Map)78 ConnectorSession (io.trino.spi.connector.ConnectorSession)76 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)71 Optional (java.util.Optional)70 List (java.util.List)69 SchemaTableName (io.trino.spi.connector.SchemaTableName)66 Constraint (io.trino.spi.connector.Constraint)61 Objects.requireNonNull (java.util.Objects.requireNonNull)53 ImmutableSet (com.google.common.collect.ImmutableSet)51 NullableValue (io.trino.spi.predicate.NullableValue)50 Type (io.trino.spi.type.Type)48 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)45 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)45