use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.
the class TestApplyTableScanRedirection method testApplyTableScanRedirectionWithFilter.
@Test
public void testApplyTableScanRedirectionWithFilter() {
try (RuleTester ruleTester = defaultRuleTester()) {
// make the mock connector return a table scan on different table
// source table handle has a pushed down predicate
ApplyTableScanRedirect applyTableScanRedirect = getMockApplyRedirect(ImmutableMap.of(SOURCE_COLUMN_HANDLE_A, DESTINATION_COLUMN_NAME_A, SOURCE_COLUMN_HANDLE_B, DESTINATION_COLUMN_NAME_B));
MockConnectorFactory mockFactory = createMockFactory(Optional.of(applyTableScanRedirect));
ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
ApplyTableScanRedirection applyTableScanRedirection = new ApplyTableScanRedirection(ruleTester.getPlannerContext());
TupleDomain<ColumnHandle> constraint = TupleDomain.withColumnDomains(ImmutableMap.of(SOURCE_COLUMN_HANDLE_A, singleValue(VARCHAR, utf8Slice("foo"))));
ruleTester.assertThat(applyTableScanRedirection).on(p -> {
Symbol column = p.symbol(SOURCE_COLUMN_NAME_A, VARCHAR);
return p.tableScan(createTableHandle(new MockConnectorTableHandle(SOURCE_TABLE, constraint, Optional.empty())), ImmutableList.of(column), ImmutableMap.of(column, SOURCE_COLUMN_HANDLE_A), constraint);
}).withSession(MOCK_SESSION).matches(filter("DEST_COL = VARCHAR 'foo'", tableScan(new MockConnectorTableHandle(DESTINATION_TABLE)::equals, TupleDomain.all(), ImmutableMap.of("DEST_COL", DESTINATION_COLUMN_HANDLE_A::equals))));
ruleTester.assertThat(applyTableScanRedirection).on(p -> {
Symbol column = p.symbol(SOURCE_COLUMN_NAME_B, VARCHAR);
return p.tableScan(createTableHandle(new MockConnectorTableHandle(SOURCE_TABLE, constraint, Optional.empty())), ImmutableList.of(column), // predicate on non-projected column
ImmutableMap.of(column, SOURCE_COLUMN_HANDLE_B), TupleDomain.all());
}).withSession(MOCK_SESSION).matches(project(ImmutableMap.of("expr", expression("DEST_COL_B")), filter("DEST_COL_A = VARCHAR 'foo'", tableScan(new MockConnectorTableHandle(DESTINATION_TABLE)::equals, TupleDomain.all(), ImmutableMap.of("DEST_COL_A", DESTINATION_COLUMN_HANDLE_A::equals, "DEST_COL_B", DESTINATION_COLUMN_HANDLE_B::equals)))));
}
}
use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.
the class TestApplyTableScanRedirection method testMismatchedTypesWithMissingCoercion.
@Test
public void testMismatchedTypesWithMissingCoercion() {
try (RuleTester ruleTester = defaultRuleTester()) {
// make the mock connector return a table scan on different table
ApplyTableScanRedirect applyTableScanRedirect = getMockApplyRedirect(ImmutableMap.of(SOURCE_COLUMN_HANDLE_A, DESTINATION_COLUMN_NAME_D));
MockConnectorFactory mockFactory = createMockFactory(Optional.of(applyTableScanRedirect));
LocalQueryRunner runner = ruleTester.getQueryRunner();
runner.createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
transaction(runner.getTransactionManager(), runner.getAccessControl()).execute(MOCK_SESSION, session -> {
assertThatThrownBy(() -> runner.createPlan(session, "SELECT source_col_a FROM test_table", WarningCollector.NOOP)).isInstanceOf(TrinoException.class).hasMessageMatching("Cast not possible from redirected column mock_catalog.target_schema.target_table.destination_col_d with type Bogus to source column .*mock_catalog.test_schema.test_table.*source_col_a.* with type: varchar");
});
}
}
use of io.trino.sql.planner.iterative.rule.test.RuleTester 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))));
}
}
use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.
the class TestApplyTableScanRedirection method testDoesNotFireForDeleteTableScan.
@Test
public void testDoesNotFireForDeleteTableScan() {
try (RuleTester ruleTester = defaultRuleTester()) {
// make the mock connector return a table scan on different table
ApplyTableScanRedirect applyTableScanRedirect = getMockApplyRedirect(ImmutableMap.of(SOURCE_COLUMN_HANDLE_A, DESTINATION_COLUMN_NAME_A));
MockConnectorFactory mockFactory = createMockFactory(Optional.of(applyTableScanRedirect));
ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
ruleTester.assertThat(new ApplyTableScanRedirection(ruleTester.getPlannerContext())).on(p -> {
Symbol column = p.symbol(SOURCE_COLUMN_NAME_A, VARCHAR);
return p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(column), ImmutableMap.of(column, SOURCE_COLUMN_HANDLE_A), true);
}).withSession(MOCK_SESSION).doesNotFire();
}
}
use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.
the class TestApplyTableScanRedirection method testApplyTableScanRedirection.
@Test
public void testApplyTableScanRedirection() {
try (RuleTester ruleTester = defaultRuleTester()) {
// make the mock connector return a table scan on different table
ApplyTableScanRedirect applyTableScanRedirect = getMockApplyRedirect(ImmutableMap.of(SOURCE_COLUMN_HANDLE_A, DESTINATION_COLUMN_NAME_A));
MockConnectorFactory mockFactory = createMockFactory(Optional.of(applyTableScanRedirect));
ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
ruleTester.assertThat(new ApplyTableScanRedirection(ruleTester.getPlannerContext())).on(p -> {
Symbol column = p.symbol(SOURCE_COLUMN_NAME_A, VARCHAR);
return p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(column), ImmutableMap.of(column, SOURCE_COLUMN_HANDLE_A));
}).withSession(MOCK_SESSION).matches(tableScan(new MockConnectorTableHandle(DESTINATION_TABLE)::equals, TupleDomain.all(), ImmutableMap.of("DEST_COL", DESTINATION_COLUMN_HANDLE_A::equals)));
}
}
Aggregations