Search in sources :

Example 21 with RuleTester

use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.

the class TestPushTopNIntoTableScan method testPushSingleTopNIntoTableScan.

@Test
public void testPushSingleTopNIntoTableScan() {
    try (RuleTester ruleTester = defaultRuleTester()) {
        MockConnectorTableHandle connectorHandle = new MockConnectorTableHandle(TEST_SCHEMA_TABLE);
        // make the mock connector return a new connectorHandle
        MockConnectorFactory.ApplyTopN applyTopN = (session, handle, topNCount, sortItems, tableAssignments) -> Optional.of(new TopNApplicationResult<>(connectorHandle, true, false));
        MockConnectorFactory mockFactory = createMockFactory(assignments, Optional.of(applyTopN));
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, mockFactory, ImmutableMap.of());
        ruleTester.assertThat(new PushTopNIntoTableScan(ruleTester.getMetadata())).on(p -> {
            Symbol dimension = p.symbol(dimensionName, VARCHAR);
            Symbol metric = p.symbol(metricName, BIGINT);
            return p.topN(1, ImmutableList.of(dimension), p.tableScan(TEST_TABLE_HANDLE, ImmutableList.of(dimension, metric), ImmutableMap.of(dimension, dimensionColumn, metric, metricColumn)));
        }).withSession(MOCK_SESSION).matches(tableScan(connectorHandle::equals, TupleDomain.all(), new HashMap<>()));
    }
}
Also used : ColumnMetadata(io.trino.spi.connector.ColumnMetadata) TopNApplicationResult(io.trino.spi.connector.TopNApplicationResult) Test(org.testng.annotations.Test) HashMap(java.util.HashMap) CatalogName(io.trino.connector.CatalogName) VARCHAR(io.trino.spi.type.VarcharType.VARCHAR) ImmutableList(com.google.common.collect.ImmutableList) MockConnectorFactory(io.trino.connector.MockConnectorFactory) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) Map(java.util.Map) ColumnHandle(io.trino.spi.connector.ColumnHandle) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) Symbol(io.trino.sql.planner.Symbol) TpchColumnHandle(io.trino.plugin.tpch.TpchColumnHandle) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) TopNNode(io.trino.sql.planner.plan.TopNNode) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) PlanMatchPattern.topN(io.trino.sql.planner.assertions.PlanMatchPattern.topN) TupleDomain(io.trino.spi.predicate.TupleDomain) SchemaTableName(io.trino.spi.connector.SchemaTableName) FIRST(io.trino.sql.tree.SortItem.NullOrdering.FIRST) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) List(java.util.List) TestingSession.testSessionBuilder(io.trino.testing.TestingSession.testSessionBuilder) TableHandle(io.trino.metadata.TableHandle) BIGINT(io.trino.spi.type.BigintType.BIGINT) Optional(java.util.Optional) PlanMatchPattern.tableScan(io.trino.sql.planner.assertions.PlanMatchPattern.tableScan) Session(io.trino.Session) ConnectorTransactionHandle(io.trino.spi.connector.ConnectorTransactionHandle) MockConnectorFactory(io.trino.connector.MockConnectorFactory) HashMap(java.util.HashMap) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) Symbol(io.trino.sql.planner.Symbol) Test(org.testng.annotations.Test)

Example 22 with RuleTester

use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableDoesNotFireForCrossJoin.

@Test
public void testPushJoinIntoTableDoesNotFireForCrossJoin() {
    try (RuleTester ruleTester = defaultRuleTester()) {
        MockConnectorFactory connectorFactory = createMockConnectorFactory((session, applyJoinType, left, right, joinConditions, leftAssignments, rightAssignments) -> {
            throw new IllegalStateException("applyJoin should not be called!");
        });
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, connectorFactory, ImmutableMap.of());
        ruleTester.assertThat(new PushJoinIntoTableScan(ruleTester.getMetadata())).on(p -> {
            Symbol columnA1Symbol = p.symbol(COLUMN_A1);
            Symbol columnA2Symbol = p.symbol(COLUMN_A2);
            Symbol columnB1Symbol = p.symbol(COLUMN_B1);
            TableScanNode left = p.tableScan(TABLE_A_HANDLE, ImmutableList.of(columnA1Symbol, columnA2Symbol), ImmutableMap.of(columnA1Symbol, COLUMN_A1_HANDLE, columnA2Symbol, COLUMN_A2_HANDLE));
            TableScanNode right = p.tableScan(TABLE_B_HANDLE, ImmutableList.of(columnB1Symbol), ImmutableMap.of(columnB1Symbol, COLUMN_B1_HANDLE));
            // cross-join - no criteria
            return p.join(INNER, left, right);
        }).withSession(MOCK_SESSION).doesNotFire();
    }
}
Also used : MockConnectorFactory(io.trino.connector.MockConnectorFactory) TableScanNode(io.trino.sql.planner.plan.TableScanNode) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) Symbol(io.trino.sql.planner.Symbol) Test(org.testng.annotations.Test)

