Search in sources :

Example 61 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class TestPushProjectionThroughExchange method testSkipIdentityProjectionIfOutputPresent.

@Test
public void testSkipIdentityProjectionIfOutputPresent() {
    // In the following example, the Projection over Exchange has got an identity assignment (a -> a).
    // The Projection is pushed down to Exchange's source, and the identity assignment is translated into
    // a0 -> a. The assignment is added to the pushed-down Projection because the input symbol 'a' is
    // required by the Exchange as a partitioning symbol.
    // When all the assignments from the parent Projection are added to the pushed-down Projection,
    // this assignment is omitted. Otherwise the doubled assignment would cause an error.
    tester().assertThat(new PushProjectionThroughExchange()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol aTimes5 = p.symbol("a_times_5");
        return p.project(Assignments.of(aTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("a"), new LongLiteral("5")), a, a.toSymbolReference()), p.exchange(e -> e.addSource(p.values(a)).addInputsSet(a).fixedHashDistributionParitioningScheme(ImmutableList.of(a), ImmutableList.of(a))));
    }).matches(exchange(strictProject(ImmutableMap.of("a_0", expression("a"), "a_times_5", expression("a * 5")), values(ImmutableList.of("a")))));
    // In the following example, the Projection over Exchange has got an identity assignment (b -> b).
    // The Projection is pushed down to Exchange's source, and the identity assignment is translated into
    // a0 -> a. The assignment is added to the pushed-down Projection because the input symbol 'a' is
    // required by the Exchange as a partitioning symbol.
    // When all the assignments from the parent Projection are added to the pushed-down Projection,
    // this assignment is omitted. Otherwise the doubled assignment would cause an error.
    tester().assertThat(new PushProjectionThroughExchange()).on(p -> {
        Symbol a = p.symbol("a");
        Symbol bTimes5 = p.symbol("b_times_5");
        Symbol b = p.symbol("b");
        return p.project(Assignments.of(bTimes5, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new SymbolReference("b"), new LongLiteral("5")), b, b.toSymbolReference()), p.exchange(e -> e.addSource(p.values(a)).addInputsSet(a).fixedHashDistributionParitioningScheme(ImmutableList.of(b), ImmutableList.of(b))));
    }).matches(exchange(strictProject(ImmutableMap.of("a_0", expression("a"), "a_times_5", expression("a * 5")), values(ImmutableList.of("a")))));
}
Also used : Symbol(io.trino.sql.planner.Symbol) PlanMatchPattern.expression(io.trino.sql.planner.assertions.PlanMatchPattern.expression) ImmutableMap(com.google.common.collect.ImmutableMap) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Assignments(io.trino.sql.planner.plan.Assignments) Test(org.testng.annotations.Test) PlanMatchPattern.values(io.trino.sql.planner.assertions.PlanMatchPattern.values) OrderingScheme(io.trino.sql.planner.OrderingScheme) FIRST(io.trino.sql.tree.SortItem.NullOrdering.FIRST) SortOrder(io.trino.spi.connector.SortOrder) PlanMatchPattern.strictProject(io.trino.sql.planner.assertions.PlanMatchPattern.strictProject) GATHER(io.trino.sql.planner.plan.ExchangeNode.Type.GATHER) ImmutableList(com.google.common.collect.ImmutableList) PlanMatchPattern.project(io.trino.sql.planner.assertions.PlanMatchPattern.project) SymbolReference(io.trino.sql.tree.SymbolReference) LongLiteral(io.trino.sql.tree.LongLiteral) PlanMatchPattern.exchange(io.trino.sql.planner.assertions.PlanMatchPattern.exchange) PlanMatchPattern.sort(io.trino.sql.planner.assertions.PlanMatchPattern.sort) REMOTE(io.trino.sql.planner.plan.ExchangeNode.Scope.REMOTE) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) ASCENDING(io.trino.sql.tree.SortItem.Ordering.ASCENDING) ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) LongLiteral(io.trino.sql.tree.LongLiteral) Symbol(io.trino.sql.planner.Symbol) SymbolReference(io.trino.sql.tree.SymbolReference) BaseRuleTest(io.trino.sql.planner.iterative.rule.test.BaseRuleTest) Test(org.testng.annotations.Test)

