use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestDefaultJdbcQueryBuilder method testEmptyBuildSql.
@Test
public void testEmptyBuildSql() throws SQLException {
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(columns.get(0), Domain.all(BIGINT), columns.get(1), Domain.onlyNull(DOUBLE)));
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_1\" IS NULL");
try (ResultSet resultSet = preparedStatement.executeQuery()) {
assertEquals(resultSet.next(), false);
}
}
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestDefaultJdbcQueryBuilder method testBuildSqlWithChar.
@Test
public void testBuildSqlWithChar() throws SQLException {
CharType charType = CharType.createCharType(0);
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(columns.get(11), Domain.create(SortedRangeSet.copyOf(charType, ImmutableList.of(Range.range(charType, utf8Slice("test_str_700"), true, utf8Slice("test_str_702"), false), Range.equal(charType, utf8Slice("test_str_180")), Range.equal(charType, utf8Slice("test_str_196")))), 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_11\" >= ? AND \"col_11\" < ?) OR \"col_11\" IN (?,?))");
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
builder.add((String) resultSet.getObject("col_11"));
}
}
assertEquals(builder.build(), ImmutableSet.of("test_str_700", "test_str_701", "test_str_180", "test_str_196"));
assertContains(preparedStatement.toString(), "\"col_11\" >= ?");
assertContains(preparedStatement.toString(), "\"col_11\" < ?");
assertContains(preparedStatement.toString(), "\"col_11\" IN (?,?)");
}
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestDefaultJdbcQueryBuilder method testBuildSqlWithVarchar.
@Test
public void testBuildSqlWithVarchar() throws SQLException {
TupleDomain<ColumnHandle> tupleDomain = TupleDomain.withColumnDomains(ImmutableMap.of(columns.get(3), Domain.create(SortedRangeSet.copyOf(VARCHAR, ImmutableList.of(Range.range(VARCHAR, utf8Slice("test_str_700"), true, utf8Slice("test_str_702"), false), Range.equal(VARCHAR, utf8Slice("test_str_180")), Range.equal(VARCHAR, utf8Slice("test_str_196")))), 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_3\" >= ? AND \"col_3\" < ?) OR \"col_3\" IN (?,?))");
ImmutableSet.Builder<String> builder = ImmutableSet.builder();
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
builder.add((String) resultSet.getObject("col_3"));
}
}
assertEquals(builder.build(), ImmutableSet.of("test_str_700", "test_str_701", "test_str_180", "test_str_196"));
assertContains(preparedStatement.toString(), "\"col_3\" >= ?");
assertContains(preparedStatement.toString(), "\"col_3\" < ?");
assertContains(preparedStatement.toString(), "\"col_3\" IN (?,?)");
}
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class RemoveRedundantPredicateAboveTableScan method apply.
@Override
public Result apply(FilterNode filterNode, Captures captures, Context context) {
Session session = context.getSession();
TableScanNode node = captures.get(TABLE_SCAN);
Expression predicate = filterNode.getPredicate();
Expression deterministicPredicate = filterDeterministicConjuncts(plannerContext.getMetadata(), predicate);
Expression nonDeterministicPredicate = filterNonDeterministicConjuncts(plannerContext.getMetadata(), predicate);
ExtractionResult decomposedPredicate = getFullyExtractedPredicates(session, deterministicPredicate, context.getSymbolAllocator().getTypes());
if (decomposedPredicate.getTupleDomain().isAll()) {
// no conjunct could be fully converted to tuple domain
return Result.empty();
}
TupleDomain<ColumnHandle> predicateDomain = decomposedPredicate.getTupleDomain().transformKeys(node.getAssignments()::get);
if (predicateDomain.isNone()) {
// to turn the subtree into a Values node
return Result.ofPlanNode(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of()));
}
if (node.getEnforcedConstraint().isNone()) {
// table scans with none domain should be converted to ValuesNode
return Result.ofPlanNode(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of()));
}
// is not NONE
Map<ColumnHandle, Domain> enforcedColumnDomains = node.getEnforcedConstraint().getDomains().orElseThrow();
TupleDomain<ColumnHandle> unenforcedDomain = predicateDomain.transformDomains((columnHandle, predicateColumnDomain) -> {
Type type = predicateColumnDomain.getType();
Domain enforcedColumnDomain = Optional.ofNullable(enforcedColumnDomains.get(columnHandle)).orElseGet(() -> Domain.all(type));
if (predicateColumnDomain.contains(enforcedColumnDomain)) {
// full enforced
return Domain.all(type);
}
return predicateColumnDomain.intersect(enforcedColumnDomain);
});
if (unenforcedDomain.equals(predicateDomain)) {
// no change in filter predicate
return Result.empty();
}
Map<ColumnHandle, Symbol> assignments = ImmutableBiMap.copyOf(node.getAssignments()).inverse();
Expression resultingPredicate = createResultingPredicate(plannerContext, session, context.getSymbolAllocator(), typeAnalyzer, // Dynamic filters are included in decomposedPredicate.getRemainingExpression()
TRUE_LITERAL, new DomainTranslator(plannerContext).toPredicate(session, unenforcedDomain.transformKeys(assignments::get)), nonDeterministicPredicate, decomposedPredicate.getRemainingExpression());
if (!TRUE_LITERAL.equals(resultingPredicate)) {
return Result.ofPlanNode(new FilterNode(context.getIdAllocator().getNextId(), node, resultingPredicate));
}
return Result.ofPlanNode(node);
}
use of io.trino.spi.connector.ColumnHandle in project trino by trinodb.
the class TestPruneTableScanColumns method testPushColumnPruningProjection.
@Test
public void testPushColumnPruningProjection() {
try (RuleTester ruleTester = defaultRuleTester()) {
String mockCatalog = "mock_catalog";
String testSchema = "test_schema";
String testTable = "test_table";
SchemaTableName testSchemaTable = new SchemaTableName(testSchema, testTable);
ColumnHandle columnHandleA = new MockConnectorColumnHandle("cola", DATE);
ColumnHandle columnHandleB = new MockConnectorColumnHandle("colb", DOUBLE);
Map<String, ColumnHandle> assignments = ImmutableMap.of("cola", columnHandleA, "colb", columnHandleB);
// Create catalog with applyProjection
MockConnectorFactory factory = MockConnectorFactory.builder().withListSchemaNames(connectorSession -> ImmutableList.of(testSchema)).withListTables((connectorSession, schema) -> testSchema.equals(schema) ? ImmutableList.of(testSchemaTable) : ImmutableList.of()).withGetColumns(schemaTableName -> assignments.entrySet().stream().map(entry -> new ColumnMetadata(entry.getKey(), ((MockConnectorColumnHandle) entry.getValue()).getType())).collect(toImmutableList())).withApplyProjection(this::mockApplyProjection).build();
ruleTester.getQueryRunner().createCatalog(mockCatalog, factory, ImmutableMap.of());
ruleTester.assertThat(new PruneTableScanColumns(ruleTester.getMetadata())).on(p -> {
Symbol symbolA = p.symbol("cola", DATE);
Symbol symbolB = p.symbol("colb", DOUBLE);
return p.project(Assignments.of(p.symbol("x"), symbolB.toSymbolReference()), p.tableScan(new TableHandle(new CatalogName(mockCatalog), new MockConnectorTableHandle(testSchemaTable), MockConnectorTransactionHandle.INSTANCE), ImmutableList.of(symbolA, symbolB), ImmutableMap.of(symbolA, columnHandleA, symbolB, columnHandleB)));
}).withSession(testSessionBuilder().setCatalog(mockCatalog).setSchema(testSchema).build()).matches(strictProject(ImmutableMap.of("expr", PlanMatchPattern.expression("COLB")), tableScan(new MockConnectorTableHandle(testSchemaTable, TupleDomain.all(), Optional.of(ImmutableList.of(columnHandleB)))::equals, TupleDomain.all(), ImmutableMap.of("COLB", columnHandleB::equals))));
}
}
Aggregations