Example 23 with RuleTester

use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableScanDoesNotFireWhenAllPushdownsDisabled.

@Test
public void testPushJoinIntoTableScanDoesNotFireWhenAllPushdownsDisabled() {
    Session allPushdownsDisabledSession = Session.builder(MOCK_SESSION).setSystemProperty("allow_pushdown_into_connectors", "false").build();
    try (RuleTester ruleTester = defaultRuleTester()) {
        MockConnectorFactory connectorFactory = createMockConnectorFactory((session, applyJoinType, left, right, joinConditions, leftAssignments, rightAssignments) -> {
            throw new IllegalStateException("applyJoin should not be called!");
        });
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, connectorFactory, ImmutableMap.of());
        ruleTester.assertThat(new PushJoinIntoTableScan(ruleTester.getMetadata())).on(p -> {
            Symbol columnA1Symbol = p.symbol(COLUMN_A1);
            Symbol columnA2Symbol = p.symbol(COLUMN_A2);
            Symbol columnB1Symbol = p.symbol(COLUMN_B1);
            TableScanNode left = p.tableScan(TABLE_A_HANDLE, ImmutableList.of(columnA1Symbol, columnA2Symbol), ImmutableMap.of(columnA1Symbol, COLUMN_A1_HANDLE, columnA2Symbol, COLUMN_A2_HANDLE));
            TableScanNode right = p.tableScan(TABLE_B_HANDLE, ImmutableList.of(columnB1Symbol), ImmutableMap.of(columnB1Symbol, COLUMN_B1_HANDLE));
            return p.join(INNER, left, right, new JoinNode.EquiJoinClause(columnA1Symbol, columnB1Symbol));
        }).withSession(allPushdownsDisabledSession).doesNotFire();
    }
}
Also used : MockConnectorFactory(io.trino.connector.MockConnectorFactory) TableScanNode(io.trino.sql.planner.plan.TableScanNode) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) Symbol(io.trino.sql.planner.Symbol) JoinNode(io.trino.sql.planner.plan.JoinNode) Session(io.trino.Session) Test(org.testng.annotations.Test)

Example 24 with RuleTester

use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableScanDoesNotFireWhenDisabled.

@Test
public void testPushJoinIntoTableScanDoesNotFireWhenDisabled() {
    Session joinPushDownDisabledSession = Session.builder(MOCK_SESSION).setSystemProperty("allow_pushdown_into_connectors", "false").build();
    try (RuleTester ruleTester = defaultRuleTester()) {
        MockConnectorFactory connectorFactory = createMockConnectorFactory((session, applyJoinType, left, right, joinConditions, leftAssignments, rightAssignments) -> {
            throw new IllegalStateException("applyJoin should not be called!");
        });
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, connectorFactory, ImmutableMap.of());
        ruleTester.assertThat(new PushJoinIntoTableScan(ruleTester.getMetadata())).on(p -> {
            Symbol columnA1Symbol = p.symbol(COLUMN_A1);
            Symbol columnA2Symbol = p.symbol(COLUMN_A2);
            Symbol columnB1Symbol = p.symbol(COLUMN_B1);
            TableScanNode left = p.tableScan(TABLE_A_HANDLE, ImmutableList.of(columnA1Symbol, columnA2Symbol), ImmutableMap.of(columnA1Symbol, COLUMN_A1_HANDLE, columnA2Symbol, COLUMN_A2_HANDLE));
            TableScanNode right = p.tableScan(TABLE_B_HANDLE, ImmutableList.of(columnB1Symbol), ImmutableMap.of(columnB1Symbol, COLUMN_B1_HANDLE));
            return p.join(INNER, left, right, new JoinNode.EquiJoinClause(columnA1Symbol, columnB1Symbol));
        }).withSession(joinPushDownDisabledSession).doesNotFire();
    }
}
Also used : MockConnectorFactory(io.trino.connector.MockConnectorFactory) TableScanNode(io.trino.sql.planner.plan.TableScanNode) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) Symbol(io.trino.sql.planner.Symbol) JoinNode(io.trino.sql.planner.plan.JoinNode) Session(io.trino.Session) Test(org.testng.annotations.Test)

Example 25 with RuleTester

use of io.trino.sql.planner.iterative.rule.test.RuleTester in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableScan.