Example 62 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableRequiresFullColumnHandleMappingInResult.

@Test
public void testPushJoinIntoTableRequiresFullColumnHandleMappingInResult() {
    try (RuleTester ruleTester = defaultRuleTester()) {
        MockConnectorFactory connectorFactory = createMockConnectorFactory((session, applyJoinType, left, right, joinConditions, leftAssignments, rightAssignments) -> Optional.of(new JoinApplicationResult<>(JOIN_CONNECTOR_TABLE_HANDLE, ImmutableMap.of(COLUMN_A1_HANDLE, JOIN_COLUMN_A1_HANDLE, COLUMN_A2_HANDLE, JOIN_COLUMN_A2_HANDLE), // mapping for COLUMN_B1_HANDLE is missing
        ImmutableMap.of(), false)));
        ruleTester.getQueryRunner().createCatalog(MOCK_CATALOG, connectorFactory, ImmutableMap.of());
        assertThatThrownBy(() -> {
            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);
                TupleDomain<ColumnHandle> leftContraint = TupleDomain.fromFixedValues(ImmutableMap.of(COLUMN_A2_HANDLE, NullableValue.of(BIGINT, 44L)));
                TupleDomain<ColumnHandle> rightConstraint = TupleDomain.fromFixedValues(ImmutableMap.of(COLUMN_B1_HANDLE, NullableValue.of(BIGINT, 45L)));
                TableScanNode left = p.tableScan(TABLE_A_HANDLE, ImmutableList.of(columnA1Symbol, columnA2Symbol), ImmutableMap.of(columnA1Symbol, COLUMN_A1_HANDLE, columnA2Symbol, COLUMN_A2_HANDLE), leftContraint);
                TableScanNode right = p.tableScan(TABLE_B_HANDLE, ImmutableList.of(columnB1Symbol), ImmutableMap.of(columnB1Symbol, COLUMN_B1_HANDLE), rightConstraint);
                return p.join(INNER, left, right, new JoinNode.EquiJoinClause(columnA1Symbol, columnB1Symbol));
            }).withSession(MOCK_SESSION).matches(anyTree());
        }).isInstanceOf(IllegalStateException.class).hasMessageContaining("Column handle mappings do not match old column handles");
    }
}
Also used : MockConnectorFactory(io.trino.connector.MockConnectorFactory) ColumnHandle(io.trino.spi.connector.ColumnHandle) MockConnectorColumnHandle(io.trino.connector.MockConnectorColumnHandle) 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) JoinApplicationResult(io.trino.spi.connector.JoinApplicationResult) Test(org.testng.annotations.Test)

Example 63 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableScanDoesNotTriggerWithUnsupportedFilter.

@Test
public void testPushJoinIntoTableScanDoesNotTriggerWithUnsupportedFilter() {
    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 ComparisonExpression(ComparisonExpression.Operator.GREATER_THAN, new ArithmeticBinaryExpression(ArithmeticBinaryExpression.Operator.MULTIPLY, new GenericLiteral("BIGINT", "44"), columnA1Symbol.toSymbolReference()), columnB1Symbol.toSymbolReference()));
        }).withSession(MOCK_SESSION).doesNotFire();
    }
}
Also used : ArithmeticBinaryExpression(io.trino.sql.tree.ArithmeticBinaryExpression) 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) Symbol(io.trino.sql.planner.Symbol) GenericLiteral(io.trino.sql.tree.GenericLiteral) Test(org.testng.annotations.Test)

Example 64 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableScanPreservesEnforcedConstraint.

@Test(dataProvider = "testPushJoinIntoTableScanPreservesEnforcedConstraintParams")
public void testPushJoinIntoTableScanPreservesEnforcedConstraint(JoinNode.Type joinType, TupleDomain<ColumnHandle> leftConstraint, TupleDomain<ColumnHandle> rightConstraint, TupleDomain<Predicate<ColumnHandle>> expectedConstraint) {
    try (RuleTester ruleTester = defaultRuleTester()) {
        MockConnectorFactory connectorFactory = createMockConnectorFactory((session, applyJoinType, left, right, joinConditions, leftAssignments, rightAssignments) -> 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), leftConstraint);
            TableScanNode right = p.tableScan(TABLE_B_HANDLE, ImmutableList.of(columnB1Symbol), ImmutableMap.of(columnB1Symbol, COLUMN_B1_HANDLE), rightConstraint);
            return p.join(joinType, left, right, new JoinNode.EquiJoinClause(columnA1Symbol, columnB1Symbol));
        }).withSession(MOCK_SESSION).matches(project(tableScan(tableHandle -> JOIN_PUSHDOWN_SCHEMA_TABLE_NAME.equals(((MockConnectorTableHandle) tableHandle).getTableName()), expectedConstraint, ImmutableMap.of())));
    }
}
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) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) JoinApplicationResult(io.trino.spi.connector.JoinApplicationResult) Test(org.testng.annotations.Test)

Example 65 with Symbol

use of io.trino.sql.planner.Symbol in project trino by trinodb.

the class TestPushJoinIntoTableScan method testPushJoinIntoTableScanDoesNotFireForDifferentCatalogs.

@Test
public void testPushJoinIntoTableScanDoesNotFireForDifferentCatalogs() {
    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.getQueryRunner().createCatalog("another_catalog", "mock", ImmutableMap.of());
        TableHandle tableBHandleAnotherCatalog = createTableHandle(new MockConnectorTableHandle(new SchemaTableName(SCHEMA, TABLE_B)), "another_catalog");
        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(tableBHandleAnotherCatalog, ImmutableList.of(columnB1Symbol), ImmutableMap.of(columnB1Symbol, COLUMN_B1_HANDLE));
            return p.join(INNER, left, right, new JoinNode.EquiJoinClause(columnA1Symbol, columnB1Symbol));
        }).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) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) Symbol(io.trino.sql.planner.Symbol) JoinNode(io.trino.sql.planner.plan.JoinNode) ConnectorTableHandle(io.trino.spi.connector.ConnectorTableHandle) MockConnectorTableHandle(io.trino.connector.MockConnectorTableHandle) TableHandle(io.trino.metadata.TableHandle) SchemaTableName(io.trino.spi.connector.SchemaTableName) Test(org.testng.annotations.Test)

Aggregations

Symbol (io.trino.sql.planner.Symbol)366 Test (org.testng.annotations.Test)171 ImmutableList (com.google.common.collect.ImmutableList)124 ImmutableMap (com.google.common.collect.ImmutableMap)106 Optional (java.util.Optional)96 PlanNode (io.trino.sql.planner.plan.PlanNode)85 Expression (io.trino.sql.tree.Expression)85 Map (java.util.Map)66 Assignments (io.trino.sql.planner.plan.Assignments)65 JoinNode (io.trino.sql.planner.plan.JoinNode)64 ProjectNode (io.trino.sql.planner.plan.ProjectNode)61 PlanMatchPattern.values (io.trino.sql.planner.assertions.PlanMatchPattern.values)56 SymbolReference (io.trino.sql.tree.SymbolReference)55 BIGINT (io.trino.spi.type.BigintType.BIGINT)52 Type (io.trino.spi.type.Type)51 Session (io.trino.Session)46 List (java.util.List)46 RuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester)43 RuleTester.defaultRuleTester (io.trino.sql.planner.iterative.rule.test.RuleTester.defaultRuleTester)43 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)43