@Test(dataProvider = "testPushJoinIntoTableScanParams")
public void testPushJoinIntoTableScan(JoinNode.Type joinType, Optional<ComparisonExpression.Operator> filterComparisonOperator) {
    try (RuleTester ruleTester = defaultRuleTester()) {
        MockConnectorFactory connectorFactory = createMockConnectorFactory((session, applyJoinType, left, right, joinConditions, leftAssignments, rightAssignments) -> {
            assertThat(((MockConnectorTableHandle) left).getTableName()).isEqualTo(TABLE_A_SCHEMA_TABLE_NAME);
            assertThat(((MockConnectorTableHandle) right).getTableName()).isEqualTo(TABLE_B_SCHEMA_TABLE_NAME);
            Assertions.assertThat(applyJoinType).isEqualTo(toSpiJoinType(joinType));
            JoinCondition.Operator expectedOperator = filterComparisonOperator.map(this::getConditionOperator).orElse(JoinCondition.Operator.EQUAL);
            Assertions.assertThat(joinConditions).containsExactly(new JoinCondition(expectedOperator, COLUMN_A1_VARIABLE, COLUMN_B1_VARIABLE));
            return Optional.of(new JoinApplicationResult<>(JOIN_CONNECTOR_TABLE_HANDLE, JOIN_TABLE_A_COLUMN_MAPPING, JOIN_TABLE_B_COLUMN_MAPPING, false));
        });
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, connectorFactory, ImmutableMap.of());
        ruleTester.assertThat(new PushJoinIntoTableScan(ruleTester.getMetadata())).on(p -> {
            Symbol columnA1Symbol = p.symbol(COLUMN_A1);
            Symbol columnA2Symbol = p.symbol(COLUMN_A2);
            Symbol columnB1Symbol = p.symbol(COLUMN_B1);
            TableScanNode left = p.tableScan(TABLE_A_HANDLE, ImmutableList.of(columnA1Symbol, columnA2Symbol), ImmutableMap.of(columnA1Symbol, COLUMN_A1_HANDLE, columnA2Symbol, COLUMN_A2_HANDLE));
            TableScanNode right = p.tableScan(TABLE_B_HANDLE, ImmutableList.of(columnB1Symbol), ImmutableMap.of(columnB1Symbol, COLUMN_B1_HANDLE));
            if (filterComparisonOperator.isEmpty()) {
                return p.join(joinType, left, right, new JoinNode.EquiJoinClause(columnA1Symbol, columnB1Symbol));
            }
            return p.join(joinType, left, right, new ComparisonExpression(filterComparisonOperator.get(), columnA1Symbol.toSymbolReference(), columnB1Symbol.toSymbolReference()));
        }).withSession(MOCK_SESSION).matches(project(tableScan(JOIN_PUSHDOWN_SCHEMA_TABLE_NAME.getTableName())));
    }
}
Also used : MockConnectorFactory(io.trino.connector.MockConnectorFactory) ComparisonExpression(io.trino.sql.tree.ComparisonExpression) TableScanNode(io.trino.sql.planner.plan.TableScanNode) RuleTester.defaultRuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester) RuleTester(io.trino.sql.planner.iterative.rule.test.RuleTester) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) Symbol(io.trino.sql.planner.Symbol) JoinNode(io.trino.sql.planner.plan.JoinNode) JoinCondition(io.trino.spi.connector.JoinCondition) Test(org.testng.annotations.Test)

Aggregations

MockConnectorFactory (io.trino.connector.MockConnectorFactory)25 RuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester)25 RuleTester.defaultRuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester)25 Test (org.testng.annotations.Test)25 Symbol (io.trino.sql.planner.Symbol)22 MockConnectorTableHandle (io.trino.connector.MockConnectorTableHandle)13 ColumnHandle (io.trino.spi.connector.ColumnHandle)11 TpchColumnHandle (io.trino.plugin.tpch.TpchColumnHandle)9 Session (io.trino.Session)8 TableHandle (io.trino.metadata.TableHandle)8 ConnectorTableHandle (io.trino.spi.connector.ConnectorTableHandle)8 SchemaTableName (io.trino.spi.connector.SchemaTableName)8 TableScanNode (io.trino.sql.planner.plan.TableScanNode)8 ImmutableList (com.google.common.collect.ImmutableList)7 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)7 ImmutableMap (com.google.common.collect.ImmutableMap)7 CatalogName (io.trino.connector.CatalogName)7 ColumnMetadata (io.trino.spi.connector.ColumnMetadata)7 TupleDomain (io.trino.spi.predicate.TupleDomain)7 PlanMatchPattern.tableScan (io.trino.sql.planner.assertions.PlanMatchPattern.tableScan)7